diff --git a/azurerm/config.go b/azurerm/config.go index a87180944416..2461f7cbf67f 100644 --- a/azurerm/config.go +++ b/azurerm/config.go @@ -166,6 +166,7 @@ type ArmClient struct { logicWorkflowsClient logic.WorkflowsClient // Monitor + actionGroupsClient insights.ActionGroupsClient monitorAlertRulesClient insights.AlertRulesClient // MSI @@ -777,6 +778,10 @@ func (c *ArmClient) registerLogicClients(endpoint, subscriptionId string, auth a } func (c *ArmClient) registerMonitorClients(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) { + actionGroupsClient := insights.NewActionGroupsClientWithBaseURI(endpoint, subscriptionId) + c.configureClient(&actionGroupsClient.Client, auth) + c.actionGroupsClient = actionGroupsClient + arc := insights.NewAlertRulesClientWithBaseURI(endpoint, subscriptionId) setUserAgent(&arc.Client) arc.Authorizer = auth diff --git a/azurerm/provider.go b/azurerm/provider.go index 35889a2774ed..48b7a59d5283 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -196,6 +196,7 @@ func Provider() terraform.ResourceProvider { "azurerm_managed_disk": resourceArmManagedDisk(), "azurerm_management_lock": resourceArmManagementLock(), "azurerm_metric_alertrule": resourceArmMetricAlertRule(), + "azurerm_monitor_action_group": resourceArmMonitorActionGroup(), "azurerm_mysql_configuration": resourceArmMySQLConfiguration(), "azurerm_mysql_database": resourceArmMySqlDatabase(), "azurerm_mysql_firewall_rule": resourceArmMySqlFirewallRule(), diff --git a/azurerm/resource_arm_monitor_action_group.go b/azurerm/resource_arm_monitor_action_group.go new file mode 100644 index 000000000000..4abbcd7ca4bc --- /dev/null +++ b/azurerm/resource_arm_monitor_action_group.go @@ -0,0 +1,317 @@ +package azurerm + +import ( + "fmt" + + "github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2018-03-01/insights" + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func resourceArmMonitorActionGroup() *schema.Resource { + return &schema.Resource{ + Create: resourceArmMonitorActionGroupCreateOrUpdate, + Read: resourceArmMonitorActionGroupRead, + Update: resourceArmMonitorActionGroupCreateOrUpdate, + Delete: resourceArmMonitorActionGroupDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.NoZeroValues, + }, + + "resource_group_name": resourceGroupNameSchema(), + + "short_name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringLenBetween(1, 12), + }, + + "enabled": { + Type: schema.TypeBool, + Optional: true, + Default: true, + }, + + "email_receiver": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.NoZeroValues, + }, + "email_address": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.NoZeroValues, + }, + }, + }, + }, + + "sms_receiver": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.NoZeroValues, + }, + "country_code": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.NoZeroValues, + }, + "phone_number": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.NoZeroValues, + }, + }, + }, + }, + + "webhook_receiver": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.NoZeroValues, + }, + "service_uri": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.NoZeroValues, + }, + }, + }, + }, + + "tags": tagsSchema(), + }, + } +} + +func resourceArmMonitorActionGroupCreateOrUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).actionGroupsClient + ctx := meta.(*ArmClient).StopContext + + name := d.Get("name").(string) + resGroup := d.Get("resource_group_name").(string) + + shortName := d.Get("short_name").(string) + enabled := d.Get("enabled").(bool) + + emailReceiversRaw := d.Get("email_receiver").([]interface{}) + smsReceiversRaw := d.Get("sms_receiver").([]interface{}) + webhookReceiversRaw := d.Get("webhook_receiver").([]interface{}) + + tags := d.Get("tags").(map[string]interface{}) + expandedTags := expandTags(tags) + + parameters := insights.ActionGroupResource{ + Location: utils.String(azureRMNormalizeLocation("Global")), + ActionGroup: &insights.ActionGroup{ + GroupShortName: utils.String(shortName), + Enabled: utils.Bool(enabled), + EmailReceivers: expandMonitorActionGroupEmailReceiver(emailReceiversRaw), + SmsReceivers: expandMonitorActionGroupSmsReceiver(smsReceiversRaw), + WebhookReceivers: expandMonitorActionGroupWebHookReceiver(webhookReceiversRaw), + }, + Tags: expandedTags, + } + + _, err := client.CreateOrUpdate(ctx, resGroup, name, parameters) + if err != nil { + return fmt.Errorf("Error creating or updating action group %q (resource group %q): %+v", name, resGroup, err) + } + + read, err := client.Get(ctx, resGroup, name) + if err != nil { + return fmt.Errorf("Error getting action group %q (resource group %q) after creation: %+v", name, resGroup, err) + } + if read.ID == nil { + return fmt.Errorf("Action group %q (resource group %q) ID is empty", name, resGroup) + } + + d.SetId(*read.ID) + + return resourceArmMonitorActionGroupRead(d, meta) +} + +func resourceArmMonitorActionGroupRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).actionGroupsClient + ctx := meta.(*ArmClient).StopContext + + id, err := parseAzureResourceID(d.Id()) + if err != nil { + return err + } + resGroup := id.ResourceGroup + name := id.Path["actionGroups"] + + resp, err := client.Get(ctx, resGroup, name) + if err != nil { + if response.WasNotFound(resp.Response.Response) { + d.SetId("") + return nil + } + return fmt.Errorf("Error getting action group %q (resource group %q): %+v", name, resGroup, err) + } + + d.Set("name", name) + d.Set("resource_group_name", resGroup) + + if group := resp.ActionGroup; group != nil { + d.Set("short_name", group.GroupShortName) + d.Set("enabled", group.Enabled) + + if err = d.Set("email_receiver", flattenMonitorActionGroupEmailReceiver(group.EmailReceivers)); err != nil { + return fmt.Errorf("Error setting `email_receiver`: %+v", err) + } + + if err = d.Set("sms_receiver", flattenMonitorActionGroupSmsReceiver(group.SmsReceivers)); err != nil { + return fmt.Errorf("Error setting `sms_receiver`: %+v", err) + } + + if err = d.Set("webhook_receiver", flattenMonitorActionGroupWebHookReceiver(group.WebhookReceivers)); err != nil { + return fmt.Errorf("Error setting `webhook_receiver`: %+v", err) + } + } + + flattenAndSetTags(d, resp.Tags) + + return nil +} + +func resourceArmMonitorActionGroupDelete(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).actionGroupsClient + ctx := meta.(*ArmClient).StopContext + + id, err := parseAzureResourceID(d.Id()) + if err != nil { + return err + } + resGroup := id.ResourceGroup + name := id.Path["actionGroups"] + + resp, err := client.Delete(ctx, resGroup, name) + if err != nil { + if !response.WasNotFound(resp.Response) { + return fmt.Errorf("Error deleting action group %q (resource group %q): %+v", name, resGroup, err) + } + } + + return nil +} + +func expandMonitorActionGroupEmailReceiver(v []interface{}) *[]insights.EmailReceiver { + receivers := make([]insights.EmailReceiver, 0) + for _, receiverValue := range v { + val := receiverValue.(map[string]interface{}) + receiver := insights.EmailReceiver{ + Name: utils.String(val["name"].(string)), + EmailAddress: utils.String(val["email_address"].(string)), + } + receivers = append(receivers, receiver) + } + return &receivers +} + +func expandMonitorActionGroupSmsReceiver(v []interface{}) *[]insights.SmsReceiver { + receivers := make([]insights.SmsReceiver, 0) + for _, receiverValue := range v { + val := receiverValue.(map[string]interface{}) + receiver := insights.SmsReceiver{ + Name: utils.String(val["name"].(string)), + CountryCode: utils.String(val["country_code"].(string)), + PhoneNumber: utils.String(val["phone_number"].(string)), + } + receivers = append(receivers, receiver) + } + return &receivers +} + +func expandMonitorActionGroupWebHookReceiver(v []interface{}) *[]insights.WebhookReceiver { + receivers := make([]insights.WebhookReceiver, 0) + for _, receiverValue := range v { + val := receiverValue.(map[string]interface{}) + receiver := insights.WebhookReceiver{ + Name: utils.String(val["name"].(string)), + ServiceURI: utils.String(val["service_uri"].(string)), + } + receivers = append(receivers, receiver) + } + return &receivers +} + +func flattenMonitorActionGroupEmailReceiver(receivers *[]insights.EmailReceiver) []interface{} { + result := make([]interface{}, 0) + if receivers != nil { + for _, receiver := range *receivers { + val := make(map[string]interface{}, 0) + if receiver.Name != nil { + val["name"] = *receiver.Name + } + if receiver.EmailAddress != nil { + val["email_address"] = *receiver.EmailAddress + } + result = append(result, val) + } + } + return result +} + +func flattenMonitorActionGroupSmsReceiver(receivers *[]insights.SmsReceiver) []interface{} { + result := make([]interface{}, 0) + if receivers != nil { + for _, receiver := range *receivers { + val := make(map[string]interface{}, 0) + if receiver.Name != nil { + val["name"] = *receiver.Name + } + if receiver.CountryCode != nil { + val["country_code"] = *receiver.CountryCode + } + if receiver.PhoneNumber != nil { + val["phone_number"] = *receiver.PhoneNumber + } + result = append(result, val) + } + } + return result +} + +func flattenMonitorActionGroupWebHookReceiver(receivers *[]insights.WebhookReceiver) []interface{} { + result := make([]interface{}, 0) + if receivers != nil { + for _, receiver := range *receivers { + val := make(map[string]interface{}, 0) + if receiver.Name != nil { + val["name"] = *receiver.Name + } + if receiver.ServiceURI != nil { + val["service_uri"] = *receiver.ServiceURI + } + result = append(result, val) + } + } + return result +} diff --git a/azurerm/resource_arm_monitor_action_group_test.go b/azurerm/resource_arm_monitor_action_group_test.go new file mode 100644 index 000000000000..48e0a8a8fd13 --- /dev/null +++ b/azurerm/resource_arm_monitor_action_group_test.go @@ -0,0 +1,505 @@ +package azurerm + +import ( + "fmt" + "net/http" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAzureRMMonitorActionGroup_basic(t *testing.T) { + resourceName := "azurerm_monitor_action_group.test" + ri := acctest.RandInt() + config := testAccAzureRMMonitorActionGroup_basic(ri, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMMonitorActionGroupDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorActionGroupExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.#", "0"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.#", "0"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.#", "0"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMMonitorActionGroup_emailReceiver(t *testing.T) { + resourceName := "azurerm_monitor_action_group.test" + ri := acctest.RandInt() + config := testAccAzureRMMonitorActionGroup_emailReceiver(ri, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMMonitorActionGroupDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorActionGroupExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.#", "1"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.0.email_address", "admin@contoso.com"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.#", "0"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.#", "0"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMMonitorActionGroup_smsReceiver(t *testing.T) { + resourceName := "azurerm_monitor_action_group.test" + ri := acctest.RandInt() + config := testAccAzureRMMonitorActionGroup_smsReceiver(ri, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMMonitorActionGroupDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorActionGroupExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.#", "0"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.#", "1"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.0.country_code", "1"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.0.phone_number", "1231231234"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.#", "0"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMMonitorActionGroup_webhookReceiver(t *testing.T) { + resourceName := "azurerm_monitor_action_group.test" + ri := acctest.RandInt() + config := testAccAzureRMMonitorActionGroup_webhookReceiver(ri, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMMonitorActionGroupDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorActionGroupExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.#", "0"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.#", "0"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.#", "1"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.0.service_uri", "http://example.com/alert"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMMonitorActionGroup_complete(t *testing.T) { + resourceName := "azurerm_monitor_action_group.test" + ri := acctest.RandInt() + config := testAccAzureRMMonitorActionGroup_complete(ri, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMMonitorActionGroupDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorActionGroupExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.#", "2"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.0.email_address", "admin@contoso.com"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.1.email_address", "devops@contoso.com"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.#", "2"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.0.country_code", "1"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.0.phone_number", "1231231234"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.1.country_code", "86"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.1.phone_number", "13888888888"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.#", "2"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.0.service_uri", "http://example.com/alert"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.1.service_uri", "https://backup.example.com/warning"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMMonitorActionGroup_disabledUpdate(t *testing.T) { + resourceName := "azurerm_monitor_action_group.test" + ri := acctest.RandInt() + location := testLocation() + preConfig := testAccAzureRMMonitorActionGroup_disabledBasic(ri, location) + postConfig := testAccAzureRMMonitorActionGroup_basic(ri, location) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMMonitorActionGroupDestroy, + Steps: []resource.TestStep{ + { + Config: preConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorActionGroupExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "false"), + ), + }, + { + Config: postConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorActionGroupExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + ), + }, + { + Config: preConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorActionGroupExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "false"), + ), + }, + }, + }) +} + +func TestAccAzureRMMonitorActionGroup_singleReceiverUpdate(t *testing.T) { + resourceName := "azurerm_monitor_action_group.test" + ri := acctest.RandInt() + location := testLocation() + emailConfig := testAccAzureRMMonitorActionGroup_emailReceiver(ri, location) + smsConfig := testAccAzureRMMonitorActionGroup_smsReceiver(ri, location) + webhookConfig := testAccAzureRMMonitorActionGroup_webhookReceiver(ri, location) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMMonitorActionGroupDestroy, + Steps: []resource.TestStep{ + { + Config: emailConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorActionGroupExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.#", "1"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.0.email_address", "admin@contoso.com"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.#", "0"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.#", "0"), + ), + }, + { + Config: smsConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorActionGroupExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.#", "0"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.#", "1"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.0.country_code", "1"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.0.phone_number", "1231231234"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.#", "0"), + ), + }, + { + Config: webhookConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorActionGroupExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.#", "0"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.#", "0"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.#", "1"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.0.service_uri", "http://example.com/alert"), + ), + }, + }, + }) +} + +func TestAccAzureRMMonitorActionGroup_multipleReceiversUpdate(t *testing.T) { + resourceName := "azurerm_monitor_action_group.test" + ri := acctest.RandInt() + location := testLocation() + basicConfig := testAccAzureRMMonitorActionGroup_basic(ri, location) + completeConfig := testAccAzureRMMonitorActionGroup_complete(ri, location) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMMonitorActionGroupDestroy, + Steps: []resource.TestStep{ + { + Config: basicConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorActionGroupExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.#", "0"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.#", "0"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.#", "0"), + ), + }, + { + Config: completeConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorActionGroupExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.#", "2"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.0.email_address", "admin@contoso.com"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.1.email_address", "devops@contoso.com"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.#", "2"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.0.country_code", "1"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.0.phone_number", "1231231234"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.1.country_code", "86"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.1.phone_number", "13888888888"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.#", "2"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.0.service_uri", "http://example.com/alert"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.1.service_uri", "https://backup.example.com/warning"), + ), + }, + { + Config: basicConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorActionGroupExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "true"), + resource.TestCheckResourceAttr(resourceName, "email_receiver.#", "0"), + resource.TestCheckResourceAttr(resourceName, "sms_receiver.#", "0"), + resource.TestCheckResourceAttr(resourceName, "webhook_receiver.#", "0"), + ), + }, + }, + }) +} + +func testAccAzureRMMonitorActionGroup_basic(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_monitor_action_group" "test" { + name = "acctestActionGroup-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + short_name = "acctestag" +} +`, rInt, location, rInt) +} + +func testAccAzureRMMonitorActionGroup_emailReceiver(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_monitor_action_group" "test" { + name = "acctestActionGroup-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + short_name = "acctestag" + + email_receiver { + name = "sendtoadmin" + email_address = "admin@contoso.com" + } +} +`, rInt, location, rInt) +} + +func testAccAzureRMMonitorActionGroup_smsReceiver(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_monitor_action_group" "test" { + name = "acctestActionGroup-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + short_name = "acctestag" + + sms_receiver { + name = "oncallmsg" + country_code = "1" + phone_number = "1231231234" + } +} +`, rInt, location, rInt) +} + +func testAccAzureRMMonitorActionGroup_webhookReceiver(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_monitor_action_group" "test" { + name = "acctestActionGroup-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + short_name = "acctestag" + + webhook_receiver { + name = "callmyapiaswell" + service_uri = "http://example.com/alert" + } +} +`, rInt, location, rInt) +} + +func testAccAzureRMMonitorActionGroup_complete(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_monitor_action_group" "test" { + name = "acctestActionGroup-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + short_name = "acctestag" + + email_receiver { + name = "sendtoadmin" + email_address = "admin@contoso.com" + } + + email_receiver { + name = "sendtodevops" + email_address = "devops@contoso.com" + } + + sms_receiver { + name = "oncallmsg" + country_code = "1" + phone_number = "1231231234" + } + + sms_receiver { + name = "remotesupport" + country_code = "86" + phone_number = "13888888888" + } + + webhook_receiver { + name = "callmyapiaswell" + service_uri = "http://example.com/alert" + } + + webhook_receiver { + name = "callmybackupapi" + service_uri = "https://backup.example.com/warning" + } +} +`, rInt, location, rInt) +} + +func testAccAzureRMMonitorActionGroup_disabledBasic(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_monitor_action_group" "test" { + name = "acctestActionGroup-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + short_name = "acctestag" + enabled = false +} +`, rInt, location, rInt) +} + +func testCheckAzureRMMonitorActionGroupDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*ArmClient).actionGroupsClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_monitor_action_group" { + continue + } + + name := rs.Primary.Attributes["name"] + resourceGroup := rs.Primary.Attributes["resource_group_name"] + + resp, err := conn.Get(ctx, resourceGroup, name) + + if err != nil { + return nil + } + + if resp.StatusCode != http.StatusNotFound { + return fmt.Errorf("Action Group still exists:\n%#v", resp) + } + } + + return nil +} + +func testCheckAzureRMMonitorActionGroupExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + // Ensure we have enough information in state to look up in API + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + resourceName := rs.Primary.Attributes["name"] + resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] + if !hasResourceGroup { + return fmt.Errorf("Bad: no resource group found in state for Action Group Instance: %s", resourceName) + } + + conn := testAccProvider.Meta().(*ArmClient).actionGroupsClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + resp, err := conn.Get(ctx, resourceGroup, resourceName) + if err != nil { + return fmt.Errorf("Bad: Get on actionGroupsClient: %+v", err) + } + + if resp.StatusCode == http.StatusNotFound { + return fmt.Errorf("Bad: Action Group Instance %q (resource group: %q) does not exist", resourceName, resourceGroup) + } + + return nil + } +} diff --git a/website/azurerm.erb b/website/azurerm.erb index 3e9e9256ba3f..7d167b1d11e6 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -685,9 +685,13 @@ -