AdSense

網頁

2023/2/13

Golang 建立GCP Partner Interconnect connection VLAN Attachment

Go以Google API Client Libraries來建立GCP Partner Interconnect connection的VLAN attachment。



事前要求

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

參考「Golang 建立GCP Interconnect Cloud Router」建立Cloud Router。



建立Partner Interconnect connection VLAN attachment

呼叫compute.InterconnectAttachmentsService.Insert輸入project id、region、compute.InterconnectAttachment參數來建立VLAN attachment。

compute.InterconnectAttachment參數屬性如下:

  • Name - 輸入VLAN atachment的名稱。
  • EdgeAvailabilityDomain - 輸入AVAILABILITY_DOMAIN_ANY
  • Router - 輸入cloud router的URL。
  • Type - 輸入PARTNER

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)
    }

    projectId := "project-id-1"
    region := "asia-east1"

    interconnectAttachmentService := compute.NewInterconnectAttachmentsService(service)
    vlanAttachement := &compute.InterconnectAttachment{
        Name:                   "demo-vlan-a-002",
        EdgeAvailabilityDomain: "AVAILABILITY_DOMAIN_1",
        Router:                 "https://www.googleapis.com/compute/v1/projects/project-id-1/regions/asia-east1/routers/demo-cloudrouter-002",
        Type:                   "PARTNER",
    }
    call := interconnectAttachmentService.Insert(projectId, region, vlanAttachement)
    _, err = call.Do()
    if err != nil {
        panic(err)
    }
}

github


測試

很奇怪用gcloud cli不需提供Interconnect URL即可成功建立VLAN attachment,但這邊用SDK API卻需要Interconnect URL?原來必須把compute.InterconnectAttachment.Type設為PARTNER才可成功新增VLAN attachment。


沒有留言:

AdSense