Skip to content

Commit 7a23c2e

Browse files
authored
feat(aws): Add Support for Cloudformation Templates (#10701)
#### Summary
1 parent e667400 commit 7a23c2e

File tree

8 files changed

+119
-19
lines changed

8 files changed

+119
-19
lines changed

plugins/source/aws/docs/tables/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
- [aws_cloudformation_stack_set_operation_results](../../../../../website/tables/aws/aws_cloudformation_stack_set_operation_results.md)
9797
- [aws_cloudformation_stacks](../../../../../website/tables/aws/aws_cloudformation_stacks.md)
9898
- [aws_cloudformation_stack_resources](../../../../../website/tables/aws/aws_cloudformation_stack_resources.md)
99+
- [aws_cloudformation_stack_templates](../../../../../website/tables/aws/aws_cloudformation_stack_templates.md)
99100
- [aws_cloudfront_cache_policies](../../../../../website/tables/aws/aws_cloudfront_cache_policies.md)
100101
- [aws_cloudfront_distributions](../../../../../website/tables/aws/aws_cloudfront_distributions.md)
101102
- [aws_cloudhsmv2_backups](../../../../../website/tables/aws/aws_cloudhsmv2_backups.md)

plugins/source/aws/resources/services/cloudformation/stack_resources.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package cloudformation
22

33
import (
4+
"context"
5+
6+
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
47
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
58
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
69
"github.com/cloudquery/plugin-sdk/v2/schema"
@@ -26,3 +29,23 @@ func stackResources() *schema.Table {
2629
},
2730
}
2831
}
32+
33+
func fetchCloudformationStackResources(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error {
34+
stack := parent.Item.(types.Stack)
35+
config := cloudformation.ListStackResourcesInput{
36+
StackName: stack.StackName,
37+
}
38+
c := meta.(*client.Client)
39+
svc := c.Services().Cloudformation
40+
paginator := cloudformation.NewListStackResourcesPaginator(svc, &config)
41+
for paginator.HasMorePages() {
42+
page, err := paginator.NextPage(ctx, func(options *cloudformation.Options) {
43+
options.Region = c.Region
44+
})
45+
if err != nil {
46+
return err
47+
}
48+
res <- page.StackResourceSummaries
49+
}
50+
return nil
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package cloudformation
2+
3+
import (
4+
"context"
5+
6+
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
7+
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
8+
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
9+
"github.com/cloudquery/plugin-sdk/v2/schema"
10+
"github.com/cloudquery/plugin-sdk/v2/transformers"
11+
)
12+
13+
func stackTemplates() *schema.Table {
14+
tableName := "aws_cloudformation_stack_templates"
15+
return &schema.Table{
16+
Name: tableName,
17+
Description: `https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_GetTemplate.html`,
18+
Resolver: fetchCloudformationStackTemplates,
19+
Multiplex: client.ServiceAccountRegionMultiplexer(tableName, "cloudformation"),
20+
Transform: transformers.TransformWithStruct(&cloudformation.GetTemplateOutput{}, transformers.WithSkipFields("ResultMetadata")),
21+
Columns: []schema.Column{
22+
client.DefaultAccountIDColumn(false),
23+
client.DefaultRegionColumn(false),
24+
{
25+
Name: "stack_arn",
26+
Type: schema.TypeString,
27+
Resolver: schema.ParentColumnResolver("arn"),
28+
CreationOptions: schema.ColumnCreationOptions{
29+
PrimaryKey: true,
30+
},
31+
},
32+
},
33+
}
34+
}
35+
36+
func fetchCloudformationStackTemplates(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error {
37+
stack := parent.Item.(types.Stack)
38+
config := cloudformation.GetTemplateInput{
39+
StackName: stack.StackName,
40+
}
41+
c := meta.(*client.Client)
42+
svc := c.Services().Cloudformation
43+
resp, err := svc.GetTemplate(ctx, &config, func(options *cloudformation.Options) {
44+
options.Region = c.Region
45+
})
46+
if err != nil {
47+
return err
48+
}
49+
res <- resp
50+
return nil
51+
}

plugins/source/aws/resources/services/cloudformation/stacks.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func Stacks() *schema.Table {
4343

4444
Relations: []*schema.Table{
4545
stackResources(),
46+
stackTemplates(),
4647
},
4748
}
4849
}
@@ -63,22 +64,3 @@ func fetchCloudformationStacks(ctx context.Context, meta schema.ClientMeta, _ *s
6364
}
6465
return nil
6566
}
66-
func fetchCloudformationStackResources(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error {
67-
stack := parent.Item.(types.Stack)
68-
config := cloudformation.ListStackResourcesInput{
69-
StackName: stack.StackName,
70-
}
71-
c := meta.(*client.Client)
72-
svc := c.Services().Cloudformation
73-
paginator := cloudformation.NewListStackResourcesPaginator(svc, &config)
74-
for paginator.HasMorePages() {
75-
page, err := paginator.NextPage(ctx, func(options *cloudformation.Options) {
76-
options.Region = c.Region
77-
})
78-
if err != nil {
79-
return err
80-
}
81-
res <- page.StackResourceSummaries
82-
}
83-
return nil
84-
}

plugins/source/aws/resources/services/cloudformation/stacks_mock_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cloudformation
33
import (
44
"testing"
55

6+
"github.com/aws/aws-sdk-go-v2/aws"
67
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
78
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
89
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
@@ -40,6 +41,21 @@ func buildStacks(t *testing.T, ctrl *gomock.Controller) client.Services {
4041
nil,
4142
)
4243

44+
var template cloudformation.GetTemplateOutput
45+
if err := faker.FakeObject(&template); err != nil {
46+
t.Fatal(err)
47+
}
48+
template.TemplateBody = aws.String(`{"foo": "bar"}`)
49+
50+
mock.EXPECT().GetTemplate(
51+
gomock.Any(),
52+
&cloudformation.GetTemplateInput{StackName: stack.StackName},
53+
gomock.Any(),
54+
).Return(
55+
&template,
56+
nil,
57+
)
58+
4359
return client.Services{Cloudformation: mock}
4460
}
4561

website/pages/docs/plugins/sources/aws/tables.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Table: aws_cloudformation_stack_templates
2+
3+
This table shows data for AWS CloudFormation Stack Templates.
4+
5+
https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_GetTemplate.html
6+
7+
The primary key for this table is **stack_arn**.
8+
9+
## Relations
10+
11+
This table depends on [aws_cloudformation_stacks](aws_cloudformation_stacks).
12+
13+
## Columns
14+
15+
| Name | Type |
16+
| ------------- | ------------- |
17+
|_cq_source_name|String|
18+
|_cq_sync_time|Timestamp|
19+
|_cq_id|UUID|
20+
|_cq_parent_id|UUID|
21+
|account_id|String|
22+
|region|String|
23+
|stack_arn (PK)|String|
24+
|stages_available|StringArray|
25+
|template_body|String|

website/tables/aws/aws_cloudformation_stacks.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The primary key for this table is **arn**.
1010

1111
The following tables depend on aws_cloudformation_stacks:
1212
- [aws_cloudformation_stack_resources](aws_cloudformation_stack_resources)
13+
- [aws_cloudformation_stack_templates](aws_cloudformation_stack_templates)
1314

1415
## Columns
1516

0 commit comments

Comments
 (0)