Skip to content

Commit

Permalink
feat: adds CRUD samples, tests for GS clusters (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
telpirion authored Apr 29, 2020
1 parent d728547 commit 730d396
Show file tree
Hide file tree
Showing 9 changed files with 689 additions and 1 deletion.
79 changes: 79 additions & 0 deletions cloud-game-servers/snippets/create_cluster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 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 cluster.
* @param {string} projectId string project identifier
* @param {string} location Compute Engine region
* @param {string} realmId the realm to use
* @param {string} gameClusterId unique identifier for the new Game Cluster
* @param {string} gkeClusterId the GKE cluster to connect to
* @param {string} gkeLocation the location of the GKE cluster
*/
function main(
projectId = 'YOUR_PROJECT_ID',
location = 'LOCATION_ID',
realmId = 'REALM_ID',
gameClusterId = 'GAME_CLUSTER_ID',
gkeClusterName = 'GKE_CLUSTER_NAME'
) {
// [START cloud_game_servers_create_cluster]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'Your Google Cloud Project ID';
// const location = 'A Compute Engine region, e.g. "us-central1"';
// const realmId = 'The ID of the realm to locate this cluster in';
// const gameClusterId = 'A unique ID for this Game Server Cluster';
// const gkeClusterName= 'The full resource name of the GKE cluster to use';
const {
GameServerClustersServiceClient,
} = require('@google-cloud/game-servers');

const client = new GameServerClustersServiceClient();

async function createGameServerCluster() {
const request = {
// Provide full resource name of a Game Server Realm
parent: `projects/${projectId}/locations/${location}/realms/${realmId}`,
gameServerClusterId: gameClusterId,
gameServerCluster: {
description: 'My Game Server Cluster',
connectionInfo: {
gkeClusterReference: {
// Provide full resource name of a Kubernetes Engine cluster
cluster: gkeClusterName,
},
namespace: 'default',
},
},
};

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

console.log('Game Server Cluster created:');
console.log(`\tCluster name: ${result.name}`);
console.log(`\tCluster description: ${result.description}`);
console.log(
`\tGKE cluster: ${result.connectionInfo.gkeClusterReference.cluster}`
);
// [END cloud_game_servers_create_cluster]
}

createGameServerCluster();
}

main(...process.argv.slice(2));
58 changes: 58 additions & 0 deletions cloud-game-servers/snippets/delete_cluster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 Server cluster
* @param {string} projectId string project identifier
* @param {string} location Compute Engine region
* @param {string} realmId the realm to use
* @param {string} gameClusterId the game cluster to delete
*/
function main(
projectId = 'YOUR_PROJECT_ID',
location = 'LOCATION_ID',
realmId = 'REALM_ID',
gameClusterId = 'GAME_CLUSTER_ID'
) {
// [START cloud_game_servers_delete_cluster]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'Your Google Cloud Project ID';
// const location = 'A Compute Engine region, e.g. "us-central1"';
// const realmId = 'The ID of the realm to locate this cluster in';
// const gameClusterId = 'The unique ID for this Game Server Cluster';
const {
GameServerClustersServiceClient,
} = require('@google-cloud/game-servers');

const client = new GameServerClustersServiceClient();

async function deleteGameServerCluster() {
const request = {
// Provide full resource name of a Game Server Realm
name: `projects/${projectId}/locations/${location}/realms/${realmId}/gameServerClusters/${gameClusterId}`,
};

await client.deleteGameServerCluster(request);

console.log('Game Server cluster deleted');
// [END cloud_game_servers_delete_cluster]
}

deleteGameServerCluster();
}

main(...process.argv.slice(2));
63 changes: 63 additions & 0 deletions cloud-game-servers/snippets/get_cluster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 Server cluster
* @param {string} projectId string project identifier
* @param {string} location Compute Engine region
* @param {string} realmId the realm to use
* @param {string} gameClusterId the game cluster to get
*/
function main(
projectId = 'YOUR_PROJECT_ID',
location = 'LOCATION_ID',
realmId = 'REALM_ID',
gameClusterId = 'GAME_CLUSTER_ID'
) {
// [START cloud_game_servers_get_cluster]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'Your Google Cloud Project ID';
// const location = 'A Compute Engine region, e.g. "us-central1"';
// const realmId = 'The ID of the realm to locate this cluster in';
// const gameClusterId = 'The unique ID for this Game Server Cluster';
const {
GameServerClustersServiceClient,
} = require('@google-cloud/game-servers');

const client = new GameServerClustersServiceClient();

async function getGameServerCluster() {
const request = {
// Provide full resource name of a Game Server Realm
name: `projects/${projectId}/locations/${location}/realms/${realmId}/gameServerClusters/${gameClusterId}`,
};

const [cluster] = await client.getGameServerCluster(request);

console.log('Game Server Cluster:');
console.log(`\tCluster name: ${cluster.name}`);
console.log(`\tCluster description: ${cluster.description}`);
console.log(
`\tGKE cluster: ${cluster.connectionInfo.gkeClusterReference.cluster}`
);
// [END cloud_game_servers_get_cluster]
}

getGameServerCluster();
}

main(...process.argv.slice(2));
63 changes: 63 additions & 0 deletions cloud-game-servers/snippets/list_clusters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 Game Server clusters
* @param {string} projectId string project identifier
* @param {string} location Compute Engine region
* @param {string} realmId the realm to use
*/
function main(
projectId = 'YOUR_PROJECT_ID',
location = 'LOCATION_ID',
realmId = 'REALM_ID'
) {
// [START cloud_game_servers_list_clusters]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'Your Google Cloud Project ID';
// const location = 'A Compute Engine region, e.g. "us-central1"';
// const realmId = 'The ID of the realm to locate this cluster in';
const {
GameServerClustersServiceClient,
} = require('@google-cloud/game-servers');

const client = new GameServerClustersServiceClient();

async function listGameServerClusters() {
const request = {
// Provide full resource name of a Game Server Realm
parent: `projects/${projectId}/locations/${location}/realms/${realmId}`,
};

const results = await client.listGameServerClusters(request);
const [clusters] = results;

for (const cluster of clusters) {
console.log('Game Server Cluster:');
console.log(`\tCluster name: ${cluster.name}`);
console.log(`\tCluster description: ${cluster.description}`);
console.log(
`\tGKE cluster: ${cluster.connectionInfo.gkeClusterReference.cluster}`
);
}
// [END cloud_game_servers_list_clusters]
}

listGameServerClusters();
}

main(...process.argv.slice(2));
2 changes: 1 addition & 1 deletion cloud-game-servers/snippets/test/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = async () => {
clustersRequest
);
const [clusters] = clustersResult;
for (const cluster of clusters.gameServerClusters) {
for (const cluster of clusters) {
const deleteClusterRequest = {
name: cluster.name,
};
Expand Down
91 changes: 91 additions & 0 deletions cloud-game-servers/snippets/test/create_cluster.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// 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, it, before, after} = require('mocha');
const {
RealmsServiceClient,
GameServerClustersServiceClient,
} = require('@google-cloud/game-servers');

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

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

const LOCATION = 'us-central1';
const GKE_CLUSTER_NAME =
process.env.SAMPLE_CLUSTER_NAME ||
'projects/1046198160504/locations/us-west1-a/clusters/grpc-bug-cluster';

describe('Game Servers Create Cluster Test', () => {
const realmsClient = new RealmsServiceClient();
const gameClustersClient = new GameServerClustersServiceClient();
let realmId, gameClusterId;

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

// Create a realm
const projectId = await realmsClient.getProjectId();
realmId = `create-realm-${uuid.v4()}`;
gameClusterId = `test-${uuid.v4()}`;

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

const [operation] = await realmsClient.createRealm(request);
await operation.promise();
});

it('should create a Game Server cluster', async () => {
const projectId = await realmsClient.getProjectId();
gameClusterId = `test-${uuid.v4()}`;

const create_output = execSync(
`node create_cluster.js ${projectId} ${LOCATION} ${realmId} ${gameClusterId} ${GKE_CLUSTER_NAME}`
);
assert.match(create_output, /Cluster name:/);
});

after(async () => {
const projectId = await realmsClient.getProjectId();

// Delete the Game Server cluster
const deleteClusterRequest = {
// Provide full resource name of a Game Server Realm
name: `projects/${projectId}/locations/${LOCATION}/realms/${realmId}/gameServerClusters/${gameClusterId}`,
};
const [operation1] = await gameClustersClient.deleteGameServerCluster(
deleteClusterRequest
);
await operation1.promise();

// Delete the realm
const deleteRealmRequest = {
name: `projects/${projectId}/locations/${LOCATION}/realms/${realmId}`,
};
const [operation2] = await realmsClient.deleteRealm(deleteRealmRequest);
await operation2.promise();
});
});
Loading

0 comments on commit 730d396

Please sign in to comment.