Create a Windows Server image
Stay organized with collections
Save and categorize content based on your preferences.
Create a Windows Server image from the specified source disk.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Go
Before trying this sample, follow the Go setup instructions in the
Compute Engine quickstart using
client libraries.
For more information, see the
Compute Engine Go API
reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
import (
"context"
"fmt"
"io"
"strings"
compute "cloud.google.com/go/compute/apiv1"
computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
"google.golang.org/protobuf/proto"
)
// createWindowsOSImage creates a new Windows image from the specified source disk.
func createWindowsOSImage(
w io.Writer,
projectID, zone, sourceDiskName, imageName, storageLocation string,
forceCreate bool,
) error {
// projectID := "your_project_id"
// zone := "europe-central2-b"
// sourceDiskName := "your_source_disk_name"
// imageName := "your_image_name"
// storageLocation := "eu"
// forceCreate := false
ctx := context.Background()
instancesClient, err := compute.NewInstancesRESTClient(ctx)
if err != nil {
return fmt.Errorf("NewInstancesRESTClient: %w", err)
}
defer instancesClient.Close()
imagesClient, err := compute.NewImagesRESTClient(ctx)
if err != nil {
return fmt.Errorf("NewImagesRESTClient: %w", err)
}
defer imagesClient.Close()
disksClient, err := compute.NewDisksRESTClient(ctx)
if err != nil {
return fmt.Errorf("NewDisksRESTClient: %w", err)
}
defer disksClient.Close()
// Getting instances where source disk is attached
diskRequest := &computepb.GetDiskRequest{
Project: projectID,
Zone: zone,
Disk: sourceDiskName,
}
sourceDisk, err := disksClient.Get(ctx, diskRequest)
if err != nil {
return fmt.Errorf("unable to get disk: %w", err)
}
// Сhecking whether the instances is stopped
for _, fullInstanceName := range sourceDisk.GetUsers() {
parsedName := strings.Split(fullInstanceName, "/")
l := len(parsedName)
if l < 5 {
return fmt.Errorf(
"API returned instance name with unexpected format",
)
}
instanceReq := &computepb.GetInstanceRequest{
Project: parsedName[l-5],
Zone: parsedName[l-3],
Instance: parsedName[l-1],
}
instance, err := instancesClient.Get(ctx, instanceReq)
if err != nil {
return fmt.Errorf("unable to get instance: %w", err)
}
if instance.GetStatus() != "TERMINATED" && instance.GetStatus() != "STOPPED" {
if !forceCreate {
return fmt.Errorf("instance %s 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",
parsedName[l-1],
)
}
}
}
if forceCreate {
fmt.Fprintf(w, "Warning: ForceCreate option compromise the integrity of your image. "+
"Stop the instance before you create the image if possible.",
)
}
req := &computepb.InsertImageRequest{
Project: projectID,
ForceCreate: &forceCreate,
ImageResource: &computepb.Image{
Name: proto.String(imageName),
SourceDisk: proto.String(fmt.Sprintf("zones/%s/disks/%s", zone, sourceDiskName)),
StorageLocations: []string{storageLocation},
},
}
op, err := imagesClient.Insert(ctx, req)
if err != nil {
return fmt.Errorf("unable to create image: %w", err)
}
if err = op.Wait(ctx); err != nil {
return fmt.Errorf("unable to wait for the operation: %w", err)
}
fmt.Fprintf(w, "Image created\n")
return nil
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[{
"type": "thumb-down",
"id": "hardToUnderstand",
"label":"Hard to understand"
},{
"type": "thumb-down",
"id": "incorrectInformationOrSampleCode",
"label":"Incorrect information or sample code"
},{
"type": "thumb-down",
"id": "missingTheInformationSamplesINeed",
"label":"Missing the information/samples I need"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]