From 7d99b927a5b08dac7732810ab7013c5a3288a38d Mon Sep 17 00:00:00 2001 From: pooneh-m <46979170+pooneh-m@users.noreply.github.com> Date: Fri, 24 Jul 2020 10:34:08 -0700 Subject: [PATCH] Add sample for game server rollout (#102) Co-authored-by: Justin Beckwith --- generated,README.md/get_rollout.js | 70 ++++++ generated,README.md/test/rollout.js | 207 ++++++++++++++++++ generated,README.md/update_rollout_default.js | 73 ++++++ .../update_rollout_override.js | 86 ++++++++ .../update_rollout_remove_default.js | 70 ++++++ .../update_rollout_remove_override.js | 69 ++++++ 6 files changed, 575 insertions(+) create mode 100644 generated,README.md/get_rollout.js create mode 100644 generated,README.md/test/rollout.js create mode 100644 generated,README.md/update_rollout_default.js create mode 100644 generated,README.md/update_rollout_override.js create mode 100644 generated,README.md/update_rollout_remove_default.js create mode 100644 generated,README.md/update_rollout_remove_override.js diff --git a/generated,README.md/get_rollout.js b/generated,README.md/get_rollout.js new file mode 100644 index 0000000000..72ba1ff80c --- /dev/null +++ b/generated,README.md/get_rollout.js @@ -0,0 +1,70 @@ +// Copyright 2020, Google LLC. +// 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. + +'use strict'; + +/** + * Get a Game Servers Rollout. + * @param {string} projectId string project identifier + * @param {string} deploymentId unique identifier for the new Game Server Deployment + */ +async function main( + projectId = 'YOUR_PROJECT_ID', + deploymentId = 'DEPLOYMENT_ID' +) { + // [START cloud_game_servers_rollout_get] + const { + GameServerDeploymentsServiceClient, + } = require('@google-cloud/game-servers'); + + const client = new GameServerDeploymentsServiceClient(); + + async function getGameServerDeploymentRollout() { + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'Your Google Cloud Project ID'; + // const deploymentId = 'A unique ID for the Game Server Deployment'; + const request = { + // The full resource name + name: client.gameServerDeploymentPath(projectId, 'global', deploymentId), + }; + + const [rollout] = await client.getGameServerDeploymentRollout(request); + console.log(`Rollout name: ${rollout.name}`); + console.log(`Rollout default: ${rollout.defaultGameServerConfig}`); + for (const override of rollout.gameServerConfigOverrides) { + console.log(`Rollout config overrides: ${override.configVersion}`); + console.log( + `Rollout realm overrides: ${JSON.stringify(override.realmsSelector)}` + ); + } + + const updateTime = rollout.updateTime; + const updateDate = new Date(updateTime.seconds * 1000); + console.log(`Rollout updated on: ${updateDate.toLocaleDateString()}\n`); + } + + getGameServerDeploymentRollout(); + + // [END cloud_game_servers_rollout_get] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/generated,README.md/test/rollout.js b/generated,README.md/test/rollout.js new file mode 100644 index 0000000000..975a78f936 --- /dev/null +++ b/generated,README.md/test/rollout.js @@ -0,0 +1,207 @@ +// Copyright 2020 Google LLC +// +// 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 +// +// https://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. + +'use strict'; + +const {assert} = require('chai'); +const cleanup = require('./clean.js'); +const {describe, before, after, it} = require('mocha'); +const { + GameServerConfigsServiceClient, + GameServerDeploymentsServiceClient, + RealmsServiceClient, +} = require('@google-cloud/game-servers'); + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +describe('Game Server Rollout Test', () => { + const configClient = new GameServerConfigsServiceClient(); + const deploymentClient = new GameServerDeploymentsServiceClient(); + const realmClient = new RealmsServiceClient(); + let projectId; + const deploymentId = `test-nodejs-${uuid.v4()}`; + const configId = `test-nodejs-${uuid.v4()}`; + const realmId = `test-nodejs-${uuid.v4()}`; + const realmLocation = 'us-central1'; + + before(async () => { + await cleanup(); + + projectId = await deploymentClient.getProjectId(); + + const request = { + parent: `projects/${projectId}/locations/global`, + deploymentId: deploymentId, + }; + const [operation] = await deploymentClient.createGameServerDeployment( + request + ); + await operation.promise(); + + const request2 = { + parent: configClient.gameServerDeploymentPath( + projectId, + 'global', + deploymentId + ), + configId: configId, + }; + const [operation2] = await configClient.createGameServerConfig(request2); + await operation2.promise(); + + const request3 = { + parent: `projects/${projectId}/locations/${realmLocation}`, + realmId: realmId, + realm: { + timeZone: 'US/Pacific', + description: 'Test Game Server realm', + }, + }; + + const [operation3] = await realmClient.createRealm(request3); + await operation3.promise(); + }); + + it('should update rollout default', async () => { + const updateDefaultOutput = execSync( + `node update_rollout_default.js ${projectId} ${deploymentId} ${configId}` + ); + + assert.match(updateDefaultOutput, /Deployment updated:/); + + const getRolloutOutput = execSync( + `node get_rollout.js ${projectId} ${deploymentId} ${configId}` + ); + + const configName = configClient.gameServerConfigPath( + projectId, + 'global', + deploymentId, + configId + ); + assert.match(getRolloutOutput, /Rollout name:/); + assert.match( + getRolloutOutput, + new RegExp(`Rollout default: ${configName}`) + ); + }); + + it('should remove rollout default', async () => { + const removeDefaultOutput = execSync( + `node update_rollout_remove_default.js ${projectId} ${deploymentId} ${configId}` + ); + + assert.match(removeDefaultOutput, /Deployment updated:/); + + const request = { + // The full resource name + name: deploymentClient.gameServerDeploymentPath( + projectId, + 'global', + deploymentId + ), + }; + + const [rollout] = await deploymentClient.getGameServerDeploymentRollout( + request + ); + assert.strictEqual(rollout.defaultGameServerConfig, ''); + }); + + it('should update rollout override', async () => { + const updateOverrideOutput = execSync( + `node update_rollout_override.js ${projectId} ${deploymentId} ${configId} ${realmId} ${realmLocation}` + ); + assert.match(updateOverrideOutput, /Deployment updated:/); + + const getRolloutOutput = execSync( + `node get_rollout.js ${projectId} ${deploymentId} ${configId}` + ); + + const configName = configClient.gameServerConfigPath( + projectId, + 'global', + deploymentId, + configId + ); + const realmName = realmClient.realmPath(projectId, realmLocation, realmId); + assert.match( + getRolloutOutput, + new RegExp(`Rollout config overrides: .*${configName}`) + ); + assert.match(getRolloutOutput, new RegExp(realmName)); + }); + + it('should remove rollout override', async () => { + const removeOverrideOutput = execSync( + `node update_rollout_remove_override.js ${projectId} ${deploymentId}` + ); + + assert.match(removeOverrideOutput, /Deployment updated:/); + + const request = { + // The full resource name + name: deploymentClient.gameServerDeploymentPath( + projectId, + 'global', + deploymentId + ), + }; + + const [rollout] = await deploymentClient.getGameServerDeploymentRollout( + request + ); + assert.strictEqual(rollout.gameServerConfigOverrides.length, 0); + }); + + after(async () => { + // Delete the Game Server Config + const request = { + // Provide full resource name of a Game Server Config + name: configClient.gameServerConfigPath( + projectId, + 'global', + deploymentId, + configId + ), + }; + const [operation] = await configClient.deleteGameServerConfig(request); + await operation.promise(); + + // Delete the Game Server Deployment + const request2 = { + // Provide full resource name of a Game Server Deployment + name: deploymentClient.gameServerDeploymentPath( + projectId, + 'global', + deploymentId + ), + }; + const [operation2] = await deploymentClient.deleteGameServerDeployment( + request2 + ); + await operation2.promise(); + + // Delete the Realm + const request3 = { + // Provide full resource name of a Realm + name: realmClient.realmPath(projectId, realmLocation, realmId), + }; + const [operation3] = await realmClient.deleteRealm(request3); + await operation3.promise(); + }); +}); diff --git a/generated,README.md/update_rollout_default.js b/generated,README.md/update_rollout_default.js new file mode 100644 index 0000000000..1aa9edf372 --- /dev/null +++ b/generated,README.md/update_rollout_default.js @@ -0,0 +1,73 @@ +// Copyright 2020, Google LLC. +// 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. + +'use strict'; + +/** + * Rollout update to add default config. + * @param {string} projectId string project identifier + * @param {string} deploymentId unique identifier for the new Game Server Deployment + * @param {string} configId unique identifier for the new Game Server Config + */ +async function main( + projectId = 'YOUR_PROJECT_ID', + deploymentId = 'DEPLOYMENT_ID', + configId = 'CONFIG_ID' +) { + // [START cloud_game_servers_deployment_rollout_default] + const { + GameServerDeploymentsServiceClient, + } = require('@google-cloud/game-servers'); + + const client = new GameServerDeploymentsServiceClient(); + + async function rolloutDefaultGameServerDeployment() { + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'Your Google Cloud Project ID'; + // const deploymentId = 'A unique ID for the Game Server Deployment'; + // const configId = 'A unique ID for the Game Server Config'; + const request = { + rollout: { + name: client.gameServerDeploymentPath( + projectId, + 'global', + deploymentId + ), + defaultGameServerConfig: configId, + }, + updateMask: { + paths: ['default_game_server_config'], + }, + }; + + const [operation] = await client.updateGameServerDeploymentRollout(request); + const [deployment] = await operation.promise(); + + console.log(`Deployment updated: ${deployment.name}`); + } + + rolloutDefaultGameServerDeployment(); + + // [END cloud_game_servers_deployment_rollout_default] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/generated,README.md/update_rollout_override.js b/generated,README.md/update_rollout_override.js new file mode 100644 index 0000000000..ee27d4b738 --- /dev/null +++ b/generated,README.md/update_rollout_override.js @@ -0,0 +1,86 @@ +// Copyright 2020, Google LLC. +// 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. + +'use strict'; + +/** + * Rollout update to add override config. + * @param {string} projectId string project identifier + * @param {string} deploymentId unique identifier for the new Game Server Deployment + * @param {string} configId unique identifier for the new Game Server Config + * @param {string} realmId the unique ID of the realm + * @param {string} realmLocation Compute Engine region for realm location. + */ +async function main( + projectId = 'YOUR_PROJECT_ID', + deploymentId = 'DEPLOYMENT_ID', + configId = 'CONFIG_ID', + realmId = 'REALM_ID', + realmLocation = 'REALM_LOCATION' +) { + // [START cloud_game_servers_deployment_rollout_override] + const { + GameServerDeploymentsServiceClient, + } = require('@google-cloud/game-servers'); + + const client = new GameServerDeploymentsServiceClient(); + + async function rolloutGameServerDeploymentOverride() { + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'Your Google Cloud Project ID'; + // const deploymentId = 'A unique ID for the Game Server Deployment'; + // const configId = 'A unique ID for the Game Server Config'; + // const realmId = 'A unique ID for the realm' + // const realmLocation = 'compute engine region for realm location' + const request = { + rollout: { + name: client.gameServerDeploymentPath( + projectId, + 'global', + deploymentId + ), + gameServerConfigOverrides: [ + { + realmsSelector: { + realms: [client.realmPath(projectId, realmLocation, realmId)], + }, + configVersion: configId, + }, + ], + }, + updateMask: { + paths: ['game_server_config_overrides'], + }, + }; + + const [operation] = await client.updateGameServerDeploymentRollout(request); + const [deployment] = await operation.promise(); + + console.log(`Deployment updated: ${deployment.name}`); + } + + rolloutGameServerDeploymentOverride(); + + // [END cloud_game_servers_deployment_rollout_override] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/generated,README.md/update_rollout_remove_default.js b/generated,README.md/update_rollout_remove_default.js new file mode 100644 index 0000000000..7584a00bfc --- /dev/null +++ b/generated,README.md/update_rollout_remove_default.js @@ -0,0 +1,70 @@ +// Copyright 2020, Google LLC. +// 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. + +'use strict'; + +/** + * Rollout update to remove default config. + * @param {string} projectId string project identifier + * @param {string} deploymentId unique identifier for the new Game Server Deployment + */ +async function main( + projectId = 'YOUR_PROJECT_ID', + deploymentId = 'DEPLOYMENT_ID' +) { + // [START cloud_game_servers_deployment_rollout_remove_default] + const { + GameServerDeploymentsServiceClient, + } = require('@google-cloud/game-servers'); + + const client = new GameServerDeploymentsServiceClient(); + + async function removeRolloutDefaultGameServerDeployment() { + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'Your Google Cloud Project ID'; + // const deploymentId = 'A unique ID for the Game Server Deployment'; + const request = { + rollout: { + name: client.gameServerDeploymentPath( + projectId, + 'global', + deploymentId + ), + defaultGameServerConfig: '', + }, + updateMask: { + paths: ['default_game_server_config'], + }, + }; + + const [operation] = await client.updateGameServerDeploymentRollout(request); + const [deployment] = await operation.promise(); + + console.log(`Deployment updated: ${deployment.name}`); + } + + removeRolloutDefaultGameServerDeployment(); + + // [END cloud_game_servers_deployment_rollout_remove_default] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/generated,README.md/update_rollout_remove_override.js b/generated,README.md/update_rollout_remove_override.js new file mode 100644 index 0000000000..031e950c01 --- /dev/null +++ b/generated,README.md/update_rollout_remove_override.js @@ -0,0 +1,69 @@ +// Copyright 2020, Google LLC. +// 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. + +'use strict'; + +/** + * Rollout update to remove override config. + * @param {string} projectId string project identifier + * @param {string} deploymentId unique identifier for the new Game Server Deployment + */ +async function main( + projectId = 'YOUR_PROJECT_ID', + deploymentId = 'DEPLOYMENT_ID' +) { + // [START cloud_game_servers_deployment_rollout_remove_override] + const { + GameServerDeploymentsServiceClient, + } = require('@google-cloud/game-servers'); + + const client = new GameServerDeploymentsServiceClient(); + + async function removeRolloutGameServerDeploymentOverride() { + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'Your Google Cloud Project ID'; + // const deploymentId = 'A unique ID for the Game Server Deployment'; + const request = { + rollout: { + name: client.gameServerDeploymentPath( + projectId, + 'global', + deploymentId + ), + }, + updateMask: { + paths: ['game_server_config_overrides'], + }, + }; + + const [operation] = await client.updateGameServerDeploymentRollout(request); + const [deployment] = await operation.promise(); + + console.log(`Deployment updated: ${deployment.name}`); + } + + removeRolloutGameServerDeploymentOverride(); + + // [END cloud_game_servers_deployment_rollout_remove_override] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +});