|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * Creates a Compute Engine VM instance from an instance template, but overrides the disk and machine type options in the template. |
| 17 | + * |
| 18 | + * @param {string} projectId - ID or number of the project you want to use. |
| 19 | + * @param {string} zone - Name of the zone you want to check, for example: us-west3-b |
| 20 | + * @param {string} instanceName - Name of the new instance. |
| 21 | + * @param {string} instanceTemplateName - Name of the instance template to use when creating the new instance. |
| 22 | + * @param {string} machineType - Machine type you want to set in following format: |
| 23 | + * "zones/{zone}/machineTypes/{type_name}". For example: |
| 24 | + * "zones/europe-west3-c/machineTypes/f1-micro" |
| 25 | + * You can find the list of available machine types using: |
| 26 | + * https://cloud.google.com/sdk/gcloud/reference/compute/machine-types/list |
| 27 | + * @param {string} newDiskSourceImage - Path the the disk image you want to use for your new |
| 28 | + * disk. This can be one of the public images |
| 29 | + * (like "projects/debian-cloud/global/images/family/debian-10") |
| 30 | + * or a private image you have access to. |
| 31 | + * For a list of available public images, see the documentation: |
| 32 | + * http://cloud.google.com/compute/docs/images |
| 33 | + */ |
| 34 | +function main( |
| 35 | + projectId, |
| 36 | + zone, |
| 37 | + instanceName, |
| 38 | + instanceTemplateName, |
| 39 | + machineType = 'n1-standard-2', |
| 40 | + newDiskSourceImage = 'projects/debian-cloud/global/images/family/debian-10' |
| 41 | +) { |
| 42 | + // [START compute_instances_create_from_template_with_overrides] |
| 43 | + /** |
| 44 | + * TODO(developer): Uncomment and replace these variables before running the sample. |
| 45 | + */ |
| 46 | + // const projectId = 'YOUR_PROJECT_ID'; |
| 47 | + // const zone = 'europe-central2-b'; |
| 48 | + // const instanceName = 'YOUR_INSTANCE_NAME'; |
| 49 | + // const instanceTemplateName = 'YOUR_INSTANCE_TEMPLATE_NAME'; |
| 50 | + // const machineType = 'n1-standard-1'; |
| 51 | + // const newDiskSourceImage = 'projects/debian-cloud/global/images/family/debian-10'; |
| 52 | + |
| 53 | + const compute = require('@google-cloud/compute'); |
| 54 | + const computeProtos = compute.protos.google.cloud.compute.v1; |
| 55 | + |
| 56 | + // Creates a new instance in the specified project and zone using a selected template, but overrides the disk and machine type options in the template. |
| 57 | + async function createInstanceFromTemplateWithOverrides() { |
| 58 | + const instancesClient = new compute.InstancesClient(); |
| 59 | + const instanceTemplatesClient = new compute.InstanceTemplatesClient(); |
| 60 | + |
| 61 | + console.log( |
| 62 | + `Creating the ${instanceName} instance in ${zone} from template ${instanceTemplateName}...` |
| 63 | + ); |
| 64 | + |
| 65 | + // Retrieve an instance template by name. |
| 66 | + const [instanceTemplate] = await instanceTemplatesClient.get({ |
| 67 | + project: projectId, |
| 68 | + instanceTemplate: instanceTemplateName, |
| 69 | + }); |
| 70 | + |
| 71 | + // Adjust diskType field of the instance template to use the URL formatting required by instances.insert.diskType |
| 72 | + // For instance template, there is only a name, not URL. |
| 73 | + for (const disk of instanceTemplate.properties.disks) { |
| 74 | + if (disk.initializeParams.diskType) { |
| 75 | + disk.initializeParams.diskType = `zones/${zone}/diskTypes/${disk.initializeParams.diskType}`; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + const [response] = await instancesClient.insert({ |
| 80 | + project: projectId, |
| 81 | + zone, |
| 82 | + instanceResource: { |
| 83 | + name: instanceName, |
| 84 | + machineType: `zones/${zone}/machineTypes/${machineType}`, |
| 85 | + disks: [ |
| 86 | + // If you override a repeated field, all repeated values |
| 87 | + // for that property are replaced with the |
| 88 | + // corresponding values provided in the request. |
| 89 | + // When adding a new disk to existing disks, |
| 90 | + // insert all existing disks as well. |
| 91 | + ...instanceTemplate.properties.disks, |
| 92 | + { |
| 93 | + initializeParams: { |
| 94 | + diskSizeGb: '10', |
| 95 | + sourceImage: newDiskSourceImage, |
| 96 | + }, |
| 97 | + autoDelete: true, |
| 98 | + boot: false, |
| 99 | + type: computeProtos.AttachedDisk.Type.PERSISTENT, |
| 100 | + }, |
| 101 | + ], |
| 102 | + }, |
| 103 | + sourceInstanceTemplate: instanceTemplate.selfLink, |
| 104 | + }); |
| 105 | + let operation = response.latestResponse; |
| 106 | + const operationsClient = new compute.ZoneOperationsClient(); |
| 107 | + |
| 108 | + // Wait for the create operation to complete. |
| 109 | + while (operation.status !== 'DONE') { |
| 110 | + [operation] = await operationsClient.wait({ |
| 111 | + operation: operation.name, |
| 112 | + project: projectId, |
| 113 | + zone: operation.zone.split('/').pop(), |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + console.log('Instance created.'); |
| 118 | + } |
| 119 | + |
| 120 | + createInstanceFromTemplateWithOverrides(); |
| 121 | + // [END compute_instances_create_from_template_with_overrides] |
| 122 | +} |
| 123 | + |
| 124 | +main(...process.argv.slice(2)); |
0 commit comments