From 4d77253b692802410554dc908954c98d4d07348a Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 10 Jul 2018 15:37:11 +0200 Subject: [PATCH] New Resource: `azurerm_logic_app_trigger_custom` --- ...mport_arm_logic_app_trigger_custom_test.go | 29 +++++ azurerm/provider.go | 1 + .../resource_arm_logic_app_trigger_custom.go | 122 ++++++++++++++++++ ...ource_arm_logic_app_trigger_custom_test.go | 64 +++++++++ website/azurerm.erb | 8 ++ .../r/logic_app_action_custom.html.markdown | 2 +- .../r/logic_app_trigger_custom.html.markdown | 70 ++++++++++ 7 files changed, 295 insertions(+), 1 deletion(-) create mode 100644 azurerm/import_arm_logic_app_trigger_custom_test.go create mode 100644 azurerm/resource_arm_logic_app_trigger_custom.go create mode 100644 azurerm/resource_arm_logic_app_trigger_custom_test.go create mode 100644 website/docs/r/logic_app_trigger_custom.html.markdown diff --git a/azurerm/import_arm_logic_app_trigger_custom_test.go b/azurerm/import_arm_logic_app_trigger_custom_test.go new file mode 100644 index 0000000000000..c42c820917da8 --- /dev/null +++ b/azurerm/import_arm_logic_app_trigger_custom_test.go @@ -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, + }, + }, + }) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index af62d5ad26888..ad3a16ca7aeff 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -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(), diff --git a/azurerm/resource_arm_logic_app_trigger_custom.go b/azurerm/resource_arm_logic_app_trigger_custom.go new file mode 100644 index 0000000000000..49351214ba018 --- /dev/null +++ b/azurerm/resource_arm_logic_app_trigger_custom.go @@ -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 +} diff --git a/azurerm/resource_arm_logic_app_trigger_custom_test.go b/azurerm/resource_arm_logic_app_trigger_custom_test.go new file mode 100644 index 0000000000000..8023f7b1b0588 --- /dev/null +++ b/azurerm/resource_arm_logic_app_trigger_custom_test.go @@ -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 = <Logic App Resources