Skip to content

Commit f9bee80

Browse files
docs(samples): Add Game Server deployment samples (#92)
Co-authored-by: Justin Beckwith <justin.beckwith@gmail.com>
1 parent df6a323 commit f9bee80

File tree

7 files changed

+323
-3
lines changed

7 files changed

+323
-3
lines changed

game-servers/snippets/create_cluster.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
* @param {string} location Compute Engine region
2020
* @param {string} realmId the realm to use
2121
* @param {string} gameClusterId unique identifier for the new Game Cluster
22-
* @param {string} gkeClusterId the GKE cluster to connect to
23-
* @param {string} gkeLocation the location of the GKE cluster
22+
* @param {string} gkeClusterName The full resource name of the GKE cluster to use
2423
*/
2524
async function main(
2625
projectId = 'YOUR_PROJECT_ID',
@@ -54,6 +53,7 @@ async function main(
5453
connectionInfo: {
5554
gkeClusterReference: {
5655
// Provide full resource name of a Kubernetes Engine cluster
56+
// In the form of 'projects/<project-id>/locations/<location>/clusters/<gke-cluster-name>'
5757
cluster: gkeClusterName,
5858
},
5959
namespace: 'default',
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 Deployment.
18+
* @param {string} projectId string project identifier
19+
* @param {string} deploymentId unique identifier for the new Game Server Deployment
20+
*/
21+
async function main(
22+
projectId = 'YOUR_PROJECT_ID',
23+
deploymentId = 'DEPLOYMENT_ID'
24+
) {
25+
// [START cloud_game_servers_create_deployment]
26+
const {
27+
GameServerDeploymentsServiceClient,
28+
} = require('@google-cloud/game-servers');
29+
30+
const client = new GameServerDeploymentsServiceClient();
31+
32+
async function createGameServerDeployment() {
33+
/**
34+
* TODO(developer): Uncomment these variables before running the sample.
35+
*/
36+
// const projectId = 'Your Google Cloud Project ID';
37+
// const deploymentId = 'A unique ID for the Game Server Deployment';
38+
const request = {
39+
parent: `projects/${projectId}/locations/global`,
40+
deploymentId: deploymentId,
41+
gameServerDeployment: {
42+
description: 'nodejs test deployment',
43+
},
44+
};
45+
46+
const [operation] = await client.createGameServerDeployment(request);
47+
const [result] = await operation.promise();
48+
49+
console.log('Game Server Deployment created:');
50+
console.log(`\t Deployment name: ${result.name}`);
51+
console.log(`\t Deployment description: ${result.description}`);
52+
}
53+
54+
createGameServerDeployment();
55+
56+
// [END cloud_game_servers_create_deployment]
57+
}
58+
59+
main(...process.argv.slice(2)).catch(err => {
60+
console.error(err.message);
61+
process.exitCode = 1;
62+
});
63+
process.on('unhandledRejection', err => {
64+
console.error(err.message);
65+
process.exitCode = 1;
66+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 Game Servers Deployment.
18+
* @param {string} projectId string project identifier
19+
* @param {string} deploymentId unique identifier for the new Game Server Deployment
20+
*/
21+
async function main(
22+
projectId = 'YOUR_PROJECT_ID',
23+
deploymentId = 'DEPLOYMENT_ID'
24+
) {
25+
// [START cloud_game_servers_delete_deployment]
26+
const {
27+
GameServerDeploymentsServiceClient,
28+
} = require('@google-cloud/game-servers');
29+
30+
const client = new GameServerDeploymentsServiceClient();
31+
32+
async function deleteGameServerDeployment() {
33+
/**
34+
* TODO(developer): Uncomment these variables before running the sample.
35+
*/
36+
// const projectId = 'Your Google Cloud Project ID';
37+
// const deploymentId = 'A unique ID for the Game Server Deployment';
38+
const request = {
39+
// The full resource name
40+
name: client.gameServerDeploymentPath(projectId, 'global', deploymentId),
41+
};
42+
43+
await client.deleteGameServerDeployment(request);
44+
45+
console.log('deleted.');
46+
}
47+
48+
deleteGameServerDeployment();
49+
50+
// [END cloud_game_servers_delete_deployment]
51+
}
52+
53+
main(...process.argv.slice(2)).catch(err => {
54+
console.error(err.message);
55+
process.exitCode = 1;
56+
});
57+
process.on('unhandledRejection', err => {
58+
console.error(err.message);
59+
process.exitCode = 1;
60+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 Game Servers Deployment.
18+
* @param {string} projectId string project identifier
19+
* @param {string} deploymentId unique identifier for the new Game Server Deployment
20+
*/
21+
async function main(
22+
projectId = 'YOUR_PROJECT_ID',
23+
deploymentId = 'DEPLOYMENT_ID'
24+
) {
25+
// [START cloud_game_servers_get_deployment]
26+
const {
27+
GameServerDeploymentsServiceClient,
28+
} = require('@google-cloud/game-servers');
29+
30+
const client = new GameServerDeploymentsServiceClient();
31+
32+
async function getGameServerDeployment() {
33+
/**
34+
* TODO(developer): Uncomment these variables before running the sample.
35+
*/
36+
// const projectId = 'Your Google Cloud Project ID';
37+
// const deploymentId = 'A unique ID for the Game Server Deployment';
38+
const request = {
39+
// The full resource name
40+
name: client.gameServerDeploymentPath(projectId, 'global', deploymentId),
41+
};
42+
43+
const [deployment] = await client.getGameServerDeployment(request);
44+
console.log(`Deployment name: ${deployment.name}`);
45+
console.log(`Deployment description: ${deployment.description}`);
46+
47+
const createTime = deployment.createTime;
48+
const createDate = new Date(createTime.seconds * 1000);
49+
console.log(`Deployment created on: ${createDate.toLocaleDateString()}\n`);
50+
}
51+
52+
getGameServerDeployment();
53+
54+
// [END cloud_game_servers_get_deployment]
55+
}
56+
57+
main(...process.argv.slice(2)).catch(err => {
58+
console.error(err.message);
59+
process.exitCode = 1;
60+
});
61+
process.on('unhandledRejection', err => {
62+
console.error(err.message);
63+
process.exitCode = 1;
64+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 the Game Servers Deployments.
18+
* @param {string} projectId string project identifier
19+
*/
20+
async function main(projectId = 'YOUR_PROJECT_ID') {
21+
// [START cloud_game_servers_list_deployments]
22+
const {
23+
GameServerDeploymentsServiceClient,
24+
} = require('@google-cloud/game-servers');
25+
26+
const client = new GameServerDeploymentsServiceClient();
27+
28+
async function listGameServerDeployments() {
29+
/**
30+
* TODO(developer): Uncomment these variables before running the sample.
31+
*/
32+
// const projectId = 'Your Google Cloud Project ID';
33+
const request = {
34+
parent: `projects/${projectId}/locations/global`,
35+
};
36+
37+
const [results] = await client.listGameServerDeployments(request);
38+
for (const deployment of results) {
39+
console.log(`Deployment name: ${deployment.name}`);
40+
console.log(`Deployment description: ${deployment.description}`);
41+
42+
const createTime = deployment.createTime;
43+
const createDate = new Date(createTime.seconds * 1000);
44+
console.log(
45+
`Deployment created on: ${createDate.toLocaleDateString()}\n`
46+
);
47+
}
48+
}
49+
50+
listGameServerDeployments();
51+
52+
// [END cloud_game_servers_list_deployments]
53+
}
54+
55+
main(...process.argv.slice(2)).catch(err => {
56+
console.error(err.message);
57+
process.exitCode = 1;
58+
});
59+
process.on('unhandledRejection', err => {
60+
console.error(err.message);
61+
process.exitCode = 1;
62+
});

game-servers/snippets/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"@google-cloud/game-servers": "^2.0.2"
1717
},
1818
"devDependencies": {
19-
"c8": "^7.0.0",
19+
"c8": "^7.2.1",
2020
"chai": "^4.2.0",
2121
"mocha": "^8.0.0",
2222
"uuid": "^8.0.0"
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 cleanup = require('./clean.js');
19+
const {describe, before, it} = require('mocha');
20+
const {
21+
GameServerDeploymentsServiceClient,
22+
} = require('@google-cloud/game-servers');
23+
24+
const cp = require('child_process');
25+
const uuid = require('uuid');
26+
27+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
28+
29+
describe('Game Server Deployment Test', () => {
30+
const client = new GameServerDeploymentsServiceClient();
31+
let deploymentId;
32+
let projectId;
33+
34+
before(async () => {
35+
projectId = await client.getProjectId();
36+
deploymentId = `test-${uuid.v4()}`;
37+
38+
await cleanup();
39+
});
40+
41+
it('should create a game server deployment', async () => {
42+
const create_output = execSync(
43+
`node create_deployment.js ${projectId} ${deploymentId}`
44+
);
45+
assert.match(create_output, /Deployment name:/);
46+
});
47+
48+
it('should get a game server deployment', async () => {
49+
const get_output = execSync(
50+
`node get_deployment.js ${projectId} ${deploymentId}`
51+
);
52+
assert.match(get_output, /Deployment name:/);
53+
});
54+
55+
it('should list a game server deployment', async () => {
56+
const list_output = execSync(
57+
`node list_deployments.js ${projectId} ${deploymentId}`
58+
);
59+
assert.match(list_output, /Deployment name:/);
60+
});
61+
62+
it('should delete a game server deployment', async () => {
63+
const delete_output = execSync(
64+
`node delete_deployment.js ${projectId} ${deploymentId}`
65+
);
66+
assert.match(delete_output, /deleted./);
67+
});
68+
});

0 commit comments

Comments
 (0)