Skip to content
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

azurerm_cdn_frontdoor_rule - allow cache_duration to be null if cache_behavior is set to HonorOrigin #19378

Merged
merged 2 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions internal/services/cdn/cdn_frontdoor_rule_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,21 @@ func TestAccCdnFrontDoorRule_multipleQueryStringParametersError(t *testing.T) {
})
}

func TestAccCdnFrontDoorRule_honorOrigin(t *testing.T) {
// NOTE: Regression test case for issue #19311
data := acceptance.BuildTestData(t, "azurerm_cdn_frontdoor_rule", "test")
r := CdnFrontDoorRuleResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.honorOrigin(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (r CdnFrontDoorRuleResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.FrontDoorRuleID(state.ID)
if err != nil {
Expand Down Expand Up @@ -1062,3 +1077,45 @@ resource "azurerm_cdn_frontdoor_rule" "test" {
}
`, template, data.RandomInteger)
}

func (r CdnFrontDoorRuleResource) honorOrigin(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

%s

resource "azurerm_cdn_frontdoor_rule" "test" {
depends_on = [azurerm_cdn_frontdoor_origin_group.test, azurerm_cdn_frontdoor_origin.test]

name = "accTestRule%d"
cdn_frontdoor_rule_set_id = azurerm_cdn_frontdoor_rule_set.test.id

order = 0

actions {
route_configuration_override_action {
cache_behavior = "HonorOrigin"
compression_enabled = true
query_string_caching_behavior = "IgnoreQueryString"
}
}

conditions {
url_path_condition {
match_values = ["data/", ]
negate_condition = false
operator = "BeginsWith"
}

url_path_condition {
match_values = [".html", ".htm"]
negate_condition = false
operator = "EndsWith"
}
}
}
`, template, data.RandomInteger)
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,13 @@ func ExpandCdnFrontDoorRouteConfigurationOverrideAction(input []interface{}) (*[

var originGroupOverride cdn.OriginGroupOverride
var cacheConfiguration cdn.CacheConfiguration

originGroupIdRaw := item["cdn_frontdoor_origin_group_id"].(string)
protocol := item["forwarding_protocol"].(string)
cacheBehavior := item["cache_behavior"].(string)
compressionEnabled := cdn.RuleIsCompressionEnabledEnabled
queryStringCachingBehavior := item["query_string_caching_behavior"].(string)
cacheDuration := item["cache_duration"].(string)

if !item["compression_enabled"].(bool) {
compressionEnabled = cdn.RuleIsCompressionEnabledDisabled
Expand Down Expand Up @@ -250,59 +253,55 @@ func ExpandCdnFrontDoorRouteConfigurationOverrideAction(input []interface{}) (*[
}

if cacheBehavior == string(cdn.RuleIsCompressionEnabledDisabled) {
if queryStringCachingBehavior := cdn.RuleQueryStringCachingBehavior(item["query_string_caching_behavior"].(string)); queryStringCachingBehavior != "" {
if queryStringCachingBehavior != "" {
return nil, fmt.Errorf("the 'route_configuration_override_action' block is not valid, if the 'cache_behavior' is set to 'Disabled' you cannot define the 'query_string_caching_behavior', got %q", queryStringCachingBehavior)
}

if queryParameters := item["query_string_parameters"].([]interface{}); len(queryParameters) != 0 {
return nil, fmt.Errorf("the 'route_configuration_override_action' block is not valid, if the 'cache_behavior' is set to 'Disabled' you cannot define the 'query_string_parameters', got %d", len(queryParameters))
}

if cacheDuration := item["cache_duration"].(string); cacheDuration != "" {
if cacheDuration != "" {
return nil, fmt.Errorf("the 'route_configuration_override_action' block is not valid, if the 'cache_behavior' is set to 'Disabled' you cannot define the 'cache_duration', got %q", cacheDuration)
}
} else {
var cacheDuration string
var queryStringCachingBehavior string
var cacheBehavior string

if !features.FourPointOhBeta() {
// since 'cache_duration', 'query_string_caching_behavior' and 'cache_behavior' are optional create a default values
// for those values if not set.
cacheDuration = "1.12:00:00"
queryStringCachingBehavior = string(cdn.RuleQueryStringCachingBehaviorIgnoreQueryString)
cacheBehavior = string(cdn.RuleCacheBehaviorHonorOrigin)

if cacheBehaviorRaw := item["cache_behavior"].(string); cacheBehaviorRaw != "" {
cacheBehavior = cacheBehaviorRaw
if cacheBehavior == "" {
cacheBehavior = string(cdn.RuleCacheBehaviorHonorOrigin)
}

if cacheDurationRaw := item["cache_duration"].(string); cacheDurationRaw != "" {
cacheDuration = cacheDurationRaw
if queryStringCachingBehavior == "" {
queryStringCachingBehavior = string(cdn.RuleQueryStringCachingBehaviorIgnoreQueryString)
}

if queryStringCachingBehaviorRaw := item["query_string_caching_behavior"].(string); queryStringCachingBehaviorRaw != "" {
queryStringCachingBehavior = queryStringCachingBehaviorRaw
// NOTE: if the cacheBehavior is 'HonorOrigin' the cacheDuration must be null, issue #19311
if cacheBehavior != string(cdn.RuleCacheBehaviorHonorOrigin) {
if cacheDuration == "" {
cacheDuration = "1.12:00:00"
}
} else if cacheDuration != "" {
return nil, fmt.Errorf("the 'route_configuration_override_action' block is not valid, if the 'cache_behavior' field is set to 'HonorOrigin' the 'cache_duration' must not be set")
}
}

if features.FourPointOhBeta() {
if cacheBehaviorRaw := item["cache_behavior"].(string); cacheBehaviorRaw == "" {
if cacheBehavior == "" {
return nil, fmt.Errorf("the 'route_configuration_override_action' block is not valid, the 'cache_behavior' field must be set")
} else {
cacheBehavior = cacheBehaviorRaw
}

if cacheDurationRaw := item["cache_duration"].(string); cacheDurationRaw == "" {
return nil, fmt.Errorf("the 'route_configuration_override_action' block is not valid, the 'cache_duration' field must be set")
} else {
cacheDuration = cacheDurationRaw
if queryStringCachingBehavior == "" {
return nil, fmt.Errorf("the 'route_configuration_override_action' block is not valid, the 'query_string_caching_behavior' field must be set")
}

if queryStringCachingBehaviorRaw := item["query_string_caching_behavior"].(string); queryStringCachingBehaviorRaw == "" {
return nil, fmt.Errorf("the 'route_configuration_override_action' block is not valid, the 'query_string_caching_behavior' field must be set")
} else {
queryStringCachingBehavior = queryStringCachingBehaviorRaw
// NOTE: if the cacheBehavior is 'HonorOrigin' cacheDuration must be null, issue #19311
if cacheBehavior != string(cdn.RuleCacheBehaviorHonorOrigin) {
if cacheDuration == "" {
return nil, fmt.Errorf("the 'route_configuration_override_action' block is not valid, the 'cache_duration' field must be set")
}
} else if cacheDuration != "" {
return nil, fmt.Errorf("the 'route_configuration_override_action' block is not valid, the 'cache_duration' field must not be set if the 'cache_behavior' is 'HonorOrigin'")
}
}

Expand All @@ -311,7 +310,10 @@ func ExpandCdnFrontDoorRouteConfigurationOverrideAction(input []interface{}) (*[
QueryParameters: expandStringSliceToCsvFormat(item["query_string_parameters"].([]interface{})),
IsCompressionEnabled: compressionEnabled,
CacheBehavior: cdn.RuleCacheBehavior(cacheBehavior),
CacheDuration: utils.String(cacheDuration),
}

if cacheDuration != "" {
cacheConfiguration.CacheDuration = utils.String(cacheDuration)
}

if queryParameters := cacheConfiguration.QueryParameters; queryParameters == nil {
Expand All @@ -335,7 +337,7 @@ func ExpandCdnFrontDoorRouteConfigurationOverrideAction(input []interface{}) (*[
routeConfigurationOverrideAction.Parameters.OriginGroupOverride = &originGroupOverride
}

if cacheConfiguration.CacheDuration != nil {
if cacheConfiguration.CacheDuration != nil || cacheConfiguration.CacheBehavior == cdn.RuleCacheBehaviorHonorOrigin {
routeConfigurationOverrideAction.Parameters.CacheConfiguration = &cacheConfiguration
}

Expand Down Expand Up @@ -439,8 +441,11 @@ func FlattenCdnFrontDoorRouteConfigurationOverrideAction(input cdn.DeliveryRuleR
queryStringCachingBehavior = string(config.QueryStringCachingBehavior)
cacheBehavior = string(config.CacheBehavior)
compressionEnabled = config.IsCompressionEnabled == cdn.RuleIsCompressionEnabledEnabled
cacheDuration = *config.CacheDuration
queryParameters = flattenCsvToStringSlice(config.QueryParameters)

if config.CacheDuration != nil {
cacheDuration = *config.CacheDuration
}
} else {
cacheBehavior = string(cdn.RuleIsCompressionEnabledDisabled)
}
Expand Down