Skip to content

Commit c42eea2

Browse files
Add sample for game server rollout (#102)
Co-authored-by: Justin Beckwith <justin.beckwith@gmail.com>
1 parent 71ce195 commit c42eea2

File tree

6 files changed

+575
-0
lines changed

6 files changed

+575
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 Rollout.
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_rollout_get]
26+
const {
27+
GameServerDeploymentsServiceClient,
28+
} = require('@google-cloud/game-servers');
29+
30+
const client = new GameServerDeploymentsServiceClient();
31+
32+
async function getGameServerDeploymentRollout() {
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 [rollout] = await client.getGameServerDeploymentRollout(request);
44+
console.log(`Rollout name: ${rollout.name}`);
45+
console.log(`Rollout default: ${rollout.defaultGameServerConfig}`);
46+
for (const override of rollout.gameServerConfigOverrides) {
47+
console.log(`Rollout config overrides: ${override.configVersion}`);
48+
console.log(
49+
`Rollout realm overrides: ${JSON.stringify(override.realmsSelector)}`
50+
);
51+
}
52+
53+
const updateTime = rollout.updateTime;
54+
const updateDate = new Date(updateTime.seconds * 1000);
55+
console.log(`Rollout updated on: ${updateDate.toLocaleDateString()}\n`);
56+
}
57+
58+
getGameServerDeploymentRollout();
59+
60+
// [END cloud_game_servers_rollout_get]
61+
}
62+
63+
main(...process.argv.slice(2)).catch(err => {
64+
console.error(err.message);
65+
process.exitCode = 1;
66+
});
67+
process.on('unhandledRejection', err => {
68+
console.error(err.message);
69+
process.exitCode = 1;
70+
});
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
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, after, it} = require('mocha');
20+
const {
21+
GameServerConfigsServiceClient,
22+
GameServerDeploymentsServiceClient,
23+
RealmsServiceClient,
24+
} = require('@google-cloud/game-servers');
25+
26+
const cp = require('child_process');
27+
const uuid = require('uuid');
28+
29+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
30+
31+
describe('Game Server Rollout Test', () => {
32+
const configClient = new GameServerConfigsServiceClient();
33+
const deploymentClient = new GameServerDeploymentsServiceClient();
34+
const realmClient = new RealmsServiceClient();
35+
let projectId;
36+
const deploymentId = `test-nodejs-${uuid.v4()}`;
37+
const configId = `test-nodejs-${uuid.v4()}`;
38+
const realmId = `test-nodejs-${uuid.v4()}`;
39+
const realmLocation = 'us-central1';
40+
41+
before(async () => {
42+
await cleanup();
43+
44+
projectId = await deploymentClient.getProjectId();
45+
46+
const request = {
47+
parent: `projects/${projectId}/locations/global`,
48+
deploymentId: deploymentId,
49+
};
50+
const [operation] = await deploymentClient.createGameServerDeployment(
51+
request
52+
);
53+
await operation.promise();
54+
55+
const request2 = {
56+
parent: configClient.gameServerDeploymentPath(
57+
projectId,
58+
'global',
59+
deploymentId
60+
),
61+
configId: configId,
62+
};
63+
const [operation2] = await configClient.createGameServerConfig(request2);
64+
await operation2.promise();
65+
66+
const request3 = {
67+
parent: `projects/${projectId}/locations/${realmLocation}`,
68+
realmId: realmId,
69+
realm: {
70+
timeZone: 'US/Pacific',
71+
description: 'Test Game Server realm',
72+
},
73+
};
74+
75+
const [operation3] = await realmClient.createRealm(request3);
76+
await operation3.promise();
77+
});
78+
79+
it('should update rollout default', async () => {
80+
const updateDefaultOutput = execSync(
81+
`node update_rollout_default.js ${projectId} ${deploymentId} ${configId}`
82+
);
83+
84+
assert.match(updateDefaultOutput, /Deployment updated:/);
85+
86+
const getRolloutOutput = execSync(
87+
`node get_rollout.js ${projectId} ${deploymentId} ${configId}`
88+
);
89+
90+
const configName = configClient.gameServerConfigPath(
91+
projectId,
92+
'global',
93+
deploymentId,
94+
configId
95+
);
96+
assert.match(getRolloutOutput, /Rollout name:/);
97+
assert.match(
98+
getRolloutOutput,
99+
new RegExp(`Rollout default: ${configName}`)
100+
);
101+
});
102+
103+
it('should remove rollout default', async () => {
104+
const removeDefaultOutput = execSync(
105+
`node update_rollout_remove_default.js ${projectId} ${deploymentId} ${configId}`
106+
);
107+
108+
assert.match(removeDefaultOutput, /Deployment updated:/);
109+
110+
const request = {
111+
// The full resource name
112+
name: deploymentClient.gameServerDeploymentPath(
113+
projectId,
114+
'global',
115+
deploymentId
116+
),
117+
};
118+
119+
const [rollout] = await deploymentClient.getGameServerDeploymentRollout(
120+
request
121+
);
122+
assert.strictEqual(rollout.defaultGameServerConfig, '');
123+
});
124+
125+
it('should update rollout override', async () => {
126+
const updateOverrideOutput = execSync(
127+
`node update_rollout_override.js ${projectId} ${deploymentId} ${configId} ${realmId} ${realmLocation}`
128+
);
129+
assert.match(updateOverrideOutput, /Deployment updated:/);
130+
131+
const getRolloutOutput = execSync(
132+
`node get_rollout.js ${projectId} ${deploymentId} ${configId}`
133+
);
134+
135+
const configName = configClient.gameServerConfigPath(
136+
projectId,
137+
'global',
138+
deploymentId,
139+
configId
140+
);
141+
const realmName = realmClient.realmPath(projectId, realmLocation, realmId);
142+
assert.match(
143+
getRolloutOutput,
144+
new RegExp(`Rollout config overrides: .*${configName}`)
145+
);
146+
assert.match(getRolloutOutput, new RegExp(realmName));
147+
});
148+
149+
it('should remove rollout override', async () => {
150+
const removeOverrideOutput = execSync(
151+
`node update_rollout_remove_override.js ${projectId} ${deploymentId}`
152+
);
153+
154+
assert.match(removeOverrideOutput, /Deployment updated:/);
155+
156+
const request = {
157+
// The full resource name
158+
name: deploymentClient.gameServerDeploymentPath(
159+
projectId,
160+
'global',
161+
deploymentId
162+
),
163+
};
164+
165+
const [rollout] = await deploymentClient.getGameServerDeploymentRollout(
166+
request
167+
);
168+
assert.strictEqual(rollout.gameServerConfigOverrides.length, 0);
169+
});
170+
171+
after(async () => {
172+
// Delete the Game Server Config
173+
const request = {
174+
// Provide full resource name of a Game Server Config
175+
name: configClient.gameServerConfigPath(
176+
projectId,
177+
'global',
178+
deploymentId,
179+
configId
180+
),
181+
};
182+
const [operation] = await configClient.deleteGameServerConfig(request);
183+
await operation.promise();
184+
185+
// Delete the Game Server Deployment
186+
const request2 = {
187+
// Provide full resource name of a Game Server Deployment
188+
name: deploymentClient.gameServerDeploymentPath(
189+
projectId,
190+
'global',
191+
deploymentId
192+
),
193+
};
194+
const [operation2] = await deploymentClient.deleteGameServerDeployment(
195+
request2
196+
);
197+
await operation2.promise();
198+
199+
// Delete the Realm
200+
const request3 = {
201+
// Provide full resource name of a Realm
202+
name: realmClient.realmPath(projectId, realmLocation, realmId),
203+
};
204+
const [operation3] = await realmClient.deleteRealm(request3);
205+
await operation3.promise();
206+
});
207+
});
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
* Rollout update to add default config.
18+
* @param {string} projectId string project identifier
19+
* @param {string} deploymentId unique identifier for the new Game Server Deployment
20+
* @param {string} configId unique identifier for the new Game Server Config
21+
*/
22+
async function main(
23+
projectId = 'YOUR_PROJECT_ID',
24+
deploymentId = 'DEPLOYMENT_ID',
25+
configId = 'CONFIG_ID'
26+
) {
27+
// [START cloud_game_servers_deployment_rollout_default]
28+
const {
29+
GameServerDeploymentsServiceClient,
30+
} = require('@google-cloud/game-servers');
31+
32+
const client = new GameServerDeploymentsServiceClient();
33+
34+
async function rolloutDefaultGameServerDeployment() {
35+
/**
36+
* TODO(developer): Uncomment these variables before running the sample.
37+
*/
38+
// const projectId = 'Your Google Cloud Project ID';
39+
// const deploymentId = 'A unique ID for the Game Server Deployment';
40+
// const configId = 'A unique ID for the Game Server Config';
41+
const request = {
42+
rollout: {
43+
name: client.gameServerDeploymentPath(
44+
projectId,
45+
'global',
46+
deploymentId
47+
),
48+
defaultGameServerConfig: configId,
49+
},
50+
updateMask: {
51+
paths: ['default_game_server_config'],
52+
},
53+
};
54+
55+
const [operation] = await client.updateGameServerDeploymentRollout(request);
56+
const [deployment] = await operation.promise();
57+
58+
console.log(`Deployment updated: ${deployment.name}`);
59+
}
60+
61+
rolloutDefaultGameServerDeployment();
62+
63+
// [END cloud_game_servers_deployment_rollout_default]
64+
}
65+
66+
main(...process.argv.slice(2)).catch(err => {
67+
console.error(err.message);
68+
process.exitCode = 1;
69+
});
70+
process.on('unhandledRejection', err => {
71+
console.error(err.message);
72+
process.exitCode = 1;
73+
});

0 commit comments

Comments
 (0)