Skip to content

Commit

Permalink
Add samples for GameServerConfigs (#98)
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 d32da4d commit 71ce195
Show file tree
Hide file tree
Showing 8 changed files with 457 additions and 20 deletions.
144 changes: 144 additions & 0 deletions cloud-game-servers/snippets/create_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// 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 Config.
* @param {string} projectId string project identifier
* @param {string} deploymentId unique identifier for the parent Game Server Deployment
* @param {string} configId unique identifier for the new Game Server Config
* @param {string} fleetName fleet name to be stored in Agones
*/
async function main(
projectId = 'YOUR_PROJECT_ID',
deploymentId = 'DEPLOYMENT_ID',
configId = 'CONFIG_ID',
fleetName = 'FLEET_NAME'
) {
// [START cloud_game_servers_config_create]
const {
GameServerConfigsServiceClient,
} = require('@google-cloud/game-servers');

const client = new GameServerConfigsServiceClient();

async function createGameServerConfig() {
/**
* 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 fleetName = 'The fleet name to be stored in Agones';
// fleet is the spec portion of an agones Fleet. It must be in JSON format.
// See https://agones.dev/site/docs/reference/fleet/ for more on fleets.
const fleet = `
{
"replicas": 10,
"scheduling": "Packed",
"strategy": {
"type": "RollingUpdate",
"rollingUpdate": {
"maxSurge": "25%",
"maxUnavailable": "25%"
}
},
"template": {
"metadata": {
"labels": {
"gameName": "udp-server"
}
},
"spec": {
"ports": [
{
"name": "default",
"portPolicy": "Dynamic",
"containerPort": 2156,
"protocol": "TCP"
}
],
"health": {
"initialDelaySeconds": 30,
"periodSeconds": 60
},
"sdkServer": {
"logLevel": "Info",
"grpcPort": 9357,
"httpPort": 9358
},
"template": {
"spec": {
"containers": [
{
"name": "dedicated",
"image": "gcr.io/agones-images/udp-server:0.17",
"imagePullPolicy": "Always",
"resources": {
"requests": {
"memory": "200Mi",
"cpu": "500m"
},
"limits": {
"memory": "200Mi",
"cpu": "500m"
}
}
}
]
}
}
}
}
}
`;
const request = {
parent: client.gameServerDeploymentPath(
projectId,
'global',
deploymentId
),
configId: configId,
gameServerConfig: {
fleetConfigs: [
{
name: fleetName,
fleetSpec: fleet,
},
],
description: 'nodejs test config',
},
};

const [operation] = await client.createGameServerConfig(request);
const [result] = await operation.promise();

console.log('Game Server Config created:');
console.log(`\t Config name: ${result.name}`);
console.log(`\t Config description: ${result.description}`);
}

createGameServerConfig();

// [END cloud_game_servers_config_create]
}

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;
});
3 changes: 2 additions & 1 deletion cloud-game-servers/snippets/delete_cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ async function main(
),
};

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

console.log('Game Server cluster deleted');
}
Expand Down
68 changes: 68 additions & 0 deletions cloud-game-servers/snippets/delete_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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 Game Servers 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_config_delete]
const {
GameServerConfigsServiceClient,
} = require('@google-cloud/game-servers');

const client = new GameServerConfigsServiceClient();

async function deleteGameServerConfig() {
/**
* 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 = {
// The full resource name
name: client.gameServerConfigPath(
projectId,
'global',
deploymentId,
configId
),
};

const [operation] = await client.deleteGameServerConfig(request);
await operation.promise();
console.log(`Config with name ${request.name} deleted.`);
}

deleteGameServerConfig();

// [END cloud_game_servers_config_delete]
}

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;
});
5 changes: 3 additions & 2 deletions cloud-game-servers/snippets/delete_deployment.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ async function main(
name: client.gameServerDeploymentPath(projectId, 'global', deploymentId),
};

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

console.log('deleted.');
console.log(`Deployment with name ${request.name} deleted.`);
}

deleteGameServerDeployment();
Expand Down
72 changes: 72 additions & 0 deletions cloud-game-servers/snippets/get_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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 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_config_get]
const {
GameServerConfigsServiceClient,
} = require('@google-cloud/game-servers');

const client = new GameServerConfigsServiceClient();

async function getGameServerConfig() {
/**
* 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 = {
// The full resource name
name: client.gameServerConfigPath(
projectId,
'global',
deploymentId,
configId
),
};

const [config] = await client.getGameServerConfig(request);
console.log(`Config name: ${config.name}`);
console.log(`Config description: ${config.description}`);

const createTime = config.createTime;
const createDate = new Date(createTime.seconds * 1000);
console.log(`Config created on: ${createDate.toLocaleDateString()}\n`);
}

getGameServerConfig();

// [END cloud_game_servers_config_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;
});
69 changes: 69 additions & 0 deletions cloud-game-servers/snippets/list_configs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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 the Game Servers Configs.
* @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_config_list]
const {
GameServerConfigsServiceClient,
} = require('@google-cloud/game-servers');

const client = new GameServerConfigsServiceClient();

async function listGameServerConfigs() {
/**
* 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 = {
parent: client.gameServerDeploymentPath(
projectId,
'global',
deploymentId
),
};

const [results] = await client.listGameServerConfigs(request);
for (const config of results) {
console.log(`Config name: ${config.name}`);
console.log(`Config description: ${config.description}`);

const createTime = config.createTime;
const createDate = new Date(createTime.seconds * 1000);
console.log(`Config created on: ${createDate.toLocaleDateString()}\n`);
}
}

listGameServerConfigs();

// [END cloud_game_servers_config_list]
}

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 71ce195

Please sign in to comment.