Skip to content

Commit

Permalink
cloudfront/distribution: Clean up cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
YakDriver committed Sep 28, 2023
1 parent 78ff713 commit 080dad3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 55 deletions.
83 changes: 36 additions & 47 deletions internal/service/cloudfront/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ func resourceDistributionDelete(ctx context.Context, d *schema.ResourceData, met
return create.DiagError(names.CloudFront, create.ErrActionDeleting, ResNameDistribution, d.Id(), err)
}

if err := WaitDistributionDeployed(ctx, conn, d.Id()); err != nil {
if err := WaitDistributionDeployed(ctx, conn, d.Id()); err != nil && !tfresource.NotFound(err) {
return diag.Errorf("waiting until CloudFront Distribution (%s) is deployed: %s", d.Id(), err)
}
}
Expand Down Expand Up @@ -1022,7 +1022,7 @@ func resourceDistributionDelete(ctx context.Context, d *schema.ResourceData, met
if tfawserr.ErrCodeEquals(err, cloudfront.ErrCodePreconditionFailed, cloudfront.ErrCodeInvalidIfMatchVersion) {
_, err = tfresource.RetryWhenAWSErrCodeEquals(ctx, 1*time.Minute, func() (interface{}, error) {
return nil, deleteDistribution(ctx, conn, d.Id())
}, cloudfront.ErrCodePreconditionFailed)
}, cloudfront.ErrCodePreconditionFailed, cloudfront.ErrCodeInvalidIfMatchVersion)
}

