使用Go语言在AWS Cost Explorer中使用多个标签来过滤资源。

huangapple go评论78阅读模式
英文:

Multiple tags to filter resource in AWS costexplorer using Go

问题

你好,你可以通过创建一个包含多个标签的切片来解决这个问题。以下是示例代码:

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{
        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"), aws.String("prod")},
                    MatchOptions: []*string{
                        aws.String("EQUALS"),
                    },
                },
            },
        },
    },
}

在这个示例中,我们使用了 And 字段来表示同时满足多个条件。每个条件都是一个 costexplorer.Expression 结构体,其中包含了相应的标签信息。

希望这可以帮助到你!如果你还有其他问题,请随时提问。

英文:

How do you add multiple tags to filter resources using AWS golang SDK?

Hello, I have the input variable to be use for the GetCostAndUsage function from AWS SDK

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"),
				},
			},
		},
	}

This does works. However, I would like to filter resources using another tag which is stage with the values of dev or prod.

Thus I tried adding more tags in the Filter and it looked something like this

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"),
				},
			},
		},

Of course Go doesn't like that and gives duplicate field name tags on struct literal. Can you give me an idea on how should I approach this problem?

答案1

得分: 3

使用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

英文:

Use the And expression:

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"),
				},
			},
		},
	},
},

Reference: https://pkg.go.dev/github.com/aws/aws-sdk-go/service/costexplorer#Expression

答案2

得分: 1

如您所见,我在Expression结构体中添加了一个And字段,它允许您指定多个条件,所有条件都必须为真才能匹配过滤器。然后,我在And字段中添加了两个单独的Expression结构体,分别表示项目和阶段标签。

请注意,我还在阶段标签的Values字段中添加了一个标签值的切片,以指定dev和prod的值。

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{
        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"), aws.String("prod")},
                    MatchOptions: []*string{
                        aws.String("EQUALS"),
                    },
                },
            },
        },
    },
}
英文:

as you can see, I added an And field to the Expression struct, which allows you to specify multiple conditions that must all be true for the filter to match. I then added two separate Expression structs to the And field, each with its own Tags field representing the project and stage tags, respectively.

Note that I also added a slice of tag values to the Values field of the stage tag, to specify both the dev and prod values.

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{
        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"), aws.String("prod")},
                    MatchOptions: []*string{
                        aws.String("EQUALS"),
                    },
                },
            },
        },
    },
}

huangapple
  • 本文由 发表于 2023年2月16日 14:49:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定