From e3880827725818c07e61a9d7749af8aad2c4182f Mon Sep 17 00:00:00 2001 From: ziyeqf Date: Wed, 21 Dec 2022 12:15:24 +0800 Subject: [PATCH] `azurerm_logic_app_action_http` - `body` property support "@" symbol --- .../logic/logic_app_action_http_resource.go | 43 +++++++++---- .../logic_app_action_http_resource_test.go | 60 ++++++++++++++++++ ...gration_account_agreement_content_as2.json | 2 +- ...gration_account_agreement_content_x12.json | 2 +- .../integration_account_map_content.liquid | 2 +- .../integration_account_map_content.xsd | 2 +- .../integration_account_map_content2.xsd | 2 +- .../integration_account_schema_content.xsd | 2 +- .../integration_account_schema_content2.xsd | 2 +- internal/services/logic/testdata/log4net.dll | Bin 270336 -> 270336 bytes 10 files changed, 96 insertions(+), 21 deletions(-) diff --git a/internal/services/logic/logic_app_action_http_resource.go b/internal/services/logic/logic_app_action_http_resource.go index 9e16a5a6c4d8..25b763b2429e 100644 --- a/internal/services/logic/logic_app_action_http_resource.go +++ b/internal/services/logic/logic_app_action_http_resource.go @@ -9,6 +9,7 @@ import ( "time" "github.com/Azure/azure-sdk-for-go/services/logic/mgmt/2019-05-01/logic" // nolint: staticcheck + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" "github.com/hashicorp/terraform-provider-azurerm/internal/services/logic/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tags" @@ -67,10 +68,14 @@ func resourceLogicAppActionHTTP() *pluginsdk.Resource { }, "body": { - Type: pluginsdk.TypeString, - Optional: true, - ValidateFunc: validation.StringIsJSON, - DiffSuppressFunc: pluginsdk.SuppressJsonDiff, + Type: pluginsdk.TypeString, + Optional: true, + DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool { + if json.Valid([]byte(oldValue)) && json.Valid([]byte(newValue)) { + return pluginsdk.SuppressJsonDiff(k, oldValue, newValue, d) + } + return false + }, }, "headers": { @@ -143,13 +148,18 @@ func resourceLogicAppActionHTTPCreateUpdate(d *pluginsdk.ResourceData, meta inte "queries": queries, } - // storing action's body in json object to keep consistent with azure portal + // if it's json object then storing action's body in json object to keep consistent with azure portal + // if starts with dynamic function (starts with "@") then store it as string if bodyRaw, ok := d.GetOk("body"); ok { - var body map[string]interface{} - if err := json.Unmarshal([]byte(bodyRaw.(string)), &body); err != nil { - return fmt.Errorf("unmarshalling JSON for Action %q: %+v", id.Name, err) + if json.Valid([]byte(bodyRaw.(string))) { + var body map[string]interface{} + if err := json.Unmarshal([]byte(bodyRaw.(string)), &body); err != nil { + return fmt.Errorf("unmarshalling JSON for Action %q: %+v", id.Name, err) + } + inputs["body"] = body + } else { + inputs["body"] = bodyRaw.(string) } - inputs["body"] = body } action := map[string]interface{}{ @@ -215,12 +225,17 @@ func resourceLogicAppActionHTTPRead(d *pluginsdk.ResourceData, meta interface{}) } if body := inputs["body"]; body != nil { - // if user edit workflow in portal, the body becomes json object - v, err := json.Marshal(body) - if err != nil { - return fmt.Errorf("serializing `body` for Action %q: %+v", id.Name, err) + switch body.(type) { + case map[string]interface{}: + // if user edit workflow in portal, the body becomes json object + v, err := json.Marshal(body) + if err != nil { + return fmt.Errorf("serializing `body` for Action %q: %+v", id.Name, err) + } + d.Set("body", string(v)) + case string: + d.Set("body", body) } - d.Set("body", string(v)) } if headers := inputs["headers"]; headers != nil { diff --git a/internal/services/logic/logic_app_action_http_resource_test.go b/internal/services/logic/logic_app_action_http_resource_test.go index 4d30ccff5c87..750e84c36752 100644 --- a/internal/services/logic/logic_app_action_http_resource_test.go +++ b/internal/services/logic/logic_app_action_http_resource_test.go @@ -91,6 +91,50 @@ func TestAccLogicAppActionHttp_queries(t *testing.T) { }) } +func TestAccLogicAppActionHttp_dynamicFunction(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_logic_app_action_http", "test") + r := LogicAppActionHttpResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.withDynamicFunction(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccLogicAppActionHttp_bodyDiff(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_logic_app_action_http", "test") + r := LogicAppActionHttpResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.withDynamicFunction(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.complete(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func TestAccLogicAppActionHttp_disappears(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_logic_app_action_http", "test") r := LogicAppActionHttpResource{} @@ -264,6 +308,22 @@ resource "azurerm_logic_app_action_http" "test" { `, r.template(data), data.RandomInteger, data.RandomInteger, data.RandomInteger, condition, condition) } +func (r LogicAppActionHttpResource) withDynamicFunction(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_logic_app_action_http" "test" { + name = "action%d" + logic_app_id = azurerm_logic_app_workflow.test.id + method = "POST" + uri = "http://example.com/hello" + body = <Terraform Test \ No newline at end of file +

Terraform Test

diff --git a/internal/services/logic/testdata/integration_account_map_content.xsd b/internal/services/logic/testdata/integration_account_map_content.xsd index 503ea2bcf30f..7ac28613525f 100644 --- a/internal/services/logic/testdata/integration_account_map_content.xsd +++ b/internal/services/logic/testdata/integration_account_map_content.xsd @@ -67,4 +67,4 @@ - \ No newline at end of file + diff --git a/internal/services/logic/testdata/integration_account_map_content2.xsd b/internal/services/logic/testdata/integration_account_map_content2.xsd index f38ee6f44fc1..d320be0d6060 100644 --- a/internal/services/logic/testdata/integration_account_map_content2.xsd +++ b/internal/services/logic/testdata/integration_account_map_content2.xsd @@ -67,4 +67,4 @@ - \ No newline at end of file + diff --git a/internal/services/logic/testdata/integration_account_schema_content.xsd b/internal/services/logic/testdata/integration_account_schema_content.xsd index 7eaa796c631d..696b0354b0af 100644 --- a/internal/services/logic/testdata/integration_account_schema_content.xsd +++ b/internal/services/logic/testdata/integration_account_schema_content.xsd @@ -219,4 +219,4 @@ - \ No newline at end of file + diff --git a/internal/services/logic/testdata/integration_account_schema_content2.xsd b/internal/services/logic/testdata/integration_account_schema_content2.xsd index 6f998b66d6f7..2ce4c499a21c 100644 --- a/internal/services/logic/testdata/integration_account_schema_content2.xsd +++ b/internal/services/logic/testdata/integration_account_schema_content2.xsd @@ -219,4 +219,4 @@ - \ No newline at end of file + diff --git a/internal/services/logic/testdata/log4net.dll b/internal/services/logic/testdata/log4net.dll index e74861d2437fb14f7d8c80fc6a1539086fe23a79..b926f6fc45dc45c240aead45fdef2e75e6c23565 100644 GIT binary patch delta 26 hcmZoTAkc6?U_x1Ad22aiYdKSEIrG+XmQHy_E&!80348zm delta 24 fcmZoTAkc6?U_x1AX=^ECYbjG}Df8A+mM(b!fA9%t