/
table_aws_cost_by_record_type_monthly.go
80 lines (69 loc) · 2.25 KB
/
table_aws_cost_by_record_type_monthly.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package aws
import (
"context"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/costexplorer"
"github.com/aws/aws-sdk-go-v2/service/costexplorer/types"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)
func tableAwsCostByRecordTypeMonthly(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_cost_by_record_type_monthly",
Description: "AWS Cost Explorer - Cost by Record Type (Monthly)",
List: &plugin.ListConfig{
Hydrate: listCostByRecordTypeMonthly,
Tags: map[string]string{"service": "ce", "action": "GetCostAndUsage"},
},
Columns: awsGlobalRegionColumns(
costExplorerColumns([]*plugin.Column{
{
Name: "linked_account_id",
Description: "The linked AWS Account ID.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Dimension1"),
},
{
Name: "record_type",
Description: "The different types of charges such as RI fees, usage, costs, tax refunds, and credits.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Dimension2"),
},
}),
),
}
}
//// LIST FUNCTION
func listCostByRecordTypeMonthly(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
params := buildCostByRecordTypeInput("MONTHLY")
return streamCostAndUsage(ctx, d, params)
}
func buildCostByRecordTypeInput(granularity string) *costexplorer.GetCostAndUsageInput {
timeFormat := "2006-01-02"
if granularity == "HOURLY" {
timeFormat = "2006-01-02T15:04:05Z"
}
endTime := time.Now().Format(timeFormat)
startTime := getCEStartDateForGranularity(granularity).Format(timeFormat)
params := &costexplorer.GetCostAndUsageInput{
TimePeriod: &types.DateInterval{
Start: aws.String(startTime),
End: aws.String(endTime),
},
Granularity: types.Granularity(granularity),
Metrics: AllCostMetrics(),
GroupBy: []types.GroupDefinition{
{
Type: types.GroupDefinitionType("DIMENSION"),
Key: aws.String("LINKED_ACCOUNT"),
},
{
Type: types.GroupDefinitionType("DIMENSION"),
Key: aws.String("RECORD_TYPE"),
},
},
}
return params
}