diff --git a/azurerm/data_source_monitor_action_group.go b/azurerm/data_source_monitor_action_group.go new file mode 100644 index 000000000000..bac1c74dff4f --- /dev/null +++ b/azurerm/data_source_monitor_action_group.go @@ -0,0 +1,125 @@ +package azurerm + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceArmMonitorActionGroup() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmMonitorActionGroupRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.NoZeroValues, + }, + + "resource_group_name": resourceGroupNameForDataSourceSchema(), + + "short_name": { + Type: schema.TypeString, + Computed: true, + }, + + "enabled": { + Type: schema.TypeBool, + Computed: true, + }, + + "email_receiver": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Computed: true, + }, + "email_address": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + + "sms_receiver": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Computed: true, + }, + "country_code": { + Type: schema.TypeString, + Computed: true, + }, + "phone_number": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + + "webhook_receiver": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Computed: true, + }, + "service_uri": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourceArmMonitorActionGroupRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).monitorActionGroupsClient + ctx := meta.(*ArmClient).StopContext + + name := d.Get("name").(string) + resourceGroup := d.Get("resource_group_name").(string) + + resp, err := client.Get(ctx, resourceGroup, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Error: Action Group %q (Resource Group %q) was not found", name, resourceGroup) + } + return fmt.Errorf("Error making Read request on Action Group %q (Resource Group %q): %+v", name, resourceGroup, err) + } + d.SetId(*resp.ID) + + 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) + } + } + + return nil +} diff --git a/azurerm/data_source_monitor_action_group_test.go b/azurerm/data_source_monitor_action_group_test.go new file mode 100644 index 000000000000..72e5499bddf7 --- /dev/null +++ b/azurerm/data_source_monitor_action_group_test.go @@ -0,0 +1,181 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceArmMonitorActionGroup_basic(t *testing.T) { + dataSourceName := "data.azurerm_monitor_action_group.test" + ri := acctest.RandInt() + config := testAccDataSourceArmMonitorActionGroup_basic(ri, testLocation()) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(dataSourceName, "id"), + resource.TestCheckResourceAttr(dataSourceName, "enabled", "true"), + resource.TestCheckResourceAttr(dataSourceName, "short_name", "acctestag"), + resource.TestCheckResourceAttr(dataSourceName, "email_receiver.#", "0"), + resource.TestCheckResourceAttr(dataSourceName, "sms_receiver.#", "0"), + resource.TestCheckResourceAttr(dataSourceName, "webhook_receiver.#", "0"), + ), + }, + }, + }) +} + +func TestAccDataSourceArmMonitorActionGroup_disabledBasic(t *testing.T) { + dataSourceName := "data.azurerm_monitor_action_group.test" + ri := acctest.RandInt() + config := testAccDataSourceArmMonitorActionGroup_disabledBasic(ri, testLocation()) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(dataSourceName, "id"), + resource.TestCheckResourceAttr(dataSourceName, "enabled", "false"), + resource.TestCheckResourceAttr(dataSourceName, "short_name", "acctestag"), + resource.TestCheckResourceAttr(dataSourceName, "email_receiver.#", "0"), + resource.TestCheckResourceAttr(dataSourceName, "sms_receiver.#", "0"), + resource.TestCheckResourceAttr(dataSourceName, "webhook_receiver.#", "0"), + ), + }, + }, + }) +} + +func TestAccDataSourceArmMonitorActionGroup_complete(t *testing.T) { + dataSourceName := "data.azurerm_monitor_action_group.test" + ri := acctest.RandInt() + config := testAccDataSourceArmMonitorActionGroup_complete(ri, testLocation()) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(dataSourceName, "id"), + resource.TestCheckResourceAttr(dataSourceName, "enabled", "true"), + resource.TestCheckResourceAttr(dataSourceName, "email_receiver.#", "2"), + resource.TestCheckResourceAttr(dataSourceName, "email_receiver.0.email_address", "admin@contoso.com"), + resource.TestCheckResourceAttr(dataSourceName, "email_receiver.1.email_address", "devops@contoso.com"), + resource.TestCheckResourceAttr(dataSourceName, "sms_receiver.#", "2"), + resource.TestCheckResourceAttr(dataSourceName, "sms_receiver.0.country_code", "1"), + resource.TestCheckResourceAttr(dataSourceName, "sms_receiver.0.phone_number", "1231231234"), + resource.TestCheckResourceAttr(dataSourceName, "sms_receiver.1.country_code", "86"), + resource.TestCheckResourceAttr(dataSourceName, "sms_receiver.1.phone_number", "13888888888"), + resource.TestCheckResourceAttr(dataSourceName, "webhook_receiver.#", "2"), + resource.TestCheckResourceAttr(dataSourceName, "webhook_receiver.0.service_uri", "http://example.com/alert"), + resource.TestCheckResourceAttr(dataSourceName, "webhook_receiver.1.service_uri", "https://backup.example.com/warning"), + ), + }, + }, + }) +} + +func testAccDataSourceArmMonitorActionGroup_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" +} + +data "azurerm_monitor_action_group" "test" { + resource_group_name = "${azurerm_resource_group.test.name}" + name = "${azurerm_monitor_action_group.test.name}" +} +`, rInt, location, rInt) +} + +func testAccDataSourceArmMonitorActionGroup_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 +} + +data "azurerm_monitor_action_group" "test" { + resource_group_name = "${azurerm_resource_group.test.name}" + name = "${azurerm_monitor_action_group.test.name}" +} +`, rInt, location, rInt) +} + +func testAccDataSourceArmMonitorActionGroup_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" + } +} + +data "azurerm_monitor_action_group" "test" { + resource_group_name = "${azurerm_resource_group.test.name}" + name = "${azurerm_monitor_action_group.test.name}" +} +`, rInt, location, rInt) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index 9336bc664ac2..dfef8b0e46e3 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -97,6 +97,7 @@ func Provider() terraform.ResourceProvider { "azurerm_logic_app_workflow": dataSourceArmLogicAppWorkflow(), "azurerm_managed_disk": dataSourceArmManagedDisk(), "azurerm_management_group": dataSourceArmManagementGroup(), + "azurerm_monitor_action_group": dataSourceArmMonitorActionGroup(), "azurerm_monitor_diagnostic_categories": dataSourceArmMonitorDiagnosticCategories(), "azurerm_monitor_log_profile": dataSourceArmMonitorLogProfile(), "azurerm_network_interface": dataSourceArmNetworkInterface(), diff --git a/website/azurerm.erb b/website/azurerm.erb index 2b67dddc5242..a09cef371264 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -136,6 +136,10 @@ azurerm_management_group +