From d679dc7d8688590739c047232caee80320b09a20 Mon Sep 17 00:00:00 2001 From: Joanna Grycz Date: Fri, 6 Sep 2024 14:19:59 +0200 Subject: [PATCH] feat: add compute_regional_template_create/get/delete --- .../createRegionalTemplate.js | 109 ++++++++++++++++++ .../deleteRegionalTemplate.js | 68 +++++++++++ .../getRegionalTemplate.js | 57 +++++++++ compute/test/regionalTemplate.test.js | 103 +++++++++++++++++ 4 files changed, 337 insertions(+) create mode 100644 compute/create-instance-templates/createRegionalTemplate.js create mode 100644 compute/create-instance-templates/deleteRegionalTemplate.js create mode 100644 compute/create-instance-templates/getRegionalTemplate.js create mode 100644 compute/test/regionalTemplate.test.js diff --git a/compute/create-instance-templates/createRegionalTemplate.js b/compute/create-instance-templates/createRegionalTemplate.js new file mode 100644 index 00000000000..b3cc5b60411 --- /dev/null +++ b/compute/create-instance-templates/createRegionalTemplate.js @@ -0,0 +1,109 @@ +/* + * Copyright 2024 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'; + +async function main() { + // [START compute_regional_template_create] + // Import the Compute library + const computeLib = require('@google-cloud/compute'); + const compute = computeLib.protos.google.cloud.compute.v1; + + // Instantiate a regionInstanceTemplatesClient + const regionInstanceTemplatesClient = + new computeLib.RegionInstanceTemplatesClient(); + // Instantiate a regionOperationsClient + const regionOperationsClient = new computeLib.RegionOperationsClient(); + + /** + * TODO(developer): Update these variables before running the sample. + */ + // The ID of the project that you want to use. + const projectId = await regionInstanceTemplatesClient.getProjectId(); + // The region in which to create a template. + const region = 'us-central1'; + // The name of the new template to create. + const templateName = 'regional-template-name'; + + // Create a new instance template with the provided name and a specific instance configuration. + async function createRegionalTemplate() { + // Define the boot disk for the instance template + const disk = new compute.AttachedDisk({ + initializeParams: new compute.AttachedDiskInitializeParams({ + sourceImage: + 'projects/debian-cloud/global/images/debian-12-bookworm-v20240815', + diskSizeGb: '100', + diskType: 'pd-balanced', + }), + autoDelete: true, + boot: true, + type: 'PERSISTENT', + }); + + // Define the network interface for the instance template + const network = new compute.NetworkInterface({ + network: `projects/${projectId}/global/networks/default`, + }); + + // Define instance template + const instanceTemplate = new compute.InstanceTemplate({ + name: templateName, + properties: { + disks: [disk], + region, + machineType: 'e2-medium', + // The template connects the instance to the `default` network, + // without specifying a subnetwork. + networkInterfaces: [network], + }, + }); + + const [response] = await regionInstanceTemplatesClient.insert({ + project: projectId, + region, + instanceTemplateResource: instanceTemplate, + }); + + let operation = response.latestResponse; + + // Wait for the create operation to complete. + while (operation.status !== 'DONE') { + [operation] = await regionOperationsClient.wait({ + operation: operation.name, + project: projectId, + region, + }); + } + + const createdTemplate = ( + await regionInstanceTemplatesClient.get({ + project: projectId, + region, + instanceTemplate: templateName, + }) + )[0]; + + console.log(JSON.stringify(createdTemplate)); + } + + createRegionalTemplate(); + // [END compute_regional_template_create] +} + +main().catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/compute/create-instance-templates/deleteRegionalTemplate.js b/compute/create-instance-templates/deleteRegionalTemplate.js new file mode 100644 index 00000000000..163720d2c4b --- /dev/null +++ b/compute/create-instance-templates/deleteRegionalTemplate.js @@ -0,0 +1,68 @@ +/* + * Copyright 2024 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'; + +async function main() { + // [START compute_regional_template_delete] + // Import the Compute library + const computeLib = require('@google-cloud/compute'); + + // Instantiate a regionInstanceTemplatesClient + const regionInstanceTemplatesClient = + new computeLib.RegionInstanceTemplatesClient(); + // Instantiate a regionOperationsClient + const regionOperationsClient = new computeLib.RegionOperationsClient(); + + /** + * TODO(developer): Update these variables before running the sample. + */ + // The ID of the project that you want to use. + const projectId = await regionInstanceTemplatesClient.getProjectId(); + // The region where template is created. + const region = 'us-central1'; + // The name of the template to delete. + const templateName = 'regional-template-name'; + + async function deleteRegionalTemplate() { + const [response] = await regionInstanceTemplatesClient.delete({ + project: projectId, + instanceTemplate: templateName, + region, + }); + + let operation = response.latestResponse; + + // Wait for the delete operation to complete. + while (operation.status !== 'DONE') { + [operation] = await regionOperationsClient.wait({ + operation: operation.name, + project: projectId, + region, + }); + } + + console.log(`Template: ${templateName} deleted.`); + } + + deleteRegionalTemplate(); + // [END compute_regional_template_delete] +} + +main().catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/compute/create-instance-templates/getRegionalTemplate.js b/compute/create-instance-templates/getRegionalTemplate.js new file mode 100644 index 00000000000..e4c743af152 --- /dev/null +++ b/compute/create-instance-templates/getRegionalTemplate.js @@ -0,0 +1,57 @@ +/* + * Copyright 2024 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'; + +async function main() { + // [START compute_regional_template_get] + // Import the Compute library + const computeLib = require('@google-cloud/compute'); + + // Instantiate a regionInstanceTemplatesClient + const regionInstanceTemplatesClient = + new computeLib.RegionInstanceTemplatesClient(); + + /** + * TODO(developer): Update these variables before running the sample. + */ + // The ID of the project that you want to use. + const projectId = await regionInstanceTemplatesClient.getProjectId(); + // The region where template is created. + const region = 'us-central1'; + // The name of the template to return. + const templateName = 'regional-template-name'; + + async function getRegionalTemplate() { + const template = ( + await regionInstanceTemplatesClient.get({ + project: projectId, + region, + instanceTemplate: templateName, + }) + )[0]; + + console.log(JSON.stringify(template)); + } + + getRegionalTemplate(); + // [END compute_regional_template_get] +} + +main().catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/compute/test/regionalTemplate.test.js b/compute/test/regionalTemplate.test.js new file mode 100644 index 00000000000..05b3dd92af5 --- /dev/null +++ b/compute/test/regionalTemplate.test.js @@ -0,0 +1,103 @@ +/* + * Copyright 2024 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 path = require('path'); +const {assert, expect} = require('chai'); +const {before, describe, it} = require('mocha'); +const cp = require('child_process'); +const {RegionInstanceTemplatesClient} = require('@google-cloud/compute').v1; + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +describe('Regional instance template', async () => { + const templateName = 'regional-template-name'; + const region = 'us-central1'; + const regionInstanceTemplatesClient = new RegionInstanceTemplatesClient(); + let projectId; + let template; + + before(async () => { + projectId = await regionInstanceTemplatesClient.getProjectId(); + }); + + it('should create a new template', () => { + const networkInterface = { + name: 'nic0', + network: `https://www.googleapis.com/compute/v1/projects/${projectId}/global/networks/default`, + }; + const disk = { + sourceImage: + 'projects/debian-cloud/global/images/debian-12-bookworm-v20240815', + diskSizeGb: '100', + diskType: 'pd-balanced', + }; + + template = JSON.parse( + execSync('node ./create-instance-templates/createRegionalTemplate.js', { + cwd, + }) + ); + + assert.equal(template.name, templateName); + assert.equal( + template.region, + `https://www.googleapis.com/compute/v1/projects/${projectId}/regions/${region}` + ); + expect(template.properties.networkInterfaces[0]).includes(networkInterface); + expect(template.properties.disks[0].initializeParams).includes(disk); + }); + + it('should return template', () => { + const response = JSON.parse( + execSync('node ./create-instance-templates/getRegionalTemplate.js', { + cwd, + }) + ); + + assert.deepEqual(response, template); + }); + + it('should delete template', async () => { + const response = execSync( + 'node ./create-instance-templates/deleteRegionalTemplate.js', + { + cwd, + } + ); + + expect(response).includes(`Template: ${templateName} deleted.`); + + try { + // Try to get the deleted template + await regionInstanceTemplatesClient.get({ + project: projectId, + region, + instanceTemplate: templateName, + }); + + // If the template is found, the test should fail + throw new Error('Template was not deleted.'); + } catch (error) { + // Assert that the error message indicates the template wasn't found + expect(error.message).to.include( + `The resource 'projects/${projectId}/regions/${region}/instanceTemplates/${templateName}' was not found` + ); + } + }); +});