-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CloudFront Origin Request Policy #17342
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d7cee0e
Add cloudfront_origin_request_policy resource
bill-rich 22c43f9
wip
bill-rich dab088d
Add cloudfront_origin_request_policy data source
bill-rich 0e8e5c3
Fix cloudfront_distribution request policy tests
bill-rich 2201670
Fix formatting
bill-rich 0b986b4
Remove trailing whitespace
bill-rich 767e5ef
Address issues from origin_policy code review
bill-rich e11003e
Address feedback from cache policy review
bill-rich c387d87
Fix data source name lookup behavior
bill-rich 72bab54
Add IsNewResource check on read
bill-rich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,180 @@ | ||
package aws | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/cloudfront" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func expandCloudFrontOriginRequestPolicyCookieNames(tfMap map[string]interface{}) *cloudfront.CookieNames { | ||
if tfMap == nil { | ||
return nil | ||
} | ||
|
||
apiObject := &cloudfront.CookieNames{} | ||
|
||
var items []*string | ||
for _, item := range tfMap["items"].(*schema.Set).List() { | ||
items = append(items, aws.String(item.(string))) | ||
} | ||
apiObject.Items = items | ||
apiObject.Quantity = aws.Int64(int64(len(items))) | ||
|
||
return apiObject | ||
} | ||
|
||
func expandCloudFrontOriginRequestPolicyCookiesConfig(tfMap map[string]interface{}) *cloudfront.OriginRequestPolicyCookiesConfig { | ||
if tfMap == nil { | ||
return nil | ||
} | ||
|
||
apiObject := &cloudfront.OriginRequestPolicyCookiesConfig{ | ||
CookieBehavior: aws.String(tfMap["cookie_behavior"].(string)), | ||
} | ||
|
||
if items, ok := tfMap["cookies"].([]interface{}); ok && len(items) == 1 { | ||
apiObject.Cookies = expandCloudFrontOriginRequestPolicyCookieNames(items[0].(map[string]interface{})) | ||
} | ||
|
||
return apiObject | ||
} | ||
|
||
func expandCloudFrontOriginRequestPolicyHeaders(tfMap map[string]interface{}) *cloudfront.Headers { | ||
if tfMap == nil { | ||
return nil | ||
} | ||
|
||
var items []*string | ||
for _, item := range tfMap["items"].(*schema.Set).List() { | ||
items = append(items, aws.String(item.(string))) | ||
} | ||
|
||
apiObject := &cloudfront.Headers{ | ||
Items: items, | ||
Quantity: aws.Int64(int64(len(items))), | ||
} | ||
|
||
return apiObject | ||
} | ||
|
||
func expandCloudFrontOriginRequestPolicyHeadersConfig(tfMap map[string]interface{}) *cloudfront.OriginRequestPolicyHeadersConfig { | ||
if tfMap == nil { | ||
return nil | ||
} | ||
|
||
apiObject := &cloudfront.OriginRequestPolicyHeadersConfig{ | ||
HeaderBehavior: aws.String(tfMap["header_behavior"].(string)), | ||
} | ||
|
||
if items, ok := tfMap["headers"].([]interface{}); ok && len(items) == 1 && tfMap["header_behavior"] != "none" { | ||
apiObject.Headers = expandCloudFrontOriginRequestPolicyHeaders(items[0].(map[string]interface{})) | ||
} | ||
|
||
return apiObject | ||
} | ||
|
||
func expandCloudFrontOriginRequestPolicyQueryStringNames(tfMap map[string]interface{}) *cloudfront.QueryStringNames { | ||
if tfMap == nil { | ||
return nil | ||
} | ||
|
||
var items []*string | ||
for _, item := range tfMap["items"].(*schema.Set).List() { | ||
items = append(items, aws.String(item.(string))) | ||
} | ||
|
||
apiObject := &cloudfront.QueryStringNames{ | ||
Items: items, | ||
Quantity: aws.Int64(int64(len(items))), | ||
} | ||
|
||
return apiObject | ||
} | ||
|
||
func expandCloudFrontOriginRequestPolicyQueryStringsConfig(tfMap map[string]interface{}) *cloudfront.OriginRequestPolicyQueryStringsConfig { | ||
if tfMap == nil { | ||
return nil | ||
} | ||
|
||
apiObject := &cloudfront.OriginRequestPolicyQueryStringsConfig{ | ||
QueryStringBehavior: aws.String(tfMap["query_string_behavior"].(string)), | ||
} | ||
|
||
if items, ok := tfMap["query_strings"].([]interface{}); ok && len(items) == 1 { | ||
apiObject.QueryStrings = expandCloudFrontOriginRequestPolicyQueryStringNames(items[0].(map[string]interface{})) | ||
} | ||
|
||
return apiObject | ||
} | ||
|
||
func expandCloudFrontOriginRequestPolicyConfig(d *schema.ResourceData) *cloudfront.OriginRequestPolicyConfig { | ||
apiObject := &cloudfront.OriginRequestPolicyConfig{ | ||
Comment: aws.String(d.Get("comment").(string)), | ||
Name: aws.String(d.Get("name").(string)), | ||
CookiesConfig: expandCloudFrontOriginRequestPolicyCookiesConfig(d.Get("cookies_config").([]interface{})[0].(map[string]interface{})), | ||
HeadersConfig: expandCloudFrontOriginRequestPolicyHeadersConfig(d.Get("headers_config").([]interface{})[0].(map[string]interface{})), | ||
QueryStringsConfig: expandCloudFrontOriginRequestPolicyQueryStringsConfig(d.Get("query_strings_config").([]interface{})[0].(map[string]interface{})), | ||
} | ||
|
||
return apiObject | ||
} | ||
|
||
func flattenCloudFrontOriginRequestPolicyCookiesConfig(cookiesConfig *cloudfront.OriginRequestPolicyCookiesConfig) []map[string]interface{} { | ||
cookiesConfigFlat := map[string]interface{}{} | ||
|
||
cookies := []map[string]interface{}{} | ||
if cookiesConfig.Cookies != nil { | ||
cookies = []map[string]interface{}{ | ||
{ | ||
"items": cookiesConfig.Cookies.Items, | ||
}, | ||
} | ||
} | ||
|
||
cookiesConfigFlat["cookie_behavior"] = aws.StringValue(cookiesConfig.CookieBehavior) | ||
cookiesConfigFlat["cookies"] = cookies | ||
|
||
return []map[string]interface{}{ | ||
cookiesConfigFlat, | ||
} | ||
} | ||
|
||
func flattenCloudFrontOriginRequestPolicyHeadersConfig(headersConfig *cloudfront.OriginRequestPolicyHeadersConfig) []map[string]interface{} { | ||
headersConfigFlat := map[string]interface{}{} | ||
|
||
headers := []map[string]interface{}{} | ||
if headersConfig.Headers != nil { | ||
headers = []map[string]interface{}{ | ||
{ | ||
"items": headersConfig.Headers.Items, | ||
}, | ||
} | ||
} | ||
|
||
headersConfigFlat["header_behavior"] = aws.StringValue(headersConfig.HeaderBehavior) | ||
headersConfigFlat["headers"] = headers | ||
|
||
return []map[string]interface{}{ | ||
headersConfigFlat, | ||
} | ||
} | ||
|
||
func flattenCloudFrontOriginRequestPolicyQueryStringsConfig(queryStringsConfig *cloudfront.OriginRequestPolicyQueryStringsConfig) []map[string]interface{} { | ||
queryStringsConfigFlat := map[string]interface{}{} | ||
|
||
queryStrings := []map[string]interface{}{} | ||
if queryStringsConfig.QueryStrings != nil { | ||
queryStrings = []map[string]interface{}{ | ||
{ | ||
"items": queryStringsConfig.QueryStrings.Items, | ||
}, | ||
} | ||
} | ||
|
||
queryStringsConfigFlat["query_string_behavior"] = aws.StringValue(queryStringsConfig.QueryStringBehavior) | ||
queryStringsConfigFlat["query_strings"] = queryStrings | ||
|
||
return []map[string]interface{}{ | ||
queryStringsConfigFlat, | ||
} | ||
} |
161 changes: 161 additions & 0 deletions
161
aws/data_source_aws_cloudfront_origin_request_policy.go
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,161 @@ | ||||||
package aws | ||||||
|
||||||
import ( | ||||||
"fmt" | ||||||
|
||||||
"github.com/aws/aws-sdk-go/aws" | ||||||
"github.com/aws/aws-sdk-go/service/cloudfront" | ||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||||||
) | ||||||
|
||||||
func dataSourceAwsCloudFrontOriginRequestPolicy() *schema.Resource { | ||||||
return &schema.Resource{ | ||||||
Read: dataSourceAwsCloudFrontOriginRequestPolicyRead, | ||||||
|
||||||
Schema: map[string]*schema.Schema{ | ||||||
"comment": { | ||||||
Type: schema.TypeString, | ||||||
Computed: true, | ||||||
}, | ||||||
"cookies_config": { | ||||||
Type: schema.TypeList, | ||||||
Computed: true, | ||||||
Elem: &schema.Resource{ | ||||||
Schema: map[string]*schema.Schema{ | ||||||
"cookie_behavior": { | ||||||
Computed: true, | ||||||
Type: schema.TypeString, | ||||||
}, | ||||||
"cookies": { | ||||||
Type: schema.TypeList, | ||||||
Computed: true, | ||||||
Elem: &schema.Resource{ | ||||||
Schema: map[string]*schema.Schema{ | ||||||
"items": { | ||||||
Type: schema.TypeSet, | ||||||
Computed: true, | ||||||
Elem: &schema.Schema{Type: schema.TypeString}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
"etag": { | ||||||
Type: schema.TypeString, | ||||||
Computed: true, | ||||||
}, | ||||||
"headers_config": { | ||||||
Type: schema.TypeList, | ||||||
Computed: true, | ||||||
Elem: &schema.Resource{ | ||||||
Schema: map[string]*schema.Schema{ | ||||||
"header_behavior": { | ||||||
Computed: true, | ||||||
Type: schema.TypeString, | ||||||
}, | ||||||
"headers": { | ||||||
Type: schema.TypeList, | ||||||
Computed: true, | ||||||
Elem: &schema.Resource{ | ||||||
Schema: map[string]*schema.Schema{ | ||||||
"items": { | ||||||
Type: schema.TypeSet, | ||||||
Computed: true, | ||||||
Elem: &schema.Schema{Type: schema.TypeString}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
"id": { | ||||||
Type: schema.TypeString, | ||||||
Optional: true, | ||||||
}, | ||||||
"name": { | ||||||
Type: schema.TypeString, | ||||||
Optional: true, | ||||||
}, | ||||||
"query_strings_config": { | ||||||
Type: schema.TypeList, | ||||||
Computed: true, | ||||||
Elem: &schema.Resource{ | ||||||
Schema: map[string]*schema.Schema{ | ||||||
"query_string_behavior": { | ||||||
Type: schema.TypeString, | ||||||
Computed: true, | ||||||
}, | ||||||
"query_strings": { | ||||||
Type: schema.TypeList, | ||||||
Computed: true, | ||||||
Elem: &schema.Resource{ | ||||||
Schema: map[string]*schema.Schema{ | ||||||
"items": { | ||||||
Type: schema.TypeSet, | ||||||
Computed: true, | ||||||
Elem: &schema.Schema{Type: schema.TypeString}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
} | ||||||
} | ||||||
|
||||||
func dataSourceAwsCloudFrontOriginRequestPolicyRead(d *schema.ResourceData, meta interface{}) error { | ||||||
conn := meta.(*AWSClient).cloudfrontconn | ||||||
|
||||||
if d.Get("id").(string) == "" { | ||||||
if err := dataSourceAwsCloudFrontOriginRequestPolicyFindByName(d, conn); err != nil { | ||||||
return fmt.Errorf("Unable to find origin request policy by name: %s", err.Error()) | ||||||
} | ||||||
} | ||||||
|
||||||
if d.Id() != "" { | ||||||
request := &cloudfront.GetOriginRequestPolicyInput{ | ||||||
Id: aws.String(d.Id()), | ||||||
} | ||||||
|
||||||
resp, err := conn.GetOriginRequestPolicy(request) | ||||||
if err != nil { | ||||||
return fmt.Errorf("Unable to retrieve origin request policy with ID %s: %s", d.Id(), err.Error()) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
d.Set("etag", aws.StringValue(resp.ETag)) | ||||||
|
||||||
originRequestPolicy := *resp.OriginRequestPolicy.OriginRequestPolicyConfig | ||||||
d.Set("comment", aws.StringValue(originRequestPolicy.Comment)) | ||||||
d.Set("name", aws.StringValue(originRequestPolicy.Name)) | ||||||
d.Set("cookies_config", flattenCloudFrontOriginRequestPolicyCookiesConfig(originRequestPolicy.CookiesConfig)) | ||||||
d.Set("headers_config", flattenCloudFrontOriginRequestPolicyHeadersConfig(originRequestPolicy.HeadersConfig)) | ||||||
d.Set("query_strings_config", flattenCloudFrontOriginRequestPolicyQueryStringsConfig(originRequestPolicy.QueryStringsConfig)) | ||||||
} | ||||||
|
||||||
return nil | ||||||
} | ||||||
|
||||||
func dataSourceAwsCloudFrontOriginRequestPolicyFindByName(d *schema.ResourceData, conn *cloudfront.CloudFront) error { | ||||||
var originRequestPolicy *cloudfront.OriginRequestPolicy | ||||||
request := &cloudfront.ListOriginRequestPoliciesInput{} | ||||||
resp, err := conn.ListOriginRequestPolicies(request) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
for _, policySummary := range resp.OriginRequestPolicyList.Items { | ||||||
if *policySummary.OriginRequestPolicy.OriginRequestPolicyConfig.Name == d.Get("name").(string) { | ||||||
originRequestPolicy = policySummary.OriginRequestPolicy | ||||||
break | ||||||
} | ||||||
} | ||||||
|
||||||
if originRequestPolicy != nil { | ||||||
d.SetId(aws.StringValue(originRequestPolicy.Id)) | ||||||
} | ||||||
return nil | ||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.