Skip to content

Commit 102067a

Browse files
authored
feat(aws): Add support for Cloudfront Functions (#11669)
#### Summary Add support for cloudfront functions
1 parent f91a730 commit 102067a

File tree

6 files changed

+155
-0
lines changed

6 files changed

+155
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
- [aws_cloudformation_template_summaries](../../../../../website/tables/aws/aws_cloudformation_template_summaries.md)
106106
- [aws_cloudfront_cache_policies](../../../../../website/tables/aws/aws_cloudfront_cache_policies.md)
107107
- [aws_cloudfront_distributions](../../../../../website/tables/aws/aws_cloudfront_distributions.md)
108+
- [aws_cloudfront_functions](../../../../../website/tables/aws/aws_cloudfront_functions.md)
108109
- [aws_cloudhsmv2_backups](../../../../../website/tables/aws/aws_cloudhsmv2_backups.md)
109110
- [aws_cloudhsmv2_clusters](../../../../../website/tables/aws/aws_cloudhsmv2_clusters.md)
110111
- [aws_cloudtrail_events](../../../../../website/tables/aws/aws_cloudtrail_events.md) (Incremental)

plugins/source/aws/resources/plugin/tables.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ func tables() []*schema.Table {
160160
cloudformation.StackSets(),
161161
cloudfront.CachePolicies(),
162162
cloudfront.Distributions(),
163+
cloudfront.Functions(),
163164
cloudhsmv2.Backups(),
164165
cloudhsmv2.Clusters(),
165166
cloudtrail.Events(),
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package cloudfront
2+
3+
import (
4+
"context"
5+
6+
"github.com/apache/arrow/go/v13/arrow"
7+
"github.com/aws/aws-sdk-go-v2/aws"
8+
"github.com/aws/aws-sdk-go-v2/service/cloudfront"
9+
"github.com/aws/aws-sdk-go-v2/service/cloudfront/types"
10+
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
11+
"github.com/cloudquery/plugin-sdk/v3/schema"
12+
"github.com/cloudquery/plugin-sdk/v3/transformers"
13+
)
14+
15+
func Functions() *schema.Table {
16+
tableName := "aws_cloudfront_functions"
17+
return &schema.Table{
18+
Name: tableName,
19+
Description: `https://docs.aws.amazon.com/cloudfront/latest/APIReference/API_DescribeFunction.html`,
20+
Resolver: fetchFunctions,
21+
PreResourceResolver: getFunction,
22+
Multiplex: client.ServiceAccountRegionMultiplexer(tableName, "cloudfront"),
23+
Transform: transformers.TransformWithStruct(&cloudfront.DescribeFunctionOutput{}),
24+
Columns: []schema.Column{
25+
client.DefaultAccountIDColumn(false),
26+
{
27+
Name: "stage",
28+
Type: arrow.BinaryTypes.String,
29+
Resolver: schema.PathResolver("FunctionSummary.FunctionMetadata.Stage"),
30+
PrimaryKey: true,
31+
},
32+
{
33+
Name: "arn",
34+
Type: arrow.BinaryTypes.String,
35+
Resolver: schema.PathResolver("FunctionSummary.FunctionMetadata.FunctionARN"),
36+
PrimaryKey: true,
37+
},
38+
},
39+
}
40+
}
41+
42+
func fetchFunctions(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error {
43+
var config cloudfront.ListFunctionsInput
44+
cl := meta.(*client.Client)
45+
s := cl.Services()
46+
svc := s.Cloudfront
47+
for {
48+
response, err := svc.ListFunctions(ctx, &config, func(options *cloudfront.Options) {
49+
options.Region = cl.Region
50+
})
51+
if err != nil {
52+
return err
53+
}
54+
55+
if response.FunctionList != nil && len(response.FunctionList.Items) > 0 {
56+
res <- response.FunctionList.Items
57+
}
58+
59+
if aws.ToString(response.FunctionList.NextMarker) == "" {
60+
break
61+
}
62+
config.Marker = response.FunctionList.NextMarker
63+
}
64+
return nil
65+
}
66+
func getFunction(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource) error {
67+
cl := meta.(*client.Client)
68+
svc := cl.Services().Cloudfront
69+
70+
f := resource.Item.(types.FunctionSummary)
71+
72+
function, err := svc.DescribeFunction(ctx, &cloudfront.DescribeFunctionInput{
73+
Name: f.Name,
74+
Stage: f.FunctionMetadata.Stage,
75+
}, func(options *cloudfront.Options) {
76+
options.Region = cl.Region
77+
})
78+
if err != nil {
79+
return err
80+
}
81+
resource.Item = function
82+
return nil
83+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cloudfront
2+
3+
import (
4+
"testing"
5+
6+
"github.com/aws/aws-sdk-go-v2/service/cloudfront"
7+
cloudfrontTypes "github.com/aws/aws-sdk-go-v2/service/cloudfront/types"
8+
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
9+
"github.com/cloudquery/cloudquery/plugins/source/aws/client/mocks"
10+
"github.com/cloudquery/plugin-sdk/v3/faker"
11+
"github.com/golang/mock/gomock"
12+
)
13+
14+
func buildCloudfronFunctionsMock(t *testing.T, ctrl *gomock.Controller) client.Services {
15+
m := mocks.NewMockCloudfrontClient(ctrl)
16+
services := client.Services{
17+
Cloudfront: m,
18+
}
19+
fs := cloudfrontTypes.FunctionSummary{}
20+
if err := faker.FakeObject(&fs); err != nil {
21+
t.Fatal(err)
22+
}
23+
cloudfrontOutput := &cloudfront.ListFunctionsOutput{
24+
FunctionList: &cloudfrontTypes.FunctionList{
25+
Items: []cloudfrontTypes.FunctionSummary{fs},
26+
},
27+
}
28+
m.EXPECT().ListFunctions(gomock.Any(), gomock.Any(), gomock.Any()).Return(
29+
cloudfrontOutput,
30+
nil,
31+
)
32+
33+
function := &cloudfront.DescribeFunctionOutput{}
34+
if err := faker.FakeObject(&function); err != nil {
35+
t.Fatal(err)
36+
}
37+
m.EXPECT().DescribeFunction(gomock.Any(), gomock.Any(), gomock.Any()).Return(
38+
function,
39+
nil,
40+
)
41+
42+
return services
43+
}
44+
45+
func TestCloudfrontFunctions(t *testing.T) {
46+
client.AwsMockTestHelper(t, Functions(), buildCloudfronFunctionsMock, client.TestOptions{})
47+
}

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Table: aws_cloudfront_functions
2+
3+
This table shows data for Cloudfront Functions.
4+
5+
https://docs.aws.amazon.com/cloudfront/latest/APIReference/API_DescribeFunction.html
6+
7+
The composite primary key for this table is (**stage**, **arn**).
8+
9+
## Columns
10+
11+
| Name | Type |
12+
| ------------- | ------------- |
13+
|_cq_source_name|`utf8`|
14+
|_cq_sync_time|`timestamp[us, tz=UTC]`|
15+
|_cq_id|`uuid`|
16+
|_cq_parent_id|`uuid`|
17+
|account_id|`utf8`|
18+
|stage (PK)|`utf8`|
19+
|arn (PK)|`utf8`|
20+
|e_tag|`utf8`|
21+
|function_summary|`json`|
22+
|result_metadata|`json`|

0 commit comments

Comments
 (0)