|
| 1 | +// Copyright 2022 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 | + * Create a new VM instance using selected disks. The first disk in diskNames will be used as boot disk. |
| 17 | + * |
| 18 | + * @param {string} projectId - Project ID or project number of the Cloud project you want to use. |
| 19 | + * @param {string} zone - Name of the zone to create the instance in. For example: "us-west3-b" |
| 20 | + * @param {string} instanceName - Name of the new virtual machine (VM) instance. |
| 21 | + * @param {Array<string>} diskNames - Array of disk names to be attached to the new virtual machine. |
| 22 | + * First disk in this list will be used as the boot device. |
| 23 | + */ |
| 24 | +function main(projectId, zone, instanceName, diskNames) { |
| 25 | + // [START compute_instances_create_with_existing_disks] |
| 26 | + /** |
| 27 | + * TODO(developer): Uncomment and replace these variables before running the sample. |
| 28 | + */ |
| 29 | + // const projectId = 'YOUR_PROJECT_ID'; |
| 30 | + // const zone = 'europe-central2-b'; |
| 31 | + // const instanceName = 'YOUR_INSTANCE_NAME'; |
| 32 | + // const diskNames = ['boot_disk', 'disk1', 'disk2']; |
| 33 | + |
| 34 | + const compute = require('@google-cloud/compute'); |
| 35 | + |
| 36 | + async function createWithExistingDisks() { |
| 37 | + const instancesClient = new compute.InstancesClient(); |
| 38 | + const disksClient = new compute.DisksClient(); |
| 39 | + |
| 40 | + if (diskNames.length < 1) { |
| 41 | + throw new Error('At least one disk should be provided'); |
| 42 | + } |
| 43 | + |
| 44 | + const disks = []; |
| 45 | + for (const diskName of diskNames) { |
| 46 | + const [disk] = await disksClient.get({ |
| 47 | + project: projectId, |
| 48 | + zone, |
| 49 | + disk: diskName, |
| 50 | + }); |
| 51 | + disks.push(disk); |
| 52 | + } |
| 53 | + |
| 54 | + const attachedDisks = []; |
| 55 | + |
| 56 | + for (const disk of disks) { |
| 57 | + attachedDisks.push({ |
| 58 | + source: disk.selfLink, |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + attachedDisks[0].boot = true; |
| 63 | + |
| 64 | + const [response] = await instancesClient.insert({ |
| 65 | + project: projectId, |
| 66 | + zone, |
| 67 | + instanceResource: { |
| 68 | + name: instanceName, |
| 69 | + disks: attachedDisks, |
| 70 | + machineType: `zones/${zone}/machineTypes/n1-standard-1`, |
| 71 | + networkInterfaces: [ |
| 72 | + { |
| 73 | + name: 'global/networks/default', |
| 74 | + }, |
| 75 | + ], |
| 76 | + }, |
| 77 | + }); |
| 78 | + let operation = response.latestResponse; |
| 79 | + const operationsClient = new compute.ZoneOperationsClient(); |
| 80 | + |
| 81 | + // Wait for the create operation to complete. |
| 82 | + while (operation.status !== 'DONE') { |
| 83 | + [operation] = await operationsClient.wait({ |
| 84 | + operation: operation.name, |
| 85 | + project: projectId, |
| 86 | + zone: operation.zone.split('/').pop(), |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + console.log('Instance created.'); |
| 91 | + } |
| 92 | + |
| 93 | + createWithExistingDisks(); |
| 94 | + // [END compute_instances_create_with_existing_disks] |
| 95 | +} |
| 96 | + |
| 97 | +process.on('unhandledRejection', err => { |
| 98 | + console.error(err.message); |
| 99 | + process.exitCode = 1; |
| 100 | +}); |
| 101 | + |
| 102 | +const args = process.argv.slice(2); |
| 103 | +args[3] = args[3].split(','); |
| 104 | +main(...args); |
0 commit comments