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

Bug: azurerm_frontdoor fix for caching issue #5358

Merged
merged 35 commits into from
Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
465d09e
Port PR to new code base
WodansSon Jan 9, 2020
4904a20
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
f62f456
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
c85c9c1
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
cfe157d
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
ff57240
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
d9aecd8
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
17c05f6
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
e46221e
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
b88ef6b
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
5e21698
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
5b17a51
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
644ac71
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
8ef9e23
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
9cf59c2
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
2ca5437
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
e8978c5
Update website/docs/r/front_door.html.markdown
WodansSon Jan 13, 2020
ba86c82
Progress
WodansSon Jan 14, 2020
6c9af26
Saving progress
WodansSon Jan 15, 2020
9923e35
Almost right
WodansSon Jan 17, 2020
ffafcb9
Cache working
WodansSon Jan 17, 2020
20032ed
Fully working without defaults
WodansSon Jan 22, 2020
dadf851
Update docs to document new behavior
WodansSon Jan 22, 2020
3eb71d9
Fix tests and documentation
WodansSon Jan 23, 2020
fe605a3
Merge branch 'master' into rf_frontdoor_cache
WodansSon Jan 23, 2020
9c358bd
Add test case for cache regression
WodansSon Jan 23, 2020
6adb437
Fix test code terrafmt
WodansSon Jan 23, 2020
8efc0ac
Fixed linting error in comment
WodansSon Jan 23, 2020
613bc0e
pull value through test
katbyte Jan 23, 2020
3412cf1
Update docs and remove commented code
WodansSon Jan 24, 2020
b2eda4f
Update test case
WodansSon Jan 24, 2020
4b6ecba
Merge remote-tracking branch 'origin/master' into rf_frontdoor_cache
katbyte Jan 24, 2020
a2e78b0
terrafmt
katbyte Jan 24, 2020
2c4f4f4
Minor doc update
WodansSon Jan 24, 2020
efdfb58
Added nil check per PR review
WodansSon Jan 25, 2020
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
105 changes: 82 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,35 @@ 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

//get `forwarding_configuration`
if o, ok := oldByName[*name]; ok {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name could be nil so we'll need to nil check it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep yep

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 +1486,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