Skip to content

Commit

Permalink
Add sample for game server rollout (#102)
Browse files Browse the repository at this point in the history
Co-authored-by: Justin Beckwith <justin.beckwith@gmail.com>
  • Loading branch information
pooneh-m and JustinBeckwith authored Jul 24, 2020
1 parent 71ce195 commit c42eea2
Show file tree
Hide file tree
Showing 6 changed files with 575 additions and 0 deletions.
70 changes: 70 additions & 0 deletions cloud-game-servers/snippets/get_rollout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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 Game Servers Rollout.
* @param {string} projectId string project identifier
* @param {string} deploymentId unique identifier for the new Game Server Deployment
*/
async function main(
projectId = 'YOUR_PROJECT_ID',
deploymentId = 'DEPLOYMENT_ID'
) {
// [START cloud_game_servers_rollout_get]
const {
GameServerDeploymentsServiceClient,
} = require('@google-cloud/game-servers');

const client = new GameServerDeploymentsServiceClient();

async function getGameServerDeploymentRollout() {
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'Your Google Cloud Project ID';
// const deploymentId = 'A unique ID for the Game Server Deployment';
const request = {
// The full resource name
name: client.gameServerDeploymentPath(projectId, 'global', deploymentId),
};

const [rollout] = await client.getGameServerDeploymentRollout(request);
console.log(`Rollout name: ${rollout.name}`);
console.log(`Rollout default: ${rollout.defaultGameServerConfig}`);
for (const override of rollout.gameServerConfigOverrides) {
console.log(`Rollout config overrides: ${override.configVersion}`);
console.log(
`Rollout realm overrides: ${JSON.stringify(override.realmsSelector)}`
);
}

const updateTime = rollout.updateTime;
const updateDate = new Date(updateTime.seconds * 1000);
console.log(`Rollout updated on: ${updateDate.toLocaleDateString()}\n`);
}

getGameServerDeploymentRollout();

// [END cloud_game_servers_rollout_get]
}

main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
207 changes: 207 additions & 0 deletions cloud-game-servers/snippets/test/rollout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
// 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 cleanup = require('./clean.js');
const {describe, before, after, it} = require('mocha');
const {
GameServerConfigsServiceClient,
GameServerDeploymentsServiceClient,
RealmsServiceClient,
} = require('@google-cloud/game-servers');

const cp = require('child_process');
const uuid = require('uuid');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

describe('Game Server Rollout Test', () => {
const configClient = new GameServerConfigsServiceClient();
const deploymentClient = new GameServerDeploymentsServiceClient();
const realmClient = new RealmsServiceClient();
let projectId;
const deploymentId = `test-nodejs-${uuid.v4()}`;
const configId = `test-nodejs-${uuid.v4()}`;
const realmId = `test-nodejs-${uuid.v4()}`;
const realmLocation = 'us-central1';

before(async () => {
await cleanup();

projectId = await deploymentClient.getProjectId();

const request = {
parent: `projects/${projectId}/locations/global`,
deploymentId: deploymentId,
};
const [operation] = await deploymentClient.createGameServerDeployment(
request
);
await operation.promise();

const request2 = {
parent: configClient.gameServerDeploymentPath(
projectId,
'global',
deploymentId
),
configId: configId,
};
const [operation2] = await configClient.createGameServerConfig(request2);
await operation2.promise();

const request3 = {
parent: `projects/${projectId}/locations/${realmLocation}`,
realmId: realmId,
realm: {
timeZone: 'US/Pacific',
description: 'Test Game Server realm',
},
};

const [operation3] = await realmClient.createRealm(request3);
await operation3.promise();
});

it('should update rollout default', async () => {
const updateDefaultOutput = execSync(
`node update_rollout_default.js ${projectId} ${deploymentId} ${configId}`
);

assert.match(updateDefaultOutput, /Deployment updated:/);

const getRolloutOutput = execSync(
`node get_rollout.js ${projectId} ${deploymentId} ${configId}`
);

const configName = configClient.gameServerConfigPath(
projectId,
'global',
deploymentId,
configId
);
assert.match(getRolloutOutput, /Rollout name:/);
assert.match(
getRolloutOutput,
new RegExp(`Rollout default: ${configName}`)
);
});

it('should remove rollout default', async () => {
const removeDefaultOutput = execSync(
`node update_rollout_remove_default.js ${projectId} ${deploymentId} ${configId}`
);

assert.match(removeDefaultOutput, /Deployment updated:/);

const request = {
// The full resource name
name: deploymentClient.gameServerDeploymentPath(
projectId,
'global',
deploymentId
),
};

const [rollout] = await deploymentClient.getGameServerDeploymentRollout(
request
);
assert.strictEqual(rollout.defaultGameServerConfig, '');
});

it('should update rollout override', async () => {
const updateOverrideOutput = execSync(
`node update_rollout_override.js ${projectId} ${deploymentId} ${configId} ${realmId} ${realmLocation}`
);
assert.match(updateOverrideOutput, /Deployment updated:/);

const getRolloutOutput = execSync(
`node get_rollout.js ${projectId} ${deploymentId} ${configId}`
);

const configName = configClient.gameServerConfigPath(
projectId,
'global',
deploymentId,
configId
);
const realmName = realmClient.realmPath(projectId, realmLocation, realmId);
assert.match(
getRolloutOutput,
new RegExp(`Rollout config overrides: .*${configName}`)
);
assert.match(getRolloutOutput, new RegExp(realmName));
});

it('should remove rollout override', async () => {
const removeOverrideOutput = execSync(
`node update_rollout_remove_override.js ${projectId} ${deploymentId}`
);

assert.match(removeOverrideOutput, /Deployment updated:/);

const request = {
// The full resource name
name: deploymentClient.gameServerDeploymentPath(
projectId,
'global',
deploymentId
),
};

const [rollout] = await deploymentClient.getGameServerDeploymentRollout(
request
);
assert.strictEqual(rollout.gameServerConfigOverrides.length, 0);
});

after(async () => {
// Delete the Game Server Config
const request = {
// Provide full resource name of a Game Server Config
name: configClient.gameServerConfigPath(
projectId,
'global',
deploymentId,
configId
),
};
const [operation] = await configClient.deleteGameServerConfig(request);
await operation.promise();

// Delete the Game Server Deployment
const request2 = {
// Provide full resource name of a Game Server Deployment
name: deploymentClient.gameServerDeploymentPath(
projectId,
'global',
deploymentId
),
};
const [operation2] = await deploymentClient.deleteGameServerDeployment(
request2
);
await operation2.promise();

// Delete the Realm
const request3 = {
// Provide full resource name of a Realm
name: realmClient.realmPath(projectId, realmLocation, realmId),
};
const [operation3] = await realmClient.deleteRealm(request3);
await operation3.promise();
});
});
73 changes: 73 additions & 0 deletions cloud-game-servers/snippets/update_rollout_default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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';

/**
* Rollout update to add default config.
* @param {string} projectId string project identifier
* @param {string} deploymentId unique identifier for the new Game Server Deployment
* @param {string} configId unique identifier for the new Game Server Config
*/
async function main(
projectId = 'YOUR_PROJECT_ID',
deploymentId = 'DEPLOYMENT_ID',
configId = 'CONFIG_ID'
) {
// [START cloud_game_servers_deployment_rollout_default]
const {
GameServerDeploymentsServiceClient,
} = require('@google-cloud/game-servers');

const client = new GameServerDeploymentsServiceClient();

async function rolloutDefaultGameServerDeployment() {
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'Your Google Cloud Project ID';
// const deploymentId = 'A unique ID for the Game Server Deployment';
// const configId = 'A unique ID for the Game Server Config';
const request = {
rollout: {
name: client.gameServerDeploymentPath(
projectId,
'global',
deploymentId
),
defaultGameServerConfig: configId,
},
updateMask: {
paths: ['default_game_server_config'],
},
};

const [operation] = await client.updateGameServerDeploymentRollout(request);
const [deployment] = await operation.promise();

console.log(`Deployment updated: ${deployment.name}`);
}

rolloutDefaultGameServerDeployment();

// [END cloud_game_servers_deployment_rollout_default]
}

main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
Loading

0 comments on commit c42eea2

Please sign in to comment.