async function createSnapshot()

in compute/snapshots/createSnapshot.js [59:119]


  async function createSnapshot() {
    const snapshotsClient = new compute.SnapshotsClient();

    let disk;

    if (!zone && !region) {
      throw new Error(
        'You need to specify `zone` or `region` for this function to work.'
      );
    }

    if (zone && region) {
      throw new Error("You can't set both `zone` and `region` parameters");
    }

    if (!diskProjectId) {
      diskProjectId = projectId;
    }

    if (zone) {
      const disksClient = new compute.DisksClient();
      [disk] = await disksClient.get({
        project: diskProjectId,
        zone,
        disk: diskName,
      });
    } else {
      const regionDisksClient = new compute.RegionDisksClient();
      [disk] = await regionDisksClient.get({
        project: diskProjectId,
        region,
        disk: diskName,
      });
    }

    const snapshotResource = {
      name: snapshotName,
      sourceDisk: disk.selfLink,
    };

    if (location) {
      snapshotResource.storageLocations = [location];
    }

    const [response] = await snapshotsClient.insert({
      project: projectId,
      snapshotResource,
    });
    let operation = response.latestResponse;
    const operationsClient = new compute.GlobalOperationsClient();

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

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