更新集群

更新游戏服务器集群的详细信息。

代码示例

Java

如需了解如何安装和使用 Game Servers 客户端库,请参阅 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

如需了解如何安装和使用 Game Servers 客户端库,请参阅 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)

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器