From a3424863dd2b8aca333eafb03aee6cd201d713af Mon Sep 17 00:00:00 2001 From: Mani Bindra Date: Thu, 1 Aug 2019 20:30:56 +0530 Subject: [PATCH] New Resource azurerm_dev_test_lab_schedule - Fix for issue 2334 (#3554) This pull request is to fix issue #2334, It has changes to add new resource provider **azurerm_dev_test_lab_schedule**. **Additional Points** * Resource provider has been created based on [microsoft.devtestlab/2016-05-15/schedules](https://docs.microsoft.com/en-us/azure/templates/microsoft.devtestlab/2016-05-15/schedules) (fixes #2334) --- .../internal/services/devtestlabs/client.go | 4 + azurerm/provider.go | 1 + azurerm/resource_arm_dev_test_lab_schedule.go | 453 ++++++++++++++++++ ...resource_arm_dev_test_lab_schedule_test.go | 383 +++++++++++++++ .../r/dev_test_lab_schedule.html.markdown | 109 +++++ 5 files changed, 950 insertions(+) create mode 100644 azurerm/resource_arm_dev_test_lab_schedule.go create mode 100644 azurerm/resource_arm_dev_test_lab_schedule_test.go create mode 100644 website/docs/r/dev_test_lab_schedule.html.markdown diff --git a/azurerm/internal/services/devtestlabs/client.go b/azurerm/internal/services/devtestlabs/client.go index 191d5e7c4829..cb500dfca3a5 100644 --- a/azurerm/internal/services/devtestlabs/client.go +++ b/azurerm/internal/services/devtestlabs/client.go @@ -7,6 +7,7 @@ import ( type Client struct { LabsClient dtl.LabsClient + LabSchedulesClient dtl.SchedulesClient PoliciesClient dtl.PoliciesClient VirtualMachinesClient dtl.VirtualMachinesClient VirtualNetworksClient dtl.VirtualNetworksClient @@ -27,5 +28,8 @@ func BuildClient(o *common.ClientOptions) *Client { c.VirtualNetworksClient = dtl.NewVirtualNetworksClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&c.VirtualNetworksClient.Client, o.ResourceManagerAuthorizer) + c.LabSchedulesClient = dtl.NewSchedulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) + o.ConfigureClient(&c.LabSchedulesClient.Client, o.ResourceManagerAuthorizer) + return &c } diff --git a/azurerm/provider.go b/azurerm/provider.go index ddc906692dfd..a3b7b0f1f0cb 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -266,6 +266,7 @@ func Provider() terraform.ResourceProvider { "azurerm_databricks_workspace": resourceArmDatabricksWorkspace(), "azurerm_ddos_protection_plan": resourceArmDDoSProtectionPlan(), "azurerm_dev_test_lab": resourceArmDevTestLab(), + "azurerm_dev_test_schedule": resourceArmDevTestLabSchedules(), "azurerm_dev_test_linux_virtual_machine": resourceArmDevTestLinuxVirtualMachine(), "azurerm_dev_test_policy": resourceArmDevTestPolicy(), "azurerm_dev_test_virtual_network": resourceArmDevTestVirtualNetwork(), diff --git a/azurerm/resource_arm_dev_test_lab_schedule.go b/azurerm/resource_arm_dev_test_lab_schedule.go new file mode 100644 index 000000000000..051177d7dc53 --- /dev/null +++ b/azurerm/resource_arm_dev_test_lab_schedule.go @@ -0,0 +1,453 @@ +package azurerm + +import ( + "fmt" + "regexp" + + "github.com/Azure/azure-sdk-for-go/services/devtestlabs/mgmt/2016-05-15/dtl" + "github.com/Azure/azure-sdk-for-go/services/scheduler/mgmt/2016-03-01/scheduler" + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func resourceArmDevTestLabSchedules() *schema.Resource { + return &schema.Resource{ + Create: resourceArmDevTestLabSchedulesCreateUpdate, + Read: resourceArmDevTestLabSchedulesRead, + Update: resourceArmDevTestLabSchedulesCreateUpdate, + Delete: resourceArmDevTestLabSchedulesDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validate.NoEmptyStrings, + }, + + "location": azure.SchemaLocation(), + + "resource_group_name": azure.SchemaResourceGroupName(), + + "lab_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validate.DevTestLabName(), + }, + + "status": { + Type: schema.TypeString, + Optional: true, + Default: dtl.EnableStatusDisabled, + ValidateFunc: validation.StringInSlice([]string{ + string(dtl.EnableStatusEnabled), + string(dtl.EnableStatusDisabled), + }, false), + }, + + "task_type": { + Type: schema.TypeString, + Required: true, + }, + + "weekly_recurrence": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "time": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringMatch( + regexp.MustCompile("^(0[0-9]|1[0-9]|2[0-3]|[0-9])[0-5][0-9]$"), + "Time of day must match the format HHmm where HH is 00-23 and mm is 00-59", + ), + }, + + "week_days": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.StringInSlice([]string{ + string(scheduler.Monday), + string(scheduler.Tuesday), + string(scheduler.Wednesday), + string(scheduler.Thursday), + string(scheduler.Friday), + string(scheduler.Saturday), + string(scheduler.Sunday), + }, false), + }, + }, + }, + }, + }, + + "daily_recurrence": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "time": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringMatch( + regexp.MustCompile("^(0[0-9]|1[0-9]|2[0-3]|[0-9])[0-5][0-9]$"), + "Time of day must match the format HHmm where HH is 00-23 and mm is 00-59", + ), + }, + }, + }, + }, + + "hourly_recurrence": { + Type: schema.TypeList, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "minute": { + Type: schema.TypeInt, + Required: true, + ValidateFunc: validation.IntBetween(0, 59), + }, + }, + }, + }, + + "time_zone_id": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validateAzureVirtualMachineTimeZone(), + }, + + "notification_settings": { + Type: schema.TypeList, + Required: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "status": { + Type: schema.TypeString, + Optional: true, + Default: dtl.NotificationStatusDisabled, + ValidateFunc: validation.StringInSlice([]string{ + string(dtl.NotificationStatusEnabled), + string(dtl.NotificationStatusDisabled), + }, false), + }, + "time_in_minutes": { + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntAtLeast(1), + }, + "webhook_url": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, + }, + + "tags": tagsSchema(), + }, + } +} + +func resourceArmDevTestLabSchedulesCreateUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).devTestLabs.LabSchedulesClient + + ctx := meta.(*ArmClient).StopContext + + name := d.Get("name").(string) + devTestLabName := d.Get("lab_name").(string) + resGroup := d.Get("resource_group_name").(string) + + if requireResourcesToBeImported && d.IsNewResource() { + existing, err := client.Get(ctx, resGroup, devTestLabName, name, "") + if err != nil { + if !utils.ResponseWasNotFound(existing.Response) { + return fmt.Errorf("Error checking for presence of existing Schedule %q (Dev Test Lab %q / Resource Group %q): %s", name, devTestLabName, resGroup, err) + } + } + + if existing.ID != nil && *existing.ID != "" { + return tf.ImportAsExistsError("azurerm_dev_test_schedule", *existing.ID) + } + } + + location := azure.NormalizeLocation(d.Get("location").(string)) + tags := d.Get("tags").(map[string]interface{}) + + schedule := dtl.Schedule{ + Location: &location, + ScheduleProperties: &dtl.ScheduleProperties{}, + Tags: expandTags(tags), + } + + switch status := d.Get("status"); status { + case string(dtl.EnableStatusEnabled): + schedule.ScheduleProperties.Status = dtl.EnableStatusEnabled + case string(dtl.EnableStatusDisabled): + schedule.ScheduleProperties.Status = dtl.EnableStatusDisabled + default: + } + + if taskType := d.Get("task_type").(string); taskType != "" { + schedule.ScheduleProperties.TaskType = &taskType + } + + if timeZoneId := d.Get("time_zone_id").(string); timeZoneId != "" { + schedule.ScheduleProperties.TimeZoneID = &timeZoneId + } + + if v, ok := d.GetOk("weekly_recurrence"); ok { + weekRecurrence := expandArmDevTestLabScheduleRecurrenceWeekly(v) + + schedule.WeeklyRecurrence = weekRecurrence + } + + if v, ok := d.GetOk("daily_recurrence"); ok { + dailyRecurrence := expandArmDevTestLabScheduleRecurrenceDaily(v) + schedule.DailyRecurrence = dailyRecurrence + } + + if v, ok := d.GetOk("hourly_recurrence"); ok { + hourlyRecurrence := expandArmDevTestLabScheduleRecurrenceHourly(v) + + schedule.HourlyRecurrence = hourlyRecurrence + } + + if _, ok := d.GetOk("notification_settings"); ok { + notificationSettings := expandArmDevTestLabScheduleNotificationSettings(d) + schedule.NotificationSettings = notificationSettings + } + + if _, err := client.CreateOrUpdate(ctx, resGroup, devTestLabName, name, schedule); err != nil { + return err + } + + read, err := client.Get(ctx, resGroup, devTestLabName, name, "") + if err != nil { + return err + } + + if read.ID == nil { + return fmt.Errorf("Cannot read Dev Test Lab Schedule %s (resource group %s) ID", name, resGroup) + } + + d.SetId(*read.ID) + + return resourceArmDevTestLabSchedulesRead(d, meta) +} + +func resourceArmDevTestLabSchedulesRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).devTestLabs.LabSchedulesClient + ctx := meta.(*ArmClient).StopContext + + id, err := parseAzureResourceID(d.Id()) + if err != nil { + return err + } + resGroup := id.ResourceGroup + devTestLabName := id.Path["labs"] + name := id.Path["schedules"] + + resp, err := client.Get(ctx, resGroup, devTestLabName, name, "") + + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + d.SetId("") + return nil + } + return fmt.Errorf("Error making Read request on Dev Test Lab Schedule %s: %s", name, err) + } + + d.Set("name", resp.Name) + if location := resp.Location; location != nil { + d.Set("location", azure.NormalizeLocation(*location)) + } + d.Set("lab_name", devTestLabName) + d.Set("resource_group_name", resGroup) + d.Set("task_type", resp.TaskType) + + if props := resp.ScheduleProperties; props != nil { + + d.Set("time_zone_id", props.TimeZoneID) + + d.Set("status", string(props.Status)) + + if err := d.Set("weekly_recurrence", flattenAzureRmDevTestLabScheduleRecurrenceWeekly(props.WeeklyRecurrence)); err != nil { + return fmt.Errorf("Error setting `weeklyRecurrence`: %#v", err) + } + + if err := d.Set("daily_recurrence", flattenAzureRmDevTestLabScheduleRecurrenceDaily(props.DailyRecurrence)); err != nil { + return fmt.Errorf("Error setting `dailyRecurrence`: %#v", err) + } + + if err := d.Set("hourly_recurrence", flattenAzureRmDevTestLabScheduleRecurrenceHourly(props.HourlyRecurrence)); err != nil { + return fmt.Errorf("Error setting `dailyRecurrence`: %#v", err) + } + + if err := d.Set("notification_settings", flattenAzureRmDevTestLabScheduleNotificationSettings(props.NotificationSettings)); err != nil { + return fmt.Errorf("Error setting `notificationSettings`: %#v", err) + } + } + + flattenAndSetTags(d, resp.Tags) + + return nil +} + +func resourceArmDevTestLabSchedulesDelete(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).vmExtensionClient + ctx := meta.(*ArmClient).StopContext + + id, err := parseAzureResourceID(d.Id()) + if err != nil { + return err + } + resGroup := id.ResourceGroup + name := id.Path["schedules"] + devTestLabName := id.Path["labs"] + + future, err := client.Delete(ctx, resGroup, devTestLabName, name) + if err != nil { + return err + } + + return future.WaitForCompletionRef(ctx, client.Client) +} + +func expandArmDevTestLabScheduleRecurrenceDaily(recurrence interface{}) *dtl.DayDetails { + dailyRecurrenceConfigs := recurrence.([]interface{}) + dailyRecurrenceConfig := dailyRecurrenceConfigs[0].(map[string]interface{}) + dailyTime := dailyRecurrenceConfig["time"].(string) + + return &dtl.DayDetails{ + Time: &dailyTime, + } +} + +func flattenAzureRmDevTestLabScheduleRecurrenceDaily(dailyRecurrence *dtl.DayDetails) []interface{} { + if dailyRecurrence == nil { + return []interface{}{} + } + + result := make(map[string]interface{}) + + if dailyRecurrence.Time != nil { + result["time"] = *dailyRecurrence.Time + } + + return []interface{}{result} +} + +func expandArmDevTestLabScheduleRecurrenceWeekly(recurrence interface{}) *dtl.WeekDetails { + weeklyRecurrenceConfigs := recurrence.([]interface{}) + weeklyRecurrenceConfig := weeklyRecurrenceConfigs[0].(map[string]interface{}) + weeklyTime := weeklyRecurrenceConfig["time"].(string) + + weekDays := make([]string, 0) + for _, dayItem := range weeklyRecurrenceConfig["week_days"].([]interface{}) { + weekDays = append(weekDays, dayItem.(string)) + } + + return &dtl.WeekDetails{ + Time: &weeklyTime, + Weekdays: &weekDays, + } +} + +func flattenAzureRmDevTestLabScheduleRecurrenceWeekly(weeklyRecurrence *dtl.WeekDetails) []interface{} { + if weeklyRecurrence == nil { + return []interface{}{} + } + + result := make(map[string]interface{}) + + if weeklyRecurrence.Time != nil { + result["time"] = *weeklyRecurrence.Time + } + + weekDays := make([]string, 0) + if w := weeklyRecurrence.Weekdays; w != nil { + weekDays = *w + } + result["week_days"] = weekDays + + return []interface{}{result} +} + +func expandArmDevTestLabScheduleRecurrenceHourly(recurrence interface{}) *dtl.HourDetails { + hourlyRecurrenceConfigs := recurrence.([]interface{}) + hourlyRecurrenceConfig := hourlyRecurrenceConfigs[0].(map[string]interface{}) + hourlyMinute := int32(hourlyRecurrenceConfig["minute"].(int)) + + return &dtl.HourDetails{ + Minute: &hourlyMinute, + } +} + +func flattenAzureRmDevTestLabScheduleRecurrenceHourly(hourlyRecurrence *dtl.HourDetails) []interface{} { + if hourlyRecurrence == nil { + return []interface{}{} + } + + result := make(map[string]interface{}) + + if hourlyRecurrence.Minute != nil { + result["minute"] = *hourlyRecurrence.Minute + } + + return []interface{}{result} +} + +func expandArmDevTestLabScheduleNotificationSettings(d *schema.ResourceData) *dtl.NotificationSettings { + + notificationSettingsConfigs := d.Get("notification_settings").([]interface{}) + notificationSettingsConfig := notificationSettingsConfigs[0].(map[string]interface{}) + webhookUrl := notificationSettingsConfig["webhook_url"].(string) + timeInMinutes := int32(notificationSettingsConfig["time_in_minutes"].(int)) + + notificationStatus := dtl.NotificationStatus(notificationSettingsConfig["status"].(string)) + + return &dtl.NotificationSettings{ + WebhookURL: &webhookUrl, + TimeInMinutes: &timeInMinutes, + Status: notificationStatus, + } +} + +func flattenAzureRmDevTestLabScheduleNotificationSettings(notificationSettings *dtl.NotificationSettings) []interface{} { + if notificationSettings == nil { + return []interface{}{} + } + + result := make(map[string]interface{}) + + if notificationSettings.WebhookURL != nil { + result["webhook_url"] = *notificationSettings.WebhookURL + } + + if notificationSettings.TimeInMinutes != nil { + result["time_in_minutes"] = *notificationSettings.TimeInMinutes + } + + if string(notificationSettings.Status) != "" { + result["status"] = string(notificationSettings.Status) + } + + return []interface{}{result} +} diff --git a/azurerm/resource_arm_dev_test_lab_schedule_test.go b/azurerm/resource_arm_dev_test_lab_schedule_test.go new file mode 100644 index 000000000000..f1dd60ce587b --- /dev/null +++ b/azurerm/resource_arm_dev_test_lab_schedule_test.go @@ -0,0 +1,383 @@ +package azurerm + +import ( + "fmt" + "net/http" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" +) + +func TestAccAzureRMDevTestLabSchedule_autoShutdownBasic(t *testing.T) { + resourceName := "azurerm_dev_test_schedule.test" + ri := tf.AccRandTimeInt() + location := testLocation() + preConfig := testAccAzureRMDevTestLabSchedule_autoShutdownBasic(ri, location) + postConfig := testAccAzureRMDevTestLabSchedule_autoShutdownBasicUpdate(ri, location) + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMDevTestLabScheduleDestroy, + Steps: []resource.TestStep{ + { + Config: preConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMDevTestLabScheduleExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "status", "Disabled"), + resource.TestCheckResourceAttr(resourceName, "notification_settings.#", "1"), + resource.TestCheckResourceAttr(resourceName, "notification_settings.0.status", "Disabled"), + resource.TestCheckResourceAttr(resourceName, "daily_recurrence.#", "1"), + resource.TestCheckResourceAttr(resourceName, "daily_recurrence.0.time", "0100"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: postConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMDevTestLabScheduleExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "status", "Enabled"), + resource.TestCheckResourceAttr(resourceName, "notification_settings.#", "1"), + resource.TestCheckResourceAttr(resourceName, "notification_settings.0.status", "Enabled"), + resource.TestCheckResourceAttr(resourceName, "notification_settings.0.time_in_minutes", "30"), + resource.TestCheckResourceAttr(resourceName, "notification_settings.0.webhook_url", "https://www.bing.com/2/4"), + resource.TestCheckResourceAttr(resourceName, "daily_recurrence.#", "1"), + resource.TestCheckResourceAttr(resourceName, "daily_recurrence.0.time", "0900"), + ), + }, + }, + }) +} + +func TestAccAzureRMDevTestLabSchedule_autoStartupBasic(t *testing.T) { + resourceName := "azurerm_dev_test_schedule.test" + ri := tf.AccRandTimeInt() + location := testLocation() + preConfig := testAccAzureRMDevTestLabSchedule_autoStartupBasic(ri, location) + postConfig := testAccAzureRMDevTestLabSchedule_autoStartupBasicUpdate(ri, location) + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMDevTestLabScheduleDestroy, + Steps: []resource.TestStep{ + { + Config: preConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMDevTestLabScheduleExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "status", "Disabled"), + resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.#", "1"), + resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.0.time", "1100"), + resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.0.week_days.#", "2"), + resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.0.week_days.1", "Tuesday"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"task_type"}, + }, + { + Config: postConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMDevTestLabScheduleExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "status", "Enabled"), + resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.#", "1"), + resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.0.time", "1000"), + resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.0.week_days.#", "3"), + resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.0.week_days.1", "Thursday"), + ), + }, + }, + }) +} + +func TestAccAzureRMDevTestLabSchedule_concurrent(t *testing.T) { + firstResourceName := "azurerm_dev_test_schedule.test" + secondResourceName := "azurerm_dev_test_schedule.test2" + ri := tf.AccRandTimeInt() + config := testAccAzureRMDevTestLabSchedule_concurrent(ri, testLocation()) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMDevTestLabScheduleDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMDevTestLabScheduleExists(firstResourceName), + testCheckAzureRMDevTestLabScheduleExists(secondResourceName), + ), + }, + }, + }) +} + +func testCheckAzureRMDevTestLabScheduleExists(resourceName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Not found: %s", resourceName) + } + + name := rs.Primary.Attributes["name"] + devTestLabName := rs.Primary.Attributes["lab_name"] + resourceGroup := rs.Primary.Attributes["resource_group_name"] + + client := testAccProvider.Meta().(*ArmClient).devTestLabs.LabSchedulesClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + resp, err := client.Get(ctx, resourceGroup, devTestLabName, name, "") + if err != nil { + return fmt.Errorf("Bad: Get on devTestLabSchedulesClient: %s", err) + } + + if resp.StatusCode == http.StatusNotFound { + return fmt.Errorf("Bad: Dev Test Lab Schedule %q (resource group: %q) does not exist", name, resourceGroup) + } + + return nil + } +} + +func testCheckAzureRMDevTestLabScheduleDestroy(s *terraform.State) error { + client := testAccProvider.Meta().(*ArmClient).devTestLabs.LabSchedulesClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_dev_test_schedule" { + continue + } + + name := rs.Primary.Attributes["name"] + devTestLabName := rs.Primary.Attributes["azurerm_dev_test_lab"] + resourceGroup := rs.Primary.Attributes["resource_group_name"] + + resp, err := client.Get(ctx, resourceGroup, devTestLabName, name, "") + + if err != nil { + return nil + } + + if resp.StatusCode != http.StatusNotFound { + return fmt.Errorf("Dev Test Lab Schedule still exists:\n%#v", resp.ScheduleProperties) + } + } + + return nil +} + +func testAccAzureRMDevTestLabSchedule_autoShutdownBasic(rInt int, location string) string { + return fmt.Sprintf(` + resource "azurerm_resource_group" "test" { + name = "acctestrg-%d" + location = "%s" + } + + resource "azurerm_dev_test_lab" "test" { + name = "acctdtl-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + } + + resource "azurerm_dev_test_schedule" "test" { + name = "LabVmsShutdown" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + lab_name = "${azurerm_dev_test_lab.test.name}" + daily_recurrence { + time = "0100" + } + time_zone_id = "India Standard Time" + task_type = "LabVmsShutdownTask" + notification_settings { + + } + + tags = { + environment = "Production" + } + } +`, rInt, location, rInt) +} + +func testAccAzureRMDevTestLabSchedule_autoShutdownBasicUpdate(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestrg-%d" + location = "%s" +} + +resource "azurerm_dev_test_lab" "test" { + name = "acctdtl-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + +} + +resource "azurerm_dev_test_schedule" "test" { + name = "LabVmsShutdown" + status = "Enabled" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + lab_name = "${azurerm_dev_test_lab.test.name}" + daily_recurrence { + time = "0900" + } + time_zone_id = "India Standard Time" + task_type = "LabVmsShutdownTask" + notification_settings { + time_in_minutes = 30 + webhook_url = "https://www.bing.com/2/4" + status = "Enabled" + } +tags = { + environment = "Production" + } +} + +`, rInt, location, rInt) +} + +func testAccAzureRMDevTestLabSchedule_autoStartupBasic(rInt int, location string) string { + return fmt.Sprintf(` + resource "azurerm_resource_group" "test" { + name = "acctestrg-%d" + location = "%s" + } + + resource "azurerm_dev_test_lab" "test" { + name = "acctdtl-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + } + + resource "azurerm_dev_test_schedule" "test" { + name = "LabVmAutoStart" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + lab_name = "${azurerm_dev_test_lab.test.name}" + weekly_recurrence { + time = "1100" + week_days = ["Monday", "Tuesday"] + } + + time_zone_id = "India Standard Time" + task_type = "LabVmsStartupTask" + + notification_settings { + + } + + tags = { + environment = "Production" + } + } +`, rInt, location, rInt) +} + +func testAccAzureRMDevTestLabSchedule_autoStartupBasicUpdate(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestrg-%d" + location = "%s" +} + +resource "azurerm_dev_test_lab" "test" { + name = "acctdtl-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + +} + +resource "azurerm_dev_test_schedule" "test" { + name = "LabVmAutoStart" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + lab_name = "${azurerm_dev_test_lab.test.name}" + weekly_recurrence { + time = "1000" + week_days = ["Wednesday", "Thursday", "Friday"] + } + + time_zone_id = "India Standard Time" + task_type = "LabVmsStartupTask" + + notification_settings { + + } + + status = "Enabled" + + tags = { + environment = "Production" + } +} + +`, rInt, location, rInt) +} + +func testAccAzureRMDevTestLabSchedule_concurrent(rInt int, location string) string { + return fmt.Sprintf(` + resource "azurerm_resource_group" "test" { + name = "acctestrg-%d" + location = "%s" + } + + resource "azurerm_dev_test_lab" "test" { + name = "acctdtl-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + } + + resource "azurerm_dev_test_schedule" "test" { + name = "LabVmAutoStart" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + lab_name = "${azurerm_dev_test_lab.test.name}" + weekly_recurrence { + time = "1100" + week_days = ["Monday", "Tuesday"] + } + + time_zone_id = "India Standard Time" + task_type = "LabVmsStartupTask" + + notification_settings { + + } + + tags = { + environment = "Production" + } + } + + resource "azurerm_dev_test_schedule" "test2" { + name = "LabVmsShutdown" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + lab_name = "${azurerm_dev_test_lab.test.name}" + daily_recurrence { + time = "0100" + } + time_zone_id = "India Standard Time" + task_type = "LabVmsShutdownTask" + notification_settings { + + } + + tags = { + environment = "Production" + } + } +`, rInt, location, rInt) +} diff --git a/website/docs/r/dev_test_lab_schedule.html.markdown b/website/docs/r/dev_test_lab_schedule.html.markdown new file mode 100644 index 000000000000..80e4b2ed23ab --- /dev/null +++ b/website/docs/r/dev_test_lab_schedule.html.markdown @@ -0,0 +1,109 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_dev_test_schedule" +sidebar_current: "docs-azurerm-dev-test-lab-schedule" +description: |- + Manages automated startup and shutdown schedules for Azure Dev Test Lab. +--- + +# azurerm_dev_test_schedule + +Manages automated startup and shutdown schedules for Azure Dev Test Lab. + + +## Example Usage + +```hcl +resource "azurerm_resource_group" "sample" { + name = "acctestRG" + location = "West US" +} + +resource "azurerm_dev_test_lab" "sample" { + name = "YourDevTestLab" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + +} + +resource "azurerm_dev_test_schedule" "sample" { + name = "LabVmAutoStart" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + lab_name = "${azurerm_dev_test_lab.test.name}" + + weekly_recurrence { + time = "1100" + week_days = ["Monday", "Tuesday"] + } + + time_zone_id = "Pacific Standard Time" + task_type = "LabVmsStartupTask" + + notification_settings { + } + + tags = { + environment = "Production" + } +} + +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the dev test lab schedule. Valid value for name depends on the `task_type`. For instance for task_type `LabVmsStartupTask` the name needs to be `LabVmAutoStart`. + +* `location` - (Required) The location where the schedule is created. Changing this forces a new resource to be created. + +* `resource_group_name` - (Required) The name of the resource group in which to create the dev test lab schedule. Changing this forces a new resource to be created. + +* `lab_name` - (Required) The name of the dev test lab. Changing this forces a new resource to be created. + +* `status` - The status of this schedule. Possible values are `Enabled` and `Disabled`. Defaults to `Disabled`. + +* `task_type` - (Required) The task type of the schedule. Possible values include `LabVmsShutdownTask` and `LabVmAutoStart`. + +* `time_zone_id` - (Required) The time zone ID (e.g. Pacific Standard time). + +* `tags` - (Optional) A mapping of tags to assign to the resource. + +--- + +A `weekly_recurrence` - block supports the following: + +* `time` - The time when the schedule takes effect. + +* `week_days` - A list of days that this schedule takes effect . Possible values include `Monday`, `Tuesday`, `Wednesday`, `Thursday`, `Friday`, `Saturday` and `Sunday`. + +--- + +A `daily_recurrence` - block supports the following: + +* `time` - The time each day when the schedule takes effect. + +--- + +A `notification_settings` - (Required) - block supports the following: + +* `status` - The status of the notification. Possible values are `Enabled` and `Disabled`. Defaults to `Disabled` + +* `time_in_minutes` - Time in minutes before event at which notification will be sent. + +* `webhook_url` - The webhook URL to which the notification will be sent. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The Dev Test Lab Schedule ID. + +## Import + +Dev Test Schedule can be imported using the `resource id`, e.g. + +```shell +terraform import azurerm_dev_test_schedule.test /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.DevTestLab/labs/myDevTestLab/schedules/labvmautostart +```