亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 go 按標簽列出 AWS 中的負載均衡器

使用 go 按標簽列出 AWS 中的負載均衡器

Go
一只甜甜圈 2023-03-21 17:36:06
有沒有一種方法可以通過標簽名稱列出 aws 中的所有負載均衡器?我在他們的SDK 文檔中找不到任何內容??梢詥??如何?
查看完整描述

2 回答

?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

我不熟悉 Golang AWS SDK,但如果是我,我在 SDK 文檔中找不到它,我會查找 AWS CLI 文檔并在那里找到命令,因為它們可能會轉換為 SDK 或最壞情況下的運行系統Golang 中的命令來執行 CLI 并獲得相同的結果。

GetResources

https://docs.aws.amazon.com/cli/latest/reference/resourcegroupstaggingapi/get-resources.html

返回與位于 AWS 賬戶的指定區域中的指定標簽(鍵和值)關聯的所有標記資源。

因此,您可以使用它來獲取具有特定標簽的所有資源,然后在 Golang 中迭代結果以僅選擇 ELB

看起來您可以在一個命令中按標簽和資源進行過濾:

http://img1.sycdn.imooc.com//64197aa50001731506540385.jpg

看起來 golang sdk 確實存在該命令

https://docs.aws.amazon.com/sdk-for-go/api/service/resourcegroupstaggingapi/#ResourceGroupsTaggingAPI.GetResources

我認為我上面強調的選項可供您使用。

在 Golang 中執行

您將使用諸如 exec 之類的東西。 https://golang.org/pkg/os/exec/

本教程可能包含如何從 exec https://nathanleclaire.com/blog/2014/12/29/shelled-out-commands-in-golang/獲取結果

示例代碼

package main


import (

    "fmt"

    "strings"


    "github.com/aws/aws-sdk-go/aws"

    "github.com/aws/aws-sdk-go/aws/credentials"

    "github.com/aws/aws-sdk-go/aws/session"

    "github.com/aws/aws-sdk-go/service/elb"

    "github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi"

)


const (

    // ProviderName is the cloud provider providing loadbalancing functionality

    ProviderName = "aws"

)


// ELB is the struct implementing the lbprovider interface

type ELB struct {

    client            *elb.ELB

    elbResourceName   string

    resourceapiClient *resourcegroupstaggingapi.ResourceGroupsTaggingAPI

}


// NewELB is the factory method for ELB

func NewELB(id string, secret string, region string) (*ELB, error) {

    awsConfig := &aws.Config{

        Region:      aws.String(region),

        Credentials: credentials.NewStaticCredentials(id, secret, ""),

    }


    awsConfig = awsConfig.WithCredentialsChainVerboseErrors(true)

    sess, err := session.NewSession(awsConfig)

    if err != nil {

        return nil, fmt.Errorf("Unable to initialize AWS session: %v", err)

    }


    return &ELB{

        client:            elb.New(sess),

        resourceapiClient: resourcegroupstaggingapi.New(sess),

        elbResourceName:   "elasticloadbalancing",

    }, nil

}


// GetLoadbalancersByTag gets the loadbalancers by tag

func (e *ELB) GetLoadbalancersByTag(tagKey string, tagValue string) ([]string, error) {

    tagFilters := &resourcegroupstaggingapi.TagFilter{}

    tagFilters.Key = aws.String(tagKey)

    tagFilters.Values = append(tagFilters.Values, aws.String(tagValue))


    getResourcesInput := &resourcegroupstaggingapi.GetResourcesInput{}

    getResourcesInput.TagFilters = append(getResourcesInput.TagFilters, tagFilters)

    getResourcesInput.ResourceTypeFilters = append(

        getResourcesInput.ResourceTypeFilters,

        aws.String(e.elbResourceName),

    )


    resources, err := e.resourceapiClient.GetResources(getResourcesInput)

    if err != nil {

        return nil, err

    }


    elbs := []string{}

    for _, resource := range resources.ResourceTagMappingList {

        elbs = append(elbs, strings.Split(*resource.ResourceARN, "/")[1])

    }

    return elbs, nil

}


查看完整回答
反對 回復 2023-03-21
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

您可以簡單地遍歷 ELB 列表并按標簽過濾。


    svc := elbv2.New(...)


    input := &elbv2.DescribeLoadBalancersInput{

            LoadBalancerArns: []*string{},

    }


    result, _ := svc.DescribeLoadBalancers(input)


    var list_of_arns []*string

    for _, lb := range result.LoadBalancers{

            list_of_arns = append(list_of_arns, lb.LoadBalancerArn)

    }


    input2 := &elbv2.DescribeTagsInput{

            ResourceArns: list_of_arns,

    }


    result2, _ := svc.DescribeTags(input2)

    fmt.Println(result2.TagDescriptions)


查看完整回答
反對 回復 2023-03-21
  • 2 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號