Skip to content

Commit

Permalink
fix(cloudfront-distribution): use correct etag value
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Feb 7, 2025
1 parent ef061ac commit b41bdc7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions resources/cloudfront-distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"time"

"github.com/gotidy/ptr"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudfront"
rtypes "github.com/aws/aws-sdk-go-v2/service/cloudfront/types"
Expand Down Expand Up @@ -94,29 +96,31 @@ func (r *CloudFrontDistribution) Properties() types.Properties {
}

func (r *CloudFrontDistribution) Remove(ctx context.Context) error {
// Get Existing eTag
var etag *string
resp, err := r.svc.GetDistributionConfig(ctx, &cloudfront.GetDistributionConfigInput{
Id: r.ID,
})
if err != nil {
return err
}
etag = resp.ETag

if *resp.DistributionConfig.Enabled {
*resp.DistributionConfig.Enabled = false
_, err := r.svc.UpdateDistribution(ctx, &cloudfront.UpdateDistributionInput{
if ptr.ToBool(resp.DistributionConfig.Enabled) {
resp.DistributionConfig.Enabled = ptr.Bool(false)
upResp, err := r.svc.UpdateDistribution(ctx, &cloudfront.UpdateDistributionInput{
Id: r.ID,
DistributionConfig: resp.DistributionConfig,
IfMatch: resp.ETag,
})
if err != nil {
return err
}
etag = upResp.ETag
}

_, err = r.svc.DeleteDistribution(ctx, &cloudfront.DeleteDistributionInput{
Id: r.ID,
IfMatch: resp.ETag,
IfMatch: etag,
})

return err
Expand Down
4 changes: 2 additions & 2 deletions resources/cloudfront-distribution_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func Test_CloudFrontDistribution_RemoveGone(t *testing.T) {
mockSvc.AssertExpectations(t)
}

func Test_CloudFrontDistribution_RemoveWrongETag(t *testing.T) {
func Test_CloudFrontDistribution_RemoveUpdateETag(t *testing.T) {
mockSvc := new(mockCloudFrontClient)
mockSvc.On("GetDistributionConfig", mock.Anything, mock.Anything).Return(&cloudfront.GetDistributionConfigOutput{
DistributionConfig: &types.DistributionConfig{Enabled: ptr.Bool(true)},
Expand All @@ -109,7 +109,7 @@ func Test_CloudFrontDistribution_RemoveWrongETag(t *testing.T) {
}, nil)
mockSvc.On("DeleteDistribution", mock.Anything, &cloudfront.DeleteDistributionInput{
Id: ptr.String("test-id"),
IfMatch: ptr.String("test-etag1"),
IfMatch: ptr.String("test-etag2"),
}).Return(&cloudfront.DeleteDistributionOutput{}, nil)

// Bug where etag was wrong so nothing was deleted
Expand Down

0 comments on commit b41bdc7

Please sign in to comment.