/
table_aws_codeartifact_domain.go
347 lines (312 loc) · 9.78 KB
/
table_aws_codeartifact_domain.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package aws
import (
"context"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/codeartifact"
"github.com/aws/aws-sdk-go-v2/service/codeartifact/types"
codeartifactv1 "github.com/aws/aws-sdk-go/service/codeartifact"
"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"
)
//// TABLE DEFINITION
func tableAwsCodeArtifactDomain(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_codeartifact_domain",
Description: "AWS Code Artifact Domain",
Get: &plugin.GetConfig{
KeyColumns: []*plugin.KeyColumn{
{
Name: "name",
Require: plugin.Required,
},
{
Name: "owner",
Require: plugin.Optional,
},
},
Hydrate: getCodeArtifactDomain,
Tags: map[string]string{"service": "codeartifact", "action": "DescribeDomain"},
},
List: &plugin.ListConfig{
Hydrate: listCodeArtifactDomains,
Tags: map[string]string{"service": "codeartifact", "action": "ListDomains"},
},
HydrateConfig: []plugin.HydrateConfig{
{
Func: getCodeArtifactDomainTags,
Tags: map[string]string{"service": "codeartifact", "action": "ListTagsForResource"},
},
{
Func: getCodeArtifactDomainPermissionsPolicy,
Tags: map[string]string{"service": "codeartifact", "action": "GetDomainPermissionsPolicy"},
},
{
Func: getCodeArtifactDomain,
Tags: map[string]string{"service": "codeartifact", "action": "DescribeDomain"},
},
},
GetMatrixItemFunc: SupportedRegionMatrix(codeartifactv1.EndpointsID),
Columns: awsRegionalColumns([]*plugin.Column{
{
Name: "name",
Description: "The name of the domain.",
Type: proto.ColumnType_STRING,
},
{
Name: "arn",
Description: "The Amazon Resource Name (ARN) specifying the domain.",
Type: proto.ColumnType_STRING,
},
{
Name: "asset_size_bytes",
Description: "The total size of all assets in the domain.",
Type: proto.ColumnType_INT,
Hydrate: getCodeArtifactDomain,
},
{
Name: "created_time",
Description: "A timestamp that contains the date and time the domain was created.",
Type: proto.ColumnType_TIMESTAMP,
},
{
Name: "encryption_key",
Description: "The key used to encrypt the domain.",
Type: proto.ColumnType_STRING,
},
{
Name: "owner",
Description: "The 12-digit account number of the Amazon Web Services account that owns the domain. It does not include dashes or spaces.",
Type: proto.ColumnType_STRING,
},
{
Name: "repository_count",
Description: "The number of repositories in the domain.",
Type: proto.ColumnType_INT,
Hydrate: getCodeArtifactDomain,
},
{
Name: "s3_bucket_arn",
Description: "The Amazon Resource Name (ARN) of the Amazon S3 bucket that is used to store package assets in the domain.",
Type: proto.ColumnType_STRING,
Hydrate: getCodeArtifactDomain,
Transform: transform.FromField("S3BucketArn"),
},
{
Name: "status",
Description: "A string that contains the status of the domain.",
Type: proto.ColumnType_STRING,
},
{
Name: "policy",
Description: "An CodeArtifact resource policy that contains a resource ARN, document details, and a revision.",
Type: proto.ColumnType_JSON,
Hydrate: getCodeArtifactDomainPermissionsPolicy,
Transform: transform.FromValue(),
},
{
Name: "policy_std",
Description: "Contains the contents of the resource-based policy in a canonical form for easier searching.",
Type: proto.ColumnType_JSON,
Hydrate: getCodeArtifactDomainPermissionsPolicy,
Transform: transform.FromValue().Transform(unescape).Transform(policyToCanonical),
},
{
Name: "tags_src",
Description: "A list of tags assigned to the resource.",
Type: proto.ColumnType_JSON,
Hydrate: getCodeArtifactDomainTags,
Transform: transform.FromField("Tags"),
},
// Steampipe standard columns
{
Name: "title",
Description: resourceInterfaceDescription("title"),
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Hydrate: getCodeArtifactDomainTags,
Transform: transform.FromField("Tags").Transform(codeArtifactDomainTurbotTags),
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Type: proto.ColumnType_JSON,
Transform: transform.FromField("Arn").Transform(transform.EnsureStringArray),
},
}),
}
}
//// LIST FUNCTION
func listCodeArtifactDomains(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
logger := plugin.Logger(ctx)
// Create session
svc, err := CodeArtifactClient(ctx, d)
if err != nil {
logger.Error("aws_codeartifact_domain.listCodeArtifactDomains", "service_creation_error", err)
return nil, err
}
if svc == nil {
// Unsupported region, return no data
return nil, nil
}
// Reduce the basic request limit down if the user has only requested a small number of rows
maxLimit := int32(100)
if d.QueryContext.Limit != nil {
limit := int32(*d.QueryContext.Limit)
if limit < maxLimit {
if limit < 1 {
maxLimit = 1
} else {
maxLimit = limit
}
}
}
input := codeartifact.ListDomainsInput{
MaxResults: &maxLimit,
}
paginator := codeartifact.NewListDomainsPaginator(svc, &input, func(o *codeartifact.ListDomainsPaginatorOptions) {
o.Limit = maxLimit
o.StopOnDuplicateToken = true
})
for paginator.HasMorePages() {
// apply rate limiting
d.WaitForListRateLimit(ctx)
output, err := paginator.NextPage(ctx)
if err != nil {
plugin.Logger(ctx).Error("aws_codeartifact_domain.listCodeArtifactDomains", "api_error", err)
return nil, err
}
for _, domain := range output.Domains {
item := &types.DomainDescription{
Arn: domain.Arn,
CreatedTime: domain.CreatedTime,
EncryptionKey: domain.EncryptionKey,
Name: domain.Name,
Owner: domain.Owner,
Status: domain.Status,
}
d.StreamListItem(ctx, item)
// Context may get cancelled due to manual cancellation or if the limit has been reached
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}
return nil, err
}
//// HYDRATE FUNCTIONS
func getCodeArtifactDomain(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
logger := plugin.Logger(ctx)
var name, owner string
if h.Item != nil {
data := h.Item.(*types.DomainDescription)
name = *data.Name
owner = *data.Owner
} else {
name = d.EqualsQuals["name"].GetStringValue()
owner = d.EqualsQuals["owner"].GetStringValue()
}
if name == "" {
return nil, nil
}
// Build the params
params := &codeartifact.DescribeDomainInput{
Domain: &name,
}
if owner != "" {
params.DomainOwner = &owner
}
// Create session
svc, err := CodeArtifactClient(ctx, d)
if err != nil {
logger.Error("aws_codeartifact_domain.getCodeArtifactDomain", "service_creation_error", err)
return nil, err
}
if svc == nil {
// Unsupported region, return no data
return nil, nil
}
// Get call
data, err := svc.DescribeDomain(ctx, params)
if err != nil {
logger.Error("aws_codeartifact_domain.getCodeArtifactDomain", "api_error", err)
return nil, err
}
return data.Domain, nil
}
func getCodeArtifactDomainTags(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
logger := plugin.Logger(ctx)
data := h.Item.(*types.DomainDescription)
// Create Session
svc, err := CodeArtifactClient(ctx, d)
if err != nil {
logger.Error("aws_codeartifact_domain.getCodeArtifactDomainTags", "service_creation_error", err)
return nil, err
}
if svc == nil {
// Unsupported region, return no data
return nil, nil
}
// Build the params
params := &codeartifact.ListTagsForResourceInput{
ResourceArn: aws.String(*data.Arn),
}
// Get call
op, err := svc.ListTagsForResource(ctx, params)
if err != nil {
logger.Error("aws_codeartifact_domain.getCodeArtifactDomainTags", "api_error", err)
return nil, err
}
return op, nil
}
func getCodeArtifactDomainPermissionsPolicy(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
logger := plugin.Logger(ctx)
data := h.Item.(*types.DomainDescription)
// Create Session
svc, err := CodeArtifactClient(ctx, d)
if err != nil {
logger.Error("aws_codeartifact_domain.getCodeArtifactDomainPermissionsPolicy", "service_creation_error", err)
return nil, err
}
if svc == nil {
// Unsupported region, return no data
return nil, nil
}
// Build the params
params := &codeartifact.GetDomainPermissionsPolicyInput{
Domain: aws.String(*data.Name),
DomainOwner: aws.String(*data.Owner),
}
// Get call
op, err := svc.GetDomainPermissionsPolicy(ctx, params)
if err != nil {
if strings.Contains(err.Error(), "ResourceNotFoundException") {
return nil, nil
}
logger.Error("aws_codeartifact_domain.getCodeArtifactDomainPermissionsPolicy", "api_error", err)
return nil, err
}
if op != nil && op.Policy != nil {
return op.Policy.Document, nil
}
return nil, nil
}
//// TRANSFORM FUNCTIONS
func codeArtifactDomainTurbotTags(ctx context.Context, d *transform.TransformData) (interface{}, error) {
tags := d.HydrateItem.(*codeartifact.ListTagsForResourceOutput)
// Mapping the resource tags inside turbotTags
var turbotTagsMap map[string]string
if tags != nil {
turbotTagsMap = map[string]string{}
for _, i := range tags.Tags {
turbotTagsMap[*i.Key] = *i.Value
}
}
return turbotTagsMap, nil
}