-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtable_gcp_compute_resource_policy.go
269 lines (237 loc) · 8.16 KB
/
table_gcp_compute_resource_policy.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
package gcp
import (
"context"
"strings"
"github.com/turbot/go-kit/types"
"github.com/turbot/steampipe-plugin-sdk/v2/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v2/plugin"
"github.com/turbot/steampipe-plugin-sdk/v2/plugin/transform"
"google.golang.org/api/compute/v1"
)
//// TABLE DEFINITION
func tableGcpComputeResourcePolicy(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "gcp_compute_resource_policy",
Description: "GCP Compute Resource Policy",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("name"),
Hydrate: getComputeResourcePolicy,
},
List: &plugin.ListConfig{
Hydrate: listComputeResourcePolicies,
ShouldIgnoreError: isIgnorableError([]string{"403"}),
KeyColumns: plugin.KeyColumnSlice{
// String columns
{Name: "status", Require: plugin.Optional, Operators: []string{"<>", "="}},
},
},
Columns: []*plugin.Column{
{
Name: "name",
Description: "The name of the resource, provided by the client when initially creating the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "id",
Description: "The unique identifier for the resource.",
Type: proto.ColumnType_INT,
Transform: transform.FromField("Id"),
},
{
Name: "status",
Description: "The status of resource policy creation. Possible values are: 'CREATING', 'DELETING', 'INVALID', and 'READY'.",
Type: proto.ColumnType_STRING,
},
{
Name: "self_link",
Description: "A server-defined fully-qualified URL for this resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "creation_timestamp",
Description: "The date and time, when the policy was created.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "description",
Description: "An user-defined, human-readable description for this resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "kind",
Description: "Type of the resource. Always compute#resource_policies for resource policies.",
Type: proto.ColumnType_STRING,
},
{
Name: "group_placement_policy",
Description: "Resource policy for instances for placement configuration.",
Type: proto.ColumnType_JSON,
},
{
Name: "instance_schedule_policy",
Description: "Resource policy for scheduling instance operations.",
Type: proto.ColumnType_JSON,
},
{
Name: "resource_status",
Description: "The system status of the resource policy.",
Type: proto.ColumnType_JSON,
},
{
Name: "snapshot_schedule_policy",
Description: "Resource policy for persistent disks for creating snapshots.",
Type: proto.ColumnType_JSON,
},
{
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: getComputeResourcePolicyIamPolicy,
Transform: transform.FromValue(),
},
// 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(gcpComputeResourcePolicyAkas),
},
// GCP standard columns
{
Name: "location",
Description: ColumnDescriptionLocation,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Region").Transform(lastPathElement),
},
{
Name: "project",
Description: ColumnDescriptionProject,
Type: proto.ColumnType_STRING,
Transform: transform.FromP(gcpComputeResourcePolicyAkas, "Project"),
},
},
}
}
//// LIST FUNCTION
func listComputeResourcePolicies(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("listComputeResourcePolicies")
// Create Service Connection
service, err := ComputeService(ctx, d)
if err != nil {
return nil, err
}
filterString := ""
if d.KeyColumnQuals["status"] != nil {
filterString = "status=" + d.KeyColumnQuals["status"].GetStringValue()
}
// Max limit is set as per documentation
// https://pkg.go.dev/google.golang.org/[email protected]/compute/v1?utm_source=gopls#ResourcePoliciesAggregatedListCall.MaxResults
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)
var resp *compute.ResourcePoliciesAggregatedListCall
if filterString == "" {
resp = service.ResourcePolicies.AggregatedList(project).MaxResults(*pageSize)
} else {
resp = service.ResourcePolicies.AggregatedList(project).Filter(filterString).MaxResults(*pageSize)
}
if err := resp.Pages(
ctx,
func(page *compute.ResourcePolicyAggregatedList) error {
for _, item := range page.Items {
for _, policy := range item.ResourcePolicies {
d.StreamListItem(ctx, policy)
// 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.QueryStatus.RowsRemaining(ctx) == 0 {
page.NextPageToken = ""
return nil
}
}
}
return nil
},
); err != nil {
return nil, err
}
return nil, nil
}
//// HYDRATE FUNCTIONS
func getComputeResourcePolicy(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getComputeResourcePolicy")
// Create Service Connection
service, err := ComputeService(ctx, d)
if err != nil {
return nil, err
}
// Get project details
getProjectCached := plugin.HydrateFunc(getProject).WithCache()
projectId, err := getProjectCached(ctx, d, h)
if err != nil {
return nil, err
}
project := projectId.(string)
var resourcePolicy compute.ResourcePolicy
name := d.KeyColumnQuals["name"].GetStringValue()
// Return nil, if no input provided
if name == "" {
return nil, nil
}
resp := service.ResourcePolicies.AggregatedList(project).Filter("name=" + name)
if err := resp.Pages(
ctx,
func(page *compute.ResourcePolicyAggregatedList) error {
for _, item := range page.Items {
for _, i := range item.ResourcePolicies {
resourcePolicy = *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(resourcePolicy.Name) < 1 {
return nil, nil
}
return &resourcePolicy, nil
}
func getComputeResourcePolicyIamPolicy(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
data := h.Item.(*compute.ResourcePolicy)
// Create Service Connection
service, err := ComputeService(ctx, d)
if err != nil {
return nil, err
}
project := strings.Split(data.SelfLink, "/")[6]
region := getLastPathElement(types.SafeString(data.Region))
resp, err := service.ResourcePolicies.GetIamPolicy(project, region, data.Name).Do()
if err != nil {
return nil, err
}
return resp, nil
}
//// TRANSFORM FUNCTIONS
func gcpComputeResourcePolicyAkas(_ context.Context, d *transform.TransformData) (interface{}, error) {
data := d.HydrateItem.(*compute.ResourcePolicy)
akas := strings.ReplaceAll(data.SelfLink, "https://www.googleapis.com/compute/v1/", "gcp://compute.googleapis.com/")
return []string{akas}, nil
}