Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_logic_app_trigger_custom - adding exported attribute callback_url #25979

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions internal/services/logic/logic_app_trigger_custom_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func resourceLogicAppTriggerCustom() *pluginsdk.Resource {
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: pluginsdk.SuppressJsonDiff,
},
"callback_url": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -88,9 +92,7 @@ func resourceLogicAppTriggerCustomRead(d *pluginsdk.ResourceData, meta interface
return err
}

workflowId := workflows.NewWorkflowID(id.SubscriptionId, id.ResourceGroupName, id.WorkflowName)

t, app, err := retrieveLogicAppTrigger(d, meta, workflowId, id.TriggerName)
t, app, url, err := retrieveLogicAppTrigger(d, meta, *id)
if err != nil {
return err
}
Expand All @@ -101,16 +103,22 @@ func resourceLogicAppTriggerCustomRead(d *pluginsdk.ResourceData, meta interface
return nil
}

action := *t
trigger := *t

d.Set("name", id.TriggerName)
d.Set("logic_app_id", app.Id)

if url != nil {
d.Set("callback_url", url)
} else {
d.Set("callback_url", "")
}
nickel-tyler marked this conversation as resolved.
Show resolved Hide resolved

// Azure returns an additional field called evaluatedRecurrence in the trigger body which
// is a copy of the recurrence specified in the body property and breaks the diff suppress logic
delete(action, "evaluatedRecurrence")
delete(trigger, "evaluatedRecurrence")

body, err := json.Marshal(action)
body, err := json.Marshal(trigger)
if err != nil {
return fmt.Errorf("serializing `body` for %s: %+v", id.ID(), err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,38 @@ func TestAccLogicAppTriggerCustom_basic(t *testing.T) {
})
}

func TestAccLogicAppTriggerCustom_callbackUrl(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_logic_app_trigger_custom", "test")
r := LogicAppTriggerCustomResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("callback_url").IsEmpty(),
),
},
data.ImportStep(),
})
}
nickel-tyler marked this conversation as resolved.
Show resolved Hide resolved

func TestAccLogicAppTriggerCustom_callbackUrl2(t *testing.T) {
nickel-tyler marked this conversation as resolved.
Show resolved Hide resolved
data := acceptance.BuildTestData(t, "azurerm_logic_app_trigger_custom", "test")
r := LogicAppTriggerCustomResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.nonEmptyCallback(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("callback_url").IsNotEmpty(),
),
},
data.ImportStep(),
})
}

func TestAccLogicAppTriggerCustom_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_logic_app_trigger_custom", "test")
r := LogicAppTriggerCustomResource{}
Expand Down Expand Up @@ -76,6 +108,31 @@ BODY
`, template, data.RandomInteger)
}

func (LogicAppTriggerCustomResource) nonEmptyCallback(data acceptance.TestData) string {
template := LogicAppTriggerCustomResource{}.template(data)
return fmt.Sprintf(`
%s

