Crea un'immagine Windows Server
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Crea un'immagine Windows Server dal disco di origine specificato.
Per saperne di più
Per la documentazione dettagliata che include questo esempio di codice, vedi quanto segue:
Esempio di codice
Go
Prima di provare questo esempio, segui le istruzioni di configurazione di Go disponibili nella
guida rapida di Compute Engine sull'utilizzo
delle librerie client.
Per ulteriori informazioni, consulta la documentazione di riferimento dell'API Go di Compute Engine.
Per eseguire l'autenticazione in Compute Engine, configura Credenziali predefinite dell'applicazione.
Per maggiori informazioni, consulta
Configurare l'autenticazione per un ambiente di sviluppo locale.
import (
"context"
"fmt"
"io"
"strings"
compute "cloud.google.com/go/compute/apiv1"
computepb "cloud.google.com/go/compute/apiv1/computepb"
"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
}
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[{
"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": "translationIssue",
"label":"Problema di traduzione"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Altra"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Facile da capire"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Il problema è stato risolto"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Altra"
}]