網頁

2023/2/7

Golang 解除AWS Direct Connect gateway與Virtual private gateway關聯

Go以AWS提供的SDK aws-sdk-go-v2來解除Direct Connect gateway與Virtual private gateway的關聯。



事前要求

參考「AWS 建立IAM管理使用者及credentials」設定供應用程式存取AWS需要的credentials。

參考「Golang 關聯AWS Direct Connect gateway與Virtual private gateway」建立Direct Connect gateway與Virtual private gateway關聯。

參考「Golang 取得AWS Direct Connect gateway與Virtual private gateway關聯」取得關聯資訊。


解除Direct Connect gateway與Virtual private gateway關聯

呼叫directconnect.Client.DeleteDirectConnectGatewayAssociation傳入參數directconnect.DeleteDirectConnectGatewayAssociationInput來取得Direct Connect gateway與Virtual private gateway的關聯。

刪除關聯directconnect.DeleteDirectConnectGatewayAssociationInput.AssociationId要填入Direct Connect gateway與Virtual gateway的關聯ID;或填入directconnect.DeleteDirectConnectGatewayAssociationInput.DirectConnectGatewayIddirectconnect.DeleteDirectConnectGatewayAssociationInput.VirtualGatewayId

main.go

package main

import (
    "context"
    "fmt"

    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/service/directconnect"
)

func main() {
    ctx := context.TODO()
    client := NewDirectConnectClient(ctx)

    // directConnectGatewayId := "e44e0dfb-82b9-4e4f-bcc1-9d196f25d0af"
    // virtualGatewayId := "vgw-0670c529abefaee33"
    associationId := "047dd041-3388-4907-a1df-f61de4644c0b"
    params := &directconnect.DeleteDirectConnectGatewayAssociationInput{
        // DirectConnectGatewayId: &directConnectGatewayId,
        // VirtualGatewayId:       &virtualGatewayId,
        AssociationId: &associationId,
    }

    output, err := client.DeleteDirectConnectGatewayAssociation(ctx, params)
    if err != nil {
        panic(err)
    }

    fmt.Println(*output.DirectConnectGatewayAssociation.DirectConnectGatewayId) // e44e0dfb-82b9-4e4f-bcc1-9d196f25d0af
    fmt.Println(*output.DirectConnectGatewayAssociation.VirtualGatewayId)       // vgw-0670c529abefaee33
    fmt.Println(output.DirectConnectGatewayAssociation.AssociationState)        // disassociating
}

func NewDirectConnectClient(ctx context.Context) *directconnect.Client {
    cfg, err := config.LoadDefaultConfig(
        ctx,
        config.WithRegion("ap-northeast-1"),
    )
    if err != nil {
        panic(err)
    }

    return directconnect.NewFromConfig(cfg) // Create an Amazon Direct Connect service client
}

github


測試

執行Go應用程式輸出以下結果。

e44e0dfb-82b9-4e4f-bcc1-9d196f25d0af
vgw-0670c529abefaee33
disassociating

在AWS console檢視的Direct Connect gateway已解除Virtual private gateway關聯。




沒有留言:

張貼留言