Go以Google API Client Libraries for Go對VM instance取下磁碟操作時出現錯誤googleapi: Error 400: Idempotent request error: The type of existing operation is detachDisk, while the operation type of the request is delete., idempotentRequestError
。
狀況
例如要把GCP instance(執行個體)的磁碟取下(detach disk)然後刪除時執行以下程式碼。當執行到cs.Disks.Delete().RequestId().Do()
出現上述錯誤。
import (
"context"
"google.golang.org/api/compute/v1"
)
func DetachThenDeleteDisk(cs *compute.Service, project, zone, requestId, instanceName, deviceName string, diskName) (err error) {
op, err := cs.Instances.DetachDisk(project, zone, instanceName, deviceName).
RequestId(requestId).
Do()
if err != nil {
return
}
op, err = cs.ZoneOperations.
Wait(project, zone, op.Name).
Do()
if err != nil {
return
}
op, err := cs.Disks.Delete(project, zone, diskName).
RequestId(requestId).
Do()
if err != nil {
return // Error 400: Idempotent request error: The type of existing operation is detachDisk, while the operation type of the request is delete., idempotentRequestError
}
_, err = c.ZoneOperations.
Wait(project, zone, op.Name).
Do()
}
解決
兩個GCP資源操作使用到同一個RequestId造成此錯誤。因為GCP的不同操作的RequestId必須是唯一的,目的是用來分辨不同的操作請求以利追蹤。兩個資源操作的call要用不同的RequestId。
事隔4個月又踩到同個坑。
沒有留言:
張貼留言