Skip to content

Commit f0f2cd1

Browse files
authored
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
1 parent 20bb010 commit f0f2cd1

File tree

10 files changed

+507
-3
lines changed

10 files changed

+507
-3
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2020, Google LLC.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
/**
17+
* Create a Game Servers realm.
18+
* @param {string} projectId string project identifier.
19+
* @param {string} location Compute Engine region.
20+
* @param {string} realmId a unique identifier for the new realm
21+
*/
22+
function main(
23+
projectId = 'YOUR_PROJECT_ID',
24+
location = 'LOCATION_ID',
25+
realmId = 'REALM_ID'
26+
) {
27+
// [START cloud_game_servers_create_realm]
28+
/**
29+
* TODO(developer): Uncomment these variables before running the sample.
30+
*/
31+
// const projectId = 'Your Google Cloud Project ID';
32+
// const location = 'A Compute Engine region, e.g. "us-central1"';
33+
// const realmId = 'A unique identifier for the realm';
34+
const {RealmsServiceClient} = require('@google-cloud/game-servers');
35+
36+
const client = new RealmsServiceClient();
37+
38+
async function createRealm() {
39+
const request = {
40+
parent: `projects/${projectId}/locations/${location}`,
41+
realmId: realmId,
42+
realm: {
43+
// Must use a supported time zone name.
44+
// See https://cloud.google.com/dataprep/docs/html/Supported-Time-Zone-Values_66194188
45+
timeZone: 'US/Pacific',
46+
description: 'My Game Server realm',
47+
},
48+
};
49+
50+
const [operation] = await client.createRealm(request);
51+
const results = await operation.promise();
52+
const [realm] = results;
53+
54+
console.log('Realm created:');
55+
56+
console.log(`\tRealm name: ${realm.name}`);
57+
console.log(`\tRealm description: ${realm.description}`);
58+
console.log(`\tRealm time zone: ${realm.timeZone}`);
59+
// [END cloud_game_servers_create_realm]
60+
}
61+
62+
createRealm();
63+
}
64+
65+
main(...process.argv.slice(2));
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2020, Google LLC.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
/**
17+
* Delete a realm by ID
18+
* @param {string} projectId string project identifier.
19+
* @param {string} location Compute Engine region.
20+
* @param {string} realmId the unique ID of the realm
21+
*/
22+
function main(
23+
projectId = 'YOUR_PROJECT_ID',
24+
location = 'LOCATION_ID',
25+
realmId = 'REALM_ID'
26+
) {
27+
// [START cloud_game_servers_delete_realm]
28+
/**
29+
* TODO(developer): Uncomment these variables before running the sample.
30+
*/
31+
// const projectId = 'Your Google Cloud Project ID';
32+
// const location = 'A Compute Engine region, e.g. "us-central1"';
33+
// const realmId = 'Unique identifier of the realm';
34+
const {RealmsServiceClient} = require('@google-cloud/game-servers');
35+
36+
const client = new RealmsServiceClient();
37+
38+
async function deleteRealm() {
39+
const request = {
40+
// Realm name is the full resource name including project ID,location,
41+
// and the realm ID.
42+
name: `projects/${projectId}/locations/${location}/realms/${realmId}`,
43+
};
44+
45+
const [operation] = await client.deleteRealm(request);
46+
await operation.promise();
47+
console.log(`Realm with ID ${realmId} deleted.`);
48+
49+
// [END cloud_game_servers_delete_realm]
50+
}
51+
52+
deleteRealm();
53+
}
54+
55+
main(...process.argv.slice(2));

