From ab781b532989970f703d06f6620612f0b49afd42 Mon Sep 17 00:00:00 2001 From: Eric Schmidt Date: Mon, 2 Mar 2020 16:42:59 -0800 Subject: [PATCH] feat: adds CRUD samples for GS realms (#21) * Adds Game Servers realm samples * feat: adds realm CRUD tests * feat: adds quickstart test * fix: updates region tags --- generated,README.md/create_realm.js | 65 +++++++++++++++++++ generated,README.md/delete_realm.js | 55 ++++++++++++++++ generated,README.md/get_realm.js | 54 +++++++++++++++ generated,README.md/list_realms.js | 49 ++++++++++++++ generated,README.md/quickstart.js | 7 +- generated,README.md/test/create_realm.test.js | 50 ++++++++++++++ generated,README.md/test/delete_realm.test.js | 54 +++++++++++++++ generated,README.md/test/get_realm.test.js | 63 ++++++++++++++++++ generated,README.md/test/list_realms.test.js | 63 ++++++++++++++++++ generated,README.md/test/quickstart.test.js | 50 ++++++++++++++ 10 files changed, 507 insertions(+), 3 deletions(-) create mode 100644 generated,README.md/create_realm.js create mode 100644 generated,README.md/delete_realm.js create mode 100644 generated,README.md/get_realm.js create mode 100644 generated,README.md/list_realms.js create mode 100644 generated,README.md/test/create_realm.test.js create mode 100644 generated,README.md/test/delete_realm.test.js create mode 100644 generated,README.md/test/get_realm.test.js create mode 100644 generated,README.md/test/list_realms.test.js create mode 100644 generated,README.md/test/quickstart.test.js diff --git a/generated,README.md/create_realm.js b/generated,README.md/create_realm.js new file mode 100644 index 0000000000..8ce712035f --- /dev/null +++ b/generated,README.md/create_realm.js @@ -0,0 +1,65 @@ +// 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'; + +/** + * Create a Game Servers realm. + * @param {string} projectId string project identifier. + * @param {string} location Compute Engine region. + * @param {string} realmId a unique identifier for the new realm + */ +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'LOCATION_ID', + realmId = 'REALM_ID' +) { + // [START cloud_game_servers_create_realm] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'Your Google Cloud Project ID'; + // const location = 'A Compute Engine region, e.g. "us-central1"'; + // const realmId = 'A unique identifier for the realm'; + const {RealmsServiceClient} = require('@google-cloud/game-servers'); + + const client = new RealmsServiceClient(); + + async function createRealm() { + const request = { + parent: `projects/${projectId}/locations/${location}`, + realmId: realmId, + realm: { + // Must use a supported time zone name. + // See https://cloud.google.com/dataprep/docs/html/Supported-Time-Zone-Values_66194188 + timeZone: 'US/Pacific', + description: 'My Game Server realm', + }, + }; + + const [operation] = await client.createRealm(request); + const results = await operation.promise(); + const [realm] = results; + + console.log('Realm created:'); + + console.log(`\tRealm name: ${realm.name}`); + console.log(`\tRealm description: ${realm.description}`); + console.log(`\tRealm time zone: ${realm.timeZone}`); + // [END cloud_game_servers_create_realm] + } + + createRealm(); +} + +main(...process.argv.slice(2)); diff --git a/generated,README.md/delete_realm.js b/generated,README.md/delete_realm.js new file mode 100644 index 0000000000..f5224437c3 --- /dev/null +++ b/generated,README.md/delete_realm.js @@ -0,0 +1,55 @@ +// 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'; + +/** + * Delete a realm by ID + * @param {string} projectId string project identifier. + * @param {string} location Compute Engine region. + * @param {string} realmId the unique ID of the realm + */ +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'LOCATION_ID', + realmId = 'REALM_ID' +) { + // [START cloud_game_servers_delete_realm] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'Your Google Cloud Project ID'; + // const location = 'A Compute Engine region, e.g. "us-central1"'; + // const realmId = 'Unique identifier of the realm'; + const {RealmsServiceClient} = require('@google-cloud/game-servers'); + + const client = new RealmsServiceClient(); + + async function deleteRealm() { + const request = { + // Realm name is the full resource name including project ID,location, + // and the realm ID. + name: `projects/${projectId}/locations/${location}/realms/${realmId}`, + }; + + const [operation] = await client.deleteRealm(request); + await operation.promise(); + console.log(`Realm with ID ${realmId} deleted.`); + + // [END cloud_game_servers_delete_realm] + } + + deleteRealm(); +} + +main(...process.argv.slice(2)); diff --git a/generated,README.md/get_realm.js b/generated,README.md/get_realm.js new file mode 100644 index 0000000000..f2379ad5ae --- /dev/null +++ b/generated,README.md/get_realm.js @@ -0,0 +1,54 @@ +// 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 realm by ID + * @param {string} projectId string project identifier. + * @param {string} location Compute Engine region. + * @param {string} realmId the unique ID of the realm + */ +function main( + projectId = 'YOUR_PROJECT_ID', + location = 'LOCATION_ID', + realmId = 'REALM_ID' +) { + // [START cloud_game_servers_get_realm] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'Your Google Cloud Project ID'; + // const location = 'A Compute Engine region, e.g. "us-central1"'; + // const realmId = 'Unique identifier of the realm'; + const {RealmsServiceClient} = require('@google-cloud/game-servers'); + + const client = new RealmsServiceClient(); + + async function getRealm() { + const request = { + // Realm name is the full resource name including project ID and location + name: `projects/${projectId}/locations/${location}/realms/${realmId}`, + }; + + const [realm] = await client.getRealm(request); + console.log(`Realm name: ${realm.name}`); + console.log(`Realm description: ${realm.description}`); + console.log(`Realm time zone: ${realm.timeZone}`); + // [END cloud_game_servers_get_realm] + } + + getRealm(); +} + +main(...process.argv.slice(2)); diff --git a/generated,README.md/list_realms.js b/generated,README.md/list_realms.js new file mode 100644 index 0000000000..d8a7f9c6d8 --- /dev/null +++ b/generated,README.md/list_realms.js @@ -0,0 +1,49 @@ +// 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'; + +/** + * List all of the realms in a project. + * @param {string} projectId string project identifier. + * @param {string} location Compute Engine region. + */ +function main(projectId = 'YOUR_PROJECT_ID', location = 'LOCATION_ID') { + // [START cloud_game_servers_list_realms] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'Your Google Cloud Project ID'; + // const location = 'A Compute Engine region, e.g. "us-central1"'; + const {RealmsServiceClient} = require('@google-cloud/game-servers'); + + const client = new RealmsServiceClient(); + + async function listRealms() { + const request = { + parent: `projects/${projectId}/locations/${location}`, + }; + + const [results] = await client.listRealms(request); + for (const realm of results.realms) { + console.log(`Realm name: ${realm.name}`); + console.log(`Realm description: ${realm.description}`); + console.log(`Realm time zone: ${realm.timeZone}\n`); + } + // [END cloud_game_servers_list_realms] + } + + listRealms(); +} + +main(...process.argv.slice(2)); diff --git a/generated,README.md/quickstart.js b/generated,README.md/quickstart.js index a6dc596f2b..3a9c542322 100644 --- a/generated,README.md/quickstart.js +++ b/generated,README.md/quickstart.js @@ -22,9 +22,10 @@ * Create a Game Servers realm. * @param {string} projectId string project identifier. * @param {string} location Compute Engine region. + * @param {string} realmId unique identifier for the realm. */ async function main(projectId, location, realmId) { - // [START game_servers_quickstart] + // [START cloud_game_servers_quickstart] const {RealmsServiceClient} = require('@google-cloud/game-servers'); async function quickstart() { @@ -33,7 +34,7 @@ async function main(projectId, location, realmId) { // TODO(developer): uncomment the following section, and add values // const projectId = 'YOUR_PROJECT_ID'; // const location = 'us-central1; - // const realIm = 'DESIRED_REALM_ID'; + // const realmId = 'DESIRED_REALM_ID'; const request = { parent: `projects/${projectId}/locations/${location}`, @@ -57,7 +58,7 @@ async function main(projectId, location, realmId) { console.log(`\tRealm time zone: ${realm.timeZone}`); } quickstart(); - // [END game_servers_quickstart] + // [END cloud_game_servers_quickstart] } main(...process.argv.slice(2)).catch(err => { diff --git a/generated,README.md/test/create_realm.test.js b/generated,README.md/test/create_realm.test.js new file mode 100644 index 0000000000..293298a2dc --- /dev/null +++ b/generated,README.md/test/create_realm.test.js @@ -0,0 +1,50 @@ +// 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 {describe, it, after} = require('mocha'); +const {RealmsServiceClient} = require('@google-cloud/game-servers'); + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const LOCATION = 'us-central1'; + +describe('Game Servers Create Realm Test', () => { + const client = new RealmsServiceClient(); + let realmId; + + it('should create a realm', async () => { + const projectId = await client.getProjectId(); + realmId = `test-${uuid.v4()}`; + + const create_output = execSync( + `node create_realm.js ${projectId} ${LOCATION} ${realmId}` + ); + assert.match(create_output, /Realm time zone:/); + }); + + after(async () => { + const projectId = await client.getProjectId(); + const request = { + name: `projects/${projectId}/locations/${LOCATION}/realms/${realmId}`, + }; + const [operation] = await client.deleteRealm(request); + await operation.promise(); + }); +}); diff --git a/generated,README.md/test/delete_realm.test.js b/generated,README.md/test/delete_realm.test.js new file mode 100644 index 0000000000..e88a1d3530 --- /dev/null +++ b/generated,README.md/test/delete_realm.test.js @@ -0,0 +1,54 @@ +// 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 {describe, it, before} = require('mocha'); +const {RealmsServiceClient} = require('@google-cloud/game-servers'); + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const LOCATION = 'us-central1'; + +describe('Game Servers Delete Realm Test', () => { + const client = new RealmsServiceClient(); + let realmId; + + before(async () => { + const projectId = await client.getProjectId(); + realmId = `test-${uuid.v4()}`; + + await client.createRealm({ + parent: `projects/${projectId}/locations/${LOCATION}`, + realmId: realmId, + realm: { + timeZone: 'US/Pacific', + description: 'Test Game Realm', + }, + }); + }); + + it('should delete a realm', async () => { + const projectId = await client.getProjectId(); + + const delete_output = execSync( + `node delete_realm.js ${projectId} ${LOCATION} ${realmId}` + ); + assert.match(delete_output, /deleted./); + }); +}); diff --git a/generated,README.md/test/get_realm.test.js b/generated,README.md/test/get_realm.test.js new file mode 100644 index 0000000000..4eb9ebc9c8 --- /dev/null +++ b/generated,README.md/test/get_realm.test.js @@ -0,0 +1,63 @@ +// 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 {describe, it, before, after} = require('mocha'); +const {RealmsServiceClient} = require('@google-cloud/game-servers'); + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const LOCATION = 'us-central1'; + +describe('Game Servers Get Realms Test', () => { + const client = new RealmsServiceClient(); + let realmId; + + before(async () => { + const projectId = await client.getProjectId(); + realmId = `test-${uuid.v4()}`; + + await client.createRealm({ + parent: `projects/${projectId}/locations/${LOCATION}`, + realmId: realmId, + realm: { + timeZone: 'US/Pacific', + description: 'Test Game Realm', + }, + }); + }); + + it('should get a realm', async () => { + const projectId = await client.getProjectId(); + + const get_output = execSync( + `node get_realm.js ${projectId} ${LOCATION} ${realmId}` + ); + assert.match(get_output, /Realm description:/); + }); + + after(async () => { + const projectId = await client.getProjectId(); + const request = { + name: `projects/${projectId}/locations/${LOCATION}/realms/${realmId}`, + }; + const [operation] = await client.deleteRealm(request); + await operation.promise(); + }); +}); diff --git a/generated,README.md/test/list_realms.test.js b/generated,README.md/test/list_realms.test.js new file mode 100644 index 0000000000..35c78613bd --- /dev/null +++ b/generated,README.md/test/list_realms.test.js @@ -0,0 +1,63 @@ +// 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 {describe, it, before, after} = require('mocha'); +const {RealmsServiceClient} = require('@google-cloud/game-servers'); + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const LOCATION = 'us-central1'; + +describe('Game Servers List Realms Test', () => { + const client = new RealmsServiceClient(); + let realmId; + + before(async () => { + const projectId = await client.getProjectId(); + realmId = `test-${uuid.v4()}`; + + await client.createRealm({ + parent: `projects/${projectId}/locations/${LOCATION}`, + realmId: realmId, + realm: { + timeZone: 'US/Pacific', + description: 'Test Game Realm', + }, + }); + }); + + it('should list realms', async () => { + const projectId = await client.getProjectId(); + + const list_output = execSync( + `node list_realms.js ${projectId} ${LOCATION}` + ); + assert.match(list_output, /Realm name:/); + }); + + after(async () => { + const projectId = await client.getProjectId(); + const request = { + name: `projects/${projectId}/locations/${LOCATION}/realms/${realmId}`, + }; + const [operation] = await client.deleteRealm(request); + await operation.promise(); + }); +}); diff --git a/generated,README.md/test/quickstart.test.js b/generated,README.md/test/quickstart.test.js new file mode 100644 index 0000000000..74b1644527 --- /dev/null +++ b/generated,README.md/test/quickstart.test.js @@ -0,0 +1,50 @@ +// 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 {describe, it, after} = require('mocha'); +const {RealmsServiceClient} = require('@google-cloud/game-servers'); + +const cp = require('child_process'); +const uuid = require('uuid'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const LOCATION = 'us-central1'; + +describe('Game Servers Quickstart Test', () => { + const client = new RealmsServiceClient(); + let realmId; + + it('should create a realm', async () => { + const projectId = await client.getProjectId(); + realmId = `test-${uuid.v4()}`; + + const quickstart_output = execSync( + `node quickstart.js ${projectId} ${LOCATION} ${realmId}` + ); + assert.match(quickstart_output, /Realm time zone:/); + }); + + after(async () => { + const projectId = await client.getProjectId(); + const request = { + name: `projects/${projectId}/locations/${LOCATION}/realms/${realmId}`, + }; + const [operation] = await client.deleteRealm(request); + await operation.promise(); + }); +});