diff --git a/game-servers/snippets/package.json b/game-servers/snippets/package.json index 7e445c2d4a..c844a88d0c 100644 --- a/game-servers/snippets/package.json +++ b/game-servers/snippets/package.json @@ -18,6 +18,7 @@ "devDependencies": { "c8": "^5.0.1", "chai": "^4.2.0", - "mocha": "^6.1.4" + "mocha": "^6.1.4", + "uuid": "^7.0.1" } } diff --git a/game-servers/snippets/quickstart.js b/game-servers/snippets/quickstart.js index 197b63d827..a6dc596f2b 100644 --- a/game-servers/snippets/quickstart.js +++ b/game-servers/snippets/quickstart.js @@ -13,37 +13,50 @@ 'use strict'; +// sample-metadata: +// title: Create Game Server Realm +// description: Creates a new Realm within Cloud Game Servers +// usage: node quickstart.js + /** * Create a Game Servers realm. * @param {string} projectId string project identifier. * @param {string} location Compute Engine region. */ -async function main(projectId, location) { +async function main(projectId, location, realmId) { // [START game_servers_quickstart] const {RealmsServiceClient} = require('@google-cloud/game-servers'); - const client = new RealmsServiceClient(); + async function quickstart() { + const client = new RealmsServiceClient(); + + // TODO(developer): uncomment the following section, and add values + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'us-central1; + // const realIm = 'DESIRED_REALM_ID'; - const request = { - parent: `projects/${projectId}/locations/${location}`, - realmId: 'my-realm', - realm: { - // Must use a valid support time zone. - // See https://cloud.google.com/dataprep/docs/html/Supported-Time-Zone-Values_66194188 - timeZone: 'US/Pacific', - description: 'My Game Server realm', - }, - }; + const request = { + parent: `projects/${projectId}/locations/${location}`, + realmId, + realm: { + // Must use a valid support time zone. + // 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; + const [operation] = await client.createRealm(request); + const results = await operation.promise(); + const [realm] = results; - console.log('Realm created:'); + console.log('Realm created:'); - console.log(`\tRealm name: ${realm.name}`); - console.log(`\tRealm description: ${realm.description}`); - console.log(`\tRealm time zone: ${realm.timeZone}`); + console.log(`\tRealm name: ${realm.name}`); + console.log(`\tRealm description: ${realm.description}`); + console.log(`\tRealm time zone: ${realm.timeZone}`); + } + quickstart(); // [END game_servers_quickstart] } diff --git a/game-servers/snippets/test/quickstart.js b/game-servers/snippets/test/quickstart.js index e8365800a0..1c04c5ac07 100644 --- a/game-servers/snippets/test/quickstart.js +++ b/game-servers/snippets/test/quickstart.js @@ -18,17 +18,35 @@ 'use strict'; -const path = require('path'); const {assert} = require('chai'); const cp = require('child_process'); -const {describe, it} = require('mocha'); +const {describe, it, after, before} = require('mocha'); +const uuid = require('uuid'); +const {RealmsServiceClient} = require('@google-cloud/game-servers'); const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); -const cwd = path.join(__dirname, '..'); - describe('Quickstart', () => { + let projectId; + let client; + const location = 'us-central1'; + const realmId = `realm-${uuid.v4().split('-')[0]}`; + + before(async () => { + client = new RealmsServiceClient(); + projectId = await client.getProjectId(); + }); + it('should run quickstart', async () => { - const stdout = execSync(`node quickstart.js`, {cwd}); + const stdout = execSync( + `node quickstart.js ${projectId} ${location} ${realmId}` + ); + assert.include(stdout, 'Realm created:'); + }); + + after(async () => { + await client.deleteRealm({ + name: client.realmPath(projectId, location, realmId), + }); }); });