async function createWithExistingDisks()

in compute/instances/create-start-instance/createInstanceWithExistingDisks.js [36:91]


  async function createWithExistingDisks() {
    const instancesClient = new compute.InstancesClient();
    const disksClient = new compute.DisksClient();

    if (diskNames.length < 1) {
      throw new Error('At least one disk should be provided');
    }

    const disks = [];
    for (const diskName of diskNames) {
      const [disk] = await disksClient.get({
        project: projectId,
        zone,
        disk: diskName,
      });
      disks.push(disk);
    }

    const attachedDisks = [];

    for (const disk of disks) {
      attachedDisks.push({
        source: disk.selfLink,
      });
    }

    attachedDisks[0].boot = true;

    const [response] = await instancesClient.insert({
      project: projectId,
      zone,
      instanceResource: {
        name: instanceName,
        disks: attachedDisks,
        machineType: `zones/${zone}/machineTypes/n1-standard-1`,
        networkInterfaces: [
          {
            name: 'global/networks/default',
          },
        ],
      },
    });
    let operation = response.latestResponse;
    const operationsClient = new compute.ZoneOperationsClient();

    // Wait for the create operation to complete.
    while (operation.status !== 'DONE') {
      [operation] = await operationsClient.wait({
        operation: operation.name,
        project: projectId,
        zone: operation.zone.split('/').pop(),
      });
    }

    console.log('Instance created.');
  }