클러스터 업데이트

Game Server 클러스터 세부정보를 업데이트합니다.

더 살펴보기

이 코드 샘플이 포함된 자세한 문서는 다음을 참조하세요.

코드 샘플

C#

Game Servers용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Game Servers 클라이언트 라이브러리를 참조하세요.


using Google.Cloud.Gaming.V1;
using Google.LongRunning;
using Google.Protobuf.WellKnownTypes;
using System.Threading.Tasks;

public class UpdateClusterSample
{
    public async Task<GameServerCluster> UpdateClusterAsync(
        string projectId, string regionId, string realmId, string clusterId)
    {
        // Create the client.
        GameServerClustersServiceClient client = await GameServerClustersServiceClient.CreateAsync();

        GameServerCluster cluster = new GameServerCluster()
        {
            GameServerClusterName = GameServerClusterName.FromProjectLocationRealmCluster(projectId, regionId, realmId, clusterId)
        };
        cluster.Labels.Add("label-key-1", "label-value-1");
        cluster.Labels.Add("label-key-2", "label-value-2");

        UpdateGameServerClusterRequest request = new UpdateGameServerClusterRequest
        {
            GameServerCluster = cluster,
            UpdateMask = new FieldMask { Paths = { "labels" } }
        };

        // Make the request.
        Operation<GameServerCluster, OperationMetadata> response = await client.UpdateGameServerClusterAsync(request);
        Operation<GameServerCluster, OperationMetadata> completedResponse = await response.PollUntilCompletedAsync();

        // Retrieve the operation result. This result will NOT contain the updated labels.
        // If you want to get the updated resource, use a GET request on the resource.
        return completedResponse.Result;
    }
}

Go

Game Servers용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Game Servers 클라이언트 라이브러리를 참조하세요.


import (
	"context"
	"fmt"
	"io"

	gaming "cloud.google.com/go/gaming/apiv1"
	gamingpb "google.golang.org/genproto/googleapis/cloud/gaming/v1"
	"google.golang.org/protobuf/types/known/fieldmaskpb"
)

// updateGameServerCluster updates a game server cluster.
func updateGameServerCluster(w io.Writer, projectID, location, realmID, clusterID string) error {
	// projectID := "my-project"
	// location := "global"
	// realmID := "myrealm"
	// clusterID := "mycluster"
	ctx := context.Background()
	client, err := gaming.NewGameServerClustersClient(ctx)
	if err != nil {
		return fmt.Errorf("NewGameServerClustersClient: %v", err)
	}
	defer client.Close()

	req := &gamingpb.UpdateGameServerClusterRequest{
		GameServerCluster: &gamingpb.GameServerCluster{
			Name:        fmt.Sprintf("projects/%s/locations/%s/realms/%s/gameServerClusters/%s", projectID, location, realmID, clusterID),
			Description: "My Updated Game Server Cluster",
			Labels: map[string]string{
				"label-key-1": "label-value-1",
			},
		},
		UpdateMask: &fieldmaskpb.FieldMask{
			Paths: []string{
				"description", "labels",
			},
		},
	}

	op, err := client.UpdateGameServerCluster(ctx, req)
	if err != nil {
		return fmt.Errorf("UpdateGameServerCluster: %v", err)
	}
	resp, err := op.Wait(ctx)
	if err != nil {
		return fmt.Errorf("Wait: %v", err)
	}

	fmt.Fprintf(w, "Cluster updated: %v", resp.Name)
	return nil
}

자바

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());
    }
  }
}

Node.js

Game Servers용 클라이언트 라이브러리를 설치하고 사용하는 방법은 Game Servers 클라이언트 라이브러리를 참조하세요.

const {
  GameServerClustersServiceClient,
} = require('@google-cloud/game-servers');

const client = new GameServerClustersServiceClient();

async function updateGameServerCluster() {
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const projectId = 'Your Google Cloud Project ID';
  // const location = 'A Compute Engine region, e.g. "us-central1"';
  // const realmId = 'The ID of the realm of this cluster';
  // const gameClusterId = 'A unique ID for this Game Server Cluster';
  const request = {
    // Provide full resource name of a Game Server Cluster
    gameServerCluster: {
      name: client.gameServerClusterPath(
        projectId,
        location,
        realmId,
        gameClusterId
      ),
      labels: {
        'label-key-1': 'label-value-1',
      },
      description: 'My updated Game Server Cluster',
    },
    updateMask: {
      paths: ['labels', 'description'],
    },
  };

  const [operation] = await client.updateGameServerCluster(request);
  const [result] = await operation.promise();

  console.log(`Cluster updated: ${result.name}`);
}

updateGameServerCluster();

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 샘플 브라우저를 참조하세요.