Skip to content

Commit

Permalink
azurerm_logic_app_action_http - body property support "@" symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyeqf committed Dec 21, 2022
1 parent 8f1fde6 commit e388082
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 21 deletions.
43 changes: 29 additions & 14 deletions internal/services/logic/logic_app_action_http_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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{}{
Expand Down Expand Up @@ -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 {
Expand Down
60 changes: 60 additions & 0 deletions internal/services/logic/logic_app_action_http_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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 = <<BODY
@concat('{\"summary\": \"Foo\", \"text\": \"',triggerBody()?['data']?['essentials']?['description'],'\"}')
BODY
}
`, r.template(data), data.RandomInteger)
}

func (LogicAppActionHttpResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<h2>Terraform Test</h2>
<h2>Terraform Test</h2>
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@
</StringRightTrimDestination>
</ns0:Root>
</xsl:template>
</xsl:stylesheet>
</xsl:stylesheet>
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@
</StringRightTrimDestination>
</ns0:Root>
</xsl:template>
</xsl:stylesheet>
</xsl:stylesheet>
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,4 @@
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</xs:schema>
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,4 @@
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</xs:schema>
Binary file modified internal/services/logic/testdata/log4net.dll
Binary file not shown.

0 comments on commit e388082

Please sign in to comment.