From 833b9c2d1c7be30f33888eca64fa08a8b0498ed7 Mon Sep 17 00:00:00 2001 From: Chuan-Yen Chiang Date: Fri, 20 Oct 2023 20:23:49 +0200 Subject: [PATCH] feat: Add `policy` column to `aws_efs_filesystems` table (#14672) #### Summary resolves cloudquery/cloudquery#14632 I'm looking for a way to reduce the duplicate implementations, for example, the following part is a way to get a service to resolve data for the table. It works well when a table relays on a single resolver. But if a table needs multiple resolver then might be a good way to do it. ```go p := resource.Item.(types.FileSystemDescription) config := efs.DescribeFileSystemPolicyInput{ FileSystemId: p.FileSystemId, } cl := meta.(*client.Client) svc := cl.Services(client.AWSServiceEfs).Efs ``` --- .../aws/resources/services/efs/filesystems.go | 32 +++++++++++++++++-- .../services/efs/filesystems_mock_test.go | 5 +++ website/tables/aws/aws_efs_filesystems.md | 1 + 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/plugins/source/aws/resources/services/efs/filesystems.go b/plugins/source/aws/resources/services/efs/filesystems.go index 11788be9537fd1..c465f4cd85ffe9 100644 --- a/plugins/source/aws/resources/services/efs/filesystems.go +++ b/plugins/source/aws/resources/services/efs/filesystems.go @@ -32,7 +32,12 @@ func Filesystems() *schema.Table { { Name: "backup_policy_status", Type: arrow.BinaryTypes.String, - Resolver: ResolveEfsFilesystemBackupPolicyStatus, + Resolver: resolveEfsFilesystemBackupPolicyStatus, + }, + { + Name: "file_system_policy", + Type: arrow.BinaryTypes.String, + Resolver: resolveEfsFilesystemPolicy, }, { Name: "tags", @@ -60,7 +65,7 @@ func fetchEfsFilesystems(ctx context.Context, meta schema.ClientMeta, parent *sc return nil } -func ResolveEfsFilesystemBackupPolicyStatus(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error { +func resolveEfsFilesystemBackupPolicyStatus(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error { p := resource.Item.(types.FileSystemDescription) config := efs.DescribeBackupPolicyInput{ FileSystemId: p.FileSystemId, @@ -82,3 +87,26 @@ func ResolveEfsFilesystemBackupPolicyStatus(ctx context.Context, meta schema.Cli return resource.Set(c.Name, response.BackupPolicy.Status) } + +func resolveEfsFilesystemPolicy(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error { + p := resource.Item.(types.FileSystemDescription) + config := efs.DescribeFileSystemPolicyInput{ + FileSystemId: p.FileSystemId, + } + cl := meta.(*client.Client) + svc := cl.Services(client.AWSServiceEfs).Efs + response, err := svc.DescribeFileSystemPolicy(ctx, &config, func(options *efs.Options) { + options.Region = cl.Region + }) + if err != nil { + if cl.IsNotFoundError(err) { + return nil + } + return err + } + if response.Policy == nil { + return nil + } + + return resource.Set(c.Name, response.Policy) +} diff --git a/plugins/source/aws/resources/services/efs/filesystems_mock_test.go b/plugins/source/aws/resources/services/efs/filesystems_mock_test.go index 89478864363f9f..11076b8a3ba716 100644 --- a/plugins/source/aws/resources/services/efs/filesystems_mock_test.go +++ b/plugins/source/aws/resources/services/efs/filesystems_mock_test.go @@ -28,6 +28,11 @@ func buildEfsFilesystemsMock(t *testing.T, ctrl *gomock.Controller) client.Servi m.EXPECT().DescribeBackupPolicy(gomock.Any(), gomock.Any(), gomock.Any()).Return( &b, nil) + p := efs.DescribeFileSystemPolicyOutput{} + require.NoError(t, faker.FakeObject(&p)) + m.EXPECT().DescribeFileSystemPolicy(gomock.Any(), gomock.Any(), gomock.Any()).Return( + &p, nil) + return client.Services{ Efs: m, } diff --git a/website/tables/aws/aws_efs_filesystems.md b/website/tables/aws/aws_efs_filesystems.md index 00620afb433a62..e7d1dcb6621ac6 100644 --- a/website/tables/aws/aws_efs_filesystems.md +++ b/website/tables/aws/aws_efs_filesystems.md @@ -16,6 +16,7 @@ The primary key for this table is **arn**. |region|`utf8`| |arn (PK)|`utf8`| |backup_policy_status|`utf8`| +|file_system_policy|`utf8`| |tags|`json`| |creation_time|`timestamp[us, tz=UTC]`| |creation_token|`utf8`|