Skip to content

Commit 9ac641e

Browse files
authored
feat: add compute_regional_template_create/get/delete (#3833)
1 parent 9383872 commit 9ac641e

File tree

4 files changed

+314
-0
lines changed

4 files changed

+314
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main(templateName) {
20+
// [START compute_regional_template_create]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
const compute = computeLib.protos.google.cloud.compute.v1;
24+
25+
// Instantiate a regionInstanceTemplatesClient
26+
const regionInstanceTemplatesClient =
27+
new computeLib.RegionInstanceTemplatesClient();
28+
// Instantiate a regionOperationsClient
29+
const regionOperationsClient = new computeLib.RegionOperationsClient();
30+
31+
/**
32+
* TODO(developer): Update/uncomment these variables before running the sample.
33+
*/
34+
// The ID of the project that you want to use.
35+
const projectId = await regionInstanceTemplatesClient.getProjectId();
36+
// The region in which to create a template.
37+
const region = 'us-central1';
38+
// The name of the new template to create.
39+
// const templateName = 'regional-template-name';
40+
41+
// Create a new instance template with the provided name and a specific instance configuration.
42+
async function createRegionalTemplate() {
43+
// Define the boot disk for the instance template
44+
const disk = new compute.AttachedDisk({
45+
initializeParams: new compute.AttachedDiskInitializeParams({
46+
sourceImage:
47+
'projects/debian-cloud/global/images/debian-12-bookworm-v20240815',
48+
diskSizeGb: '100',
49+
diskType: 'pd-balanced',
50+
}),
51+
autoDelete: true,
52+
boot: true,
53+
type: 'PERSISTENT',
54+
});
55+
56+
// Define the network interface for the instance template
57+
const network = new compute.NetworkInterface({
58+
network: `projects/${projectId}/global/networks/default`,
59+
});
60+
61+
// Define instance template
62+
const instanceTemplate = new compute.InstanceTemplate({
63+
name: templateName,
64+
properties: {
65+
disks: [disk],
66+
region,
67+
machineType: 'e2-medium',
68+
// The template connects the instance to the `default` network,
69+
// without specifying a subnetwork.
70+
networkInterfaces: [network],
71+
},
72+
});
73+
74+
const [response] = await regionInstanceTemplatesClient.insert({
75+
project: projectId,
76+
region,
77+
instanceTemplateResource: instanceTemplate,
78+
});
79+
80+
let operation = response.latestResponse;
81+
82+
// Wait for the create operation to complete.
83+
while (operation.status !== 'DONE') {
84+
[operation] = await regionOperationsClient.wait({
85+
operation: operation.name,
86+
project: projectId,
87+
region,
88+
});
89+
}
90+
91+
console.log(`Template: ${templateName} created.`);
92+
}
93+
94+
createRegionalTemplate();
95+
// [END compute_regional_template_create]
96+
}
97+
98+
main(...process.argv.slice(2)).catch(err => {
99+
console.error(err);
100+
process.exitCode = 1;
101+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main(templateName) {
20+
// [START compute_regional_template_delete]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
24+
// Instantiate a regionInstanceTemplatesClient
25+
const regionInstanceTemplatesClient =
26+
new computeLib.RegionInstanceTemplatesClient();
27+
// Instantiate a regionOperationsClient
28+
const regionOperationsClient = new computeLib.RegionOperationsClient();
29+
30+
/**
31+
* TODO(developer): Update/uncomment these variables before running the sample.
32+
*/
33+
// The ID of the project that you want to use.
34+
const projectId = await regionInstanceTemplatesClient.getProjectId();
35+
// The region where template is created.
36+
const region = 'us-central1';
37+
// The name of the template to delete.
38+
// const templateName = 'regional-template-name';
39+
40+
async function deleteRegionalTemplate() {
41+
const [response] = await regionInstanceTemplatesClient.delete({
42+
project: projectId,
43+
instanceTemplate: templateName,
44+
region,
45+
});
46+
47+
let operation = response.latestResponse;
48+
49+
// Wait for the delete operation to complete.
50+
while (operation.status !== 'DONE') {
51+
[operation] = await regionOperationsClient.wait({
52+
operation: operation.name,
53+
project: projectId,
54+
region,
55+
});
56+
}
57+
58+
console.log(`Template: ${templateName} deleted.`);
59+
}
60+
61+
deleteRegionalTemplate();
62+
// [END compute_regional_template_delete]
63+
}
64+
65+
main(...process.argv.slice(2)).catch(err => {
66+
console.error(err);
67+
process.exitCode = 1;
68+
});
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main(templateName) {
20+
// [START compute_regional_template_get]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
24+
// Instantiate a regionInstanceTemplatesClient
25+
const regionInstanceTemplatesClient =
26+
new computeLib.RegionInstanceTemplatesClient();
27+
28+
/**
29+
* TODO(developer): Update/uncomment these variables before running the sample.
30+
*/
31+
// The ID of the project that you want to use.
32+
const projectId = await regionInstanceTemplatesClient.getProjectId();
33+
// The region where template is created.
34+
const region = 'us-central1';
35+
// The name of the template to return.
36+
// const templateName = 'regional-template-name';
37+
38+
async function getRegionalTemplate() {
39+
const template = (
40+
await regionInstanceTemplatesClient.get({
41+
project: projectId,
42+
region,
43+
instanceTemplate: templateName,
44+
})
45+
)[0];
46+
47+
console.log(JSON.stringify(template));
48+
}
49+
50+
getRegionalTemplate();
51+
// [END compute_regional_template_get]
52+
}
53+
54+
main(...process.argv.slice(2)).catch(err => {
55+
console.error(err);
56+
process.exitCode = 1;
57+
});

compute/test/regionalTemplate.test.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the License);
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
const path = require('path');
20+
const assert = require('node:assert/strict');
21+
const {before, describe, it} = require('mocha');
22+
const cp = require('child_process');
23+
const {RegionInstanceTemplatesClient} = require('@google-cloud/compute').v1;
24+
25+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
26+
const cwd = path.join(__dirname, '..');
27+
28+
describe('Regional instance template', async () => {
29+
const templateName = `regional-template-name-745d98${Math.floor(Math.random() * 10 + 1)}f`;
30+
const region = 'us-central1';
31+
const regionInstanceTemplatesClient = new RegionInstanceTemplatesClient();
32+
let projectId;
33+
34+
before(async () => {
35+
projectId = await regionInstanceTemplatesClient.getProjectId();
36+
});
37+
38+
it('should create a new template', () => {
39+
const response = execSync(
40+
`node ./create-instance-templates/createRegionalTemplate.js ${templateName}`,
41+
{
42+
cwd,
43+
}
44+
);
45+
46+
assert(response.includes(`Template: ${templateName} created.`));
47+
});
48+
49+
it('should return template', () => {
50+
const response = JSON.parse(
51+
execSync(
52+
`node ./create-instance-templates/getRegionalTemplate.js ${templateName}`,
53+
{
54+
cwd,
55+
}
56+
)
57+
);
58+
59+
assert(response.name === templateName);
60+
});
61+
62+
it('should delete template', async () => {
63+
const response = execSync(
64+
`node ./create-instance-templates/deleteRegionalTemplate.js ${templateName}`,
65+
{
66+
cwd,
67+
}
68+
);
69+
70+
assert(response.includes(`Template: ${templateName} deleted.`));
71+
72+
try {
73+
// Try to get the deleted template
74+
await regionInstanceTemplatesClient.get({
75+
project: projectId,
76+
region,
77+
instanceTemplate: templateName,
78+
});
79+
80+
// If the template is found, the test should fail
81+
throw new Error('Template was not deleted.');
82+
} catch (error) {
83+
// Assert that the error message indicates the template wasn't found
84+
const expected = `The resource 'projects/${projectId}/regions/${region}/instanceTemplates/${templateName}' was not found`;
85+
assert(error.message && error.message.includes(expected));
86+
}
87+
});
88+
});

0 commit comments

Comments
 (0)