From 2d300031213fda468b6aa52d3ee8635215c37986 Mon Sep 17 00:00:00 2001 From: yingxin Date: Fri, 31 Jul 2020 10:57:00 -0700 Subject: [PATCH] docs: add more code samples for game servers (#32) --- samples/snippets/README.rst | 521 +++++++++++++++++- samples/snippets/README.rst.in | 67 ++- samples/snippets/create_cluster.py | 70 +++ samples/snippets/create_config.py | 133 +++++ samples/snippets/create_deployment.py | 10 +- samples/snippets/create_realm.py | 58 ++ samples/snippets/delete_cluster.py | 54 ++ samples/snippets/delete_config.py | 56 ++ samples/snippets/delete_deployment.py | 10 +- samples/snippets/delete_realm.py | 53 ++ .../snippets/deployment_and_config_test.py | 221 ++++++++ samples/snippets/deployment_test.py | 96 ---- samples/snippets/get_cluster.py | 54 ++ samples/snippets/get_config.py | 56 ++ samples/snippets/get_deployment.py | 12 +- samples/snippets/get_realm.py | 53 ++ samples/snippets/get_rollout.py | 10 +- samples/snippets/list_clusters.py | 53 ++ samples/snippets/list_configs.py | 55 ++ samples/snippets/list_deployments.py | 16 +- samples/snippets/list_realms.py | 53 ++ samples/snippets/realm_and_cluster_test.py | 159 ++++++ samples/snippets/update_cluster.py | 59 ++ samples/snippets/update_deployment.py | 60 ++ samples/snippets/update_realm.py | 58 ++ samples/snippets/update_rollout_default.py | 59 ++ samples/snippets/update_rollout_override.py | 67 +++ .../snippets/update_rollout_remove_default.py | 14 +- .../update_rollout_remove_override.py | 58 ++ 29 files changed, 2103 insertions(+), 142 deletions(-) create mode 100644 samples/snippets/create_cluster.py create mode 100644 samples/snippets/create_config.py create mode 100644 samples/snippets/create_realm.py create mode 100644 samples/snippets/delete_cluster.py create mode 100644 samples/snippets/delete_config.py create mode 100644 samples/snippets/delete_realm.py create mode 100644 samples/snippets/deployment_and_config_test.py delete mode 100644 samples/snippets/deployment_test.py create mode 100644 samples/snippets/get_cluster.py create mode 100644 samples/snippets/get_config.py create mode 100644 samples/snippets/get_realm.py create mode 100644 samples/snippets/list_clusters.py create mode 100644 samples/snippets/list_configs.py create mode 100644 samples/snippets/list_realms.py create mode 100644 samples/snippets/realm_and_cluster_test.py create mode 100644 samples/snippets/update_cluster.py create mode 100644 samples/snippets/update_deployment.py create mode 100644 samples/snippets/update_realm.py create mode 100644 samples/snippets/update_rollout_default.py create mode 100644 samples/snippets/update_rollout_override.py create mode 100644 samples/snippets/update_rollout_remove_override.py diff --git a/samples/snippets/README.rst b/samples/snippets/README.rst index 8247d645..492c3d92 100644 --- a/samples/snippets/README.rst +++ b/samples/snippets/README.rst @@ -65,7 +65,7 @@ Install Dependencies Samples ------------------------------------------------------------------------------- -Create deployment +Create game server deployment +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .. image:: https://gstatic.com/cloudssh/images/open-btn.png @@ -92,7 +92,7 @@ To run this sample: -Delete deployment +Delete game server deployment +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .. image:: https://gstatic.com/cloudssh/images/open-btn.png @@ -119,7 +119,7 @@ To run this sample: -Get deployment +Get game server deployment +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .. image:: https://gstatic.com/cloudssh/images/open-btn.png @@ -146,6 +146,33 @@ To run this sample: +Update game server deployment ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/update_deployment.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python update_deployment.py + + usage: update_deployment.py [-h] --project-id PROJECT_ID --deployment-id + DEPLOYMENT_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --deployment-id DEPLOYMENT_ID + Your game server deployment ID. + + + Get rollout +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -173,7 +200,7 @@ To run this sample: -List deployments +List game server deployments +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .. image:: https://gstatic.com/cloudssh/images/open-btn.png @@ -197,7 +224,70 @@ To run this sample: -Update rollout by removing default config +Update rollout to set default config ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/update_rollout_default.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python update_rollout_default.py + + usage: update_rollout_default.py [-h] --project-id PROJECT_ID --deployment-id + DEPLOYMENT_ID --config-id CONFIG_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --deployment-id DEPLOYMENT_ID + Your game server deployment ID. + --config-id CONFIG_ID + Your game server config ID. + + + +Update rollout to set override config ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/update_rollout_override.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python update_rollout_override.py + + usage: update_rollout_override.py [-h] --project-id PROJECT_ID --deployment-id + DEPLOYMENT_ID --config-id CONFIG_ID + --realm-location REALM_LOCATION --realm-id + REALM_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --deployment-id DEPLOYMENT_ID + Your game server deployment ID. + --config-id CONFIG_ID + Your game server config ID. + --realm-location REALM_LOCATION + Your game server config ID. + --realm-id REALM_ID Your game server config ID. + + + +Update rollout to remove default config +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .. image:: https://gstatic.com/cloudssh/images/open-btn.png @@ -224,6 +314,427 @@ To run this sample: +Update rollout to remove override config ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/update_rollout_remove_override.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python update_rollout_remove_override.py + + usage: update_rollout_remove_override.py [-h] --project-id PROJECT_ID + --deployment-id DEPLOYMENT_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --deployment-id DEPLOYMENT_ID + Your game server deployment ID. + + + +Create game server config ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/create_config.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python create_config.py + + usage: create_config.py [-h] --project-id PROJECT_ID --deployment-id + DEPLOYMENT_ID --config-id CONFIG_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --deployment-id DEPLOYMENT_ID + Your game server deployment ID. + --config-id CONFIG_ID + Your game server config ID. + + + +Delete game server config ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/delete_config.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python delete_config.py + + usage: delete_config.py [-h] --project-id PROJECT_ID --deployment-id + DEPLOYMENT_ID --config-id CONFIG_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --deployment-id DEPLOYMENT_ID + Your game server deployment ID. + --config-id CONFIG_ID + Your game server config ID. + + + +Get game server config ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/get_config.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python get_config.py + + usage: get_config.py [-h] --project-id PROJECT_ID --deployment-id + DEPLOYMENT_ID --config-id CONFIG_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --deployment-id DEPLOYMENT_ID + Your game server deployment ID. + --config-id CONFIG_ID + Your game server config ID. + + + +List game server configs ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/list_configs.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python list_configs.py + + usage: list_configs.py [-h] --project-id PROJECT_ID --deployment-id + DEPLOYMENT_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --deployment-id DEPLOYMENT_ID + Your game server deployment ID. + + + +Create realm ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/create_realm.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python create_realm.py + + usage: create_realm.py [-h] --project-id PROJECT_ID --location LOCATION + --realm-id REALM_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --location LOCATION Your realm location. + --realm-id REALM_ID Your realm ID. + + + +Delete realm ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/delete_realm.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python delete_realm.py + + usage: delete_realm.py [-h] --project-id PROJECT_ID --location LOCATION + --realm-id REALM_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --location LOCATION Your realm location. + --realm-id REALM_ID Your realm ID. + + + +Get realm ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/get_realm.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python get_realm.py + + usage: get_realm.py [-h] --project-id PROJECT_ID --location LOCATION + --realm-id REALM_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --location LOCATION Your realm location. + --realm-id REALM_ID Your realm ID. + + + +Update realm ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/update_realm.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python update_realm.py + + usage: update_realm.py [-h] --project-id PROJECT_ID --location LOCATION + --realm-id REALM_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --location LOCATION Your realm location. + --realm-id REALM_ID Your realm ID. + + + +List realms ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/list_realms.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python list_realms.py + + usage: list_realms.py [-h] --project-id PROJECT_ID --location LOCATION + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --location LOCATION Your realm location. + + + +Create game server cluster ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/create_cluster.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python create_cluster.py + + usage: create_cluster.py [-h] --project-id PROJECT_ID --location LOCATION + --realm-id REALM_ID --cluster-id CLUSTER_ID + --gke-cluster-name GKE_CLUSTER_NAME + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --location LOCATION Your realm location. + --realm-id REALM_ID Your realm ID. + --cluster-id CLUSTER_ID + Your game server cluster ID. + --gke-cluster-name GKE_CLUSTER_NAME + The name of the GKE cluster which is managed by the + game server cluster being created. + + + +Delete game server cluster ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/delete_cluster.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python delete_cluster.py + + usage: delete_cluster.py [-h] --project-id PROJECT_ID --location LOCATION + --realm-id REALM_ID --cluster-id CLUSTER_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --location LOCATION Your realm location. + --realm-id REALM_ID Your realm ID. + --cluster-id CLUSTER_ID + Your game server cluster ID. + + + +Get game server cluster ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/get_cluster.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python get_cluster.py + + usage: get_cluster.py [-h] --project-id PROJECT_ID --location LOCATION + --realm-id REALM_ID --cluster-id CLUSTER_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --location LOCATION Your realm location. + --realm-id REALM_ID Your realm ID. + --cluster-id CLUSTER_ID + Your game server cluster ID. + + + +Update game server cluster ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/update_cluster.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python update_cluster.py + + usage: update_cluster.py [-h] --project-id PROJECT_ID --location LOCATION + --realm-id REALM_ID --cluster-id CLUSTER_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --location LOCATION Your realm location. + --realm-id REALM_ID Your realm ID. + --cluster-id CLUSTER_ID + Your game server cluster ID. + + + +List game server cluster ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/list_clusters.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python list_clusters.py + + usage: list_clusters.py [-h] --project-id PROJECT_ID --location LOCATION + --realm-id REALM_ID + + optional arguments: + -h, --help show this help message and exit + --project-id PROJECT_ID + Your cloud project ID. + --location LOCATION Your realm location. + --realm-id REALM_ID Your realm ID. + + + The client library diff --git a/samples/snippets/README.rst.in b/samples/snippets/README.rst.in index 5da3af02..d2c076f9 100644 --- a/samples/snippets/README.rst.in +++ b/samples/snippets/README.rst.in @@ -14,23 +14,80 @@ setup: - install_deps samples: -- name: Create deployment +- name: Create game server deployment file: create_deployment.py show_help: True -- name: Delete deployment +- name: Delete game server deployment file: delete_deployment.py show_help: True -- name: Get deployment +- name: Get game server deployment file: get_deployment.py show_help: True +- name: Update game server deployment + file: update_deployment.py + show_help: True - name: Get rollout file: get_rollout.py show_help: True -- name: List deployments +- name: List game server deployments file: list_deployments.py show_help: True -- name: Update rollout by removing default config +- name: Update rollout to set default config + file: update_rollout_default.py + show_help: True +- name: Update rollout to set override config + file: update_rollout_override.py + show_help: True +- name: Update rollout to remove default config file: update_rollout_remove_default.py show_help: True +- name: Update rollout to remove override config + file: update_rollout_remove_override.py + show_help: True + +- name: Create game server config + file: create_config.py + show_help: True +- name: Delete game server config + file: delete_config.py + show_help: True +- name: Get game server config + file: get_config.py + show_help: True +- name: List game server configs + file: list_configs.py + show_help: True + +- name: Create realm + file: create_realm.py + show_help: True +- name: Delete realm + file: delete_realm.py + show_help: True +- name: Get realm + file: get_realm.py + show_help: True +- name: Update realm + file: update_realm.py + show_help: True +- name: List realms + file: list_realms.py + show_help: True + +- name: Create game server cluster + file: create_cluster.py + show_help: True +- name: Delete game server cluster + file: delete_cluster.py + show_help: True +- name: Get game server cluster + file: get_cluster.py + show_help: True +- name: Update game server cluster + file: update_cluster.py + show_help: True +- name: List game server cluster + file: list_clusters.py + show_help: True cloud_client_library: true diff --git a/samples/snippets/create_cluster.py b/samples/snippets/create_cluster.py new file mode 100644 index 00000000..e630d7eb --- /dev/null +++ b/samples/snippets/create_cluster.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for creating a game server cluster. + +Example usage: + python create_cluster.py --project-id --location + --realm-id --cluster-id --gke-cluster-name +""" + +import argparse + +from google.cloud import gaming +from google.cloud.gaming_v1.types import game_server_clusters + + +# [START cloud_game_servers_cluster_create] +def create_cluster(project_id, location, realm_id, cluster_id, gke_cluster_name): + """Creates a game server cluster.""" + + client = gaming.GameServerClustersServiceClient() + + gke_cluster_reference = game_server_clusters.GkeClusterReference( + cluster=gke_cluster_name, + ) + request = game_server_clusters.CreateGameServerClusterRequest( + parent=f"projects/{project_id}/locations/{location}/realms/{realm_id}", + game_server_cluster_id=cluster_id, + game_server_cluster=game_server_clusters.GameServerCluster( + description="My Game Server Cluster", + connection_info=game_server_clusters.GameServerClusterConnectionInfo( + gke_cluster_reference=gke_cluster_reference, + namespace="default", + ), + ), + ) + + operation = client.create_game_server_cluster(request) + print(f"Create cluster operation: {operation.operation.name}") + operation.result(timeout=120) +# [END cloud_game_servers_cluster_create] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--location', help='Your realm location.', required=True) + parser.add_argument('--realm-id', help='Your realm ID.', required=True) + parser.add_argument('--cluster-id', help='Your game server cluster ID.', required=True) + parser.add_argument( + '--gke-cluster-name', + help='The name of the GKE cluster which is managed by the game server cluster being created.', + required=True) + + args = parser.parse_args() + + create_cluster(args.project_id, args.location, args.realm_id, args.cluster_id, args.gke_cluster_name) diff --git a/samples/snippets/create_config.py b/samples/snippets/create_config.py new file mode 100644 index 00000000..e8479831 --- /dev/null +++ b/samples/snippets/create_config.py @@ -0,0 +1,133 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for creating a game server config. + +Example usage: + python create_config.py --project-id --deployment-id --config-id +""" + +import argparse + +from google.cloud import gaming +from google.cloud.gaming_v1.types import game_server_configs + +# [START cloud_game_servers_config_create] + +# FLEET_SPEC is the spec portion of an agones Fleet. It must be in JSON format. +# See https://agones.dev/site/docs/reference/fleet/ for more on fleets. +FLEET_SPEC = """ +{ + "replicas": 10, + "scheduling": "Packed", + "strategy": { + "type": "RollingUpdate", + "rollingUpdate": { + "maxSurge": "25%", + "maxUnavailable": "25%" + } + }, + "template": { + "metadata": { + "labels": { + "gameName": "udp-server" + } + }, + "spec": { + "ports": [ + { + "name": "default", + "portPolicy": "Dynamic", + "containerPort": 2156, + "protocol": "TCP" + } + ], + "health": { + "initialDelaySeconds": 30, + "periodSeconds": 60 + }, + "sdkServer": { + "logLevel": "Info", + "grpcPort": 9357, + "httpPort": 9358 + }, + "template": { + "spec": { + "containers": [ + { + "name": "dedicated", + "image": "gcr.io/agones-images/udp-server:0.17", + "imagePullPolicy": "Always", + "resources": { + "requests": { + "memory": "200Mi", + "cpu": "500m" + }, + "limits": { + "memory": "200Mi", + "cpu": "500m" + } + } + } + ] + } + } + } + } +} +""" + + +def create_config(project_id, deployment_id, config_id): + """Creates a game server config.""" + + client = gaming.GameServerConfigsServiceClient() + + fleet_config = game_server_configs.FleetConfig( + name="my-fleet-spec", fleet_spec=FLEET_SPEC, + ) + + # Location is hard coded as global, as game server configs can + # only be created in global. This is done for all operations on + # game server configs. + request = game_server_configs.CreateGameServerConfigRequest( + parent=f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}", + config_id=config_id, + game_server_config=game_server_configs.GameServerConfig( + description="My Game Server Config", fleet_configs=[fleet_config], + ), + ) + + operation = client.create_game_server_config(request) + print(f"Create config operation: {operation.operation.name}") + operation.result(timeout=120) + + +# [END cloud_game_servers_config_create] + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--project-id", help="Your cloud project ID.", required=True) + parser.add_argument( + "--deployment-id", help="Your game server deployment ID.", required=True + ) + parser.add_argument( + "--config-id", help="Your game server config ID.", required=True + ) + + args = parser.parse_args() + + create_config(args.project_id, args.deployment_id, args.config_id) diff --git a/samples/snippets/create_deployment.py b/samples/snippets/create_deployment.py index d57eaefb..b8bd7047 100644 --- a/samples/snippets/create_deployment.py +++ b/samples/snippets/create_deployment.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Google Cloud Game Servers sample for creating a Game Server Deployment. +"""Google Cloud Game Servers sample for creating a game server deployment. Example usage: python create_deployment.py --project-id --deployment-id @@ -26,15 +26,15 @@ from google.cloud.gaming_v1.types import game_server_deployments -# [START cloud_game_servers_create_deployment] +# [START cloud_game_servers_deployment_create] 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 + # 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. + # 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, @@ -46,7 +46,7 @@ def create_deployment(project_id, deployment_id): operation = client.create_game_server_deployment(request) print(f"Create deployment operation: {operation.operation.name}") operation.result(timeout=120) -# [END cloud_game_servers_create_deployment] +# [END cloud_game_servers_deployment_create] if __name__ == "__main__": diff --git a/samples/snippets/create_realm.py b/samples/snippets/create_realm.py new file mode 100644 index 00000000..8ab30726 --- /dev/null +++ b/samples/snippets/create_realm.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for creating a realm. + +Example usage: + python create_realm.py --project-id --location --realm-id +""" + +import argparse + +from google.cloud import gaming +from google.cloud.gaming_v1.types import realms + + +# [START cloud_game_servers_realm_create] +def create_realm(project_id, location, realm_id): + """Creates a realm.""" + + client = gaming.RealmsServiceClient() + + request = realms.CreateRealmRequest( + parent=f"projects/{project_id}/locations/{location}", + realm_id=realm_id, + realm=realms.Realm( + description="My Realm", + time_zone="US/Pacific", + ), + ) + + operation = client.create_realm(request) + print(f"Create realm operation: {operation.operation.name}") + operation.result(timeout=120) +# [END cloud_game_servers_realm_create] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--location', help='Your realm location.', required=True) + parser.add_argument('--realm-id', help='Your realm ID.', required=True) + + args = parser.parse_args() + + create_realm(args.project_id, args.location, args.realm_id) diff --git a/samples/snippets/delete_cluster.py b/samples/snippets/delete_cluster.py new file mode 100644 index 00000000..ee44cbe9 --- /dev/null +++ b/samples/snippets/delete_cluster.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for deleting a game server cluster. + +Example usage: + python delete_cluster.py --project-id --location --realm-id --cluster-id +""" + +import argparse + +from google.cloud import gaming +from google.cloud.gaming_v1.types import game_server_clusters + + +# [START cloud_game_servers_cluster_delete] +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) +# [END cloud_game_servers_cluster_delete] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--location', help='Your realm location.', required=True) + parser.add_argument('--realm-id', help='Your realm ID.', required=True) + parser.add_argument('--cluster-id', help='Your game server cluster ID.', required=True) + + args = parser.parse_args() + + delete_cluster(args.project_id, args.location, args.realm_id, args.cluster_id) diff --git a/samples/snippets/delete_config.py b/samples/snippets/delete_config.py new file mode 100644 index 00000000..92834d7a --- /dev/null +++ b/samples/snippets/delete_config.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for deleting a game server config. + +Example usage: + python delete_config.py --project-id --deployment-id --config-id +""" + +import argparse + +from google.cloud import gaming +from google.cloud.gaming_v1.types import game_server_configs + + +# [START cloud_game_servers_config_delete] +def delete_config(project_id, deployment_id, config_id): + """Deletes a game server config.""" + + client = gaming.GameServerConfigsServiceClient() + + # Location is hard coded as global, as game server configs can + # only be created in global. This is done for all operations on + # game server configs. + request = game_server_configs.DeleteGameServerConfigRequest( + name=f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}/configs/{config_id}", + ) + + operation = client.delete_game_server_config(request) + print(f"Delete config operation: {operation.operation.name}") + operation.result(timeout=120) +# [END cloud_game_servers_config_delete] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--deployment-id', help='Your game server deployment ID.', required=True) + parser.add_argument('--config-id', help='Your game server config ID.', required=True) + + args = parser.parse_args() + + delete_config(args.project_id, args.deployment_id, args.config_id) diff --git a/samples/snippets/delete_deployment.py b/samples/snippets/delete_deployment.py index 6ca87d06..ab7d39c5 100644 --- a/samples/snippets/delete_deployment.py +++ b/samples/snippets/delete_deployment.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Google Cloud Game Servers sample for deleting a Game Server Deployment. +"""Google Cloud Game Servers sample for deleting a game server deployment. Example usage: python delete_deployment.py --project-id --deployment-id @@ -26,15 +26,15 @@ from google.cloud.gaming_v1.types import game_server_deployments -# [START cloud_game_servers_delete_deployment] +# [START cloud_game_servers_deployment_delete] 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 + # 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. + # 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}", ) @@ -42,7 +42,7 @@ def delete_deployment(project_id, deployment_id): operation = client.delete_game_server_deployment(request) print(f"Delete deployment operation: {operation.operation.name}") operation.result(timeout=120) -# [END cloud_game_servers_delete_deployment] +# [END cloud_game_servers_deployment_delete] if __name__ == "__main__": diff --git a/samples/snippets/delete_realm.py b/samples/snippets/delete_realm.py new file mode 100644 index 00000000..3cd8d42f --- /dev/null +++ b/samples/snippets/delete_realm.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for deleting a realm. + +Example usage: + python delete_realm.py --project-id --location --realm-id +""" + +import argparse + +from google.cloud import gaming +from google.cloud.gaming_v1.types import realms + + +# [START cloud_game_servers_realm_delete] +def delete_realm(project_id, location, realm_id): + """Deletes a realm.""" + + client = gaming.RealmsServiceClient() + + request = realms.DeleteRealmRequest( + name=f"projects/{project_id}/locations/{location}/realms/{realm_id}", + ) + + operation = client.delete_realm(request) + print(f"Delete realm operation: {operation.operation.name}") + operation.result(timeout=120) +# [END cloud_game_servers_realm_delete] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--location', help='Your realm location.', required=True) + parser.add_argument('--realm-id', help='Your realm ID.', required=True) + + args = parser.parse_args() + + delete_realm(args.project_id, args.location, args.realm_id) diff --git a/samples/snippets/deployment_and_config_test.py b/samples/snippets/deployment_and_config_test.py new file mode 100644 index 00000000..1d370b5d --- /dev/null +++ b/samples/snippets/deployment_and_config_test.py @@ -0,0 +1,221 @@ +# Copyright 2020, Google, Inc. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import uuid + +from google.api_core import exceptions + +import pytest + +import create_config +import create_deployment +import create_realm +import delete_config +import delete_deployment +import delete_realm +import get_config +import get_deployment +import get_rollout +import list_configs +import list_deployments +import update_deployment +import update_rollout_default +import update_rollout_override +import update_rollout_remove_default +import update_rollout_remove_override + +PROJECT_ID = "python-docs-samples-tests" +CONFIG_ID = "my-game-server-config" +REALM_LOCATION = "us-central1" + + +@pytest.fixture(scope="function") +def test_deployment(): + deployment_id = "deployment-{}".format(uuid.uuid4()) + + print(f"Creating deployment {deployment_id} in project {PROJECT_ID}") + create_deployment.create_deployment(PROJECT_ID, deployment_id) + + yield deployment_id + + print(f"Cleaning up deployment {deployment_id} in teardown") + clean_up_deployment(deployment_id) + + +def clean_up_deployment(deployment_id): + # Delete the deployment if it still exists. + print(f"Deleting deployment: {deployment_id}") + try: + delete_deployment.delete_deployment(PROJECT_ID, deployment_id) + except exceptions.NotFound: # Ignore the non-existent deployment + return + + +@pytest.fixture(scope="function") +def test_deployment_with_config(): + deployment_id = "deployment-{}".format(uuid.uuid4()) + + print(f"Creating deployment {deployment_id} in project {PROJECT_ID}") + create_deployment.create_deployment(PROJECT_ID, deployment_id) + + print(f"Creating config {CONFIG_ID} in deployment {deployment_id} in project {PROJECT_ID}") + create_config.create_config(PROJECT_ID, deployment_id, CONFIG_ID) + + yield deployment_id + + print(f"Cleaning up deployment {deployment_id} in teardown") + clean_up_deployment_and_configs(deployment_id) + + +def clean_up_deployment_and_configs(deployment_id): + # Delete the deployment and the game server configs in the deployment. + try: + get_deployment.get_deployment(PROJECT_ID, deployment_id) + except exceptions.NotFound: # Ignore the non-existent deployment + return + + try: + update_rollout_remove_default.update_rollout_remove_default(PROJECT_ID, deployment_id) + except exceptions.NotFound: # Ignore the non-existent deployment + return + + try: + update_rollout_remove_override.update_rollout_remove_override(PROJECT_ID, deployment_id) + except exceptions.NotFound: # Ignore the non-existent deployment + return + + configs = list_configs.list_configs(PROJECT_ID, deployment_id) + for config in configs: + config_id = config.name.rsplit('/', 1)[-1] + print(f"Deleting config {config_id} in deployment {deployment_id}") + try: + delete_config.delete_config(PROJECT_ID, deployment_id, config_id) + except exceptions.NotFound: # Ignore the non-existent config + return + + print(f"Deleting deployment: {deployment_id}") + try: + delete_deployment.delete_deployment(PROJECT_ID, deployment_id) + except exceptions.NotFound: # Ignore the non-existent deployment + return + + +@pytest.fixture(scope="function") +def test_realm(): + realm_id = "realm-{}".format(uuid.uuid4()) + + print(f"Creating realm {realm_id} in location {REALM_LOCATION} in project {PROJECT_ID}") + create_realm.create_realm(PROJECT_ID, REALM_LOCATION, realm_id) + + yield realm_id + + print(f"Cleaning up realm {realm_id} in teardown") + clean_up_realm(realm_id) + + +def clean_up_realm(realm_id): + # Delete the realm if it still exists. + print(f"Deleting realm: {realm_id}") + try: + delete_realm.delete_realm(PROJECT_ID, REALM_LOCATION, realm_id) + except exceptions.NotFound: # Ignore the non-existent realm + return + + +def test_create_deployment(test_deployment): + print(f"Created deployment {test_deployment} in project {PROJECT_ID}") + + +def test_get_deployment(test_deployment): + deployment = get_deployment.get_deployment(PROJECT_ID, test_deployment) + assert deployment.name == f"projects/{PROJECT_ID}/locations/global/gameServerDeployments/{test_deployment}" + + +def test_list_deployments(test_deployment): + deployments = list_deployments.list_deployments(PROJECT_ID) + + deployment_name_list = [] + for deployment in deployments: + deployment_name_list.append(deployment.name) + + deployment_name = f"projects/{PROJECT_ID}/locations/global/gameServerDeployments/{test_deployment}" + assert deployment_name in deployment_name_list + + +def test_update_deployment(test_deployment): + update_deployment.update_deployment(PROJECT_ID, test_deployment) + deployment = get_deployment.get_deployment(PROJECT_ID, test_deployment) + assert deployment.labels == {"label-key-1": "label-value-1", "label-key-2": "label-value-2"} + + +def test_delete_deployment(test_deployment): + delete_deployment.delete_deployment(PROJECT_ID, test_deployment) + with pytest.raises(exceptions.NotFound): + get_deployment.get_deployment(PROJECT_ID, test_deployment) + + +def test_get_rollout(test_deployment): + rollout = get_rollout.get_rollout(PROJECT_ID, test_deployment) + assert rollout.name == f"projects/{PROJECT_ID}/locations/global/gameServerDeployments/{test_deployment}/rollout" + + +def test_update_rollout_default(test_deployment_with_config): + update_rollout_default.update_rollout_default(PROJECT_ID, test_deployment_with_config, CONFIG_ID) + rollout = get_rollout.get_rollout(PROJECT_ID, test_deployment_with_config) + assert rollout.default_game_server_config == f"projects/{PROJECT_ID}/locations/global/gameServerDeployments/{test_deployment_with_config}/configs/{CONFIG_ID}" + + +def test_update_rollout_override(test_realm, test_deployment_with_config): + update_rollout_override.update_rollout_override(PROJECT_ID, test_deployment_with_config, CONFIG_ID, REALM_LOCATION, test_realm) + rollout = get_rollout.get_rollout(PROJECT_ID, test_deployment_with_config) + assert len(rollout.game_server_config_overrides) == 1 + assert rollout.game_server_config_overrides[0].config_version == f"projects/{PROJECT_ID}/locations/global/gameServerDeployments/{test_deployment_with_config}/configs/{CONFIG_ID}" + assert rollout.game_server_config_overrides[0].realms_selector.realms == [f"projects/{PROJECT_ID}/locations/{REALM_LOCATION}/realms/{test_realm}"] + + +def test_update_rollout_remove_default(test_deployment): + update_rollout_remove_default.update_rollout_remove_default(PROJECT_ID, test_deployment) + rollout = get_rollout.get_rollout(PROJECT_ID, test_deployment) + assert rollout.default_game_server_config == "" + + +def test_update_rollout_remove_override(test_deployment): + update_rollout_remove_override.update_rollout_remove_override(PROJECT_ID, test_deployment) + rollout = get_rollout.get_rollout(PROJECT_ID, test_deployment) + assert len(rollout.game_server_config_overrides) == 0 + + +def test_create_conifg(test_deployment_with_config): + print(f"Created config {CONFIG_ID} in deployment {test_deployment_with_config} in project {PROJECT_ID}") + + +def test_get_config(test_deployment_with_config): + config = get_config.get_config(PROJECT_ID, test_deployment_with_config, CONFIG_ID) + assert config.name == f"projects/{PROJECT_ID}/locations/global/gameServerDeployments/{test_deployment_with_config}/configs/{CONFIG_ID}" + + +def test_list_configs(test_deployment_with_config): + configs = list_configs.list_configs(PROJECT_ID, test_deployment_with_config) + + config_name_list = [] + for config in configs: + config_name_list.append(config.name) + + config_name = f"projects/{PROJECT_ID}/locations/global/gameServerDeployments/{test_deployment_with_config}/configs/{CONFIG_ID}" + assert config_name in config_name_list + + +def test_delete_config(test_deployment_with_config): + delete_config.delete_config(PROJECT_ID, test_deployment_with_config, CONFIG_ID) + with pytest.raises(exceptions.NotFound): + get_config.get_config(PROJECT_ID, test_deployment_with_config, CONFIG_ID) diff --git a/samples/snippets/deployment_test.py b/samples/snippets/deployment_test.py deleted file mode 100644 index 4ab28e68..00000000 --- a/samples/snippets/deployment_test.py +++ /dev/null @@ -1,96 +0,0 @@ -# Copyright 2020, Google, Inc. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import uuid - -from google.api_core import exceptions - -import pytest - -import create_deployment -import delete_deployment -import get_deployment -import get_rollout -import list_deployments -import update_rollout_remove_default - -PROJECT_ID = "python-docs-samples-tests" - - -@pytest.fixture(scope="function") -def test_deployment(): - deployment_id = "deployment-{}".format(uuid.uuid4()) - - print(f"Creating deployment {deployment_id} in project {PROJECT_ID}") - create_deployment.create_deployment(PROJECT_ID, deployment_id) - - yield deployment_id - - print(f"Cleaning up deployment {deployment_id} in teardown") - clean_up_deployment(deployment_id) - - -def clean_up_deployment(deployment_id): - # Delete the deployment if it still exists. - try: - get_deployment.get_deployment(PROJECT_ID, deployment_id) - except exceptions.NotFound: # Ignore the non-existent deployment - return - - try: - update_rollout_remove_default.update_rollout_remove_default(PROJECT_ID, deployment_id) - except exceptions.NotFound: # Ignore the non-existent deployment - return - - print(f"Deleting deployment: {deployment_id}") - try: - delete_deployment.delete_deployment(PROJECT_ID, deployment_id) - except exceptions.NotFound: # Ignore the non-existent deployment - return - - -def test_create_deployment(test_deployment): - print(f"Created deployment {test_deployment} in project {PROJECT_ID}") - - -def test_get_deployment(test_deployment): - deployment = get_deployment.get_deployment(PROJECT_ID, test_deployment) - assert deployment.name == f"projects/{PROJECT_ID}/locations/global/gameServerDeployments/{test_deployment}" - - -def test_list_deployments(test_deployment): - deployments = list_deployments.list_deployments(PROJECT_ID) - - deployment_name_list = [] - for deployment in deployments: - deployment_name_list.append(deployment.name) - - deployment_name = f"projects/{PROJECT_ID}/locations/global/gameServerDeployments/{test_deployment}" - assert deployment_name in deployment_name_list - - -def test_delete_deployment(test_deployment): - delete_deployment.delete_deployment(PROJECT_ID, test_deployment) - with pytest.raises(exceptions.NotFound): - get_deployment.get_deployment(PROJECT_ID, test_deployment) - - -def test_get_rollout(test_deployment): - rollout = get_rollout.get_rollout(PROJECT_ID, test_deployment) - assert rollout.name == f"projects/{PROJECT_ID}/locations/global/gameServerDeployments/{test_deployment}/rollout" - - -def test_update_rollout_remove_default(test_deployment): - update_rollout_remove_default.update_rollout_remove_default(PROJECT_ID, test_deployment) - rollout = get_rollout.get_rollout(PROJECT_ID, test_deployment) - assert rollout.default_game_server_config == "" diff --git a/samples/snippets/get_cluster.py b/samples/snippets/get_cluster.py new file mode 100644 index 00000000..1cc6c25e --- /dev/null +++ b/samples/snippets/get_cluster.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for getting a game server cluster. + +Example usage: + python get_cluster.py --project-id --location --realm-id --cluster-id +""" + +import argparse + +from google.cloud import gaming +from google.cloud.gaming_v1.types import game_server_clusters + + +# [START cloud_game_servers_cluster_get] +def get_cluster(project_id, location, realm_id, cluster_id): + """Gets a game server cluster.""" + + client = gaming.GameServerClustersServiceClient() + + request = game_server_clusters.GetGameServerClusterRequest( + name=f"projects/{project_id}/locations/{location}/realms/{realm_id}/gameServerClusters/{cluster_id}", + ) + + response = client.get_game_server_cluster(request) + print(f"Get cluster response:\n{response}") + return response +# [END cloud_game_servers_cluster_get] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--location', help='Your realm location.', required=True) + parser.add_argument('--realm-id', help='Your realm ID.', required=True) + parser.add_argument('--cluster-id', help='Your game server cluster ID.', required=True) + + args = parser.parse_args() + + get_cluster(args.project_id, args.location, args.realm_id, args.cluster_id) diff --git a/samples/snippets/get_config.py b/samples/snippets/get_config.py new file mode 100644 index 00000000..814c98e8 --- /dev/null +++ b/samples/snippets/get_config.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for getting a game server config. + +Example usage: + python get_config.py --project-id --deployment-id --config-id +""" + +import argparse + +from google.cloud import gaming +from google.cloud.gaming_v1.types import game_server_configs + + +# [START cloud_game_servers_config_get] +def get_config(project_id, deployment_id, config_id): + """Gets a game server config.""" + + client = gaming.GameServerConfigsServiceClient() + + # Location is hard coded as global, as game server configs can + # only be created in global. This is done for all operations on + # game server configs. + request = game_server_configs.GetGameServerConfigRequest( + name=f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}/configs/{config_id}", + ) + + response = client.get_game_server_config(request) + print(f"Get config response:\n{response}") + return response +# [END cloud_game_servers_config_get] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--deployment-id', help='Your game server deployment ID.', required=True) + parser.add_argument('--config-id', help='Your game server config ID.', required=True) + + args = parser.parse_args() + + get_config(args.project_id, args.config_id, args.deployment_id) diff --git a/samples/snippets/get_deployment.py b/samples/snippets/get_deployment.py index b7085b1f..54d6d514 100644 --- a/samples/snippets/get_deployment.py +++ b/samples/snippets/get_deployment.py @@ -14,10 +14,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Google Cloud Game Servers sample for getting a Game Server Deployment. +"""Google Cloud Game Servers sample for getting a game server deployment. Example usage: - python get_deployments.py --project-id --deployment-id + python get_deployment.py --project-id --deployment-id """ import argparse @@ -26,15 +26,15 @@ from google.cloud.gaming_v1.types import game_server_deployments -# [START cloud_game_servers_get_deployment] +# [START cloud_game_servers_deployment_get] def get_deployment(project_id, deployment_id): """Gets a game server deployment.""" client = gaming.GameServerDeploymentsServiceClient() - # Location is hard coded as global, as Game Server Deployments can + # 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. + # game server deployments, as well as for its child resource types. request = game_server_deployments.GetGameServerDeploymentRequest( name=f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}", ) @@ -42,7 +42,7 @@ def get_deployment(project_id, deployment_id): response = client.get_game_server_deployment(request) print(f"Get deployment response:\n{response}") return response -# [END cloud_game_servers_get_deployment] +# [END cloud_game_servers_deployment_get] if __name__ == "__main__": diff --git a/samples/snippets/get_realm.py b/samples/snippets/get_realm.py new file mode 100644 index 00000000..260df550 --- /dev/null +++ b/samples/snippets/get_realm.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for getting a realm. + +Example usage: + python get_realm.py --project-id --location --realm-id +""" + +import argparse + +from google.cloud import gaming +from google.cloud.gaming_v1.types import realms + + +# [START cloud_game_servers_realm_get] +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 +# [END cloud_game_servers_realm_get] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--location', help='Your realm location.', required=True) + parser.add_argument('--realm-id', help='Your realm ID.', required=True) + + args = parser.parse_args() + + get_realm(args.project_id, args.location, args.realm_id) diff --git a/samples/snippets/get_rollout.py b/samples/snippets/get_rollout.py index 7fc78900..b4397297 100644 --- a/samples/snippets/get_rollout.py +++ b/samples/snippets/get_rollout.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Google Cloud Game Servers sample for getting the rollout of a Game Server Deployment. +"""Google Cloud Game Servers sample for getting the rollout of a game server deployment. Example usage: python get_rollout.py --project-id --deployment-id @@ -26,15 +26,15 @@ from google.cloud.gaming_v1.types import game_server_deployments -# [START cloud_game_servers_get_rollout] +# [START cloud_game_servers_rollout_get] def get_rollout(project_id, deployment_id): """Gets the rollout of a game server deployment.""" client = gaming.GameServerDeploymentsServiceClient() - # Location is hard coded as global, as Game Server Deployments can + # 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. + # game server deployments, as well as for its child resource types. request = game_server_deployments.GetGameServerDeploymentRolloutRequest( name=f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}", ) @@ -42,7 +42,7 @@ def get_rollout(project_id, deployment_id): response = client.get_game_server_deployment_rollout(request) print(f"Get deployment response:\n{response}") return response -# [END cloud_game_servers_get_rollout] +# [END cloud_game_servers_rollout_get] if __name__ == "__main__": diff --git a/samples/snippets/list_clusters.py b/samples/snippets/list_clusters.py new file mode 100644 index 00000000..5d31a526 --- /dev/null +++ b/samples/snippets/list_clusters.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for listing game server clusters. + +Example usage: + python list_clusters.py --project-id --location --realm-id +""" + +import argparse + +from google.cloud import gaming + + +# [START cloud_game_servers_cluster_list] +def list_clusters(project_id, location, realm_id): + """Lists the existing game server clusters.""" + + client = gaming.GameServerClustersServiceClient() + + response = client.list_game_server_clusters( + parent=f"projects/{project_id}/locations/{location}/realms/{realm_id}" + ) + + for cluster in response.game_server_clusters: + print(f"Name: {cluster.name}") + + return response.game_server_clusters +# [END cloud_game_servers_cluster_list] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--location', help='Your realm location.', required=True) + parser.add_argument('--realm-id', help='Your realm ID.', required=True) + + args = parser.parse_args() + + list_clusters(args.project_id, args.location, args.realm_id) diff --git a/samples/snippets/list_configs.py b/samples/snippets/list_configs.py new file mode 100644 index 00000000..7eef0610 --- /dev/null +++ b/samples/snippets/list_configs.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for listing game server configs. + +Example usage: + python list_configs.py --project-id --deployment-id +""" + +import argparse + +from google.cloud import gaming + + +# [START cloud_game_servers_config_list] +def list_configs(project_id, deployment_id): + """Lists the existing game server deployments.""" + + client = gaming.GameServerConfigsServiceClient() + + # Location is hard coded as global, as game server configs can + # only be created in global. This is done for all operations on + # game server configs. + response = client.list_game_server_configs( + parent=f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}" + ) + + for config in response.game_server_configs: + print(f"Name: {config.name}") + + return response.game_server_configs +# [END cloud_game_servers_config_list] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--deployment-id', help='Your game server deployment ID.', required=True) + + args = parser.parse_args() + + list_configs(args.project_id, args.deployment_id) diff --git a/samples/snippets/list_deployments.py b/samples/snippets/list_deployments.py index af74b0e7..2466678f 100644 --- a/samples/snippets/list_deployments.py +++ b/samples/snippets/list_deployments.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Google Cloud Game Servers sample for listing Game Server Deployments. +"""Google Cloud Game Servers sample for listing game server deployments. Example usage: python list_deployments.py --project-id @@ -25,24 +25,24 @@ from google.cloud import gaming -# [START cloud_game_servers_list_deployments] +# [START cloud_game_servers_deployment_list] 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 + # 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. - deployments = client.list_game_server_deployments( + # 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 deployments.game_server_deployments: + for deployment in response.game_server_deployments: print(f"Name: {deployment.name}") - return deployments.game_server_deployments -# [END cloud_game_servers_list_deployments] + return response.game_server_deployments +# [END cloud_game_servers_deployment_list] if __name__ == "__main__": diff --git a/samples/snippets/list_realms.py b/samples/snippets/list_realms.py new file mode 100644 index 00000000..ea6f69ff --- /dev/null +++ b/samples/snippets/list_realms.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for listing realms. + +Example usage: + python list_realms.py --project-id --location +""" + +import argparse + +from google.cloud import gaming + +# [START cloud_game_servers_realm_list] + + +def list_realms(project_id, location): + """Lists the existing realms.""" + + client = gaming.RealmsServiceClient() + + response = client.list_realms( + parent=f"projects/{project_id}/locations/{location}" + ) + + for realm in response.realms: + print(f"Name: {realm.name}") + + return response.realms +# [END cloud_game_servers_realm_list] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--location', help='Your realm location.', required=True) + + args = parser.parse_args() + + list_realms(args.project_id, args.location) diff --git a/samples/snippets/realm_and_cluster_test.py b/samples/snippets/realm_and_cluster_test.py new file mode 100644 index 00000000..ca869093 --- /dev/null +++ b/samples/snippets/realm_and_cluster_test.py @@ -0,0 +1,159 @@ +# Copyright 2020, Google, Inc. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import uuid + +from google.api_core import exceptions + +import pytest + +import create_cluster +import create_realm +import delete_cluster +import delete_realm +import get_cluster +import get_realm +import list_clusters +import list_realms +import update_cluster +import update_realm + +PROJECT_ID = "python-docs-samples-tests" +REALM_LOCATION = "us-central1" +CLUSTER_ID = "my-cluster" +GKE_CLUSTER_NAME = "projects/gcgs-client-lib-samples/locations/us-central1/clusters/gke-shared-default" + + +@pytest.fixture(scope="function") +def test_realm(): + realm_id = "realm-{}".format(uuid.uuid4()) + + print(f"Creating realm {realm_id} in location {REALM_LOCATION} in project {PROJECT_ID}") + create_realm.create_realm(PROJECT_ID, REALM_LOCATION, realm_id) + + yield realm_id + + print(f"Cleaning up realm {realm_id} in teardown") + clean_up_realm(realm_id) + + +def clean_up_realm(realm_id): + # Delete the realm if it still exists. + print(f"Deleting realm: {realm_id}") + try: + delete_realm.delete_realm(PROJECT_ID, REALM_LOCATION, realm_id) + except exceptions.NotFound: # Ignore the non-existent realm + return + + +@pytest.fixture(scope="function") +def test_realm_with_cluster(): + realm_id = "realm-{}".format(uuid.uuid4()) + + print(f"Creating realm {realm_id} in location {REALM_LOCATION} in project {PROJECT_ID}") + create_realm.create_realm(PROJECT_ID, REALM_LOCATION, realm_id) + + print(f"Creating cluster {CLUSTER_ID} in realm {realm_id} in project {PROJECT_ID}") + create_cluster.create_cluster(PROJECT_ID, REALM_LOCATION, realm_id, CLUSTER_ID, GKE_CLUSTER_NAME) + + yield realm_id + + print(f"Cleaning up realm {realm_id} in teardown") + clean_up_realm_and_clusters(realm_id) + + +def clean_up_realm_and_clusters(realm_id): + # Delete the realm and the game server clusters in the realm. + try: + get_realm.get_realm(PROJECT_ID, REALM_LOCATION, realm_id) + except exceptions.NotFound: # Ignore the non-existent realm + return + + clusters = list_clusters.list_clusters(PROJECT_ID, REALM_LOCATION, realm_id) + for cluster in clusters: + cluster_id = cluster.name.rsplit('/', 1)[-1] + print(f"Deleting cluster {cluster_id} in realm {realm_id}") + try: + delete_cluster.delete_cluster(PROJECT_ID, REALM_LOCATION, realm_id, cluster_id) + except exceptions.NotFound: # Ignore the non-existent cluster + return + + print(f"Deleting realm: {realm_id}") + try: + delete_realm.delete_realm(PROJECT_ID, REALM_LOCATION, realm_id) + except exceptions.NotFound: # Ignore the non-existent realm + return + + +def test_create_realm(test_realm): + print(f"Created realm {test_realm} in project {PROJECT_ID}") + + +def test_get_realm(test_realm): + realm = get_realm.get_realm(PROJECT_ID, REALM_LOCATION, test_realm) + assert realm.name == f"projects/{PROJECT_ID}/locations/{REALM_LOCATION}/realms/{test_realm}" + + +def test_list_realms(test_realm): + realms = list_realms.list_realms(PROJECT_ID, REALM_LOCATION) + + realm_name_list = [] + for realm in realms: + realm_name_list.append(realm.name) + + realm_name = f"projects/{PROJECT_ID}/locations/{REALM_LOCATION}/realms/{test_realm}" + assert realm_name in realm_name_list + + +def test_update_realm(test_realm): + update_realm.update_realm(PROJECT_ID, REALM_LOCATION, test_realm) + realm = get_realm.get_realm(PROJECT_ID, REALM_LOCATION, test_realm) + assert realm.labels == {"label-key-1": "label-value-1", "label-key-2": "label-value-2"} + + +def test_delete_realm(test_realm): + delete_realm.delete_realm(PROJECT_ID, REALM_LOCATION, test_realm) + with pytest.raises(exceptions.NotFound): + get_realm.get_realm(PROJECT_ID, REALM_LOCATION, test_realm) + + +def test_create_cluster(test_realm_with_cluster): + print(f"Created cluster {CLUSTER_ID} in realm {test_realm_with_cluster} in project {PROJECT_ID}") + + +def test_get_cluster(test_realm_with_cluster): + cluster = get_cluster.get_cluster(PROJECT_ID, REALM_LOCATION, test_realm_with_cluster, CLUSTER_ID) + assert cluster.name == f"projects/{PROJECT_ID}/locations/{REALM_LOCATION}/realms/{test_realm_with_cluster}/gameServerClusters/{CLUSTER_ID}" + + +def test_list_clusters(test_realm_with_cluster): + clusters = list_clusters.list_clusters(PROJECT_ID, REALM_LOCATION, test_realm_with_cluster) + + cluster_name_list = [] + for cluster in clusters: + cluster_name_list.append(cluster.name) + + cluster_name = f"projects/{PROJECT_ID}/locations/{REALM_LOCATION}/realms/{test_realm_with_cluster}/gameServerClusters/{CLUSTER_ID}" + assert cluster_name in cluster_name_list + + +def test_update_cluster(test_realm_with_cluster): + update_cluster.update_cluster(PROJECT_ID, REALM_LOCATION, test_realm_with_cluster, CLUSTER_ID) + cluster = get_cluster.get_cluster(PROJECT_ID, REALM_LOCATION, test_realm_with_cluster, CLUSTER_ID) + assert cluster.labels == {"label-key-1": "label-value-1", "label-key-2": "label-value-2"} + + +def test_delete_cluster(test_realm_with_cluster): + delete_cluster.delete_cluster(PROJECT_ID, REALM_LOCATION, test_realm_with_cluster, CLUSTER_ID) + with pytest.raises(exceptions.NotFound): + get_cluster.get_cluster(PROJECT_ID, REALM_LOCATION, test_realm_with_cluster, CLUSTER_ID) diff --git a/samples/snippets/update_cluster.py b/samples/snippets/update_cluster.py new file mode 100644 index 00000000..30b806c8 --- /dev/null +++ b/samples/snippets/update_cluster.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for updating a game server cluster. + +Example usage: + python update_cluster.py --project-id --location --realm-id --cluster-id +""" + +import argparse + +from google.cloud import gaming +from google.cloud.gaming_v1.types import game_server_clusters +from google.protobuf import field_mask_pb2 as field_mask + + +# [START cloud_game_servers_cluster_update] +def update_cluster(project_id, location, realm_id, cluster_id): + """Updates a game server cluster.""" + + client = gaming.GameServerClustersServiceClient() + + request = game_server_clusters.UpdateGameServerClusterRequest( + game_server_cluster=game_server_clusters.GameServerCluster( + name=f"projects/{project_id}/locations/{location}/realms/{realm_id}/gameServerClusters/{cluster_id}", + labels={"label-key-1": "label-value-1", "label-key-2": "label-value-2"}, + ), + update_mask=field_mask.FieldMask(paths=["labels"]), + ) + + operation = client.update_game_server_cluster(request) + print(f"Update cluster operation: {operation.operation.name}") + operation.result(timeout=120) +# [END cloud_game_servers_cluster_update] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--location', help='Your realm location.', required=True) + parser.add_argument('--realm-id', help='Your realm ID.', required=True) + parser.add_argument('--cluster-id', help='Your game server cluster ID.', required=True) + + args = parser.parse_args() + + update_cluster(args.project_id, args.location, args.realm_id, args.cluster_id) diff --git a/samples/snippets/update_deployment.py b/samples/snippets/update_deployment.py new file mode 100644 index 00000000..0ab00aec --- /dev/null +++ b/samples/snippets/update_deployment.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for updating a game server deployment. + +Example usage: + python update_deployment.py --project-id --deployment-id +""" + +import argparse + +from google.cloud import gaming +from google.cloud.gaming_v1.types import game_server_deployments +from google.protobuf import field_mask_pb2 as field_mask + + +# [START cloud_game_servers_deployment_update] +def update_deployment(project_id, deployment_id): + """Updates 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.UpdateGameServerDeploymentRequest( + game_server_deployment=game_server_deployments.GameServerDeployment( + name=f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}", + labels={"label-key-1": "label-value-1", "label-key-2": "label-value-2"}, + ), + update_mask=field_mask.FieldMask(paths=["labels"]), + ) + + operation = client.update_game_server_deployment(request) + print(f"Update deployment operation: {operation.operation.name}") + operation.result(timeout=120) +# [END cloud_game_servers_deployment_update] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--deployment-id', help='Your game server deployment ID.', required=True) + + args = parser.parse_args() + + update_deployment(args.project_id, args.deployment_id) diff --git a/samples/snippets/update_realm.py b/samples/snippets/update_realm.py new file mode 100644 index 00000000..d12e315c --- /dev/null +++ b/samples/snippets/update_realm.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for updating a realm. + +Example usage: + python update_realm.py --project-id --location --realm-id +""" + +import argparse + +from google.cloud import gaming +from google.cloud.gaming_v1.types import realms +from google.protobuf import field_mask_pb2 as field_mask + + +# [START cloud_game_servers_realm_update] +def update_realm(project_id, location, realm_id): + """Updates a realm.""" + + client = gaming.RealmsServiceClient() + + request = realms.UpdateRealmRequest( + realm=realms.Realm( + name=f"projects/{project_id}/locations/{location}/realms/{realm_id}", + labels={"label-key-1": "label-value-1", "label-key-2": "label-value-2"}, + ), + update_mask=field_mask.FieldMask(paths=["labels"]), + ) + + operation = client.update_realm(request) + print(f"Update realm operation: {operation.operation.name}") + operation.result(timeout=120) +# [END cloud_game_servers_realm_update] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--location', help='Your realm location.', required=True) + parser.add_argument('--realm-id', help='Your realm ID.', required=True) + + args = parser.parse_args() + + update_realm(args.project_id, args.location, args.realm_id) diff --git a/samples/snippets/update_rollout_default.py b/samples/snippets/update_rollout_default.py new file mode 100644 index 00000000..b868af72 --- /dev/null +++ b/samples/snippets/update_rollout_default.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for updating the rollout of a game +server deployment to set the default config. + +Example usage: + python update_rollout_default.py --project-id --deployment-id --config-id +""" + +import argparse + +from google.cloud import gaming +from google.cloud.gaming_v1.types import game_server_deployments +from google.protobuf import field_mask_pb2 as field_mask # type: ignore + + +# [START cloud_game_servers_deployment_rollout_default] +def update_rollout_default(project_id, deployment_id, config_id): + """Update the rollout of a game server deployment to set the default config.""" + + 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.UpdateGameServerDeploymentRolloutRequest() + request.rollout.name = f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}" + request.rollout.default_game_server_config = config_id + request.update_mask = field_mask.FieldMask(paths=["default_game_server_config"]) + + operation = client.update_game_server_deployment_rollout(request) + print(f"Update deployment rollout operation: {operation.operation.name}") + operation.result(timeout=120) +# [END cloud_game_servers_deployment_rollout_default] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--deployment-id', help='Your game server deployment ID.', required=True) + parser.add_argument('--config-id', help='Your game server config ID.', required=True) + + args = parser.parse_args() + + update_rollout_default(args.project_id, args.deployment_id, args.config_id) diff --git a/samples/snippets/update_rollout_override.py b/samples/snippets/update_rollout_override.py new file mode 100644 index 00000000..5efc840a --- /dev/null +++ b/samples/snippets/update_rollout_override.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for updating the rollout of a game +server deployment to set the override config. + +Example usage: + python update_rollout_override.py --project-id --deployment-id --config-id --realm-location --realm-id +""" + +import argparse + +from google.cloud import gaming +from google.cloud.gaming_v1.types import common +from google.cloud.gaming_v1.types import game_server_deployments +from google.protobuf import field_mask_pb2 as field_mask # type: ignore + + +# [START cloud_game_servers_deployment_rollout_override] +def update_rollout_override(project_id, deployment_id, config_id, realm_location, realm_id): + """Update the rollout of a game server deployment to set the override config.""" + + 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.UpdateGameServerDeploymentRolloutRequest() + request.rollout.name = f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}" + realm_name = f"projects/{project_id}/locations/{realm_location}/realms/{realm_id}" + config_override = game_server_deployments.GameServerConfigOverride( + realms_selector=common.RealmSelector(realms=[realm_name]), + config_version=config_id, + ) + request.rollout.game_server_config_overrides = [config_override] + request.update_mask = field_mask.FieldMask(paths=["game_server_config_overrides"]) + + operation = client.update_game_server_deployment_rollout(request) + print(f"Update deployment rollout operation: {operation.operation.name}") + operation.result(timeout=120) +# [END cloud_game_servers_deployment_rollout_override] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--deployment-id', help='Your game server deployment ID.', required=True) + parser.add_argument('--config-id', help='Your game server config ID.', required=True) + parser.add_argument('--realm-location', help='Your game server config ID.', required=True) + parser.add_argument('--realm-id', help='Your game server config ID.', required=True) + + args = parser.parse_args() + + update_rollout_override(args.project_id, args.deployment_id, args.config_id, args.realm_location, args.realm_id) diff --git a/samples/snippets/update_rollout_remove_default.py b/samples/snippets/update_rollout_remove_default.py index 8805b07b..ff313630 100644 --- a/samples/snippets/update_rollout_remove_default.py +++ b/samples/snippets/update_rollout_remove_default.py @@ -14,8 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Google Cloud Game Servers sample for updating the rollout of a Game Server -Deployment to remove the default config. +"""Google Cloud Game Servers sample for updating the rollout of a game +server deployment to remove the default config. Example usage: python update_rollout_remove_default.py --project-id --deployment-id @@ -28,15 +28,15 @@ from google.protobuf import field_mask_pb2 as field_mask # type: ignore -# [START gaming_update_rollout_remove_default] +# [START cloud_game_servers_deployment_rollout_remove_default] def update_rollout_remove_default(project_id, deployment_id): - """Creates a game server deployment.""" + """Update the rollout of a game server deployment to remove the default config.""" client = gaming.GameServerDeploymentsServiceClient() - # Location is hard coded as global, as Game Server Deployments can + # 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. + # game server deployments, as well as for its child resource types. request = game_server_deployments.UpdateGameServerDeploymentRolloutRequest() request.rollout.name = f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}" request.rollout.default_game_server_config = "" @@ -45,7 +45,7 @@ def update_rollout_remove_default(project_id, deployment_id): operation = client.update_game_server_deployment_rollout(request) print(f"Update deployment rollout operation: {operation.operation.name}") operation.result(timeout=120) -# [END gaming_update_rollout_remove_default] +# [END cloud_game_servers_deployment_rollout_remove_default] if __name__ == "__main__": diff --git a/samples/snippets/update_rollout_remove_override.py b/samples/snippets/update_rollout_remove_override.py new file mode 100644 index 00000000..4149503f --- /dev/null +++ b/samples/snippets/update_rollout_remove_override.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python + +# Copyright 2020 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Game Servers sample for updating the rollout of a game +server deployment to remove the override config. + +Example usage: + python update_rollout_remove_override.py --project-id --deployment-id +""" + +import argparse + +from google.cloud import gaming +from google.cloud.gaming_v1.types import game_server_deployments +from google.protobuf import field_mask_pb2 as field_mask # type: ignore + + +# [START cloud_game_servers_deployment_rollout_remove_override] +def update_rollout_remove_override(project_id, deployment_id): + """Update the rollout of a game server deployment to remove the override config.""" + + 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.UpdateGameServerDeploymentRolloutRequest() + request.rollout.name = f"projects/{project_id}/locations/global/gameServerDeployments/{deployment_id}" + request.rollout.game_server_config_overrides = [] + request.update_mask = field_mask.FieldMask(paths=["game_server_config_overrides"]) + + operation = client.update_game_server_deployment_rollout(request) + print(f"Update deployment rollout operation: {operation.operation.name}") + operation.result(timeout=120) +# [END cloud_game_servers_deployment_rollout_remove_override] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('--project-id', help='Your cloud project ID.', required=True) + parser.add_argument('--deployment-id', help='Your game server deployment ID.', required=True) + + args = parser.parse_args() + + update_rollout_remove_override(args.project_id, args.deployment_id)