Go程式以Google API Client Libraries for Go附加磁碟(disk)於執行個體(instance)的範例如下。
範例環境:
- Go 1.19
- google.golang.org/api v0.79.0
下面AttachDisk
函式呼叫compute.Instances.AttachDisk
方法附加一顆compute.AttachedDisk
:
AttachedDisk.Source
- 磁碟的selflink。AttachedDisk.AutoDelete
- 執行個體刪除時是否自動刪除。AttachedDisk.Boot
- 是否為開機磁碟;AttachedDisk.DeviceName
- 磁碟Linux系統的裝置名稱。
import (
"context"
"google.golang.org/api/compute/v1"
)
func AttachOtherDisk(cs *compute.Service, project, zone, instanceName, requestId string, disk *compute.Disk) error {
op, err := cs.Instances.AttachDisk(project, zone, instanceName, &compute.AttachedDisk{
Source: disk.SelfLink, // e.g. "projects/tidal-mason-366907/zones/asia-east1-b/disks/disk-3bea0fec-6ce8-4a45-b38e-8e32f9b3a389"
AutoDelete: true,
Boot: false,
DeviceName: disk.Name,
}).Do()
if err != nil {
return err
}
op, err = cs.ZoneOperations.
Wait(project, zone, op.Name).
Do()
if err != nil {
return err
}
return nil
}
沒有留言:
張貼留言