Skip to content

Commit

Permalink
New Resource: azurerm_logic_app_trigger_custom
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Jul 10, 2018
1 parent 0193c1d commit 4d77253
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 1 deletion.
29 changes: 29 additions & 0 deletions azurerm/import_arm_logic_app_trigger_custom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package azurerm

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMLogicAppTriggerCustom_importBasic(t *testing.T) {
ri := acctest.RandInt()
config := testAccAzureRMLogicAppTriggerCustom_basic(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLogicAppWorkflowDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: "azurerm_logic_app_trigger_custom.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_log_analytics_workspace": resourceArmLogAnalyticsWorkspace(),
"azurerm_logic_app_action_custom": resourceArmLogicAppActionCustom(),
"azurerm_logic_app_action_http": resourceArmLogicAppActionHTTP(),
"azurerm_logic_app_trigger_custom": resourceArmLogicAppTriggerCustom(),
"azurerm_logic_app_trigger_http_request": resourceArmLogicAppTriggerHttpRequest(),
"azurerm_logic_app_trigger_recurrence": resourceArmLogicAppTriggerRecurrence(),
"azurerm_logic_app_workflow": resourceArmLogicAppWorkflow(),
Expand Down
122 changes: 122 additions & 0 deletions azurerm/resource_arm_logic_app_trigger_custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package azurerm

import (
"fmt"
"log"

"encoding/json"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/structure"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

func resourceArmLogicAppTriggerCustom() *schema.Resource {
return &schema.Resource{
Create: resourceArmLogicAppTriggerCustomCreateUpdate,
Read: resourceArmLogicAppTriggerCustomRead,
Update: resourceArmLogicAppTriggerCustomCreateUpdate,
Delete: resourceArmLogicAppTriggerCustomDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"logic_app_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},

"body": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.ValidateJsonString,
DiffSuppressFunc: structure.SuppressJsonDiff,
},
},
}
}

func resourceArmLogicAppTriggerCustomCreateUpdate(d *schema.ResourceData, meta interface{}) error {
logicAppId := d.Get("logic_app_id").(string)
name := d.Get("name").(string)
bodyRaw := d.Get("body").(string)

var body map[string]interface{}
err := json.Unmarshal([]byte(bodyRaw), &body)
if err != nil {
return fmt.Errorf("Error unmarshalling JSON for Custom Trigger %q: %+v", name, err)
}

err = resourceLogicAppTriggerUpdate(d, meta, logicAppId, name, body)
if err != nil {
return err
}

return resourceArmLogicAppTriggerCustomRead(d, meta)
}

func resourceArmLogicAppTriggerCustomRead(d *schema.ResourceData, meta interface{}) error {
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

resourceGroup := id.ResourceGroup
logicAppName := id.Path["workflows"]
name := id.Path["triggers"]

t, app, err := retrieveLogicAppTrigger(meta, resourceGroup, logicAppName, name)
if err != nil {
return err
}

if t == nil {
log.Printf("[DEBUG] Logic App %q (Resource Group %q) does not contain Trigger %q - removing from state", logicAppName, resourceGroup, name)
d.SetId("")
return nil
}

action := *t

d.Set("name", name)
d.Set("logic_app_id", app.ID)

body, err := json.Marshal(action)
if err != nil {
return fmt.Errorf("Error serializing `body` for Trigger %q: %+v", name, err)
}

if err := d.Set("body", string(body)); err != nil {
return fmt.Errorf("Error flattening `body` for Trigger %q: %+v", name, err)
}

return nil
}

func resourceArmLogicAppTriggerCustomDelete(d *schema.ResourceData, meta interface{}) error {
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

resourceGroup := id.ResourceGroup
logicAppName := id.Path["workflows"]
name := id.Path["triggers"]

err = resourceLogicAppTriggerRemove(d, meta, resourceGroup, logicAppName, name)
if err != nil {
return fmt.Errorf("Error removing Trigger %q from Logic App %q (Resource Group %q): %+v", name, logicAppName, resourceGroup, err)
}

return nil
}
64 changes: 64 additions & 0 deletions azurerm/resource_arm_logic_app_trigger_custom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMLogicAppTriggerCustom_basic(t *testing.T) {
resourceName := "azurerm_logic_app_trigger_custom.test"
ri := acctest.RandInt()
config := testAccAzureRMLogicAppTriggerCustom_basic(ri, testLocation())
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLogicAppWorkflowDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLogicAppTriggerExists(resourceName),
),
},
},
})
}

