import (
"cloud-manager/common"
"fmt"
"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/costexplorer"
"strings"
)
type stubProvider struct {
creds credentials.Value
retrievedCount int
expired bool
err error
}
func (s *stubProvider) Retrieve() (credentials.Value, error) {
s.retrievedCount++
s.expired = false
s.creds.ProviderName = "stubProvider"
return s.creds, s.err
}
func (s *stubProvider) IsExpired() bool {
return s.expired
}
func GetCostAndUsageWithResources() {
c := credentials.NewCredentials(&stubProvider{
creds: credentials.Value{
AccessKeyID: "xx",
SecretAccessKey: "xx",
SessionToken: "",
},
expired: true,
})
mySession := session.Must(session.NewSession())
// Create a CostExplorer client from just a session.
//svc := costexplorer.New(mySession)
start := "2023-03-01" //开始时间只能是14天内
end := "2023-03-19"
svc := costexplorer.New(mySession, aws.NewConfig().WithCredentials(c))
data, err := svc.GetCostAndUsageWithResources(&costexplorer.GetCostAndUsageWithResourcesInput{
Granularity: common.String("MONTHLY"),
Metrics: []*string{common.String("UnblendedCost"),
common.String("BlendedCost"),
common.String("AmortizedCost"),
common.String("NetAmortizedCost"),
common.String("NetUnblendedCost"),
common.String("NormalizedUsageAmount"),
},
TimePeriod: &costexplorer.DateInterval{
Start: &start,
End: &end,
},
Filter: &costexplorer.Expression{
Dimensions: &costexplorer.DimensionValues{ //必须过滤,否则会报错
Key: common.String("SERVICE"),
Values: []*string{common.String("Amazon Elastic Compute Cloud - Compute")},
MatchOptions: []*string{common.String("EQUALS")},
},
},
GroupBy: []*costexplorer.GroupDefinition{
{
Type: common.String("DIMENSION"),
Key: common.String("SERVICE"),
},
{
Type: common.String("DIMENSION"),
Key: common.String("RESOURCE_ID"),
},
},
})
if err != nil {
fmt.Println("失败", err.Error())
}
fmt.Println(data.ResultsByTime)
}
func GetCostAndUsage() {
c := credentials.NewCredentials(&stubProvider{
creds: credentials.Value{
AccessKeyID: "xx",
SecretAccessKey: "xxx",
SessionToken: "",
},
expired: true,
})
mySession := session.Must(session.NewSession())
start := "2023-03-01"
end := "2023-03-19"
svc := costexplorer.New(mySession, aws.NewConfig().WithCredentials(c))
data, err := svc.GetCostAndUsage(&costexplorer.GetCostAndUsageInput{
Granularity: common.String("MONTHLY"),
Metrics: []*string{common.String("UnblendedCost"),
common.String("BlendedCost"),
common.String("AmortizedCost"),
common.String("NetAmortizedCost"),
common.String("NetUnblendedCost"),
common.String("NormalizedUsageAmount"),
},
TimePeriod: &costexplorer.DateInterval{
Start: &start,
End: &end,
},
//Filter: &costexplorer.Expression{
// Dimensions: &costexplorer.DimensionValues{
// Key: common.String("SERVICE"),
// Values: []*string{common.String("Amazon Elastic Compute Cloud - Compute")},
// MatchOptions: []*string{common.String("EQUALS")},
// },
//},
GroupBy: []*costexplorer.GroupDefinition{
{
Type: common.String("DIMENSION"),
Key: common.String("SERVICE"),
},
{
Type: common.String("DIMENSION"),
Key: common.String("RESOURCE_ID"),
},
},
})
if err != nil {
fmt.Println("失败", err.Error())
}
fmt.Println(data.ResultsByTime)
}
//这里只写2个简单demo,具体查询条件和参数可根据官方文档进行修改