game-servers/snippets/get_realm.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2020, Google LLC.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
/**
17+
* Get a realm by ID
18+
* @param {string} projectId string project identifier.
19+
* @param {string} location Compute Engine region.
20+
* @param {string} realmId the unique ID of the realm
21+
*/
22+
function main(
23+
projectId = 'YOUR_PROJECT_ID',
24+
location = 'LOCATION_ID',
25+
realmId = 'REALM_ID'
26+
) {
27+
// [START cloud_game_servers_get_realm]
28+
/**
29+
* TODO(developer): Uncomment these variables before running the sample.
30+
*/
31+
// const projectId = 'Your Google Cloud Project ID';
32+
// const location = 'A Compute Engine region, e.g. "us-central1"';
33+
// const realmId = 'Unique identifier of the realm';
34+
const {RealmsServiceClient} = require('@google-cloud/game-servers');
35+
36+
const client = new RealmsServiceClient();
37+
38+
async function getRealm() {
39+
const request = {
40+
// Realm name is the full resource name including project ID and location
41+
name: `projects/${projectId}/locations/${location}/realms/${realmId}`,
42+
};
43+
44+
const [realm] = await client.getRealm(request);
45+
console.log(`Realm name: ${realm.name}`);
46+
console.log(`Realm description: ${realm.description}`);
47+
console.log(`Realm time zone: ${realm.timeZone}`);
48+
// [END cloud_game_servers_get_realm]
49+
}
50+
51+
getRealm();
52+
}
53+
54+
main(...process.argv.slice(2));
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2020, Google LLC.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
/**
17+
* List all of the realms in a project.
18+
* @param {string} projectId string project identifier.
19+
* @param {string} location Compute Engine region.
20+
*/
21+
function main(projectId = 'YOUR_PROJECT_ID', location = 'LOCATION_ID') {
22+
// [START cloud_game_servers_list_realms]
23+
/**
24+
* TODO(developer): Uncomment these variables before running the sample.
25+
*/
26+
// const projectId = 'Your Google Cloud Project ID';
27+
// const location = 'A Compute Engine region, e.g. "us-central1"';
28+
const {RealmsServiceClient} = require('@google-cloud/game-servers');
29+
30+
const client = new RealmsServiceClient();
31+
32+
async function listRealms() {
33+
const request = {
34+
parent: `projects/${projectId}/locations/${location}`,
35+
};
36+
37+
const [results] = await client.listRealms(request);
38+
for (const realm of results.realms) {
39+
console.log(`Realm name: ${realm.name}`);
40+
console.log(`Realm description: ${realm.description}`);
41+
console.log(`Realm time zone: ${realm.timeZone}\n`);
42+
}
43+
// [END cloud_game_servers_list_realms]
44+
}
45+
46+
listRealms();
47+
}
48+
49+
main(...process.argv.slice(2));

game-servers/snippets/quickstart.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
* Create a Game Servers realm.
2323
* @param {string} projectId string project identifier.
2424
* @param {string} location Compute Engine region.
25+
* @param {string} realmId unique identifier for the realm.
2526
*/
2627
async function main(projectId, location, realmId) {
27-
// [START game_servers_quickstart]
28+
// [START cloud_game_servers_quickstart]
2829
const {RealmsServiceClient} = require('@google-cloud/game-servers');
2930

3031
async function quickstart() {
@@ -33,7 +34,7 @@ async function main(projectId, location, realmId) {
3334
// TODO(developer): uncomment the following section, and add values
3435
// const projectId = 'YOUR_PROJECT_ID';
3536
// const location = 'us-central1;
36-
// const realIm = 'DESIRED_REALM_ID';
37+
// const realmId = 'DESIRED_REALM_ID';
3738

3839
const request = {
3940
parent: `projects/${projectId}/locations/${location}`,
@@ -57,7 +58,7 @@ async function main(projectId, location, realmId) {
5758
console.log(`\tRealm time zone: ${realm.timeZone}`);
5859
}
5960
quickstart();
60-
// [END game_servers_quickstart]
61+
// [END cloud_game_servers_quickstart]
6162
}
6263

6364
main(...process.argv.slice(2)).catch(err => {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it, after} = require('mocha');
19+
const {RealmsServiceClient} = require('@google-cloud/game-servers');
20+
21+
const cp = require('child_process');
22+
const uuid = require('uuid');
23+
24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
26+
const LOCATION = 'us-central1';
27+
28+
describe('Game Servers Create Realm Test', () => {
29+
const client = new RealmsServiceClient();
30+
let realmId;
31+
32+
it('should create a realm', async () => {
33+
const projectId = await client.getProjectId();
34+
realmId = `test-${uuid.v4()}`;
35+
36+
const create_output = execSync(
37+
`node create_realm.js ${projectId} ${LOCATION} ${realmId}`
38+
);
39+
assert.match(create_output, /Realm time zone:/);
40+
});
41+
42+
after(async () => {
43+
const projectId = await client.getProjectId();
44+
const request = {
45+
name: `projects/${projectId}/locations/${LOCATION}/realms/${realmId}`,
46+
};
47+
const [operation] = await client.deleteRealm(request);
48+
await operation.promise();
49+
});
50+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it, before} = require('mocha');
19+
const {RealmsServiceClient} = require('@google-cloud/game-servers');
20+
21+
const cp = require('child_process');
22+
const uuid = require('uuid');
23+
24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
26+
const LOCATION = 'us-central1';
27+
28+
describe('Game Servers Delete Realm Test', () => {
29+
const client = new RealmsServiceClient();
30+
let realmId;
31+
32+
before(async () => {
33+
const projectId = await client.getProjectId();
34+
realmId = `test-${uuid.v4()}`;
35+
36+
await client.createRealm({
37+
parent: `projects/${projectId}/locations/${LOCATION}`,
38+
realmId: realmId,
39+
realm: {
40+
timeZone: 'US/Pacific',
41+
description: 'Test Game Realm',
42+
},
43+
});
44+
});
45+
46+
it('should delete a realm', async () => {
47+
const projectId = await client.getProjectId();
48+
49+
const delete_output = execSync(
50+
`node delete_realm.js ${projectId} ${LOCATION} ${realmId}`
51+
);
52+
assert.match(delete_output, /deleted./);
53+
});
54+
});

0 commit comments

Comments
 (0)