Skip to content

Commit

Permalink
Bug: azurerm_frontdoor fix for caching issue (#5358)
Browse files Browse the repository at this point in the history
* Port PR to new code base

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Update website/docs/r/front_door.html.markdown

Co-Authored-By: Tom Harvey <[email protected]>

* Progress

* Saving progress

* Almost right

* Cache working

* Fully working without defaults

* Update docs to document new behavior

* Fix tests and documentation

* Add test case for cache regression

* Fix test code terrafmt

* Fixed linting error in comment

* pull value through test

* Update docs and remove commented code

* Update test case

* terrafmt

* Minor doc update

* Added nil check per PR review

Co-authored-by: Tom Harvey <[email protected]>
Co-authored-by: kt <[email protected]>
  • Loading branch information
3 people authored Jan 25, 2020
1 parent bab8456 commit d42f493
Show file tree
Hide file tree
Showing 5 changed files with 368 additions and 78 deletions.
107 changes: 84 additions & 23 deletions azurerm/internal/services/frontdoor/resource_arm_front_door.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ func resourceArmFrontDoor() *schema.Resource {
Required: true,
ValidateFunc: ValidateBackendPoolRoutingRuleName,
},
"cache_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"cache_use_dynamic_compression": {
Type: schema.TypeBool,
Optional: true,
Expand All @@ -190,6 +195,7 @@ func resourceArmFrontDoor() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
// TODO: In 2.0 Switch default value from MatchRequest to HTTPSOnly #4627
"forwarding_protocol": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -602,6 +608,10 @@ func resourceArmFrontDoorRead(d *schema.ResourceData, meta interface{}) error {
}
resourceGroup := id.ResourceGroup
name := id.Path["frontdoors"]
// Link to issue: https://github.com/Azure/azure-sdk-for-go/issues/6762
if name == "" {
name = id.Path["Frontdoors"]
}

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
Expand Down Expand Up @@ -655,7 +665,7 @@ func resourceArmFrontDoorRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error setting `backend_pool_load_balancing`: %+v", err)
}

if err := d.Set("routing_rule", flattenArmFrontDoorRoutingRule(properties.RoutingRules)); err != nil {
if err := d.Set("routing_rule", flattenArmFrontDoorRoutingRule(properties.RoutingRules, d.Get("routing_rule"))); err != nil {
return fmt.Errorf("Error setting `routing_rules`: %+v", err)
}
}
Expand All @@ -674,6 +684,10 @@ func resourceArmFrontDoorDelete(d *schema.ResourceData, meta interface{}) error
}
resourceGroup := id.ResourceGroup
name := id.Path["frontdoors"]
// Link to issue: https://github.com/Azure/azure-sdk-for-go/issues/6762
if name == "" {
name = id.Path["Frontdoors"]
}

