Go程式以Google API Client Libraries for Go新增磁碟(disk)的範例如下。
範例環境:
- Go 1.19
- google.golang.org/api v0.79.0
範例
下面在AddDisk
函式中呼叫compute.Service.DiskService.Insert
方法新增一顆compute.Disk
,
Disk.Name
- 磁碟名稱。Disk.Description
- 磁碟描述,選填。Disk.Type
- 磁碟類型,填入disk type link;Disk.SizeGb
- 磁碟容量。
import (
"context"
"google.golang.org/api/compute/v1"
)
func AddOtherDisk(cs *compute.Service, project, zone, requestId string) (*compute.Disk, error) {
disk := &compute.Disk{
Name: "demo-disk-001",
Description: "Demo disk",
Type: "zones/asia-east2-a/diskTypes/pd-standard"
SizeGb: 10,
}
op, err := cs.Disks.Insert(project, zone, disk).
RequestId(requestId).
Do()
if err != nil {
return nil, err
}
op, err = cs.ZoneOperations.
Wait(project, zone, op.Name).
Do()
if err != nil {
return nil, err
}
disk, err = cs.Disks.Get(project, zone, disk.Name).
Do()
if err != nil {
return nil, err
}
return disk, nil
}
沒有留言:
張貼留言