-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource:
awsex_cloudfront_distribution_invalidations
(#12)
* Create reusable funcs from invalidation * Added awsex_cloudfront_distribution_invalidations * acc test * fix tests * docs * can have 0 distribution ids
- Loading branch information
Showing
10 changed files
with
498 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "awsex_cloudfront_distribution_invalidations Resource - awsex" | ||
subcategory: "" | ||
description: |- | ||
--- | ||
|
||
# awsex_cloudfront_distribution_invalidations (Resource) | ||
|
||
|
||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `distribution_ids` (Set of String) A list of Cloudfront Distribution IDs where an invalidation will be created. | ||
- `paths` (Set of String) A list of paths to invalidate. Each path *must* start with `/`. | ||
|
||
### Optional | ||
|
||
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts)) | ||
- `triggers` (Map of String) A map of triggers that, when changed, will force Terraform to create a new invalidation. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of the invalidations. | ||
- `statuses` (Map of String) The status of each invalidation indexed by the Cloudfront Distribution ID. | ||
|
||
<a id="nestedblock--timeouts"></a> | ||
### Nested Schema for `timeouts` | ||
|
||
Optional: | ||
|
||
- `create` (String) A string that can be [parsed as a duration](https://pkg.go.dev/time#ParseDuration) consisting of numbers and unit suffixes, such as "30s" or "2h45m". Valid time units are "s" (seconds), "m" (minutes), "h" (hours). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cloudfront | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/cloudfront" | ||
cftypes "github.com/aws/aws-sdk-go-v2/service/cloudfront/types" | ||
"github.com/google/uuid" | ||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/hashicorp/terraform-provider-awsex/internal/conns" | ||
"time" | ||
) | ||
|
||
func CreateInvalidation(ctx context.Context, client *conns.Client, distributionId string, paths []string, createTimeout time.Duration) (*cftypes.Invalidation, diag.Diagnostics) { | ||
var diags diag.Diagnostics | ||
|
||
input := &cloudfront.CreateInvalidationInput{ | ||
DistributionId: &distributionId, | ||
InvalidationBatch: &cftypes.InvalidationBatch{ | ||
CallerReference: aws.String(uuid.NewString()), | ||
Paths: &cftypes.Paths{ | ||
Quantity: aws.Int32(int32(len(paths))), | ||
Items: paths, | ||
}, | ||
}, | ||
} | ||
cfClient := client.Cloudfront() | ||
out, err := cfClient.CreateInvalidation(ctx, input) | ||
if err != nil { | ||
diags.AddError("Error creating AWS Cloudfront Invalidation", err.Error()) | ||
return nil, diags | ||
} | ||
if out != nil && out.Invalidation != nil && out.Invalidation.Id != nil { | ||
tflog.Trace(ctx, "Created Cloudfront Invalidation") | ||
} else { | ||
diags.AddWarning("Unable to create AWS Cloudfront Invalidation.", "AWS did not create an invalidation and gave no reason") | ||
return nil, diags | ||
} | ||
|
||
waiter := cloudfront.NewInvalidationCompletedWaiter(cfClient) | ||
res, err := waiter.WaitForOutput(ctx, &cloudfront.GetInvalidationInput{ | ||
DistributionId: aws.String(distributionId), | ||
Id: out.Invalidation.Id, | ||
}, createTimeout) | ||
if err != nil { | ||
diags.AddError("Error waiting for creation of AWS Cloudfront Invalidation", err.Error()) | ||
return out.Invalidation, diags | ||
} else if res != nil && res.Invalidation != nil { | ||
return res.Invalidation, diags | ||
} | ||
|
||
return out.Invalidation, diags | ||
} | ||
|
||
func FindInvalidation(ctx context.Context, client *conns.Client, distributionId string, id string) (*cftypes.Invalidation, diag.Diagnostics) { | ||
var diags diag.Diagnostics | ||
|
||
input := &cloudfront.GetInvalidationInput{ | ||
DistributionId: &distributionId, | ||
Id: &id, | ||
} | ||
out, err := client.Cloudfront().GetInvalidation(ctx, input) | ||
if err != nil { | ||
var nsi *cftypes.NoSuchInvalidation | ||
if !errors.As(err, &nsi) { | ||
diags.AddError("error getting AWS Invalidation", err.Error()) | ||
} | ||
} | ||
if out != nil { | ||
return out.Invalidation, diags | ||
} | ||
return nil, diags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cloudfront | ||
|
||
import ( | ||
"context" | ||
cftypes "github.com/aws/aws-sdk-go-v2/service/cloudfront/types" | ||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-provider-awsex/internal/conns" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type invalidationResult struct { | ||
DistributionId string | ||
Invalidation *cftypes.Invalidation | ||
Diags diag.Diagnostics | ||
} | ||
|
||
func CreateInvalidations(ctx context.Context, client *conns.Client, distributionIds []string, paths []string, | ||
createTimeout time.Duration) (map[string]*cftypes.Invalidation, diag.Diagnostics) { | ||
ch := make(chan invalidationResult, len(distributionIds)) | ||
|
||
var wg sync.WaitGroup | ||
for _, distributionId := range distributionIds { | ||
wg.Add(1) | ||
go func(distributionId string) { | ||
defer wg.Done() | ||
inval, diags := CreateInvalidation(ctx, client, distributionId, paths, createTimeout) | ||
ch <- invalidationResult{ | ||
DistributionId: distributionId, | ||
Invalidation: inval, | ||
Diags: diags, | ||
} | ||
|
||
}(distributionId) | ||
} | ||
|
||
go func() { | ||
wg.Wait() | ||
close(ch) | ||
}() | ||
|
||
results := make(map[string]*cftypes.Invalidation) | ||
var diags diag.Diagnostics | ||
for cur := range ch { | ||
results[cur.DistributionId] = cur.Invalidation | ||
diags.Append(cur.Diags...) | ||
} | ||
return results, diags | ||
} | ||
|
||
func FindInvalidations(ctx context.Context, client *conns.Client, ids map[string]string) (map[string]*cftypes.Invalidation, diag.Diagnostics) { | ||
ch := make(chan invalidationResult, len(ids)) | ||
|
||
var wg sync.WaitGroup | ||
for distributionId, id := range ids { | ||
wg.Add(1) | ||
go func(distributionId, id string) { | ||
defer wg.Done() | ||
inval, diags := FindInvalidation(ctx, client, distributionId, id) | ||
ch <- invalidationResult{ | ||
DistributionId: distributionId, | ||
Invalidation: inval, | ||
Diags: diags, | ||
} | ||
|
||
}(distributionId, id) | ||
} | ||
|
||
go func() { | ||
wg.Wait() | ||
close(ch) | ||
}() | ||
|
||
results := make(map[string]*cftypes.Invalidation) | ||
var diags diag.Diagnostics | ||
for cur := range ch { | ||
results[cur.DistributionId] = cur.Invalidation | ||
diags.Append(cur.Diags...) | ||
} | ||
return results, diags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.