func testAccAzureRMLogicAppTriggerCustom_basic(rInt int, location string) string {
template := testAccAzureRMLogicAppTriggerCustom_template(rInt, location)
return fmt.Sprintf(`
%s
resource "azurerm_logic_app_trigger_custom" "test" {
name = "recurrence-%d"
logic_app_id = "${azurerm_logic_app_workflow.test.id}"
body = <<BODY
{
"recurrence": {
"frequency": "Day",
"interval": 1
},
"type": "Recurrence"
}
BODY
}
`, template, rInt)
}

func testAccAzureRMLogicAppTriggerCustom_template(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_logic_app_workflow" "test" {
name = "acctestlaw-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, rInt, location, rInt)
}
8 changes: 8 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,18 @@
<a href="#">Logic App Resources</a>
<ul class="nav nav-visible">

<li<%= sidebar_current("docs-azurerm-resource-logic-app-action-custom") %>>
<a href="/docs/providers/azurerm/r/logic_app_action_custom.html">azurerm_logic_app_action_custom</a>
</li>

<li<%= sidebar_current("docs-azurerm-resource-logic-app-action-http") %>>
<a href="/docs/providers/azurerm/r/logic_app_action_http.html">azurerm_logic_app_action_http</a>
</li>

<li<%= sidebar_current("docs-azurerm-resource-logic-app-trigger-custom") %>>
<a href="/docs/providers/azurerm/r/logic_app_trigger_custom.html">azurerm_logic_app_trigger_custom</a>
</li>

<li<%= sidebar_current("docs-azurerm-resource-logic-app-trigger-http-request") %>>
<a href="/docs/providers/azurerm/r/logic_app_trigger_http_request.html">azurerm_logic_app_trigger_http_request</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/logic_app_action_custom.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ resource "azurerm_logic_app_workflow" "test" {
}
resource "azurerm_logic_app_action_custom" "test" {
name = "webhook"
name = "example-action"
logic_app_id = "${azurerm_logic_app_workflow.test.id}"
body = <<BODY
{
Expand Down
70 changes: 70 additions & 0 deletions website/docs/r/logic_app_trigger_custom.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_logic_app_trigger_custom"
sidebar_current: "docs-azurerm-resource-logic-app-trigger-custom"
description: |-
Manages a Custom Trigger within a Logic App Workflow
---

# azurerm_logic_app_trigger_custom

Manages a Custom Trigger within a Logic App Workflow

## Example Usage

```hcl
resource "azurerm_resource_group" "test" {
name = "workflow-resources"
location = "East US"
}
resource "azurerm_logic_app_workflow" "test" {
name = "workflow1"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_logic_app_trigger_custom" "test" {
name = "example-trigger"
logic_app_id = "${azurerm_logic_app_workflow.test.id}"
body = <<BODY
{
"recurrence": {
"frequency": "Day",
"interval": 1
},
"type": "Recurrence"
}
BODY
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) Specifies the name of the HTTP Trigger to be created within the Logic App Workflow. Changing this forces a new resource to be created.

-> **NOTE:** This name must be unique across all Triggers within the Logic App Workflow.

* `logic_app_id` - (Required) Specifies the ID of the Logic App Workflow. Changing this forces a new resource to be created.

* `body` - (Required) Specifies the JSON Blob defining the Body of this Custom Trigger.

-> **NOTE:** To make the Trigger more readable, you may wish to consider using HEREDOC syntax (as shown above) or [the `local_file` resource](https://www.terraform.io/docs/providers/local/d/file.html) to load the schema from a file on disk.

## Attributes Reference

The following attributes are exported:

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

## Import

Logic App Custom Triggers can be imported using the `resource id`, e.g.

```shell
terraform import azurerm_logic_app_trigger_custom.custom1 /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Logic/workflows/workflow1/triggers/custom1
```

-> **NOTE:** This ID is unique to Terraform and doesn't directly match to any other resource. To compose this ID, you can take the ID Logic App Workflow and append `/triggers/{name of the trigger}`.

0 comments on commit 4d77253

Please sign in to comment.