登录
首页 >  Golang >  Go问答

使用 Go 在 AWS costexplorer 中筛选具有多个标签的资源

来源:stackoverflow

时间:2024-02-21 14:39:24 116浏览 收藏

学习Golang要努力,但是不要急!今天的这篇文章《使用 Go 在 AWS costexplorer 中筛选具有多个标签的资源》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!

问题内容

如何使用aws golang sdk添加多个标签来过滤资源?

您好,我有要用于 aws sdk 中的 getcostandusage 函数的输入变量

input := &costexplorer.getcostandusageinput{
        timeperiod: &costexplorer.dateinterval{
            start: aws.string(startdate.format("2006-01-02")),
            end:   aws.string(enddate.format("2006-01-02")),
        },
        granularity: aws.string("monthly"),
        metrics:     []*string{aws.string("blendedcost")},
        groupby: []*costexplorer.groupdefinition{
            {
                type: aws.string("dimension"),
                key:  aws.string("service"),
            },
        },
        filter: &costexplorer.expression{
            tags: &costexplorer.tagvalues{
                key:    aws.string("project"),
                values: []*string{aws.string("project1")},
                matchoptions: []*string{
                    aws.string("equals"),
                },
            },
        },
    }

这确实有效。但是,我想使用另一个标签 stage 来过滤资源,其值为 devprod

因此我尝试在过滤器中添加更多标签,它看起来像这样

Filter: &costexplorer.Expression{
            Tags: &costexplorer.TagValues{
                Key:    aws.String("project"),
                Values: []*string{aws.String("Project1")},
                MatchOptions: []*string{
                    aws.String("EQUALS"),
                },
            },
            Tags: &costexplorer.TagValues{
                Key:    aws.String("stage"),
                Values: []*string{aws.String("dev")},
                MatchOptions: []*string{
                    aws.String("EQUALS"),
                },
            },
        },

当然,go 不喜欢这样,并且在结构文字上给出重复的字段名称标签。您能给我一个关于如何解决这个问题的想法吗?


正确答案


使用and表达式:

Filter: &costexplorer.Expression{
    And: []*costexplorer.Expression{
        {
            Tags: &costexplorer.TagValues{
                Key:    aws.String("project"),
                Values: []*string{aws.String("Project1")},
                MatchOptions: []*string{
                    aws.String("EQUALS"),
                },
            },
        },
        {
            Tags: &costexplorer.TagValues{
                Key:    aws.String("stage"),
                Values: []*string{aws.String("dev")},
                MatchOptions: []*string{
                    aws.String("EQUALS"),
                },
            },
        },
    },
},

参考:https://pkg .go.dev/github.com/aws/aws-sdk-go/service/costexplorer#expression

今天关于《使用 Go 在 AWS costexplorer 中筛选具有多个标签的资源》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>