/
table_gcp_billing_budget.go
204 lines (179 loc) · 6.39 KB
/
table_gcp_billing_budget.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package gcp
import (
"context"
"github.com/turbot/go-kit/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"
"google.golang.org/api/billingbudgets/v1"
"google.golang.org/api/cloudbilling/v1"
)
//// TABLE DEFINITION
func tableGcpBillingBudget(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "gcp_billing_budget",
Description: "GCP Billing Budget",
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"name", "billing_account"}),
Hydrate: getBillingBudget,
},
List: &plugin.ListConfig{
KeyColumns: plugin.OptionalColumns([]string{"billing_account"}),
ParentHydrate: getBillingAccount,
Hydrate: listBillingBudgets,
},
Columns: []*plugin.Column{
{
Name: "name",
Description: "The resource name of the budget.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Budget.Name").Transform(lastPathElement),
},
{
Name: "billing_account",
Description: "The name given to the associated billing account.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("BillingAccount").Transform(lastPathElement),
},
{
Name: "display_name",
Description: "The display name given to the budget.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Budget.DisplayName"),
},
{
Name: "budget_filter",
Description: "Filters that define which resources are used to compute the actual spend against the budget amount.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Budget.BudgetFilter"),
},
{
Name: "last_period_amount",
Description: "Use the last period's actual spend as the budget for the present period.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Budget.Amount.LastPeriodAmount"),
},
{
Name: "notifications_rule",
Description: "Rules to apply to notifications sent based on budget spend and thresholds.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Budget.NotificationsRule"),
},
{
Name: "specified_amount",
Description: "Use the last period's actual spend as the budget for the present period.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Budget.Amount.SpecifiedAmount"),
},
{
Name: "threshold_rules",
Description: "Rules that trigger alerts (notifications of thresholds being crossed) when spend exceeds the specified percentages of the budget.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Budget.ThresholdRules"),
},
// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Budget.DisplayName"),
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Hydrate: getBillingBudgetAka,
Transform: transform.FromValue(),
},
// standard gcp columns
{
Name: "location",
Description: ColumnDescriptionLocation,
Type: proto.ColumnType_STRING,
Transform: transform.FromConstant("global"),
},
{
Name: "project",
Description: ColumnDescriptionProject,
Type: proto.ColumnType_STRING,
Hydrate: plugin.HydrateFunc(getProject).WithCache(),
Transform: transform.FromValue(),
},
},
}
}
type budgetInfo = struct {
BillingAccount string
Budget *billingbudgets.GoogleCloudBillingBudgetsV1Budget
}
//// LIST FUNCTION
func listBillingBudgets(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
acc := h.Item.(*cloudbilling.BillingAccount)
// Validate - User input(if any) should match with the hydrated billing account
if d.EqualsQualString("billing_account") != "" && "billingAccounts/"+d.EqualsQualString("billing_account") != acc.Name {
return nil, nil
}
pageSize := types.Int64(100)
limit := d.QueryContext.Limit
if d.QueryContext.Limit != nil {
if *limit < *pageSize {
pageSize = limit
}
}
// Create Service Connection
service, err := BillingBudgetsService(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("gcp_billing_budget.listBillingBudgets", "service_err", err)
return nil, err
}
response := service.BillingAccounts.Budgets.List(acc.Name).PageSize(*pageSize)
if err := response.Pages(
ctx,
func(page *billingbudgets.GoogleCloudBillingBudgetsV1ListBudgetsResponse) error {
for _, item := range page.Budgets {
d.StreamListItem(ctx, budgetInfo{acc.Name, item})
// Check if context has been cancelled or if the limit has been hit (if specified)
// if there is a limit, it will return the number of rows required to reach this limit
if d.RowsRemaining(ctx) == 0 {
page.NextPageToken = ""
return nil
}
}
return nil
},
); err != nil {
plugin.Logger(ctx).Error("gcp_billing_budget.listBillingBudgets", "api_err", err)
return nil, err
}
return nil, nil
}
//// HYDRATE FUNCTION
func getBillingBudget(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
name := d.EqualsQuals["name"].GetStringValue()
acc := d.EqualsQuals["billing_account"].GetStringValue()
// Create Service Connection
service, err := BillingBudgetsService(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("gcp_billing_budget.getBillingBudget", "service_err", err)
return nil, err
}
budget, err := service.BillingAccounts.Budgets.Get("billingAccounts/" + acc + "/budgets/" + name).Do()
if err != nil {
plugin.Logger(ctx).Error("gcp_billing_budget.getBillingBudget", "api_err", err)
return nil, err
}
return budgetInfo{acc, budget}, nil
}
func getBillingBudgetAka(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
// Get project details
getProjectCached := plugin.HydrateFunc(getProject).WithCache()
projectId, err := getProjectCached(ctx, d, h)
if err != nil {
plugin.Logger(ctx).Error("gcp_billing_budget.getBillingBudgetAka", "cache_err", err)
return nil, err
}
project := projectId.(string)
data := h.Item.(budgetInfo)
akas := []string{"gcp://billingbudgets.googleapis.com/projects/" + project + "/" + data.Budget.Name}
return akas, nil
}