Go以AWS提供的SDK aws-sdk-go-v2
來取得Direct Connect的Virtual interface。
事前要求
參考「AWS 建立IAM管理使用者及credentials」設定供應用程式存取AWS需要的credentials。
參考「Golang 建立AWS Direct Connect Private Virtual Interface」建立Direct Connect Connection。
取得Virtual Interface
在Go程式中呼叫config.LoadDefaultConfig
傳入region參數建立aws.Conifg
物件,AWS SDK預設會讀取$HOME/.aws/credentials
的access keys來通過權限驗證,然後依此參數呼叫directconnect.NewFromConfig
建立directconnect.Client
。
呼叫directconnect.Client.DescribeVirtualInterfaces
傳入參數directconnect.DescribeVirtualInterfacesInput
來取得Virutal interface資料。
directconnect.DescribeVirtualInterfacesInput.ConnectionId
為Virtual interface連結的Direct Connect Connection的ID。
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)
connectionId := "dxcon-fg5kq63s"
params := &directconnect.DescribeVirtualInterfacesInput{
ConnectionId: &connectionId,
}
output, err := client.DescribeVirtualInterfaces(ctx, params)
if err != nil {
panic(err)
}
fmt.Println(*output.VirtualInterfaces[0].VirtualInterfaceId) // dxvif-ffwha4ij
fmt.Println(*output.VirtualInterfaces[0].VirtualInterfaceName) // demo-virtual-interface-001
fmt.Println(output.VirtualInterfaces[0].VirtualInterfaceState) // down
fmt.Println(*output.VirtualInterfaces[0].VirtualInterfaceType) // private
fmt.Println(*output.VirtualInterfaces[0].DirectConnectGatewayId) // e44e0dfb-82b9-4e4f-bcc1-9d196f25d0af
fmt.Println(*output.VirtualInterfaces[0].AmazonSideAsn) // 64512
fmt.Println(*output.VirtualInterfaces[0].BgpPeers[0].BgpPeerId) // dxpeer-fgzqetz9
fmt.Println(output.VirtualInterfaces[0].BgpPeers[0].BgpPeerState) // available
fmt.Println(output.VirtualInterfaces[0].BgpPeers[0].BgpStatus) // down
fmt.Println(*output.VirtualInterfaces[0].BgpPeers[0].AmazonAddress) // 169.254.96.1/29
fmt.Println(*output.VirtualInterfaces[0].BgpPeers[0].CustomerAddress) // 169.254.96.6/29
}
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
}
測試
執行Go應用程式輸出以下結果。
dxvif-ffwha4ij
demo-virtual-interface-001
down
private
e44e0dfb-82b9-4e4f-bcc1-9d196f25d0af
64512
dxpeer-fgzqetz9
available
down
169.254.96.1/29
169.254.96.6/29
沒有留言:
張貼留言