Mettre à jour le cluster

Mettez à jour les détails du cluster de Game Servers.

Exemple de code

Java

Pour savoir comment installer et utiliser la bibliothèque cliente pour Game Servers, consultez la page Bibliothèques clientes Game Servers.


import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.gaming.v1.GameServerCluster;
import com.google.cloud.gaming.v1.GameServerClustersServiceClient;
import com.google.cloud.gaming.v1.OperationMetadata;
import com.google.protobuf.FieldMask;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class UpdateCluster {
  public static void updateGameServerCluster(
      String projectId, String regionId, String realmId, String clusterId)
      throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // String projectId = "your-project-id";
    // String regionId = "us-central1-f";
    // String realmId = "your-realm-id";
    // String clusterId = "your-game-server-cluster-id";
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (GameServerClustersServiceClient client = GameServerClustersServiceClient.create()) {
      String parent =
          String.format("projects/%s/locations/%s/realms/%s", projectId, regionId, realmId);
      String clusterName = String.format("%s/gameServerClusters/%s", parent, clusterId);

      GameServerCluster cluster =
          GameServerCluster.newBuilder().setName(clusterName).putLabels("key", "value").build();

      FieldMask fieldMask = FieldMask.newBuilder().addPaths("labels").build();
      OperationFuture<GameServerCluster, OperationMetadata> call =
          client.updateGameServerClusterAsync(cluster, fieldMask);

      GameServerCluster updated = call.get(1, TimeUnit.MINUTES);
      System.out.println("Game Server Cluster updated: " + updated.getName());
    }
  }
}

Python

Pour savoir comment installer et utiliser la bibliothèque cliente pour Game Servers, consultez la page Bibliothèques clientes Game Servers.

def update_cluster(project_id, location, realm_id, cluster_id):
    """Updates a game server cluster."""

    client = gaming.GameServerClustersServiceClient()

    request = game_server_clusters.UpdateGameServerClusterRequest(
        game_server_cluster=game_server_clusters.GameServerCluster(
            name=f"projects/{project_id}/locations/{location}/realms/{realm_id}/gameServerClusters/{cluster_id}",
            labels={"label-key-1": "label-value-1", "label-key-2": "label-value-2"},
        ),
        update_mask=field_mask.FieldMask(paths=["labels"]),
    )

    operation = client.update_game_server_cluster(request)
    print(f"Update cluster operation: {operation.operation.name}")
    operation.result(timeout=120)

Étape suivante

Pour rechercher et filtrer des exemples de code pour d'autres produits Google Cloud, consultez l'exemple de navigateur Google Cloud.