使用 Azure 上的 Terraform 为 Webtest 配置 XML 正确编码 URL

Properly encode URL for Webtest configuration XML with Terraform on Azure

我正在尝试使用 Terraform 设置 azurerm_application_insights_web_testconfiguration WebTest XML.

中的 Url 参数目前存在问题

我只是不知道我必须如何正确编码字符串。对于“简单的”URLs,没问题。但是,例如。 https://a.skwar.me/example=yes?问题出在 URL.

中的“=”(等号)

如果我在 XML 中设置 Url="https://a.skwar.me/example=yes"terraform apply 失败:

2021-12-03T08:32:47.4173868Z Error: creating/updating Application Insights Web Test: (Name "dummy3aaiwt" / Resource Group "SharedienDevopsTesting"): insights.WebTestsClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="BadRequest" Message="Specified argument was out of the range of valid values.\r\nParameter name: Web test 'dummy3aaiwt' is invalid. Ensure it is a well formed XML and that it adheres to the web test schema. Technical error: '=' is an unexpected token. The expected token is ';'. Line 23, position 33." InnerError={"diagnosticcontext":"6a6fb5be-fba9-4110-be6a-beffd2a44ef1","time":"2021-12-03T08:32:47.3359710Z"}

注意:技术错误:“=”是意外标记。预期的标记是 ';'

我试图简单地 urlencode() 整个 URL。 Apply 有效,但没有进行任何测试。如果我访问 Azure 门户,我会看到参数尚未解码; IE。我看到 https%3A%2F%2Fa.skwar.me%2Fexample%3Dyes。我也无法保存测试,因为 URL 无效(... d'oh!)

我知道了:

resource "azurerm_application_insights_web_test" "dummy3" {

  name = "dummy3aaiwt"

  location                = local.ai_this.location
  resource_group_name     = local.ai_this.resource_group_name
  application_insights_id = local.ai_this.id
  kind                    = "ping"
  frequency               = 900
  timeout                 = 120
  enabled                 = true
  retry_enabled           = true
  geo_locations           = split(",", local.test_locations)

  configuration = <<XML
<WebTest
  Name="abcdefXML"
  Id="${random_uuid.dummy.result}"
  Enabled="True"
  CssProjectStructure=""
  CssIteration=""
  Timeout="120"
  WorkItemIds=""
  xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010"
  Description=""
  CredentialUserName=""
  CredentialPassword=""
  PreAuthenticate="True"
  Proxy="default"
  StopOnError="False"
  RecordedResultFile=""
  ResultsLocale="">
    <Items>
        <Request
          Method="GET"
          Guid="${random_uuid.dummy2.result}"
          Version="1.1"
          Url="${urlencode("https://a.skwar.me/some-exampe?foo=bar")}"
          ThinkTime="0"
          Timeout="120"
          ParseDependentRequests="False"
          FollowRedirects="True"
          RecordResult="True"
          Cache="False"
          ResponseTimeGoal="0"
          Encoding="utf-8"
          ExpectedHttpStatusCode="302"
          ExpectedResponseUrl=""
          ReportingName=""
          IgnoreHttpStatusCode="False" />
    </Items>
</WebTest>
XML
}

使用 Terraform 版本 1.0.11 和提供商 registry.terraform.io/hashicorp/azurerm v2.86.0 ,如果我尝试在 url 参数中使用 urlencode 然后我也面临着您在门户中看到的相同问题,如下所示:

但是使用下面的代码直接提供 URL 成功并且 URL 也在门户中正确定义并且也可以保存。

resource "azurerm_application_insights_web_test" "example" {
  name                    = "tf-test-appinsights-webtest"
  location                = data.azurerm_application_insights.example.location
  resource_group_name     = data.azurerm_application_insights.example.resource_group_name
  application_insights_id = data.azurerm_application_insights.example.id
  kind                    = "ping"
  frequency               = 300
  timeout                 = 60
  enabled                 = true
  geo_locations           = ["us-ca-sjc-azr", "us-va-ash-azr"]

configuration = <<XML
<WebTest
  Name="WebTest1"
  Id="ABD48585-0831-40CB-9069-682EA6BB3583"
  Enabled="True"
  CssProjectStructure=""
  CssIteration=""
  Timeout="120"
  WorkItemIds=""
  xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010"
  Description=""
  CredentialUserName=""
  CredentialPassword=""
  PreAuthenticate="True"
  Proxy="default"
  StopOnError="False"
  RecordedResultFile=""
  ResultsLocale="">
    <Items>
        <Request
          Method="GET"
          Guid="a5f10126-e4cd-570d-961c-cea43999a200"
          Version="1.1"
          Url="https://a.skwar.me/some-exampe?foo=bar"
          ThinkTime="0"
          Timeout="300"
          ParseDependentRequests="False"
          FollowRedirects="True"
          RecordResult="True"
          Cache="False"
          ResponseTimeGoal="0"
          Encoding="utf-8"
          ExpectedHttpStatusCode="302"
          ExpectedResponseUrl=""
          ReportingName=""
          IgnoreHttpStatusCode="False" />
    </Items>
</WebTest>
XML
}

输出: