使用 Go 在 AWS costexplorer 中筛选具有多个标签的资源
时间:2024-02-21
浏览:117
作者:佚名
学习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 来过滤资源,其值为 dev 或 prod。
因此我尝试在过滤器中添加更多标签,它看起来像这样
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学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.28
热门专题
