Skip to content

Commit

Permalink
r/aws_cloudfront_origin_access_control: add sweeper
Browse files Browse the repository at this point in the history
  • Loading branch information
roberth-k committed Aug 27, 2022
1 parent 3c384af commit 0a973dc
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
17 changes: 17 additions & 0 deletions internal/service/cloudfront/list.go
Expand Up @@ -108,3 +108,20 @@ func ListResponseHeadersPoliciesPages(conn *cloudfront.CloudFront, input *cloudf
}
return nil
}

func ListOriginAccessControlsPages(conn *cloudfront.CloudFront, input *cloudfront.ListOriginAccessControlsInput, fn func(*cloudfront.ListOriginAccessControlsOutput, bool) bool) error {
for {
output, err := conn.ListOriginAccessControls(input)
if err != nil {
return err
}

lastPage := aws.StringValue(output.OriginAccessControlList.NextMarker) == ""
if !fn(output, lastPage) || lastPage {
break
}

input.Marker = output.OriginAccessControlList.NextMarker
}
return nil
}
66 changes: 66 additions & 0 deletions internal/service/cloudfront/sweep.go
Expand Up @@ -4,6 +4,7 @@
package cloudfront

import (
"context"
"fmt"
"log"

Expand Down Expand Up @@ -58,6 +59,14 @@ func init() {
F: sweepMonitoringSubscriptions,
})

resource.AddTestSweepers("aws_cloudfront_origin_access_control", &resource.Sweeper{
Name: "aws_cloudfront_origin_access_control",
F: sweepOriginAccessControls,
Dependencies: []string{
"aws_cloudfront_distribution",
},
})

resource.AddTestSweepers("aws_cloudfront_origin_request_policy", &resource.Sweeper{
Name: "aws_cloudfront_origin_request_policy",
F: sweepOriginRequestPolicies,
Expand Down Expand Up @@ -640,3 +649,60 @@ func sweepResponseHeadersPolicies(region string) error {

return nil
}

func sweepOriginAccessControls(region string) error {
client, err := sweep.SharedRegionalSweepClient(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
conn := client.(*conns.AWSClient).CloudFrontConn
input := &cloudfront.ListOriginAccessControlsInput{}
sweepResources := make([]*sweep.SweepResource, 0)

err = ListOriginAccessControlsPages(conn, input, func(page *cloudfront.ListOriginAccessControlsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, v := range page.OriginAccessControlList.Items {
id := aws.StringValue(v.Id)

output, err := findOriginAccessControlByID(context.Background(), conn, id)

if tfresource.NotFound(err) {
continue
}

if err != nil {
log.Printf("[WARN] %s", err)
continue
}

r := ResourceOriginAccessControl()
d := r.Data(nil)
d.SetId(id)
d.Set("etag", output.ETag)

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}

return !lastPage
})

if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping CloudFront Origin Access Control sweep for %s: %s", region, err)
return nil
}

if err != nil {
return fmt.Errorf("error listing CloudFront Origin Access Controls (%s): %w", region, err)
}

err = sweep.SweepOrchestrator(sweepResources)

if err != nil {
return fmt.Errorf("error sweeping CloudFront Origin Access Controls (%s): %w", region, err)
}

return nil
}

0 comments on commit 0a973dc

Please sign in to comment.