Skip to content

Commit

Permalink
azurerm_api_management_api_operation_policy fix to pass all acctest…
Browse files Browse the repository at this point in the history
…s and fix raw xml issue (#7639)

Fix: #7345
  • Loading branch information
yupwei68 authored Jul 9, 2020
1 parent 339e24f commit 15b90ab
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -46,7 +46,7 @@ func resourceArmApiManagementApiOperationPolicy() *schema.Resource {
Optional: true,
Computed: true,
ConflictsWith: []string{"xml_link"},
DiffSuppressFunc: suppress.XmlDiff,
DiffSuppressFunc: XmlWithDotNetInterpolationsDiffSuppress,
},

"xml_link": {
Expand Down Expand Up @@ -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),
}
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions azurerm/internal/services/apimanagement/diff_suppress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "&lt;", "<")
value = strings.ReplaceAll(value, "&amp;", "&")
value = strings.ReplaceAll(value, "&apos;", "'")

return value
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<policies>
<inbound>
<base />
<cors>
<allowed-origins>
<origin>*</origin>
</allowed-origins>
<allowed-methods>
<method>*</method>
</allowed-methods>
<allowed-headers>
<header>*</header>
</allowed-headers>
</cors>
<send-request mode="new" response-variable-name="getCertResponse" timeout="10" ignore-error="false">
<set-url>https://github.com/terraform-providers/terraform-provider-azurerm</set-url>
<set-method>GET</set-method>
<set-header name="Authorization" exists-action="override">
<value>@(context.Request.Headers.GetValueOrDefault("Authorization",""))</value>
</set-header>
</send-request>
<set-header name="Authorization" exists-action="override">
<value>@{
var responseObj = ((IResponse)context.Variables[&quot;getCertResponse&quot;]).Body.As&lt;JObject&gt;();
var jwt = (string)responseObj[&quot;payload&quot;];
return $&quot;Bearer {jwt}&quot;;}</value>
</set-header>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>

0 comments on commit 15b90ab

Please sign in to comment.