删除一个游戏服务器部署。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
C#
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库。
using Google.Cloud.Gaming.V1;
using Google.LongRunning;
using Google.Protobuf.WellKnownTypes;
public class DeleteDeploymentSample
{
public void DeleteDeployment(
string projectId, string deploymentId)
{
// Create the client.
GameServerDeploymentsServiceClient client = GameServerDeploymentsServiceClient.Create();
DeleteGameServerDeploymentRequest request = new DeleteGameServerDeploymentRequest
{
GameServerDeploymentName = GameServerDeploymentName.FromProjectLocationDeployment(projectId, "global", deploymentId)
};
// Make the request.
Operation<Empty, OperationMetadata> response = client.DeleteGameServerDeployment(request);
// Poll until the returned long-running operation is complete.
response.PollUntilCompleted();
}
}
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"
)
// deleteGameServerDeployment deletes a game server deployment.
func deleteGameServerDeployment(w io.Writer, projectID, deploymentID string) error {
// projectID := "my-project"
// deploymentID := "mydeployment"
ctx := context.Background()
client, err := gaming.NewGameServerDeploymentsClient(ctx)
if err != nil {
return fmt.Errorf("NewGameServerDeploymentsClient: %v", err)
}
defer client.Close()
req := &gamingpb.DeleteGameServerDeploymentRequest{
Name: fmt.Sprintf("projects/%s/locations/global/gameServerDeployments/%s", projectID, deploymentID),
}
op, err := client.DeleteGameServerDeployment(ctx, req)
if err != nil {
return fmt.Errorf("DeleteGameServerDeployment: %v", err)
}
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("Wait: %v", err)
}
fmt.Fprintf(w, "Deployment deleted.")
return nil
}
Java
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库。
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.gaming.v1.GameServerDeploymentsServiceClient;
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 DeleteDeployment {
public static void deleteGameServerDeployment(String projectId, String deploymentId)
throws InterruptedException, ExecutionException, TimeoutException, IOException {
// String projectId = "your-project-id";
// String deploymentId = "your-game-server-deployment-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 (GameServerDeploymentsServiceClient client = GameServerDeploymentsServiceClient.create()) {
String parent = String.format("projects/%s/locations/global", projectId);
String deploymentName = String.format("%s/gameServerDeployments/%s", parent, deploymentId);
OperationFuture<Empty, OperationMetadata> call =
client.deleteGameServerDeploymentAsync(deploymentName);
call.get(1, TimeUnit.MINUTES);
System.out.println("Game Server Deployment deleted: " + deploymentName);
}
}
}
Node.js
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库。
const {
GameServerDeploymentsServiceClient,
} = require('@google-cloud/game-servers');
const client = new GameServerDeploymentsServiceClient();
async function deleteGameServerDeployment() {
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'Your Google Cloud Project ID';
// const deploymentId = 'A unique ID for the Game Server Deployment';
const request = {
// The full resource name
name: client.gameServerDeploymentPath(projectId, 'global', deploymentId),
};
const [operation] = await client.deleteGameServerDeployment(request);
await operation.promise();
console.log(`Deployment with name ${request.name} deleted.`);
}
deleteGameServerDeployment();
Python
如需了解如何安装和使用 Game Servers 客户端库,请参阅 Game Servers 客户端库。
def delete_deployment(project_id, deployment_id):
"""Deletes a game server deployment."""
client = gaming.GameServerDeploymentsServiceClient()
# Location is hard coded as global, as game server deployments can
# only be created in global. This is done for all operations on
# game server deployments, as well as for its child resource types.
request = game_server_deployments.DeleteGameServerDeploymentRequest(
name=f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}",
)
operation = client.delete_game_server_deployment(request)
print(f"Delete deployment operation: {operation.operation.name}")
operation.result(timeout=120)
后续步骤
如需搜索并过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器。