future, err := client.Delete(ctx, resourceGroup, name)
if err != nil {
Expand Down Expand Up @@ -1053,32 +1067,41 @@ func expandArmFrontDoorForwardingConfiguration(input []interface{}, frontDoorPat

customForwardingPath := v["custom_forwarding_path"].(string)
forwardingProtocol := v["forwarding_protocol"].(string)
backendPoolName := v["backend_pool_name"].(string)
cacheUseDynamicCompression := v["cache_use_dynamic_compression"].(bool)
cacheQueryParameterStripDirective := v["cache_query_parameter_strip_directive"].(string)
backendPoolName := v["backend_pool_name"].(string)

useDynamicCompression := frontdoor.DynamicCompressionEnabledDisabled

if cacheUseDynamicCompression {
useDynamicCompression = frontdoor.DynamicCompressionEnabledEnabled
}

cacheConfiguration := &frontdoor.CacheConfiguration{
QueryParameterStripDirective: frontdoor.Query(cacheQueryParameterStripDirective),
DynamicCompression: useDynamicCompression,
}
cacheEnabled := v["cache_enabled"].(bool)

backend := &frontdoor.SubResource{
ID: utils.String(frontDoorPath + "/BackendPools/" + backendPoolName),
}

forwardingConfiguration := frontdoor.ForwardingConfiguration{
ForwardingProtocol: frontdoor.ForwardingProtocol(forwardingProtocol),
CacheConfiguration: cacheConfiguration,
BackendPool: backend,
OdataType: frontdoor.OdataTypeMicrosoftAzureFrontDoorModelsFrontdoorForwardingConfiguration,
}

// Per the portal, if you enable the cache the cache_query_parameter_strip_directive
// is then a required attribute else the CacheConfiguration type is null
if cacheEnabled {
// Set the default value for dynamic compression or use the value defined in the config
dynamicCompression := frontdoor.DynamicCompressionEnabledEnabled
if !cacheUseDynamicCompression {
dynamicCompression = frontdoor.DynamicCompressionEnabledDisabled
}

if cacheQueryParameterStripDirective == "" {
// Set Default Value for strip directive is not in the key slice and cache is enabled
cacheQueryParameterStripDirective = string(frontdoor.StripNone)
}

forwardingConfiguration.CacheConfiguration = &frontdoor.CacheConfiguration{
DynamicCompression: dynamicCompression,
QueryParameterStripDirective: frontdoor.Query(cacheQueryParameterStripDirective),
}
}

if customForwardingPath != "" {
forwardingConfiguration.CustomForwardingPath = utils.String(customForwardingPath)
}
Expand Down Expand Up @@ -1107,7 +1130,16 @@ func flattenArmFrontDoorBackendPools(input *[]frontdoor.BackendPool) []map[strin
if properties := v.BackendPoolProperties; properties != nil {
result["backend"] = flattenArmFrontDoorBackend(properties.Backends)
result["health_probe_name"] = flattenArmFrontDoorSubResource(properties.HealthProbeSettings, "HealthProbeSettings")
// Link to issue: https://github.com/Azure/azure-sdk-for-go/issues/6762
if result["health_probe_name"] == "" {
result["health_probe_name"] = flattenArmFrontDoorSubResource(properties.HealthProbeSettings, "healthProbeSettings")
}

result["load_balancing_name"] = flattenArmFrontDoorSubResource(properties.LoadBalancingSettings, "LoadBalancingSettings")
// Link to issue: https://github.com/Azure/azure-sdk-for-go/issues/6762
if result["load_balancing_name"] == "" {
result["load_balancing_name"] = flattenArmFrontDoorSubResource(properties.LoadBalancingSettings, "loadBalancingSettings")
}
}
output = append(output, result)
}
Expand Down Expand Up @@ -1314,20 +1346,29 @@ func flattenArmFrontDoorLoadBalancingSettingsModel(input *[]frontdoor.LoadBalanc
return []interface{}{result}
}

func flattenArmFrontDoorRoutingRule(input *[]frontdoor.RoutingRule) []interface{} {
func flattenArmFrontDoorRoutingRule(input *[]frontdoor.RoutingRule, oldBlocks interface{}) []interface{} {
if input == nil {
return make([]interface{}, 0)
}

output := make([]interface{}, 0)
oldByName := map[string]map[string]interface{}{}

for _, i := range oldBlocks.([]interface{}) {
v := i.(map[string]interface{})

oldByName[v["name"].(string)] = v
}

output := make([]interface{}, 0)
for _, v := range *input {
result := make(map[string]interface{})

if id := v.ID; id != nil {
result["id"] = *id
}
if name := v.Name; name != nil {

name := v.Name
if name != nil {
result["name"] = *name
}

Expand All @@ -1349,21 +1390,37 @@ func flattenArmFrontDoorRoutingRule(input *[]frontdoor.RoutingRule) []interface{
v := brc.(frontdoor.ForwardingConfiguration)

c["backend_pool_name"] = flattenArmFrontDoorSubResource(v.BackendPool, "BackendPools")
// Link to issue: https://github.com/Azure/azure-sdk-for-go/issues/6762
if c["backend_pool_name"] == "" {
c["backend_pool_name"] = flattenArmFrontDoorSubResource(v.BackendPool, "backendPools")
}
c["custom_forwarding_path"] = v.CustomForwardingPath
c["forwarding_protocol"] = string(v.ForwardingProtocol)

if cacheConfiguration := v.CacheConfiguration; cacheConfiguration != nil {
if queryParameter := cacheConfiguration.QueryParameterStripDirective; queryParameter != "" {
c["cache_query_parameter_strip_directive"] = string(queryParameter)
c["cache_enabled"] = true
if stripDirective := cacheConfiguration.QueryParameterStripDirective; stripDirective != "" {
c["cache_query_parameter_strip_directive"] = string(stripDirective)
} else {
c["cache_query_parameter_strip_directive"] = string(frontdoor.StripNone)
}

c["cache_use_dynamic_compression"] = false

if dynamicCompression := cacheConfiguration.DynamicCompression; dynamicCompression != "" {
if dynamicCompression == frontdoor.DynamicCompressionEnabledEnabled {
c["cache_use_dynamic_compression"] = true
c["cache_use_dynamic_compression"] = bool(string(dynamicCompression) == string(frontdoor.DynamicCompressionEnabledEnabled))
}
} else {
c["cache_enabled"] = false

if name != nil {
//get `forwarding_configuration`
if o, ok := oldByName[*name]; ok {
ofcs := o["forwarding_configuration"].([]interface{})
if len(ofcs) > 0 {
ofc := ofcs[0].(map[string]interface{})

c["cache_query_parameter_strip_directive"] = ofc["cache_query_parameter_strip_directive"]
c["cache_use_dynamic_compression"] = ofc["cache_use_dynamic_compression"]
}
}
}
}
Expand Down Expand Up @@ -1431,6 +1488,10 @@ func flattenArmFrontDoorFrontendEndpointsSubResources(input *[]frontdoor.SubReso

for _, v := range *input {
name := flattenArmFrontDoorSubResource(&v, "FrontendEndpoints")
// Link to issue: https://github.com/Azure/azure-sdk-for-go/issues/6762
if name == "" {
name = flattenArmFrontDoorSubResource(&v, "frontendEndpoints")
}
output = append(output, name)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,37 +209,65 @@ resource "azurerm_frontdoor_firewall_policy" "import" {
}

func testAccAzureRMFrontDoorFirewallPolicy_update(data acceptance.TestData, update bool) string {
inner := ""
if update {
inner = fmt.Sprintf(`
custom_rule {
name = "Rule2"
enabled = true
priority = 2
rate_limit_duration_in_minutes = 1
rate_limit_threshold = 10
type = "MatchRule"
action = "Block"
match_condition {
match_variable = "RemoteAddr"
operator = "IPMatch"
negation_condition = false
match_values = ["192.168.1.0/24"]
return testAccAzureRMFrontDoorFirewallPolicy_updated(data)
}
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "testAccRG-%d"
location = "%s"
}
resource "azurerm_frontdoor_firewall_policy" "test" {
name = "testAccFrontDoorWAF%[1]d"
resource_group_name = azurerm_resource_group.test.name
enabled = true
mode = "Prevention"
redirect_url = "https://www.contoso.com"
custom_block_response_status_code = 403
custom_block_response_body = "PGh0bWw+CjxoZWFkZXI+PHRpdGxlPkhlbGxvPC90aXRsZT48L2hlYWRlcj4KPGJvZHk+CkhlbGxvIHdvcmxkCjwvYm9keT4KPC9odG1sPg=="
custom_rule {
name = "Rule1"
enabled = true
priority = 1
rate_limit_duration_in_minutes = 1
rate_limit_threshold = 10
type = "MatchRule"
action = "Block"
match_condition {
match_variable = "RemoteAddr"
operator = "IPMatch"
negation_condition = false
match_values = ["192.168.1.0/24", "10.0.0.0/24"]
}
}
match_condition {
match_variable = "RequestHeader"
selector = "UserAgent"
operator = "Contains"
negation_condition = false
match_values = ["windows"]
transforms = ["Lowercase", "Trim"]
managed_rule {
type = "DefaultRuleSet"
version = "preview-0.1"
override {
rule_group_name = "PHP"
rule {
rule_id = "933111"
enabled = false
action = "Block"
}
}
}
managed_rule {
type = "BotProtection"
version = "preview-0.1"
}
}
`)
}
`, data.RandomInteger, data.Locations.Primary)
}

func testAccAzureRMFrontDoorFirewallPolicy_updated(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "testAccRG-%d"
Expand All @@ -248,7 +276,7 @@ resource "azurerm_resource_group" "test" {
resource "azurerm_frontdoor_firewall_policy" "test" {
name = "testAccFrontDoorWAF%[1]d"
resource_group_name = "${azurerm_resource_group.test.name}"
resource_group_name = azurerm_resource_group.test.name
enabled = true
mode = "Prevention"
redirect_url = "https://www.contoso.com"
Expand All @@ -272,7 +300,31 @@ resource "azurerm_frontdoor_firewall_policy" "test" {
}
}
%[3]s
custom_rule {
name = "Rule2"
enabled = true
priority = 2
rate_limit_duration_in_minutes = 1
rate_limit_threshold = 10
type = "MatchRule"
action = "Block"
match_condition {
match_variable = "RemoteAddr"
operator = "IPMatch"
negation_condition = false
match_values = ["192.168.1.0/24"]
}
match_condition {
match_variable = "RequestHeader"
selector = "UserAgent"
operator = "Contains"
negation_condition = false
match_values = ["windows"]
transforms = ["Lowercase", "Trim"]
}
}
managed_rule {
type = "DefaultRuleSet"
Expand All @@ -294,5 +346,5 @@ resource "azurerm_frontdoor_firewall_policy" "test" {
version = "preview-0.1"
}
}
`, data.RandomInteger, data.Locations.Primary, inner)
`, data.RandomInteger, data.Locations.Primary)
}
Loading

0 comments on commit d42f493

Please sign in to comment.