Cluster löschen

Einen Game Servers-Cluster löschen.

Dokumentationsseiten mit diesem Codebeispiel

Die folgenden Dokumente enthalten das Codebeispiel im Kontext:

Codebeispiel

Go

Informationen zum Installieren und Verwenden der Clientbibliothek für Game Servers finden Sie unter Game Servers-Clientbibliotheken.


import (
	"context"
	"fmt"
	"io"

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

// deleteCluster unregisters a game server cluster.
func deleteCluster(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.DeleteGameServerClusterRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/realms/%s/gameServerClusters/%s", projectID, location, realmID, clusterID),
	}

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

	fmt.Fprintf(w, "Cluster deleted.")
	return nil
}

Java

Informationen zum Installieren und Verwenden der Clientbibliothek für Game Servers finden Sie unter Game Servers-Clientbibliotheken.


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

public class DeleteCluster {

  public static void deleteGameServerCluster(
      String projectId, String regionId, String realmId, String clusterId)
      throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // String projectId = "your-project-id";
    // String regionId = "us-central1-f";
    // 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);

      OperationFuture<Empty, OperationMetadata> call =
          client.deleteGameServerClusterAsync(clusterName);

      call.get(1, TimeUnit.MINUTES);
      System.out.println("Game Server Cluster deleted: " + clusterName);
    }
  }
}

Node.js

Informationen zum Installieren und Verwenden der Clientbibliothek für Game Servers finden Sie unter Game Servers-Clientbibliotheken.

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

const client = new GameServerClustersServiceClient();

async function deleteGameServerCluster() {
  /**
   * 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 to locate this cluster in';
  // const gameClusterId = 'The unique ID for this Game Server Cluster';
  const request = {
    // Provide full resource name of a Game Server Realm
    name: client.gameServerClusterPath(
      projectId,
      location,
      realmId,
      gameClusterId
    ),
  };

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

  console.log('Game Server cluster deleted');
}

deleteGameServerCluster();

Python

Informationen zum Installieren und Verwenden der Clientbibliothek für Game Servers finden Sie unter Game Servers-Clientbibliotheken.

def delete_cluster(project_id, location, realm_id, cluster_id):
    """Deletes a game server cluster."""

    client = gaming.GameServerClustersServiceClient()

    request = game_server_clusters.DeleteGameServerClusterRequest(
        name=f"projects/{project_id}/locations/{location}/realms/{realm_id}/gameServerClusters/{cluster_id}",
    )

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

Nächste Schritte

Codebeispiele für andere Google Cloud-Produkte finden Sie im Google Cloud-Beispielbrowser.