resource "azurerm_logic_app_trigger_custom" "test" {
name = "request-%d"
logic_app_id = azurerm_logic_app_workflow.test.id

body = <<BODY
{
"inputs": {
"method": "POST",
"relativePath": "customers/{id}",
"schema": {}
},
"kind": "Http",
"type": "Request"
}
BODY

}
`, template, data.RandomInteger)
}

func (LogicAppTriggerCustomResource) requiresImport(data acceptance.TestData) string {
template := LogicAppTriggerCustomResource{}.basic(data)
return fmt.Sprintf(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func resourceLogicAppTriggerHttpRequestRead(d *pluginsdk.ResourceData, meta inte
return err
}

t, app, url, err := retrieveLogicAppHttpTrigger(d, meta, *id)
t, app, url, err := retrieveLogicAppTrigger(d, meta, *id)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestAccLogicAppTriggerHttpRequest_callbackUrl(t *testing.T) {
Config: r.method(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("callback_url").Exists(),
check.That(data.ResourceName).Key("callback_url").IsNotEmpty(),
),
},
data.ImportStep(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ func resourceLogicAppTriggerRecurrenceRead(d *pluginsdk.ResourceData, meta inter
return err
}

workflowId := workflows.NewWorkflowID(id.SubscriptionId, id.ResourceGroupName, id.WorkflowName)

t, app, err := retrieveLogicAppTrigger(d, meta, workflowId, id.TriggerName)
t, app, _, err := retrieveLogicAppTrigger(d, meta, *id)
if err != nil {
return err
}
Expand Down
41 changes: 33 additions & 8 deletions internal/services/logic/logic_apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/logic/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
)

Expand Down Expand Up @@ -201,22 +202,46 @@ func retrieveLogicAppAction(d *pluginsdk.ResourceData, meta interface{}, id work
return retrieveLogicAppComponent(d, meta, "Action", "actions", id, name)
}

func retrieveLogicAppHttpTrigger(d *pluginsdk.ResourceData, meta interface{}, id workflowtriggers.TriggerId) (*map[string]interface{}, *workflows.Workflow, *string, error) {
func retrieveLogicAppTrigger(d *pluginsdk.ResourceData, meta interface{}, id workflowtriggers.TriggerId) (*map[string]interface{}, *workflows.Workflow, *string, error) {
workflowId := workflows.NewWorkflowID(id.SubscriptionId, id.ResourceGroupName, id.WorkflowName)

t, app, err := retrieveLogicAppTrigger(d, meta, workflowId, id.TriggerName)
t, app, err := retrieveLogicAppComponent(d, meta, "Trigger", "triggers", workflowId, id.TriggerName)

if err != nil || t == nil {
return nil, nil, nil, err
}
url, err := retreiveLogicAppTriggerCallbackUrl(d, meta, id)
if err != nil {
return nil, nil, nil, err

trigger := *t
tType := trigger["type"]
if tType == nil {
return nil, nil, nil, fmt.Errorf("[ERROR] `type` was nil for %s", id.ID())
}

log.Printf("[DEBUG] trigger type is %s", tType.(string))

if IsCallbackType(tType.(string)) {
url, err := retreiveLogicAppTriggerCallbackUrl(d, meta, id)
if err != nil {
return nil, nil, nil, err
}

if url == nil {
return nil, nil, nil, fmt.Errorf("[ERROR] `callback_url` was nil for %s", id.ID())
}

return t, app, url, err
}
return t, app, url, err

return t, app, nil, err
}

func retrieveLogicAppTrigger(d *pluginsdk.ResourceData, meta interface{}, id workflows.WorkflowId, name string) (*map[string]interface{}, *workflows.Workflow, error) {
return retrieveLogicAppComponent(d, meta, "Trigger", "triggers", id, name)
func IsCallbackType(tType string) bool {
cTypes := []string{"ApiConnectionWebhook", "HTTPWebhook", "Request"}

valid := validation.StringInSlice(cTypes, false)
_, errors := valid(tType, "callback_url")

return len(errors) == 0
}

func retreiveLogicAppTriggerCallbackUrl(d *pluginsdk.ResourceData, meta interface{}, id workflowtriggers.TriggerId) (*string, error) {
Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/logic_app_trigger_custom.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ In addition to the Arguments listed above - the following Attributes are exporte

* `id` - The ID of the Trigger within the Logic App Workflow.

* `callback_url` - The URL of the Trigger within the Logic App Workflow. For use with certain resources like [monitor_action_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_action_group) and [security_center_automation](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/security_center_automation).

-> **NOTE:** callback_url is populated for [Triggers with a type of](https://learn.microsoft.com/en-us/azure/logic-apps/logic-apps-workflow-actions-triggers#trigger-types-list) HTTPWebhook, Request, or ApiConnectionWebhook. For all other Trigger types, callback_url will be empty and should not be referenced.
nickel-tyler marked this conversation as resolved.
Show resolved Hide resolved

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ In addition to the Arguments listed above - the following Attributes are exporte

* `id` - The ID of the HTTP Request Trigger within the Logic App Workflow.

* `callback_url` - The URL for the workflow trigger
* `callback_url` - The URL of the Trigger within the Logic App Workflow. For use with certain resources like [monitor_action_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_action_group) and [security_center_automation](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/security_center_automation).

## Timeouts

Expand Down
Loading