Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

Commit

Permalink
feat: adds CRUD samples for GS realms (#21)
Browse files Browse the repository at this point in the history
* Adds Game Servers realm samples
* feat: adds realm CRUD tests
* feat: adds quickstart test
* fix: updates region tags
  • Loading branch information
telpirion authored Mar 3, 2020
1 parent 95ef8d1 commit b1bf3e4
Show file tree
Hide file tree
Showing 11 changed files with 509 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ system-test/*key.json
.DS_Store
package-lock.json
__pycache__
*.code-workspace
launch.json
65 changes: 65 additions & 0 deletions samples/create_realm.js
Original file line number Diff line number Diff line change
@@ -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));
55 changes: 55 additions & 0 deletions samples/delete_realm.js
Original file line number Diff line number Diff line change
@@ -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));
54 changes: 54 additions & 0 deletions samples/get_realm.js
Original file line number Diff line number Diff line change
@@ -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));
49 changes: 49 additions & 0 deletions samples/list_realms.js
Original file line number Diff line number Diff line change
@@ -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));
7 changes: 4 additions & 3 deletions samples/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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}`,
Expand All @@ -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 => {
Expand Down
50 changes: 50 additions & 0 deletions samples/test/create_realm.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
54 changes: 54 additions & 0 deletions samples/test/delete_realm.test.js
Original file line number Diff line number Diff line change
@@ -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./);
});
});
Loading

0 comments on commit b1bf3e4

Please sign in to comment.