-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtable_gcp_artifact_registry_repository.go
305 lines (267 loc) · 9.07 KB
/
table_gcp_artifact_registry_repository.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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/artifactregistry/v1"
)
//// TABLE DEFINITION
func tableGcpArtifactRegistryRepository(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "gcp_artifact_registry_repository",
Description: "GCP Artifact Registry Repository",
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"name", "location"}),
Hydrate: getArtifactRegistryRepository,
},
List: &plugin.ListConfig{
Hydrate: listArtifactRegistryRepositories,
KeyColumns: plugin.KeyColumnSlice{
{
Name: "location",
Require: plugin.Optional,
},
},
},
GetMatrixItemFunc: BuildArtifactRegistryLocationList,
Columns: []*plugin.Column{
{
Name: "name",
Description: "The name of the repository.",
Type: proto.ColumnType_STRING,
Transform: transform.FromP(artifactRegistryRepositoryData, "Title"),
},
{
Name: "cleanup_policy_dry_run",
Description: "If true, the cleanup pipeline is prevented from deleting versions in this repository.",
Type: proto.ColumnType_BOOL,
},
{
Name: "create_time",
Description: "The time when the repository was created.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "description",
Description: "The user-provided description of the repository.",
Type: proto.ColumnType_STRING,
},
{
Name: "format",
Description: "The format of packages that are stored in the repository.",
Type: proto.ColumnType_STRING,
},
{
Name: "kms_key_name",
Description: "The Cloud KMS resource name of the customer managed encryption key that's used to encrypt the contents of the Repository.",
Type: proto.ColumnType_STRING,
},
{
Name: "mode",
Description: "The mode of the repository.",
Type: proto.ColumnType_STRING,
},
{
Name: "satisfies_pzs",
Description: "If set, the repository satisfies physical zone separation.",
Type: proto.ColumnType_BOOL,
},
{
Name: "size_bytes",
Description: "The size, in bytes, of all artifact storage in this repository.",
Type: proto.ColumnType_INT,
},
{
Name: "update_time",
Description: "The time when the repository was last updated.",
Type: proto.ColumnType_TIMESTAMP,
},
// JSON field
{
Name: "cleanup_policies",
Description: "Cleanup policies for this repository.",
Type: proto.ColumnType_JSON,
},
{
Name: "docker_config",
Description: "Docker repository config contains repository level configuration for the repositories of docker type.",
Type: proto.ColumnType_JSON,
},
{
Name: "maven_config",
Description: "Maven repository config contains repository level configuration for the repositories of maven type.",
Type: proto.ColumnType_JSON,
},
{
Name: "remote_repository_config",
Description: "Configuration specific for a Remote Repository.",
Type: proto.ColumnType_JSON,
},
{
Name: "sbom_config",
Description: "Config and state for sbom generation for resources within this Repository.",
Type: proto.ColumnType_JSON,
},
{
Name: "virtual_repository_config",
Description: "Configuration specific for a Virtual Repository.",
Type: proto.ColumnType_JSON,
},
{
Name: "self_link",
Description: "An URL that can be used to access the resource again.",
Type: proto.ColumnType_STRING,
Hydrate: artifactRegistryRepositorySelfLink,
Transform: transform.FromValue(),
},
{
Name: "labels",
Description: "A set of labels associated with this repository.",
Type: proto.ColumnType_JSON,
},
// standard steampipe columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromP(artifactRegistryRepositoryData, "Title"),
},
{
Name: "tags",
Description: ColumnDescriptionTags,
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Labels"),
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Transform: transform.FromP(artifactRegistryRepositoryData, "Akas"),
},
// Standard GCP columns
{
Name: "location",
Description: ColumnDescriptionLocation,
Type: proto.ColumnType_STRING,
Transform: transform.FromP(artifactRegistryRepositoryData, "Location"),
},
{
Name: "project",
Description: ColumnDescriptionProject,
Type: proto.ColumnType_STRING,
Transform: transform.FromP(artifactRegistryRepositoryData, "Project"),
},
},
}
}
//// LIST FUNCTION
func listArtifactRegistryRepositories(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
region := d.EqualsQualString("location")
var location string
matrixLocation := d.EqualsQualString(matrixKeyLocation)
// Since, when the service API is disabled, matrixLocation value will be nil
if matrixLocation != "" {
location = matrixLocation
}
// Minimize API call for given location
if region != "" && region != location {
return nil, nil
}
// Create Service Connection
service, err := ArtifactRegistryService(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("gcp_artifact_registry_repository.listArtifactRegistryRepositories", "service_error", err)
return nil, err
}
// Max limit isn't mentioned in the documentation
// Default limit is set as 1000
pageSize := types.Int64(1000)
limit := d.QueryContext.Limit
if d.QueryContext.Limit != nil {
if *limit < *pageSize {
pageSize = limit
}
}
// Get project details
projectId, err := getProject(ctx, d, h)
if err != nil {
return nil, err
}
project := projectId.(string)
data := "projects/" + project + "/locations/" + location
resp := service.Projects.Locations.Repositories.List(data).PageSize(*pageSize)
if err := resp.Pages(ctx, func(page *artifactregistry.ListRepositoriesResponse) error {
for _, repo := range page.Repositories {
d.StreamListItem(ctx, repo)
// 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_artifact_registry_repository.listArtifactRegistryRepositories", "api_error", err)
return nil, err
}
return nil, nil
}
//// HYDRATE FUNCTIONS
func getArtifactRegistryRepository(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
// Create Service Connection
service, err := ArtifactRegistryService(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("gcp_artifact_registry_repository.getArtifactRegistryRepository", "service_error", err)
return nil, err
}
// Get project details
projectId, err := getProject(ctx, d, h)
if err != nil {
return nil, err
}
project := projectId.(string)
name := d.EqualsQuals["name"].GetStringValue()
location := d.EqualsQuals["location"].GetStringValue()
// check if name or location is empty
if name == "" || location == "" {
return nil, nil
}
resp, err := service.Projects.Locations.Repositories.Get("projects/" + project + "/locations/" + location + "/repositories/" + name).Do()
if err != nil {
plugin.Logger(ctx).Error("gcp_artifact_registry_repository.getArtifactRegistryRepository", "api_error", err)
return nil, err
}
return resp, nil
}
func artifactRegistryRepositorySelfLink(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
data := h.Item.(*artifactregistry.Repository)
var location string
matrixLocation := d.EqualsQualString(matrixKeyLocation)
// Since, when the service API is disabled, matrixLocation value will be nil
if matrixLocation != "" {
location = matrixLocation
}
projectID := strings.Split(data.Name, "/")[1]
name := strings.Split(data.Name, "/")[5]
selfLink := "https://artifactregistry.googleapis.com/v1/projects/" + projectID + "/regions/" + location + "/repositories/" + name
return selfLink, nil
}
//// TRANSFORM FUNCTIONS
func artifactRegistryRepositoryData(ctx context.Context, h *transform.TransformData) (interface{}, error) {
data := h.HydrateItem.(*artifactregistry.Repository)
param := h.Param.(string)
projectID := strings.Split(data.Name, "/")[1]
name := strings.Split(data.Name, "/")[5]
location := strings.Split(data.Name, "/")[3]
turbotData := map[string]interface{}{
"Project": projectID,
"Title": name,
"Location": location,
"Akas": []string{"gcp://artifactregistry.googleapis.com/projects/" + projectID + "/repositories/" + name},
}
return turbotData[param], nil
}