網頁

2023/4/20

Golang 刪除GCP VPC network private service connection IP allocated range

Go以Google API Client Libraries來刪除GCP VPC network的private service connection的IP range。



事前要求

參考「GCP 設定本機應用程式存取憑證 Application Default Credentials」設定credential。

參考「Golang 建立GCP VPC network private service connection IP allocated range」建立VPC的allocated IP range。


刪除private service connection IP allocated range

呼叫compute.GlobalAddressesService.Delete輸入project id及allocated IP range的名稱來刪除allocated IP range。

main.go

package main

import (
    "context"

    compute "google.golang.org/api/compute/v1"
)

func main() {
    ctx := context.Background()
    service, err := compute.NewService(ctx)
    if err != nil {
        panic(err)
    }

    globalAddressesService := compute.NewGlobalAddressesService(service)

    projectId := "project-id-1"
    addressResourceName := "demo-vpc-002-allocated-range-001"
    call := globalAddressesService.Delete(projectId, addressResourceName)

    _, err = call.Do()
    if err != nil {
        panic(err)
    }

}

github


測試

執行Go應用程式並於GCP console確認VPC的private connection service的allocated IP range已被刪除。


沒有留言:

張貼留言