async function createWindowsOSImage()

in compute/instances/windows/creatingWindowsOSImage.js [59:118]


  async function createWindowsOSImage() {
    const imagesClient = new compute.ImagesClient();
    const instancesClient = new compute.InstancesClient();
    const disksClient = new compute.DisksClient();

    // Getting instances where source disk is attached
    const [sourceDisk] = await disksClient.get({
      project: projectId,
      zone,
      disk: sourceDiskName,
    });

    // Сhecking whether the instances is stopped
    for (const fullInstanceName of sourceDisk.users) {
      const [instanceName, instanceZone, instanceProjectId] =
        parseInstanceName(fullInstanceName);
      const [instance] = await instancesClient.get({
        project: instanceProjectId,
        zone: instanceZone,
        instance: instanceName,
      });

      if (
        !['TERMINATED', 'STOPPED'].includes(instance.status) &&
        !forceCreate
      ) {
        throw new Error(
          `Instance ${instanceName} should be stopped. Please stop the instance using GCESysprep command or set forceCreate parameter to true (not recommended). More information here: https://cloud.google.com/compute/docs/instances/windows/creating-windows-os-image#api.`
        );
      }
    }

    if (forceCreate) {
      console.warn(
        'Warning: forceCreate option compromise the integrity of your image. Stop the instance before you create the image if possible.'
      );
    }

    const [response] = await imagesClient.insert({
      project: projectId,
      forceCreate,
      imageResource: {
        name: imageName,
        sourceDisk: `/zones/${zone}/disks/${sourceDiskName}`,
        storageLocations: storageLocation ? [storageLocation] : [],
      },
    });
    let operation = response.latestResponse;
    const operationsClient = new compute.GlobalOperationsClient();

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

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