/
table_gcp_compute_autoscaler.go
284 lines (250 loc) · 8.22 KB
/
table_gcp_compute_autoscaler.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package gcp
import (
"context"
"strings"
"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/compute/v1"
)
func tableGcpComputeAutoscaler(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "gcp_compute_autoscaler",
Description: "GCP Compute Autoscaler",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("name"),
Hydrate: getComputeAutoscaler,
},
List: &plugin.ListConfig{
Hydrate: listComputeAutoscaler,
},
Columns: []*plugin.Column{
{
Name: "name",
Description: "Name of the Autoscaler.",
Type: proto.ColumnType_STRING,
},
{
Name: "id",
Description: "The unique identifier for this Autoscaler. This identifier is defined by the server.",
Type: proto.ColumnType_INT,
},
{
Name: "creation_timestamp",
Description: "Timestamp when the Autoscaler was created.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "description",
Description: "An optional description of this resource. Provide this property when you create the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "kind",
Description: "Type of the resource. Always compute#autoscaler for Autoscalers.",
Type: proto.ColumnType_STRING,
},
{
Name: "recommended_size",
Description: "Target recommended MIG size (number of instances) computed by autoscaler.",
Type: proto.ColumnType_INT,
},
{
Name: "region",
Description: "The URL of the region where the Autoscaler resides. Only applicable for regional resources.",
Type: proto.ColumnType_STRING,
},
// region_name is a simpler view of the region, without the full path
{
Name: "region_name",
Description: "Name of the region where the Autoscaler resides. Only applicable for regional resources.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Region").Transform(lastPathElement),
},
{
Name: "self_link",
Description: "Server-defined fully-qualified URL for this resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "status",
Description: "The status of the autoscaler configuration.",
Type: proto.ColumnType_STRING,
},
{
Name: "target",
Description: "URL of the managed instance group that this autoscaler will scale.",
Type: proto.ColumnType_STRING,
},
{
Name: "zone",
Description: "URL of the zone where the Autoscaler resides.",
Type: proto.ColumnType_STRING,
},
// zone_name is a simpler view of the zone, without the full path
{
Name: "zone_name",
Description: "The zone name in which the Autoscaler resides.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Zone").Transform(lastPathElement),
},
// JSON columns
{
Name: "autoscaling_policy",
Description: "The configuration parameters for the autoscaling algorithm.",
Type: proto.ColumnType_JSON,
},
{
Name: "scaling_schedule_status",
Description: "Status information of existing scaling schedules.",
Type: proto.ColumnType_JSON,
},
{
Name: "status_details",
Description: "Human-readable details about the current state of the autoscaler.",
Type: proto.ColumnType_JSON,
},
// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Transform: transform.From(autoscalerAka),
},
// GCP standard columns
{
Name: "location",
Description: ColumnDescriptionLocation,
Type: proto.ColumnType_STRING,
Transform: transform.FromP(autoscalerLocation, "Location"),
},
{
Name: "project",
Description: ColumnDescriptionProject,
Type: proto.ColumnType_STRING,
Transform: transform.FromP(autoscalerLocation, "Project"),
},
},
}
}
//// LIST FUNCTIONS
func listComputeAutoscaler(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
// Max limit is set as per documentation
pageSize := types.Int64(500)
limit := d.QueryContext.Limit
if d.QueryContext.Limit != nil {
if *limit < *pageSize {
pageSize = limit
}
}
// Get project details
getProjectCached := plugin.HydrateFunc(getProject).WithCache()
projectId, err := getProjectCached(ctx, d, h)
if err != nil {
return nil, err
}
project := projectId.(string)
// Create Service Connection
service, err := ComputeService(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("gcp_compute_autoscaler.listComputeAutoscaler", "service_creation_err", err)
return nil, err
}
resp := service.Autoscalers.AggregatedList(project).MaxResults(*pageSize)
if err := resp.Pages(ctx, func(page *compute.AutoscalerAggregatedList) error {
for _, item := range page.Items {
for _, autoscaler := range item.Autoscalers {
d.StreamListItem(ctx, autoscaler)
// 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_compute_autoscaler.listComputeAutoscaler", "api_err", err)
return nil, err
}
return nil, nil
}
//// HYDRATE FUNCTIONS
func getComputeAutoscaler(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 {
return nil, err
}
project := projectId.(string)
var autoscaler compute.Autoscaler
name := d.EqualsQuals["name"].GetStringValue()
if name == "" {
return nil, nil
}
// Create Service Connection
service, err := ComputeService(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("gcp_compute_autoscaler.getComputeAutoscaler", "service_creation_err", err)
return nil, err
}
resp := service.Autoscalers.AggregatedList(project).Filter("name=" + name)
if err := resp.Pages(ctx, func(page *compute.AutoscalerAggregatedList) error {
for _, item := range page.Items {
for _, i := range item.Autoscalers {
autoscaler = *i
}
}
return nil
},
); err != nil {
plugin.Logger(ctx).Error("gcp_compute_autoscaler.getComputeAutoscaler", "api_err", err)
return nil, err
}
// If the specified resource is not present, API does not return any not found errors
if len(autoscaler.Name) < 1 {
return nil, nil
}
return &autoscaler, nil
}
//// TRANSFORM FUNCTIONS
func autoscalerAka(_ context.Context, d *transform.TransformData) (interface{}, error) {
i := d.HydrateItem.(*compute.Autoscaler)
zoneName := getLastPathElement(types.SafeString(i.Zone))
regionName := getLastPathElement(types.SafeString(i.Region))
project := strings.Split(i.SelfLink, "/")[6]
autoscalerName := types.SafeString(i.Name)
var akas []string
if zoneName == "" {
akas = []string{"gcp://compute.googleapis.com/projects/" + project + "/regions/" + regionName + "/autoscalers/" + autoscalerName}
} else {
akas = []string{"gcp://compute.googleapis.com/projects/" + project + "/zones/" + zoneName + "/autoscalers/" + autoscalerName}
}
return akas, nil
}
func autoscalerLocation(_ context.Context, d *transform.TransformData) (interface{}, error) {
i := d.HydrateItem.(*compute.Autoscaler)
param := d.Param.(string)
zoneName := getLastPathElement(types.SafeString(i.Zone))
regionName := getLastPathElement(types.SafeString(i.Region))
project := strings.Split(i.SelfLink, "/")[6]
locationData := map[string]string{
"Type": "ZONAL",
"Location": zoneName,
"Project": project,
}
if zoneName == "" {
locationData["Type"] = "REGIONAL"
locationData["Location"] = regionName
}
return locationData[param], nil
}