From 15b90abd5f3d2fcbd87f52b1d0066a05f96837f9 Mon Sep 17 00:00:00 2001 From: Yuping Wei <56525716+yupwei68@users.noreply.github.com> Date: Fri, 10 Jul 2020 00:50:08 +0800 Subject: [PATCH] `azurerm_api_management_api_operation_policy` fix to pass all acctests and fix raw xml issue (#7639) Fix: #7345 --- ...anagement_api_operation_policy_resource.go | 10 ++--- .../services/apimanagement/diff_suppress.go | 4 ++ ...ment_api_operation_policy_resource_test.go | 45 ++++++++++++++++--- .../api_management_api_operation_policy.xml | 38 ++++++++++++++++ 4 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 azurerm/internal/services/apimanagement/tests/testdata/api_management_api_operation_policy.xml diff --git a/azurerm/internal/services/apimanagement/api_management_api_operation_policy_resource.go b/azurerm/internal/services/apimanagement/api_management_api_operation_policy_resource.go index 2d9ad1422ede..f084b5209e7b 100644 --- a/azurerm/internal/services/apimanagement/api_management_api_operation_policy_resource.go +++ b/azurerm/internal/services/apimanagement/api_management_api_operation_policy_resource.go @@ -2,13 +2,13 @@ package apimanagement import ( "fmt" + "html" "log" "time" "github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2019-12-01/apimanagement" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" @@ -46,7 +46,7 @@ func resourceArmApiManagementApiOperationPolicy() *schema.Resource { Optional: true, Computed: true, ConflictsWith: []string{"xml_link"}, - DiffSuppressFunc: suppress.XmlDiff, + DiffSuppressFunc: XmlWithDotNetInterpolationsDiffSuppress, }, "xml_link": { @@ -88,14 +88,14 @@ func resourceArmApiManagementAPIOperationPolicyCreateUpdate(d *schema.ResourceDa if xmlContent != "" { parameters.PolicyContractProperties = &apimanagement.PolicyContractProperties{ - Format: apimanagement.XML, + Format: apimanagement.Rawxml, Value: utils.String(xmlContent), } } if xmlLink != "" { parameters.PolicyContractProperties = &apimanagement.PolicyContractProperties{ - Format: apimanagement.XMLLink, + Format: apimanagement.RawxmlLink, Value: utils.String(xmlLink), } } @@ -153,7 +153,7 @@ func resourceArmApiManagementAPIOperationPolicyRead(d *schema.ResourceData, meta if properties := resp.PolicyContractProperties; properties != nil { // when you submit an `xml_link` to the API, the API downloads this link and stores it as `xml_content` // as such there is no way to set `xml_link` and we'll let Terraform handle it - d.Set("xml_content", properties.Value) + d.Set("xml_content", html.UnescapeString(*properties.Value)) } return nil diff --git a/azurerm/internal/services/apimanagement/diff_suppress.go b/azurerm/internal/services/apimanagement/diff_suppress.go index f0bbb23c50ab..20925c93f298 100644 --- a/azurerm/internal/services/apimanagement/diff_suppress.go +++ b/azurerm/internal/services/apimanagement/diff_suppress.go @@ -37,6 +37,10 @@ func normalizeXmlWithDotNetInterpolationsString(input string) string { value = strings.ReplaceAll(value, " ", "") value = strings.ReplaceAll(value, " ", "") value = strings.ReplaceAll(value, """, "\"") + value = strings.ReplaceAll(value, ">", ">") + value = strings.ReplaceAll(value, "<", "<") + value = strings.ReplaceAll(value, "&", "&") + value = strings.ReplaceAll(value, "'", "'") return value } diff --git a/azurerm/internal/services/apimanagement/tests/api_management_api_operation_policy_resource_test.go b/azurerm/internal/services/apimanagement/tests/api_management_api_operation_policy_resource_test.go index bd7e74cc2521..2689afbc6467 100644 --- a/azurerm/internal/services/apimanagement/tests/api_management_api_operation_policy_resource_test.go +++ b/azurerm/internal/services/apimanagement/tests/api_management_api_operation_policy_resource_test.go @@ -85,6 +85,25 @@ func TestAccAzureRMApiManagementAPIOperationPolicy_update(t *testing.T) { }) } +func TestAccAzureRMApiManagementAPIOperationPolicy_rawXml(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_api_management_api_operation_policy", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMApiManagementAPIOperationPolicyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMApiManagementAPIOperationPolicy_rawXml(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMApiManagementAPIOperationPolicyExists(data.ResourceName), + ), + }, + data.ImportStep(), + }, + }) +} + func testCheckAzureRMApiManagementAPIOperationPolicyExists(resourceName string) resource.TestCheckFunc { return func(s *terraform.State) error { conn := acceptance.AzureProvider.Meta().(*clients.Client).ApiManagement.ApiOperationPoliciesClient @@ -165,11 +184,11 @@ func testAccAzureRMApiManagementAPIOperationPolicy_requiresImport(data acceptanc %s resource "azurerm_api_management_api_operation_policy" "import" { - api_name = azurerm_api_management_api_policy.test.api_name - api_management_name = azurerm_api_management_api_policy.test.api_management_name - resource_group_name = azurerm_api_management_api_policy.test.resource_group_name - operation_id = azurerm_api_management_api_operation.test.operation_id - xml_link = azurerm_api_management_api_policy.test.xml_link + api_name = azurerm_api_management_api_operation_policy.test.api_name + api_management_name = azurerm_api_management_api_operation_policy.test.api_management_name + resource_group_name = azurerm_api_management_api_operation_policy.test.resource_group_name + operation_id = azurerm_api_management_api_operation_policy.test.operation_id + xml_link = azurerm_api_management_api_operation_policy.test.xml_link } `, template) } @@ -197,3 +216,19 @@ XML } `, template) } + +func testAccAzureRMApiManagementAPIOperationPolicy_rawXml(data acceptance.TestData) string { + template := testAccAzureRMApiManagementApiOperation_basic(data) + return fmt.Sprintf(` +%s + +resource "azurerm_api_management_api_operation_policy" "test" { + api_name = azurerm_api_management_api.test.name + api_management_name = azurerm_api_management.test.name + resource_group_name = azurerm_resource_group.test.name + operation_id = azurerm_api_management_api_operation.test.operation_id + + xml_content = file("testdata/api_management_api_operation_policy.xml") +} +`, template) +} diff --git a/azurerm/internal/services/apimanagement/tests/testdata/api_management_api_operation_policy.xml b/azurerm/internal/services/apimanagement/tests/testdata/api_management_api_operation_policy.xml new file mode 100644 index 000000000000..0cef3a3a61f6 --- /dev/null +++ b/azurerm/internal/services/apimanagement/tests/testdata/api_management_api_operation_policy.xml @@ -0,0 +1,38 @@ + + + + + + * + + + * + + +
*
+
+
+ + https://github.com/terraform-providers/terraform-provider-azurerm + GET + + @(context.Request.Headers.GetValueOrDefault("Authorization","")) + + + + @{ + var responseObj = ((IResponse)context.Variables["getCertResponse"]).Body.As<JObject>(); + var jwt = (string)responseObj["payload"]; + return $"Bearer {jwt}";} + +
+ + + + + + + + + +
\ No newline at end of file