Get realm

Get details about a Game Server realm.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample

Go

To learn how to install and use the client library for Game Servers, see the Game Servers Client Libraries.


import (
	"context"
	"fmt"
	"io"

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

// getRealm retrieves info on a realm.
func getRealm(w io.Writer, projectID, location, realmID string) error {
	// projectID := "my-project"
	// location := "global"
	// realmID := "myrealm"
	ctx := context.Background()
	client, err := gaming.NewRealmsClient(ctx)
	if err != nil {
		return fmt.Errorf("NewRealmsClient: %v", err)
	}
	defer client.Close()

	req := &gamingpb.GetRealmRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/realms/%s", projectID, location, realmID),
	}

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

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

Java

To learn how to install and use the client library for Game Servers, see the Game Servers Client Libraries.


import com.google.cloud.gaming.v1.Realm;
import com.google.cloud.gaming.v1.RealmsServiceClient;
import java.io.IOException;

public class GetRealm {
  public static void getRealm(String projectId, String regionId, String realmId)
      throws IOException {
    // String projectId = "your-project-id";
    // String regionId = "us-central1-f";
    // String realmId = "your-realm-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 (RealmsServiceClient client = RealmsServiceClient.create()) {
      String realmName =
          String.format("projects/%s/locations/%s/realms/%s", projectId, regionId, realmId);

      Realm allocationPolicy = client.getRealm(realmName);

      System.out.println("Realm found: " + allocationPolicy.getName());
    }
  }
}

Node.js

To learn how to install and use the client library for Game Servers, see the Game Servers Client Libraries.

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

const client = new RealmsServiceClient();

async function getRealm() {
  /**
   * 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 = 'Unique identifier of the realm';
  const request = {
    // Realm name is the full resource name including project ID and location
    name: client.realmPath(projectId, location, realmId),
  };

  const [realm] = await client.getRealm(request);
  console.log(`Realm name: ${realm.name}`);
  console.log(`Realm description: ${realm.description}`);
  console.log(`Realm time zone: ${realm.timeZone}`);

  const createTime = realm.createTime;
  const createDate = new Date(createTime.seconds * 1000);

  console.log(`Realm created on: ${createDate.toLocaleDateString()}`);
}

getRealm();

Python

To learn how to install and use the client library for Game Servers, see the Game Servers Client Libraries.

def get_realm(project_id, location, realm_id):
    """Gets a realm."""

    client = gaming.RealmsServiceClient()

    request = realms.GetRealmRequest(
        name=f"projects/{project_id}/locations/{location}/realms/{realm_id}",
    )

    response = client.get_realm(request)
    print(f"Get realm response:\n{response}")
    return response