Répertorier les déploiements

Répertoriez les déploiements de Game Servers dans une région ou un emplacement spécifiés.

Pages de documentation incluant cet exemple de code

Pour afficher l'exemple de code utilisé en contexte, consultez la documentation suivante :

Exemple de code

Go

Pour savoir comment installer et utiliser la bibliothèque cliente pour Game Servers, consultez la page Bibliothèques clientes Game Servers.


import (
	"context"
	"fmt"
	"io"

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

// listGameServerDeployments lists the game server deployments.
func listGameServerDeployments(w io.Writer, projectID string) error {
	// projectID := "my-project"
	// realmID := "myrealm"
	ctx := context.Background()
	client, err := gaming.NewGameServerDeploymentsClient(ctx)
	if err != nil {
		return fmt.Errorf("NewGameServerDeploymentsClient: %v", err)
	}
	defer client.Close()

	req := &gamingpb.ListGameServerDeploymentsRequest{
		Parent: fmt.Sprintf("projects/%s/locations/global", projectID),
	}

	it := client.ListGameServerDeployments(ctx, req)
	for {
		resp, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("Next: %v", err)
		}

		fmt.Fprintf(w, "Deployment listed: %v\n", resp.Name)
	}

	return nil
}

Java

Pour savoir comment installer et utiliser la bibliothèque cliente pour Game Servers, consultez la page Bibliothèques clientes Game Servers.


import com.google.cloud.gaming.v1.GameServerDeployment;
import com.google.cloud.gaming.v1.GameServerDeploymentsServiceClient;
import com.google.cloud.gaming.v1.GameServerDeploymentsServiceClient.ListGameServerDeploymentsPagedResponse;
import com.google.cloud.gaming.v1.ListGameServerDeploymentsRequest;
import com.google.common.base.Strings;
import java.io.IOException;

public class ListDeployments {
  public static void listGameServerDeployments(String projectId) throws IOException {
    // String projectId = "your-project-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);

      ListGameServerDeploymentsPagedResponse response = client.listGameServerDeployments(parent);
      for (GameServerDeployment deployment : response.iterateAll()) {
        System.out.println("Game Server Deployment found: " + deployment.getName());
      }

      while (!Strings.isNullOrEmpty(response.getNextPageToken())) {
        ListGameServerDeploymentsRequest request =
            ListGameServerDeploymentsRequest.newBuilder()
                .setParent(parent)
                .setPageToken(response.getNextPageToken())
                .build();
        response = client.listGameServerDeployments(request);
        for (GameServerDeployment deployment : response.iterateAll()) {
          System.out.println("Game Server Deployment found: " + deployment.getName());
        }
      }
    }
  }
}

Node.js

Pour savoir comment installer et utiliser la bibliothèque cliente pour Game Servers, consultez la page Bibliothèques clientes Game Servers.

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

const client = new GameServerDeploymentsServiceClient();

async function listGameServerDeployments() {
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const projectId = 'Your Google Cloud Project ID';
  const request = {
    parent: `projects/${projectId}/locations/global`,
  };

  const [results] = await client.listGameServerDeployments(request);
  for (const deployment of results) {
    console.log(`Deployment name: ${deployment.name}`);
    console.log(`Deployment description: ${deployment.description}`);

    const createTime = deployment.createTime;
    const createDate = new Date(createTime.seconds * 1000);
    console.log(
      `Deployment created on: ${createDate.toLocaleDateString()}\n`
    );
  }
}

listGameServerDeployments();

Python

Pour savoir comment installer et utiliser la bibliothèque cliente pour Game Servers, consultez la page Bibliothèques clientes Game Servers.

def list_deployments(project_id):
    """Lists the existing game server deployments."""

    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.
    response = client.list_game_server_deployments(
        parent=f"projects/{project_id}/locations/global"
    )

    for deployment in response.game_server_deployments:
        print(f"Name: {deployment.name}")

    return response.game_server_deployments

Étape suivante

Pour rechercher et filtrer des exemples de code pour d'autres produits Google Cloud, consultez l'exemple de navigateur Google Cloud.