if tfawserr.ErrCodeEquals(err, cloudfront.ErrCodeNoSuchDistribution) {
Expand Down Expand Up @@ -1059,7 +1059,7 @@ func deleteDistribution(ctx context.Context, conn *cloudfront.CloudFront, id str
return err
}

if err := WaitDistributionDeleted(ctx, conn, id); err != nil && !tfawserr.ErrCodeEquals(err, cloudfront.ErrCodeNoSuchDistribution) {
if err := WaitDistributionDeleted(ctx, conn, id); err != nil {
return err
}

Expand All @@ -1076,15 +1076,22 @@ func distroETag(ctx context.Context, conn *cloudfront.CloudFront, id string) (st
}

func disableDistribution(ctx context.Context, conn *cloudfront.CloudFront, id string) error {
if err := WaitDistributionDeployed(ctx, conn, id); err != nil {
return err
}

out, err := FindDistributionByID(ctx, conn, id)
if err != nil {
return err
}

if aws.StringValue(out.Distribution.Status) == "InProgress" {
if err := WaitDistributionDeployed(ctx, conn, id); err != nil {
return err
}

out, err = FindDistributionByID(ctx, conn, id)
if err != nil {
return err
}
}

if !aws.BoolValue(out.Distribution.DistributionConfig.Enabled) {
return nil
}
Expand Down Expand Up @@ -1133,44 +1140,17 @@ func FindDistributionByID(ctx context.Context, conn *cloudfront.CloudFront, id s
return output, nil
}

func FindDistributionByDomainName(ctx context.Context, conn *cloudfront.CloudFront, name string) (*cloudfront.DistributionSummary, error) {
var dist *cloudfront.DistributionSummary

input := &cloudfront.ListDistributionsInput{}

err := conn.ListDistributionsPagesWithContext(ctx, input, func(page *cloudfront.ListDistributionsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, d := range page.DistributionList.Items {
if d == nil {
continue
}

if aws.StringValue(d.DomainName) == name {
dist = d
return false
}
}

return !lastPage
})

return dist, err
}

// resourceAwsCloudFrontWebDistributionWaitUntilDeployed blocks until the
// distribution is deployed. It currently takes exactly 15 minutes to deploy
// but that might change in the future.
func WaitDistributionDeployed(ctx context.Context, conn *cloudfront.CloudFront, id string) error {
stateConf := &retry.StateChangeConf{
Pending: []string{"InProgress"},
Target: []string{"Deployed"},
Refresh: distributionRefreshFunc(ctx, conn, id),
Refresh: distributionDeployRefreshFunc(ctx, conn, id),
Timeout: 90 * time.Minute,
MinTimeout: 15 * time.Second,
Delay: 1 * time.Minute,
Delay: 30 * time.Second,
}

_, err := stateConf.WaitForStateContext(ctx)
Expand All @@ -1181,37 +1161,46 @@ func WaitDistributionDeleted(ctx context.Context, conn *cloudfront.CloudFront, i
stateConf := &retry.StateChangeConf{
Pending: []string{"InProgress", "Deployed"},
Target: []string{},
Refresh: distributionRefreshFunc(ctx, conn, id),
Refresh: distributionDeleteRefreshFunc(ctx, conn, id),
Timeout: 90 * time.Minute,
MinTimeout: 15 * time.Second,
Delay: 1 * time.Minute,
Delay: 15 * time.Second,
}

_, err := stateConf.WaitForStateContext(ctx)
return err
}

// The refresh function for resourceAwsCloudFrontWebDistributionWaitUntilDeployed.
func distributionRefreshFunc(ctx context.Context, conn *cloudfront.CloudFront, id string) retry.StateRefreshFunc {
func distributionDeleteRefreshFunc(ctx context.Context, conn *cloudfront.CloudFront, id string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
params := &cloudfront.GetDistributionInput{
Id: aws.String(id),
out, err := FindDistributionByID(ctx, conn, id)
if tfresource.NotFound(err) {
return nil, "", nil
}

resp, err := conn.GetDistributionWithContext(ctx, params)
if tfawserr.ErrCodeEquals(err, cloudfront.ErrCodeNoSuchDistribution) {
if err != nil {
return nil, "", err
}

if out == nil {
return nil, "", nil
}

return out.Distribution, aws.StringValue(out.Distribution.Status), nil
}
}

func distributionDeployRefreshFunc(ctx context.Context, conn *cloudfront.CloudFront, id string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
out, err := FindDistributionByID(ctx, conn, id)
if err != nil {
log.Printf("[WARN] Error retrieving CloudFront Distribution %q details: %s", id, err)
return nil, "", err
}

if resp == nil {
if out == nil {
return nil, "", nil
}

return resp.Distribution, *resp.Distribution.Status, nil
return out.Distribution, aws.StringValue(out.Distribution.Status), nil
}
}
20 changes: 12 additions & 8 deletions internal/service/cloudfront/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,23 +160,24 @@ func sweepCachePolicies(region string) error {
}

func sweepDistributions(region string) error {
// sweep:
var result *multierror.Error

// 1. Production Distributions
if err := sweepDistributionsByProductionStaging(region, false); err != nil {
log.Printf("[WARN] %s", err)
result = multierror.Append(result, err)
}

// 2. Continuous Deployment Policies
if err := sweepContinuousDeploymentPolicies(region); err != nil {
log.Printf("[WARN] %s", err)
result = multierror.Append(result, err)
}

// 3. Staging Distributions
if err := sweepDistributionsByProductionStaging(region, true); err != nil {
log.Printf("[WARN] %s", err)
result = multierror.Append(result, err)
}

return nil
return result.ErrorOrNil()
}

func sweepDistributionsByProductionStaging(region string, staging bool) error {
Expand Down Expand Up @@ -239,7 +240,6 @@ func sweepDistributionsByProductionStaging(region string, staging bool) error {
}

err = sweep.SweepOrchestrator(ctx, sweepResources)

if err != nil {
return fmt.Errorf("error sweeping CloudFront Distributions (%s): %w", region, err)
}
Expand All @@ -257,12 +257,14 @@ func sweepContinuousDeploymentPolicies(region string) error {
input := &cloudfront.ListContinuousDeploymentPoliciesInput{}

log.Printf("[INFO] Sweeping continuous deployment policies")
var result *multierror.Error

// ListContinuousDeploymentPolicies does not have a paginator
for {
output, err := conn.ListContinuousDeploymentPoliciesWithContext(ctx, input)
if err != nil {
log.Printf("[WARN] %s", err)
result = multierror.Append(result, err)
break
}

Expand All @@ -272,7 +274,9 @@ func sweepContinuousDeploymentPolicies(region string) error {
}

for _, cdp := range output.ContinuousDeploymentPolicyList.Items {
DeleteCDP(ctx, conn, aws.StringValue(cdp.ContinuousDeploymentPolicy.Id))
if err := DeleteCDP(ctx, conn, aws.StringValue(cdp.ContinuousDeploymentPolicy.Id)); err != nil {
result = multierror.Append(result, err)
}
}

if output.ContinuousDeploymentPolicyList.NextMarker == nil {
Expand All @@ -282,7 +286,7 @@ func sweepContinuousDeploymentPolicies(region string) error {
input.Marker = output.ContinuousDeploymentPolicyList.NextMarker
}

return nil
return result.ErrorOrNil()
}

func sweepFunctions(region string) error {
Expand Down

0 comments on commit 080dad3

Please sign in to comment.