-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtable_compute_node_group.go
247 lines (220 loc) · 7.56 KB
/
table_compute_node_group.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
package gcp
import (
"context"
"github.com/turbot/go-kit/types"
"github.com/turbot/steampipe-plugin-sdk/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/plugin"
"github.com/turbot/steampipe-plugin-sdk/plugin/transform"
"google.golang.org/api/compute/v1"
)
//// TABLE DEFINITION
func tableGcpComputeNodeGroup(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "gcp_compute_node_group",
Description: "GCP Compute Node Group",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("name"),
Hydrate: getComputeNodeGroup,
},
List: &plugin.ListConfig{
Hydrate: listComputeNodeGroups,
},
Columns: []*plugin.Column{
{
Name: "name",
Description: "A friendly name that identifies the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "id",
Description: "The unique identifier for the resource.",
Type: proto.ColumnType_INT,
},
{
Name: "status",
Description: "Specifies the current state of the node group.",
Type: proto.ColumnType_STRING,
},
{
Name: "size",
Description: "The total number of nodes in the node group.",
Type: proto.ColumnType_INT,
},
{
Name: "description",
Description: "A user-specified, human-readable description of the node group.",
Type: proto.ColumnType_STRING,
},
{
Name: "kind",
Description: "The type of the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "autoscaling_policy_mode",
Description: "Specifies the autoscaling mode of the node group.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("AutoscalingPolicy.Mode"),
},
{
Name: "autoscaling_policy_max_nodes",
Description: "The maximum number of nodes that the group should have. Must be set if autoscaling is enabled. Maximum value allowed is 100.",
Type: proto.ColumnType_INT,
Transform: transform.FromField("AutoscalingPolicy.MaxNodes"),
},
{
Name: "autoscaling_policy_min_nodes",
Description: "The minimum number of nodes that the group should have.",
Type: proto.ColumnType_INT,
Transform: transform.FromField("AutoscalingPolicy.MinNodes"),
},
{
Name: "creation_timestamp",
Description: "The creation timestamp of the resource.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "fingerprint",
Description: "An unique system generated string, to reduce conflicts when multiple users change any property of the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "maintenance_policy",
Description: "Specifies how to handle instances when a node in the group undergoes maintenance.",
Type: proto.ColumnType_STRING,
},
{
Name: "node_template",
Description: "The URL of the node template to create the node group from.",
Type: proto.ColumnType_STRING,
},
{
Name: "self_link",
Description: "The server-defined URL for the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "zone",
Description: "The name of the zone where the node group 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 node group resides.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Zone").Transform(lastPathElement),
},
{
Name: "iam_policy",
Description: "An Identity and Access Management (IAM) policy, which specifies access controls for Google Cloud resources. A `Policy` is a collection of `bindings`. A `binding` binds one or more `members` to a single `role`. Members can be user accounts, service accounts, Google groups, and domains (such as G Suite). A `role` is a named list of permissions; each `role` can be an IAM predefined role or a user-created custom role. For some types of Google Cloud resources, a `binding` can also specify a `condition`, which is a logical expression that allows access to a resource only if the expression evaluates to `true`.",
Type: proto.ColumnType_JSON,
Hydrate: getComputeNodeGroupIamPolicy,
Transform: transform.FromValue(),
},
// standard steampipe columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Transform: transform.From(gcpComputeNodeGroupAka),
},
// standard gcp columns
{
Name: "location",
Description: ColumnDescriptionLocation,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Zone").Transform(lastPathElement),
},
{
Name: "project",
Description: ColumnDescriptionProject,
Type: proto.ColumnType_STRING,
Transform: transform.FromConstant(activeProject()),
},
},
}
}
//// LIST FUNCTION
func listComputeNodeGroups(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("listComputeNodeGroups")
service, err := compute.NewService(ctx)
if err != nil {
return nil, err
}
project := activeProject()
resp := service.NodeGroups.AggregatedList(project)
if err := resp.Pages(ctx, func(page *compute.NodeGroupAggregatedList) error {
for _, item := range page.Items {
for _, nodeGroup := range item.NodeGroups {
d.StreamListItem(ctx, nodeGroup)
}
}
return nil
}); err != nil {
return nil, err
}
return nil, nil
}
//// HYDRATE FUNCTIONS
func getComputeNodeGroup(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
service, err := compute.NewService(ctx)
if err != nil {
return nil, err
}
var nodeGroup compute.NodeGroup
name := d.KeyColumnQuals["name"].GetStringValue()
project := activeProject()
resp := service.NodeGroups.AggregatedList(project).Filter("name=" + name)
if err := resp.Pages(
ctx,
func(page *compute.NodeGroupAggregatedList) error {
for _, item := range page.Items {
for _, i := range item.NodeGroups {
nodeGroup = *i
}
}
return nil
},
); err != nil {
return nil, err
}
// If the specified resource is not present, API does not return any not found errors
if len(nodeGroup.Name) < 1 {
return nil, nil
}
return &nodeGroup, nil
}
func getComputeNodeGroupIamPolicy(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
service, err := compute.NewService(ctx)
if err != nil {
return nil, err
}
nodeGroup := h.Item.(*compute.NodeGroup)
project := activeProject()
zoneName := getLastPathElement(types.SafeString(nodeGroup.Zone))
req, err := service.NodeGroups.GetIamPolicy(project, zoneName, nodeGroup.Name).Do()
if err != nil {
// Return nil, if the resource not present
result := isNotFoundError([]string{"404"})
if result != nil {
return nil, nil
}
return nil, err
}
return req, nil
}
//// TRANSFORM FUNCTIONS
func gcpComputeNodeGroupAka(_ context.Context, d *transform.TransformData) (interface{}, error) {
nodeGroup := d.HydrateItem.(*compute.NodeGroup)
zoneName := getLastPathElement(types.SafeString(nodeGroup.Zone))
nodeGroupName := types.SafeString(nodeGroup.Name)
akas := []string{"gcp://compute.googleapis.com/projects/" + activeProject() + "/zones/" + zoneName + "/nodeGroups/" + nodeGroupName}
return akas, nil
}