|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package lakeformation |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/lakeformation" |
| 11 | + "github.com/aws/aws-sdk-go-v2/service/lakeformation/types" |
| 12 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 13 | + "github.com/hashicorp/terraform-provider-aws/internal/conns" |
| 14 | + "github.com/hashicorp/terraform-provider-aws/internal/sweep" |
| 15 | + "github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2" |
| 16 | +) |
| 17 | + |
| 18 | +func RegisterSweepers() { |
| 19 | + sweep.Register("aws_lakeformation_permissions", sweepPermissions) |
| 20 | + |
| 21 | + sweep.Register("aws_lakeformation_resource", sweepResource) |
| 22 | +} |
| 23 | + |
| 24 | +func sweepPermissions(ctx context.Context, client *conns.AWSClient) ([]sweep.Sweepable, error) { |
| 25 | + conn := client.LakeFormationClient(ctx) |
| 26 | + |
| 27 | + var sweepResources []sweep.Sweepable |
| 28 | + r := ResourcePermissions() |
| 29 | + |
| 30 | + pages := lakeformation.NewListPermissionsPaginator(conn, &lakeformation.ListPermissionsInput{}) |
| 31 | + for pages.HasMorePages() { |
| 32 | + page, err := pages.NextPage(ctx) |
| 33 | + |
| 34 | + if awsv2.SkipSweepError(err) { |
| 35 | + tflog.Warn(ctx, "Skipping sweeper", map[string]any{ |
| 36 | + "error": err.Error(), |
| 37 | + }) |
| 38 | + return nil, nil |
| 39 | + } |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + |
| 44 | + for _, v := range page.PrincipalResourcePermissions { |
| 45 | + d := r.Data(nil) |
| 46 | + |
| 47 | + d.Set("principal", v.Principal.DataLakePrincipalIdentifier) |
| 48 | + d.Set("permissions", flattenResourcePermissions([]types.PrincipalResourcePermissions{v})) |
| 49 | + d.Set("permissions_with_grant_option", flattenGrantPermissions([]types.PrincipalResourcePermissions{v})) |
| 50 | + |
| 51 | + d.Set("catalog_resource", v.Resource.Catalog != nil) |
| 52 | + |
| 53 | + if v.Resource.DataLocation != nil { |
| 54 | + d.Set("data_location", []any{flattenDataLocationResource(v.Resource.DataLocation)}) |
| 55 | + } |
| 56 | + |
| 57 | + if v.Resource.Database != nil { |
| 58 | + d.Set("database", []any{flattenDatabaseResource(v.Resource.Database)}) |
| 59 | + } |
| 60 | + |
| 61 | + if v.Resource.DataCellsFilter != nil { |
| 62 | + d.Set("data_cells_filter", flattenDataCellsFilter(v.Resource.DataCellsFilter)) |
| 63 | + } |
| 64 | + |
| 65 | + if v.Resource.LFTag != nil { |
| 66 | + d.Set("lf_tag", []any{flattenLFTagKeyResource(v.Resource.LFTag)}) |
| 67 | + } |
| 68 | + |
| 69 | + if v.Resource.LFTagPolicy != nil { |
| 70 | + d.Set("lf_tag_policy", []any{flattenLFTagPolicyResource(v.Resource.LFTagPolicy)}) |
| 71 | + } |
| 72 | + |
| 73 | + if v.Resource.Table != nil { |
| 74 | + d.Set("table", []any{flattenTableResource(v.Resource.Table)}) |
| 75 | + } |
| 76 | + |
| 77 | + if v.Resource.TableWithColumns != nil { |
| 78 | + d.Set("table_with_columns", []any{flattenTableColumnsResource(v.Resource.TableWithColumns)}) |
| 79 | + } |
| 80 | + |
| 81 | + sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return sweepResources, nil |
| 86 | +} |
| 87 | + |
| 88 | +func sweepResource(ctx context.Context, client *conns.AWSClient) ([]sweep.Sweepable, error) { |
| 89 | + conn := client.LakeFormationClient(ctx) |
| 90 | + |
| 91 | + var sweepResources []sweep.Sweepable |
| 92 | + r := ResourceResource() |
| 93 | + |
| 94 | + pages := lakeformation.NewListResourcesPaginator(conn, &lakeformation.ListResourcesInput{}) |
| 95 | + for pages.HasMorePages() { |
| 96 | + page, err := pages.NextPage(ctx) |
| 97 | + |
| 98 | + if awsv2.SkipSweepError(err) { |
| 99 | + tflog.Warn(ctx, "Skipping sweeper", map[string]any{ |
| 100 | + "error": err.Error(), |
| 101 | + }) |
| 102 | + return nil, nil |
| 103 | + } |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + |
| 108 | + for _, v := range page.ResourceInfoList { |
| 109 | + d := r.Data(nil) |
| 110 | + d.SetId(aws.ToString(v.ResourceArn)) |
| 111 | + |
| 112 | + sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return sweepResources, nil |
| 117 | +} |
0 commit comments