Windows Server イメージを作成する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
指定したソースディスクから Windows Server イメージを作成します。
もっと見る
このコードサンプルを含む詳細なドキュメントについては、以下をご覧ください。
コードサンプル
Go
このサンプルを試す前に、Compute Engine クイックスタート: クライアント ライブラリの使用に記載されている Go の設定手順に沿って操作します。
詳細については、Compute Engine Go API リファレンス ドキュメントをご覧ください。
Compute Engine に対して認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
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
}
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
[{
"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":"翻訳に関する問題"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"その他"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"わかりやすい"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"問題の解決に役立った"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"その他"
}]