/
stacks.go
340 lines (334 loc) · 12.2 KB
/
stacks.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
package cloudformation
import (
"context"
"regexp"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
"github.com/cloudquery/cq-provider-sdk/provider/diag"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
)
var (
validStackNotFoundRegex = regexp.MustCompile("Stack with id (.*) does not exist")
)
//go:generate cq-gen --resource stacks --config gen.hcl --output .
func Stacks() *schema.Table {
return &schema.Table{
Name: "aws_cloudformation_stacks",
Description: "The Stack data type.",
Resolver: fetchCloudformationStacks,
Multiplex: client.ServiceAccountRegionMultiplexer("cloudformation"),
IgnoreError: client.IgnoreCommonErrors,
DeleteFilter: client.DeleteAccountRegionFilter,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"id"}},
Columns: []schema.Column{
{
Name: "account_id",
Description: "The AWS Account ID of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSAccount,
},
{
Name: "region",
Description: "The AWS Region of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSRegion,
},
{
Name: "arn",
Type: schema.TypeString,
Resolver: schema.PathResolver("StackId"),
},
{
Name: "creation_time",
Description: "The time at which the stack was created. This member is required.",
Type: schema.TypeTimestamp,
},
{
Name: "stack",
Description: "The name associated with the stack. This member is required.",
Type: schema.TypeString,
Resolver: schema.PathResolver("StackName"),
},
{
Name: "status",
Description: "Current status of the stack. This member is required.",
Type: schema.TypeString,
Resolver: schema.PathResolver("StackStatus"),
},
{
Name: "capabilities",
Description: "The capabilities allowed in the stack.",
Type: schema.TypeStringArray,
IgnoreInTests: true,
},
{
Name: "change_set_id",
Description: "The unique ID of the change set.",
Type: schema.TypeString,
IgnoreInTests: true,
},
{
Name: "deletion_time",
Description: "The time the stack was deleted.",
Type: schema.TypeTimestamp,
IgnoreInTests: true,
},
{
Name: "description",
Description: "A user-defined description associated with the stack.",
Type: schema.TypeString,
IgnoreInTests: true,
},
{
Name: "disable_rollback",
Description: "Boolean to enable or disable rollback on stack creation failures: * true: disable rollback. * false: enable rollback.",
Type: schema.TypeBool,
},
{
Name: "stack_drift_status",
Description: "Status of the stack's actual configuration compared to its expected template configuration. * DRIFTED: The stack differs from its expected template configuration",
Type: schema.TypeString,
Resolver: schema.PathResolver("DriftInformation.StackDriftStatus"),
},
{
Name: "drift_last_check_timestamp",
Description: "Most recent time when a drift detection operation was initiated on the stack, or any of its individual resources that support drift detection.",
Type: schema.TypeTimestamp,
Resolver: schema.PathResolver("DriftInformation.LastCheckTimestamp"),
IgnoreInTests: true,
},
{
Name: "enable_termination_protection",
Description: "Whether termination protection is enabled for the stack",
Type: schema.TypeBool,
IgnoreInTests: true,
},
{
Name: "last_updated_time",
Description: "The time the stack was last updated",
Type: schema.TypeTimestamp,
IgnoreInTests: true,
},
{
Name: "notification_arns",
Description: "Amazon SNS topic Amazon Resource Names (ARNs) to which stack related events are published.",
Type: schema.TypeStringArray,
Resolver: schema.PathResolver("NotificationARNs"),
},
{
Name: "parameters",
Description: "A list of Parameter structures.",
Type: schema.TypeJSON,
},
{
Name: "parent_id",
Description: "For nested stacks--stacks created as resources for another stack--the stack ID of the direct parent of this stack",
Type: schema.TypeString,
IgnoreInTests: true,
},
{
Name: "role_arn",
Description: "The Amazon Resource Name (ARN) of an Identity and Access Management (IAM) role that's associated with the stack",
Type: schema.TypeString,
Resolver: schema.PathResolver("RoleARN"),
IgnoreInTests: true,
},
{
Name: "rollback_configuration_monitoring_time_in_minutes",
Description: "The amount of time, in minutes, during which CloudFormation should monitor all the rollback triggers after the stack creation or update operation deploys all necessary resources",
Type: schema.TypeInt,
Resolver: schema.PathResolver("RollbackConfiguration.MonitoringTimeInMinutes"),
IgnoreInTests: true,
},
{
Name: "rollback_configuration_rollback_triggers",
Description: "The triggers to monitor during stack creation or update actions",
Type: schema.TypeJSON,
Resolver: schema.PathResolver("RollbackConfiguration.RollbackTriggers"),
IgnoreInTests: true,
},
{
Name: "root_id",
Description: "For nested stacks--stacks created as resources for another stack--the stack ID of the top-level stack to which the nested stack ultimately belongs",
Type: schema.TypeString,
IgnoreInTests: true,
},
{
Name: "id",
Description: "Unique identifier of the stack.",
Type: schema.TypeString,
Resolver: schema.PathResolver("StackId"),
},
{
Name: "stack_status_reason",
Description: "Success/failure message associated with the stack status.",
Type: schema.TypeString,
IgnoreInTests: true,
},
{
Name: "tags",
Description: "A list of Tags that specify information about the stack.",
Type: schema.TypeJSON,
Resolver: client.ResolveTags,
},
{
Name: "timeout_in_minutes",
Description: "The amount of time within which stack creation should complete.",
Type: schema.TypeInt,
},
},
Relations: []*schema.Table{
{
Name: "aws_cloudformation_stack_outputs",
Description: "The Output data type.",
Resolver: schema.PathTableResolver("Outputs"),
IgnoreInTests: true,
Columns: []schema.Column{
{
Name: "stack_cq_id",
Description: "Unique CloudQuery ID of aws_cloudformation_stacks table (FK)",
Type: schema.TypeUUID,
Resolver: schema.ParentIdResolver,
},
{
Name: "description",
Description: "User defined description associated with the output.",
Type: schema.TypeString,
},
{
Name: "export_name",
Description: "The name of the export associated with the output.",
Type: schema.TypeString,
},
{
Name: "output_key",
Description: "The key associated with the output.",
Type: schema.TypeString,
},
{
Name: "output_value",
Description: "The value associated with the output.",
Type: schema.TypeString,
},
},
},
{
Name: "aws_cloudformation_stack_resources",
Description: "Contains high-level information about the specified stack resource.",
Resolver: fetchCloudformationStackResources,
Columns: []schema.Column{
{
Name: "stack_cq_id",
Description: "Unique CloudQuery ID of aws_cloudformation_stacks table (FK)",
Type: schema.TypeUUID,
Resolver: schema.ParentIdResolver,
},
{
Name: "last_updated_timestamp",
Description: "Time the status was updated. This member is required.",
Type: schema.TypeTimestamp,
},
{
Name: "logical_resource_id",
Description: "The logical name of the resource specified in the template. This member is required.",
Type: schema.TypeString,
},
{
Name: "resource_status",
Description: "Current status of the resource. This member is required.",
Type: schema.TypeString,
},
{
Name: "resource_type",
Description: "Type of resource",
Type: schema.TypeString,
},
{
Name: "stack_resource_drift_status",
Description: "Status of the resource's actual configuration compared to its expected configuration. * DELETED: The resource differs from its expected configuration in that it has been deleted. * MODIFIED: The resource differs from its expected configuration. * NOT_CHECKED: CloudFormation hasn't checked if the resource differs from its expected configuration",
Type: schema.TypeString,
Resolver: schema.PathResolver("DriftInformation.StackResourceDriftStatus"),
},
{
Name: "drift_last_check_timestamp",
Description: "When CloudFormation last checked if the resource had drifted from its expected configuration.",
Type: schema.TypeTimestamp,
Resolver: schema.PathResolver("DriftInformation.LastCheckTimestamp"),
IgnoreInTests: true,
},
{
Name: "module_info_logical_id_hierarchy",
Description: "A concatenated list of the logical IDs of the module or modules containing the resource",
Type: schema.TypeString,
Resolver: schema.PathResolver("ModuleInfo.LogicalIdHierarchy"),
IgnoreInTests: true,
},
{
Name: "module_info_type_hierarchy",
Description: "A concatenated list of the module type or types containing the resource",
Type: schema.TypeString,
Resolver: schema.PathResolver("ModuleInfo.TypeHierarchy"),
IgnoreInTests: true,
},
{
Name: "physical_resource_id",
Description: "The name or unique identifier that corresponds to a physical instance ID of the resource.",
Type: schema.TypeString,
},
{
Name: "resource_status_reason",
Description: "Success/failure message associated with the resource.",
Type: schema.TypeString,
IgnoreInTests: true,
},
},
},
},
}
}
// ====================================================================================================================
// Table Resolver Functions
// ====================================================================================================================
func fetchCloudformationStacks(ctx context.Context, meta schema.ClientMeta, _ *schema.Resource, res chan<- interface{}) error {
var config cloudformation.DescribeStacksInput
c := meta.(*client.Client)
svc := c.Services().Cloudformation
for {
output, err := svc.DescribeStacks(ctx, &config)
if err != nil {
return diag.WrapError(err)
}
res <- output.Stacks
if aws.ToString(output.NextToken) == "" {
break
}
config.NextToken = output.NextToken
}
return nil
}
func fetchCloudformationStackResources(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
stack := parent.Item.(types.Stack)
config := cloudformation.ListStackResourcesInput{
StackName: stack.StackName,
}
c := meta.(*client.Client)
svc := c.Services().Cloudformation
for {
output, err := svc.ListStackResources(ctx, &config)
if err != nil {
if client.IsErrorRegex(err, "ValidationError", validStackNotFoundRegex) {
meta.Logger().Debug("received ValidationError on ListStackResources, stack does not exist", "region", c.Region, "err", err)
return nil
}
return diag.WrapError(err)
}
res <- output.StackResourceSummaries
if aws.ToString(output.NextToken) == "" {
break
}
config.NextToken = output.NextToken
}
return nil
}