Game Servers のデプロイを作成します。
もっと見る
このコードサンプルを含む詳細なドキュメントについては、以下をご覧ください。
コードサンプル
C#
Game Servers のクライアント ライブラリをインストールして使用する方法については、Game Servers クライアント ライブラリをご覧ください。
using Google.Api.Gax.ResourceNames;
using Google.Cloud.Gaming.V1;
using Google.LongRunning;
using System.Threading.Tasks;
public class CreateDeploymentSample
{
public async Task<GameServerDeployment> CreateDeploymentAsync(
string projectId, string deploymentId)
{
// Create the client.
GameServerDeploymentsServiceClient client = await GameServerDeploymentsServiceClient.CreateAsync();
GameServerDeployment deployment = new GameServerDeployment()
{
GameServerDeploymentName = GameServerDeploymentName.FromProjectLocationDeployment(projectId, "global", deploymentId)
};
CreateGameServerDeploymentRequest request = new CreateGameServerDeploymentRequest
{
DeploymentId = deploymentId,
ParentAsLocationName = LocationName.FromProjectLocation(projectId, "global"),
GameServerDeployment = deployment
};
// Make the request.
Operation<GameServerDeployment, OperationMetadata> response = await client.CreateGameServerDeploymentAsync(request);
// Poll until the returned long-running operation is complete.
Operation<GameServerDeployment, OperationMetadata> completedResponse = await response.PollUntilCompletedAsync();
// Retrieve the operation result.
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"
)
// createGameServerDeployment creates a game server deployment.
func createGameServerDeployment(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()
// 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.
req := &gamingpb.CreateGameServerDeploymentRequest{
Parent: fmt.Sprintf("projects/%s/locations/global", projectID),
DeploymentId: deploymentID,
GameServerDeployment: &gamingpb.GameServerDeployment{
Description: "My Game Server Deployment",
},
}
op, err := client.CreateGameServerDeployment(ctx, req)
if err != nil {
return fmt.Errorf("CreateGameServerDeployment: %v", err)
}
resp, err := op.Wait(ctx)
if err != nil {
return fmt.Errorf("Wait: %v", err)
}
fmt.Fprintf(w, "Deployment created: %v", resp.Name)
return nil
}
Java
Game Servers のクライアント ライブラリをインストールして使用する方法については、Game Servers クライアント ライブラリをご覧ください。
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.gaming.v1.CreateGameServerDeploymentRequest;
import com.google.cloud.gaming.v1.GameServerDeployment;
import com.google.cloud.gaming.v1.GameServerDeploymentsServiceClient;
import com.google.cloud.gaming.v1.OperationMetadata;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class CreateDeployment {
public static void createGameServerDeployment(String projectId, String deploymentId)
throws IOException, InterruptedException, ExecutionException, TimeoutException {
// 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()) {
// Build a spec as shown at https://agones.dev/site/docs/reference/gameserver/
JsonObject container = new JsonObject();
container.addProperty("name", "default");
container.addProperty("image", "gcr.io/agones-images/default:1.0");
JsonArray containers = new JsonArray();
containers.add(container);
JsonObject spec = new JsonObject();
spec.add("containers", containers);
JsonObject template = new JsonObject();
template.add("spec", spec);
JsonObject port = new JsonObject();
port.addProperty("name", "default");
JsonArray ports = new JsonArray();
ports.add(port);
JsonObject specObject = new JsonObject();
specObject.add("ports", ports);
specObject.add("template", template);
String parent = String.format("projects/%s/locations/global", projectId);
String deploymentName = String.format("%s/gameServerDeployments/%s", parent, deploymentId);
GameServerDeployment gameServerDeployment =
GameServerDeployment.newBuilder().setName(deploymentName).build();
CreateGameServerDeploymentRequest request =
CreateGameServerDeploymentRequest.newBuilder()
.setParent(parent)
.setDeploymentId(deploymentId)
.setGameServerDeployment(gameServerDeployment)
.build();
OperationFuture<GameServerDeployment, OperationMetadata> call =
client.createGameServerDeploymentAsync(request);
GameServerDeployment created = call.get(1, TimeUnit.MINUTES);
System.out.println("Game Server Deployment created: " + created.getName());
}
}
}
Node.js
Game Servers のクライアント ライブラリをインストールして使用する方法については、Game Servers クライアント ライブラリをご覧ください。
const {
GameServerDeploymentsServiceClient,
} = require('@google-cloud/game-servers');
const client = new GameServerDeploymentsServiceClient();
async function createGameServerDeployment() {
/**
* 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 = {
parent: `projects/${projectId}/locations/global`,
deploymentId: deploymentId,
gameServerDeployment: {
description: 'nodejs test deployment',
},
};
const [operation] = await client.createGameServerDeployment(request);
const [result] = await operation.promise();
console.log('Game Server Deployment created:');
console.log(`\t Deployment name: ${result.name}`);
console.log(`\t Deployment description: ${result.description}`);
}
createGameServerDeployment();
Python
Game Servers のクライアント ライブラリをインストールして使用する方法については、Game Servers クライアント ライブラリをご覧ください。
def create_deployment(project_id, deployment_id):
"""Creates 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.CreateGameServerDeploymentRequest(
parent=f"projects/{project_id}/locations/global",
deployment_id=deployment_id,
game_server_deployment=game_server_deployments.GameServerDeployment(
description="My Game Server Deployment"
),
)
operation = client.create_game_server_deployment(request)
print(f"Create deployment operation: {operation.operation.name}")
operation.result(timeout=120)
次のステップ
他の Google Cloud プロダクトに関連するコードサンプルの検索およびフィルタ検索を行うには、Google Cloud のサンプルをご覧ください。