From 040f74d903147d08287d5c8acfe9f7d9938aa1e6 Mon Sep 17 00:00:00 2001 From: manisbindra Date: Thu, 30 May 2019 19:29:59 +0530 Subject: [PATCH 1/7] add res provider azurerm_dev_test_lab_schedule --- azurerm/config.go | 5 + azurerm/provider.go | 1 + azurerm/resource_arm_dev_test_lab_schedule.go | 488 +++++++++++++++ ...resource_arm_dev_test_lab_schedule_test.go | 561 ++++++++++++++++++ .../r/dev_test_lab_schedule.html.markdown | 120 ++++ 5 files changed, 1175 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/config.go b/azurerm/config.go index 2885f6bb7681..be0c1428c460 100644 --- a/azurerm/config.go +++ b/azurerm/config.go @@ -171,6 +171,7 @@ type ArmClient struct { // DevTestLabs devTestLabsClient dtl.LabsClient + devTestLabSchedulesClient dtl.SchedulesClient devTestPoliciesClient dtl.PoliciesClient devTestVirtualMachinesClient dtl.VirtualMachinesClient devTestVirtualNetworksClient dtl.VirtualNetworksClient @@ -930,6 +931,10 @@ func (c *ArmClient) registerDevTestClients(endpoint, subscriptionId string, auth devTestVirtualNetworksClient := dtl.NewVirtualNetworksClientWithBaseURI(endpoint, subscriptionId) c.configureClient(&devTestVirtualNetworksClient.Client, auth) c.devTestVirtualNetworksClient = devTestVirtualNetworksClient + + devTestLabSchedulesClient := dtl.NewSchedulesClientWithBaseURI(endpoint, subscriptionId) + c.configureClient(&devTestLabSchedulesClient.Client, auth) + c.devTestLabSchedulesClient = devTestLabSchedulesClient } func (c *ArmClient) registerDevSpaceClients(endpoint, subscriptionId string, auth autorest.Authorizer) { diff --git a/azurerm/provider.go b/azurerm/provider.go index 050f360c21a8..391b6909cbd1 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -260,6 +260,7 @@ func Provider() terraform.ResourceProvider { "azurerm_databricks_workspace": resourceArmDatabricksWorkspace(), "azurerm_ddos_protection_plan": resourceArmDDoSProtectionPlan(), "azurerm_dev_test_lab": resourceArmDevTestLab(), + "azurerm_dev_test_lab_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..d58213ce5049 --- /dev/null +++ b/azurerm/resource_arm_dev_test_lab_schedule.go @@ -0,0 +1,488 @@ +package azurerm + +import ( + "fmt" + + "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/tf" + "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, + }, + + "location": locationSchema(), + + "resource_group_name": resourceGroupNameSchema(), + + "dev_test_lab_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "status": { + Type: schema.TypeString, + Optional: true, + Default: dtl.EnableStatusDisabled, + ValidateFunc: validation.StringInSlice([]string{ + string(dtl.EnableStatusEnabled), + string(dtl.EnableStatusDisabled), + }, true), + }, + + "task_type": { + Type: schema.TypeString, + Required: true, + }, + + "weekly_recurrence": { + Type: schema.TypeSet, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "time": { + Type: schema.TypeString, + Required: true, + }, + + "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), + }, true), + DiffSuppressFunc: ignoreCaseDiffSuppressFunc, + }, + // Set: set.HashStringIgnoreCase, + }, + }, + }, + }, + + "daily_recurrence": { + Type: schema.TypeSet, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "time": { + Type: schema.TypeString, + Required: true, + }, + }, + }, + }, + + "hourly_recurrence": { + Type: schema.TypeSet, + Optional: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "minute": { + Type: schema.TypeInt, + Required: true, + }, + }, + }, + }, + + "time_zone_id": { + Type: schema.TypeString, + Required: true, + }, + + "notification_settings": { + Type: schema.TypeSet, + 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), + }, true), + }, + "time_in_minutes": { + Type: schema.TypeInt, + Optional: true, + Default: 0, + }, + "webhook_url": { + Type: schema.TypeString, + Optional: true, + Default: "", + }, + }, + }, + }, + + "tags": tagsSchema(), + }, + } +} + +func resourceArmDevTestLabSchedulesCreateUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).devTestLabSchedulesClient + + ctx := meta.(*ArmClient).StopContext + + name := d.Get("name").(string) + devTestLabName := d.Get("dev_test_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_lab_schedule", *existing.ID) + } + } + + location := azureRMNormalizeLocation(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 _, ok := d.GetOk("weekly_recurrence"); ok { + weekRecurrence, err2 := expandArmDevTestLabScheduleRecurrenceWeekly(d) + if err2 != nil { + return err2 + } + + schedule.WeeklyRecurrence = weekRecurrence + } + + if _, ok := d.GetOk("daily_recurrence"); ok { + dailyRecurrence, err2 := expandArmDevTestLabScheduleRecurrenceDaily(d) + if err2 != nil { + return err2 + } + + schedule.DailyRecurrence = dailyRecurrence + } + + if _, ok := d.GetOk("hourly_recurrence"); ok { + hourlyRecurrence, err2 := expandArmDevTestLabScheduleRecurrenceHourly(d) + if err2 != nil { + return err2 + } + + schedule.HourlyRecurrence = hourlyRecurrence + } + + if _, ok := d.GetOk("notification_settings"); ok { + notificationSettings, err2 := expandArmDevTestLabScheduleNotificationSettings(d) + if err2 != nil { + return err2 + } + schedule.NotificationSettings = notificationSettings + } + + _, err := client.CreateOrUpdate(ctx, resGroup, devTestLabName, name, schedule) + if 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).devTestLabSchedulesClient + 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", azureRMNormalizeLocation(*location)) + } + d.Set("dev_test_lab_name", devTestLabName) + d.Set("resource_group_name", resGroup) + + if props := resp.ScheduleProperties; props != nil { + if timeZoneId := props.TimeZoneID; *timeZoneId != "" { + d.Set("time_zone_id", *timeZoneId) + } + + switch status := props.Status; status { + case dtl.EnableStatusEnabled: + d.Set("status", string(dtl.EnableStatusEnabled)) + case dtl.EnableStatusDisabled: + d.Set("status", string(dtl.EnableStatusDisabled)) + default: + } + + if weeklyRecurrence := props.WeeklyRecurrence; weeklyRecurrence != nil { + if err := d.Set("weekly_recurrence", flattenAzureRmDevTestLabScheduleRecurrenceWeekly(props.WeeklyRecurrence)); err != nil { + return fmt.Errorf("Error setting `weeklyRecurrence`: %#v", err) + } + } + + if dailyRecurrence := props.DailyRecurrence; dailyRecurrence != nil { + if err := d.Set("daily_recurrence", flattenAzureRmDevTestLabScheduleRecurrenceDaily(props.DailyRecurrence)); err != nil { + return fmt.Errorf("Error setting `dailyRecurrence`: %#v", err) + } + } + + if hourlyRecurrence := props.HourlyRecurrence; hourlyRecurrence != nil { + 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(d *schema.ResourceData) (*dtl.DayDetails, error) { + dailyRecurrenceConfigs := d.Get("daily_recurrence").(*schema.Set).List() + dailyRecurrenceConfig := dailyRecurrenceConfigs[0].(map[string]interface{}) + dailyTime := dailyRecurrenceConfig["time"].(string) + + return &dtl.DayDetails{ + Time: &dailyTime, + }, nil + +} + +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(d *schema.ResourceData) (*dtl.WeekDetails, error) { + weeklyRecurrenceConfigs := d.Get("weekly_recurrence").(*schema.Set).List() + 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, + }, nil + +} + +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(d *schema.ResourceData) (*dtl.HourDetails, error) { + hourlyRecurrenceConfigs := d.Get("hourly_recurrence").(*schema.Set).List() + hourlyRecurrenceConfig := hourlyRecurrenceConfigs[0].(map[string]interface{}) + hourlyMinute := int32(hourlyRecurrenceConfig["minute"].(int)) + + return &dtl.HourDetails{ + Minute: &hourlyMinute, + }, nil + +} + +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, error) { + + notificationSettingsConfigs := d.Get("notification_settings").(*schema.Set).List() + notificationSettingsConfig := notificationSettingsConfigs[0].(map[string]interface{}) + webhookUrl := notificationSettingsConfig["webhook_url"].(string) + timeInMinutes := int32(notificationSettingsConfig["time_in_minutes"].(int)) + + var notificationStatus dtl.NotificationStatus + switch status := notificationSettingsConfig["status"]; status { + case string(dtl.NotificationStatusEnabled): + notificationStatus = dtl.NotificationStatusEnabled + case string(dtl.NotificationStatusDisabled): + notificationStatus = dtl.NotificationStatusDisabled + default: + } + + return &dtl.NotificationSettings{ + WebhookURL: &webhookUrl, + TimeInMinutes: &timeInMinutes, + Status: notificationStatus, + }, nil + +} + +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) != "" { + + var notificationStatus string + switch notificationSettings.Status { + case dtl.NotificationStatusEnabled: + notificationStatus = string(dtl.NotificationStatusEnabled) + case dtl.NotificationStatusDisabled: + notificationStatus = string(dtl.NotificationStatusDisabled) + default: + } + result["status"] = notificationStatus + } + + 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..579377548f7c --- /dev/null +++ b/azurerm/resource_arm_dev_test_lab_schedule_test.go @@ -0,0 +1,561 @@ +package azurerm + +import ( + "fmt" + "net/http" + "regexp" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" +) + +func TestAccAzureRMDevTestLabSchedule_autoShutdownPropertyRulesCheck(t *testing.T) { + ri := tf.AccRandTimeInt() + location := testLocation() + + missingRecurrenceConfig := testAccAzureRMDevTestLabSchedule_autoShutdownMissingRecurrence(ri, location) + missingDailyRecurrenceConfig := testAccAzureRMDevTestLabSchedule_autoShutdownMissingDailyRecurrence(ri, location) + incorrectTaskNameConfig := testAccAzureRMDevTestLabSchedule_autoShutdownIncorrectTaskName(ri, location) + taskNameTaskTypeMismatchConfig := testAccAzureRMDevTestLabSchedule_autoShutdowntaskNameTaskTypeMismatch(ri, location) + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMDevTestLabScheduleDestroy, + Steps: []resource.TestStep{ + { + Config: missingRecurrenceConfig, + ExpectError: regexp.MustCompile("MissingRequiredProperties"), + }, + { + Config: missingDailyRecurrenceConfig, + ExpectError: regexp.MustCompile("MissingRequiredProperty"), + }, + { + Config: incorrectTaskNameConfig, + ExpectError: regexp.MustCompile("IncorrectResourceName"), + }, + { + Config: taskNameTaskTypeMismatchConfig, + ExpectError: regexp.MustCompile("IncorrectResourceName"), + }, + }, + }) + +} + +func TestAccAzureRMDevTestLabSchedule_autoShutdownBasic(t *testing.T) { + resourceName := "azurerm_dev_test_lab_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.2350700632.status", "Disabled"), + resource.TestCheckResourceAttr(resourceName, "daily_recurrence.#", "1"), + resource.TestCheckResourceAttr(resourceName, "daily_recurrence.2029622669.time", "0100"), + ), + }, + { + 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, "notification_settings.#", "1"), + resource.TestCheckResourceAttr(resourceName, "notification_settings.941185200.status", "Enabled"), + resource.TestCheckResourceAttr(resourceName, "notification_settings.941185200.time_in_minutes", "30"), + resource.TestCheckResourceAttr(resourceName, "notification_settings.941185200.webhook_url", "https://www.bing.com/2/4"), + resource.TestCheckResourceAttr(resourceName, "daily_recurrence.#", "1"), + resource.TestCheckResourceAttr(resourceName, "daily_recurrence.3175988578.time", "0900"), + ), + }, + }, + }) +} + +func TestAccAzureRMDevTestLabSchedule_autoStartupBasic(t *testing.T) { + resourceName := "azurerm_dev_test_lab_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.504166703.time", "1100"), + resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.504166703.week_days.#", "2"), + resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.504166703.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.4041497624.time", "1000"), + resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.4041497624.week_days.#", "3"), + resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.4041497624.week_days.1", "Thursday"), + ), + }, + }, + }) +} + +func TestAccAzureRMDevTestLabSchedule_concurrent(t *testing.T) { + firstResourceName := "azurerm_dev_test_lab_schedule.test" + secondResourceName := "azurerm_dev_test_lab_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["dev_test_lab_name"] + resourceGroup := rs.Primary.Attributes["resource_group_name"] + + client := testAccProvider.Meta().(*ArmClient).devTestLabSchedulesClient + 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).devTestLabSchedulesClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_dev_test_lab_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_autoShutdowntaskNameTaskTypeMismatch(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_lab_schedule" "test" { + name = "LabVmsShutdown" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + dev_test_lab_name = "${azurerm_dev_test_lab.test.name}" + daily_recurrence { + time = "0100" + } + time_zone_id = "India Standard Time" + task_type = "LabVmsStartupTask" + notification_settings { + + } + + tags = { + environment = "Production" + } + } +`, rInt, location, rInt) +} + +func testAccAzureRMDevTestLabSchedule_autoShutdownIncorrectTaskName(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_lab_schedule" "test" { + name = "NotLabVmsShutdown" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + dev_test_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_autoShutdownMissingRecurrence(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_lab_schedule" "test" { + name = "LabVmsShutdown" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + dev_test_lab_name = "${azurerm_dev_test_lab.test.name}" + + time_zone_id = "India Standard Time" + task_type = "LabVmsShutdownTask" + notification_settings { + + } + + tags = { + environment = "Production" + } + } +`, rInt, location, rInt) +} + +func testAccAzureRMDevTestLabSchedule_autoShutdownMissingDailyRecurrence(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_lab_schedule" "test" { + name = "LabVmsShutdown" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + dev_test_lab_name = "${azurerm_dev_test_lab.test.name}" + + time_zone_id = "India Standard Time" + task_type = "LabVmsShutdownTask" + + weekly_recurrence { + time = "1100" + week_days = ["Monday", "Tuesday"] + } + + notification_settings { + + } + + tags = { + environment = "Production" + } + } +`, rInt, location, rInt) +} + +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_lab_schedule" "test" { + name = "LabVmsShutdown" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + dev_test_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_lab_schedule" "test" { + name = "LabVmsShutdown" + status = "Enabled" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + dev_test_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_lab_schedule" "test" { + name = "LabVmAutoStart" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + dev_test_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_lab_schedule" "test" { + name = "LabVmAutoStart" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + dev_test_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_lab_schedule" "test" { + name = "LabVmAutoStart" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + dev_test_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_lab_schedule" "test2" { + name = "LabVmsShutdown" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + dev_test_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..98e56e5bb908 --- /dev/null +++ b/website/docs/r/dev_test_lab_schedule.html.markdown @@ -0,0 +1,120 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_dev_test_lab_schedule" +sidebar_current: "docs-azurerm-dev-test-lab-schedule" +description: |- + Manages automated startup and shutdown schedules for Azure Dev Test Lab. +--- + +# azurerm_dev_test_lab_schedule + +Manages automated startup and shutdown schedules for Azure Dev Test Lab. + + +## Example Usage + +```hcl +resource "azurerm_resource_group" "test" { + name = "acctestRG" + location = "West US" +} + +resource "azurerm_dev_test_lab" "test" { + name = "acctestDTL" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + +} + +resource "azurerm_dev_test_lab_schedule" "test" { + name = "LabVmAutoStart" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + dev_test_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. + +* `dev_test_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 Lab Schedule can be imported using the `resource id`, e.g. + +```shell +terraform import azurerm_dev_test_lab_schedule.test /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.DevTestLab/labs/myDevTestLab/schedules/labvmautostart +``` From 0149d89d66b0340aa618836585215ff2b6ddcea2 Mon Sep 17 00:00:00 2001 From: kt Date: Thu, 6 Jun 2019 12:35:04 -0700 Subject: [PATCH 2/7] fix build --- azurerm/resource_arm_dev_test_lab_schedule.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/azurerm/resource_arm_dev_test_lab_schedule.go b/azurerm/resource_arm_dev_test_lab_schedule.go index d58213ce5049..602da72ce7cb 100644 --- a/azurerm/resource_arm_dev_test_lab_schedule.go +++ b/azurerm/resource_arm_dev_test_lab_schedule.go @@ -7,6 +7,7 @@ import ( "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/utils" ) @@ -28,9 +29,9 @@ func resourceArmDevTestLabSchedules() *schema.Resource { ForceNew: true, }, - "location": locationSchema(), + "location": azure.SchemaLocation(), - "resource_group_name": resourceGroupNameSchema(), + "resource_group_name": azure.SchemaResourceGroupName(), "dev_test_lab_name": { Type: schema.TypeString, @@ -175,7 +176,7 @@ func resourceArmDevTestLabSchedulesCreateUpdate(d *schema.ResourceData, meta int } } - location := azureRMNormalizeLocation(d.Get("location").(string)) + location := azure.NormalizeLocation(d.Get("location").(string)) tags := d.Get("tags").(map[string]interface{}) schedule := dtl.Schedule{ @@ -278,7 +279,7 @@ func resourceArmDevTestLabSchedulesRead(d *schema.ResourceData, meta interface{} d.Set("name", resp.Name) if location := resp.Location; location != nil { - d.Set("location", azureRMNormalizeLocation(*location)) + d.Set("location", azure.NormalizeLocation(*location)) } d.Set("dev_test_lab_name", devTestLabName) d.Set("resource_group_name", resGroup) From 04ee925762bc8c0cc5aca4647d25085ad15dbeb6 Mon Sep 17 00:00:00 2001 From: manisbindra Date: Thu, 20 Jun 2019 13:05:36 +0530 Subject: [PATCH 3/7] incorporated review comments --- azurerm/provider.go | 2 +- azurerm/resource_arm_dev_test_lab_schedule.go | 136 ++++------ ...resource_arm_dev_test_lab_schedule_test.go | 237 +++--------------- .../r/dev_test_lab_schedule.html.markdown | 14 +- 4 files changed, 85 insertions(+), 304 deletions(-) diff --git a/azurerm/provider.go b/azurerm/provider.go index dd148a6e095a..758c244d93ab 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -261,7 +261,7 @@ func Provider() terraform.ResourceProvider { "azurerm_databricks_workspace": resourceArmDatabricksWorkspace(), "azurerm_ddos_protection_plan": resourceArmDDoSProtectionPlan(), "azurerm_dev_test_lab": resourceArmDevTestLab(), - "azurerm_dev_test_lab_schedule": resourceArmDevTestLabSchedules(), + "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 index 602da72ce7cb..067c2e8e99ca 100644 --- a/azurerm/resource_arm_dev_test_lab_schedule.go +++ b/azurerm/resource_arm_dev_test_lab_schedule.go @@ -3,6 +3,8 @@ package azurerm import ( "fmt" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" + "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" @@ -33,10 +35,11 @@ func resourceArmDevTestLabSchedules() *schema.Resource { "resource_group_name": azure.SchemaResourceGroupName(), - "dev_test_lab_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + "lab_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validate.DevTestLabName(), }, "status": { @@ -46,7 +49,7 @@ func resourceArmDevTestLabSchedules() *schema.Resource { ValidateFunc: validation.StringInSlice([]string{ string(dtl.EnableStatusEnabled), string(dtl.EnableStatusDisabled), - }, true), + }, false), }, "task_type": { @@ -55,7 +58,7 @@ func resourceArmDevTestLabSchedules() *schema.Resource { }, "weekly_recurrence": { - Type: schema.TypeSet, + Type: schema.TypeList, Optional: true, MaxItems: 1, Elem: &schema.Resource{ @@ -78,17 +81,15 @@ func resourceArmDevTestLabSchedules() *schema.Resource { string(scheduler.Friday), string(scheduler.Saturday), string(scheduler.Sunday), - }, true), - DiffSuppressFunc: ignoreCaseDiffSuppressFunc, + }, false), }, - // Set: set.HashStringIgnoreCase, }, }, }, }, "daily_recurrence": { - Type: schema.TypeSet, + Type: schema.TypeList, Optional: true, MaxItems: 1, Elem: &schema.Resource{ @@ -102,7 +103,7 @@ func resourceArmDevTestLabSchedules() *schema.Resource { }, "hourly_recurrence": { - Type: schema.TypeSet, + Type: schema.TypeList, Optional: true, MaxItems: 1, Elem: &schema.Resource{ @@ -116,12 +117,13 @@ func resourceArmDevTestLabSchedules() *schema.Resource { }, "time_zone_id": { - Type: schema.TypeString, - Required: true, + Type: schema.TypeString, + Required: true, + ValidateFunc: validateAzureVirtualMachineTimeZone(), }, "notification_settings": { - Type: schema.TypeSet, + Type: schema.TypeList, Required: true, MaxItems: 1, Elem: &schema.Resource{ @@ -133,17 +135,15 @@ func resourceArmDevTestLabSchedules() *schema.Resource { ValidateFunc: validation.StringInSlice([]string{ string(dtl.NotificationStatusEnabled), string(dtl.NotificationStatusDisabled), - }, true), + }, false), }, "time_in_minutes": { Type: schema.TypeInt, Optional: true, - Default: 0, }, "webhook_url": { Type: schema.TypeString, Optional: true, - Default: "", }, }, }, @@ -160,7 +160,7 @@ func resourceArmDevTestLabSchedulesCreateUpdate(d *schema.ResourceData, meta int ctx := meta.(*ArmClient).StopContext name := d.Get("name").(string) - devTestLabName := d.Get("dev_test_lab_name").(string) + devTestLabName := d.Get("lab_name").(string) resGroup := d.Get("resource_group_name").(string) if requireResourcesToBeImported && d.IsNewResource() { @@ -172,7 +172,7 @@ func resourceArmDevTestLabSchedulesCreateUpdate(d *schema.ResourceData, meta int } if existing.ID != nil && *existing.ID != "" { - return tf.ImportAsExistsError("azurerm_dev_test_lab_schedule", *existing.ID) + return tf.ImportAsExistsError("azurerm_dev_test_schedule", *existing.ID) } } @@ -202,37 +202,24 @@ func resourceArmDevTestLabSchedulesCreateUpdate(d *schema.ResourceData, meta int } if _, ok := d.GetOk("weekly_recurrence"); ok { - weekRecurrence, err2 := expandArmDevTestLabScheduleRecurrenceWeekly(d) - if err2 != nil { - return err2 - } + weekRecurrence := expandArmDevTestLabScheduleRecurrenceWeekly(d) schedule.WeeklyRecurrence = weekRecurrence } if _, ok := d.GetOk("daily_recurrence"); ok { - dailyRecurrence, err2 := expandArmDevTestLabScheduleRecurrenceDaily(d) - if err2 != nil { - return err2 - } - + dailyRecurrence := expandArmDevTestLabScheduleRecurrenceDaily(d) schedule.DailyRecurrence = dailyRecurrence } if _, ok := d.GetOk("hourly_recurrence"); ok { - hourlyRecurrence, err2 := expandArmDevTestLabScheduleRecurrenceHourly(d) - if err2 != nil { - return err2 - } + hourlyRecurrence := expandArmDevTestLabScheduleRecurrenceHourly(d) schedule.HourlyRecurrence = hourlyRecurrence } if _, ok := d.GetOk("notification_settings"); ok { - notificationSettings, err2 := expandArmDevTestLabScheduleNotificationSettings(d) - if err2 != nil { - return err2 - } + notificationSettings := expandArmDevTestLabScheduleNotificationSettings(d) schedule.NotificationSettings = notificationSettings } @@ -281,38 +268,25 @@ func resourceArmDevTestLabSchedulesRead(d *schema.ResourceData, meta interface{} if location := resp.Location; location != nil { d.Set("location", azure.NormalizeLocation(*location)) } - d.Set("dev_test_lab_name", devTestLabName) + d.Set("lab_name", devTestLabName) d.Set("resource_group_name", resGroup) if props := resp.ScheduleProperties; props != nil { - if timeZoneId := props.TimeZoneID; *timeZoneId != "" { - d.Set("time_zone_id", *timeZoneId) - } - switch status := props.Status; status { - case dtl.EnableStatusEnabled: - d.Set("status", string(dtl.EnableStatusEnabled)) - case dtl.EnableStatusDisabled: - d.Set("status", string(dtl.EnableStatusDisabled)) - default: - } + d.Set("time_zone_id", props.TimeZoneID) - if weeklyRecurrence := props.WeeklyRecurrence; weeklyRecurrence != nil { - if err := d.Set("weekly_recurrence", flattenAzureRmDevTestLabScheduleRecurrenceWeekly(props.WeeklyRecurrence)); err != nil { - return fmt.Errorf("Error setting `weeklyRecurrence`: %#v", err) - } + 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 dailyRecurrence := props.DailyRecurrence; dailyRecurrence != nil { - if err := d.Set("daily_recurrence", flattenAzureRmDevTestLabScheduleRecurrenceDaily(props.DailyRecurrence)); err != nil { - return fmt.Errorf("Error setting `dailyRecurrence`: %#v", err) - } + if err := d.Set("daily_recurrence", flattenAzureRmDevTestLabScheduleRecurrenceDaily(props.DailyRecurrence)); err != nil { + return fmt.Errorf("Error setting `dailyRecurrence`: %#v", err) } - if hourlyRecurrence := props.HourlyRecurrence; hourlyRecurrence != nil { - if err := d.Set("hourly_recurrence", flattenAzureRmDevTestLabScheduleRecurrenceHourly(props.HourlyRecurrence)); 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 { @@ -345,14 +319,14 @@ func resourceArmDevTestLabSchedulesDelete(d *schema.ResourceData, meta interface return future.WaitForCompletionRef(ctx, client.Client) } -func expandArmDevTestLabScheduleRecurrenceDaily(d *schema.ResourceData) (*dtl.DayDetails, error) { - dailyRecurrenceConfigs := d.Get("daily_recurrence").(*schema.Set).List() +func expandArmDevTestLabScheduleRecurrenceDaily(d *schema.ResourceData) *dtl.DayDetails { + dailyRecurrenceConfigs := d.Get("daily_recurrence").([]interface{}) dailyRecurrenceConfig := dailyRecurrenceConfigs[0].(map[string]interface{}) dailyTime := dailyRecurrenceConfig["time"].(string) return &dtl.DayDetails{ Time: &dailyTime, - }, nil + } } @@ -370,8 +344,8 @@ func flattenAzureRmDevTestLabScheduleRecurrenceDaily(dailyRecurrence *dtl.DayDet return []interface{}{result} } -func expandArmDevTestLabScheduleRecurrenceWeekly(d *schema.ResourceData) (*dtl.WeekDetails, error) { - weeklyRecurrenceConfigs := d.Get("weekly_recurrence").(*schema.Set).List() +func expandArmDevTestLabScheduleRecurrenceWeekly(d *schema.ResourceData) *dtl.WeekDetails { + weeklyRecurrenceConfigs := d.Get("weekly_recurrence").([]interface{}) weeklyRecurrenceConfig := weeklyRecurrenceConfigs[0].(map[string]interface{}) weeklyTime := weeklyRecurrenceConfig["time"].(string) @@ -384,7 +358,7 @@ func expandArmDevTestLabScheduleRecurrenceWeekly(d *schema.ResourceData) (*dtl.W return &dtl.WeekDetails{ Time: &weeklyTime, Weekdays: &weekDays, - }, nil + } } @@ -408,14 +382,14 @@ func flattenAzureRmDevTestLabScheduleRecurrenceWeekly(weeklyRecurrence *dtl.Week return []interface{}{result} } -func expandArmDevTestLabScheduleRecurrenceHourly(d *schema.ResourceData) (*dtl.HourDetails, error) { - hourlyRecurrenceConfigs := d.Get("hourly_recurrence").(*schema.Set).List() +func expandArmDevTestLabScheduleRecurrenceHourly(d *schema.ResourceData) *dtl.HourDetails { + hourlyRecurrenceConfigs := d.Get("hourly_recurrence").([]interface{}) hourlyRecurrenceConfig := hourlyRecurrenceConfigs[0].(map[string]interface{}) hourlyMinute := int32(hourlyRecurrenceConfig["minute"].(int)) return &dtl.HourDetails{ Minute: &hourlyMinute, - }, nil + } } @@ -433,27 +407,20 @@ func flattenAzureRmDevTestLabScheduleRecurrenceHourly(hourlyRecurrence *dtl.Hour return []interface{}{result} } -func expandArmDevTestLabScheduleNotificationSettings(d *schema.ResourceData) (*dtl.NotificationSettings, error) { +func expandArmDevTestLabScheduleNotificationSettings(d *schema.ResourceData) *dtl.NotificationSettings { - notificationSettingsConfigs := d.Get("notification_settings").(*schema.Set).List() + notificationSettingsConfigs := d.Get("notification_settings").([]interface{}) notificationSettingsConfig := notificationSettingsConfigs[0].(map[string]interface{}) webhookUrl := notificationSettingsConfig["webhook_url"].(string) timeInMinutes := int32(notificationSettingsConfig["time_in_minutes"].(int)) - var notificationStatus dtl.NotificationStatus - switch status := notificationSettingsConfig["status"]; status { - case string(dtl.NotificationStatusEnabled): - notificationStatus = dtl.NotificationStatusEnabled - case string(dtl.NotificationStatusDisabled): - notificationStatus = dtl.NotificationStatusDisabled - default: - } + notificationStatus := dtl.NotificationStatus(notificationSettingsConfig["status"].(string)) return &dtl.NotificationSettings{ WebhookURL: &webhookUrl, TimeInMinutes: &timeInMinutes, Status: notificationStatus, - }, nil + } } @@ -473,16 +440,7 @@ func flattenAzureRmDevTestLabScheduleNotificationSettings(notificationSettings * } if string(notificationSettings.Status) != "" { - - var notificationStatus string - switch notificationSettings.Status { - case dtl.NotificationStatusEnabled: - notificationStatus = string(dtl.NotificationStatusEnabled) - case dtl.NotificationStatusDisabled: - notificationStatus = string(dtl.NotificationStatusDisabled) - default: - } - result["status"] = notificationStatus + result["status"] = 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 index 579377548f7c..773781328eb6 100644 --- a/azurerm/resource_arm_dev_test_lab_schedule_test.go +++ b/azurerm/resource_arm_dev_test_lab_schedule_test.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "net/http" - "regexp" "testing" "github.com/hashicorp/terraform/helper/resource" @@ -11,42 +10,8 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" ) -func TestAccAzureRMDevTestLabSchedule_autoShutdownPropertyRulesCheck(t *testing.T) { - ri := tf.AccRandTimeInt() - location := testLocation() - - missingRecurrenceConfig := testAccAzureRMDevTestLabSchedule_autoShutdownMissingRecurrence(ri, location) - missingDailyRecurrenceConfig := testAccAzureRMDevTestLabSchedule_autoShutdownMissingDailyRecurrence(ri, location) - incorrectTaskNameConfig := testAccAzureRMDevTestLabSchedule_autoShutdownIncorrectTaskName(ri, location) - taskNameTaskTypeMismatchConfig := testAccAzureRMDevTestLabSchedule_autoShutdowntaskNameTaskTypeMismatch(ri, location) - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testCheckAzureRMDevTestLabScheduleDestroy, - Steps: []resource.TestStep{ - { - Config: missingRecurrenceConfig, - ExpectError: regexp.MustCompile("MissingRequiredProperties"), - }, - { - Config: missingDailyRecurrenceConfig, - ExpectError: regexp.MustCompile("MissingRequiredProperty"), - }, - { - Config: incorrectTaskNameConfig, - ExpectError: regexp.MustCompile("IncorrectResourceName"), - }, - { - Config: taskNameTaskTypeMismatchConfig, - ExpectError: regexp.MustCompile("IncorrectResourceName"), - }, - }, - }) - -} - func TestAccAzureRMDevTestLabSchedule_autoShutdownBasic(t *testing.T) { - resourceName := "azurerm_dev_test_lab_schedule.test" + resourceName := "azurerm_dev_test_schedule.test" ri := tf.AccRandTimeInt() location := testLocation() preConfig := testAccAzureRMDevTestLabSchedule_autoShutdownBasic(ri, location) @@ -62,9 +27,9 @@ func TestAccAzureRMDevTestLabSchedule_autoShutdownBasic(t *testing.T) { testCheckAzureRMDevTestLabScheduleExists(resourceName), resource.TestCheckResourceAttr(resourceName, "status", "Disabled"), resource.TestCheckResourceAttr(resourceName, "notification_settings.#", "1"), - resource.TestCheckResourceAttr(resourceName, "notification_settings.2350700632.status", "Disabled"), + resource.TestCheckResourceAttr(resourceName, "notification_settings.0.status", "Disabled"), resource.TestCheckResourceAttr(resourceName, "daily_recurrence.#", "1"), - resource.TestCheckResourceAttr(resourceName, "daily_recurrence.2029622669.time", "0100"), + resource.TestCheckResourceAttr(resourceName, "daily_recurrence.0.time", "0100"), ), }, { @@ -79,11 +44,11 @@ func TestAccAzureRMDevTestLabSchedule_autoShutdownBasic(t *testing.T) { testCheckAzureRMDevTestLabScheduleExists(resourceName), resource.TestCheckResourceAttr(resourceName, "status", "Enabled"), resource.TestCheckResourceAttr(resourceName, "notification_settings.#", "1"), - resource.TestCheckResourceAttr(resourceName, "notification_settings.941185200.status", "Enabled"), - resource.TestCheckResourceAttr(resourceName, "notification_settings.941185200.time_in_minutes", "30"), - resource.TestCheckResourceAttr(resourceName, "notification_settings.941185200.webhook_url", "https://www.bing.com/2/4"), + 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.3175988578.time", "0900"), + resource.TestCheckResourceAttr(resourceName, "daily_recurrence.0.time", "0900"), ), }, }, @@ -91,7 +56,7 @@ func TestAccAzureRMDevTestLabSchedule_autoShutdownBasic(t *testing.T) { } func TestAccAzureRMDevTestLabSchedule_autoStartupBasic(t *testing.T) { - resourceName := "azurerm_dev_test_lab_schedule.test" + resourceName := "azurerm_dev_test_schedule.test" ri := tf.AccRandTimeInt() location := testLocation() preConfig := testAccAzureRMDevTestLabSchedule_autoStartupBasic(ri, location) @@ -107,9 +72,9 @@ func TestAccAzureRMDevTestLabSchedule_autoStartupBasic(t *testing.T) { testCheckAzureRMDevTestLabScheduleExists(resourceName), resource.TestCheckResourceAttr(resourceName, "status", "Disabled"), resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.#", "1"), - resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.504166703.time", "1100"), - resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.504166703.week_days.#", "2"), - resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.504166703.week_days.1", "Tuesday"), + 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"), ), }, { @@ -124,9 +89,9 @@ func TestAccAzureRMDevTestLabSchedule_autoStartupBasic(t *testing.T) { testCheckAzureRMDevTestLabScheduleExists(resourceName), resource.TestCheckResourceAttr(resourceName, "status", "Enabled"), resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.#", "1"), - resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.4041497624.time", "1000"), - resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.4041497624.week_days.#", "3"), - resource.TestCheckResourceAttr(resourceName, "weekly_recurrence.4041497624.week_days.1", "Thursday"), + 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"), ), }, }, @@ -134,8 +99,8 @@ func TestAccAzureRMDevTestLabSchedule_autoStartupBasic(t *testing.T) { } func TestAccAzureRMDevTestLabSchedule_concurrent(t *testing.T) { - firstResourceName := "azurerm_dev_test_lab_schedule.test" - secondResourceName := "azurerm_dev_test_lab_schedule.test2" + firstResourceName := "azurerm_dev_test_schedule.test" + secondResourceName := "azurerm_dev_test_schedule.test2" ri := tf.AccRandTimeInt() config := testAccAzureRMDevTestLabSchedule_concurrent(ri, testLocation()) @@ -163,7 +128,7 @@ func testCheckAzureRMDevTestLabScheduleExists(resourceName string) resource.Test } name := rs.Primary.Attributes["name"] - devTestLabName := rs.Primary.Attributes["dev_test_lab_name"] + devTestLabName := rs.Primary.Attributes["lab_name"] resourceGroup := rs.Primary.Attributes["resource_group_name"] client := testAccProvider.Meta().(*ArmClient).devTestLabSchedulesClient @@ -187,7 +152,7 @@ func testCheckAzureRMDevTestLabScheduleDestroy(s *terraform.State) error { ctx := testAccProvider.Meta().(*ArmClient).StopContext for _, rs := range s.RootModule().Resources { - if rs.Type != "azurerm_dev_test_lab_schedule" { + if rs.Type != "azurerm_dev_test_schedule" { continue } @@ -209,148 +174,6 @@ func testCheckAzureRMDevTestLabScheduleDestroy(s *terraform.State) error { return nil } -func testAccAzureRMDevTestLabSchedule_autoShutdowntaskNameTaskTypeMismatch(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_lab_schedule" "test" { - name = "LabVmsShutdown" - location = "${azurerm_resource_group.test.location}" - resource_group_name = "${azurerm_resource_group.test.name}" - dev_test_lab_name = "${azurerm_dev_test_lab.test.name}" - daily_recurrence { - time = "0100" - } - time_zone_id = "India Standard Time" - task_type = "LabVmsStartupTask" - notification_settings { - - } - - tags = { - environment = "Production" - } - } -`, rInt, location, rInt) -} - -func testAccAzureRMDevTestLabSchedule_autoShutdownIncorrectTaskName(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_lab_schedule" "test" { - name = "NotLabVmsShutdown" - location = "${azurerm_resource_group.test.location}" - resource_group_name = "${azurerm_resource_group.test.name}" - dev_test_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_autoShutdownMissingRecurrence(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_lab_schedule" "test" { - name = "LabVmsShutdown" - location = "${azurerm_resource_group.test.location}" - resource_group_name = "${azurerm_resource_group.test.name}" - dev_test_lab_name = "${azurerm_dev_test_lab.test.name}" - - time_zone_id = "India Standard Time" - task_type = "LabVmsShutdownTask" - notification_settings { - - } - - tags = { - environment = "Production" - } - } -`, rInt, location, rInt) -} - -func testAccAzureRMDevTestLabSchedule_autoShutdownMissingDailyRecurrence(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_lab_schedule" "test" { - name = "LabVmsShutdown" - location = "${azurerm_resource_group.test.location}" - resource_group_name = "${azurerm_resource_group.test.name}" - dev_test_lab_name = "${azurerm_dev_test_lab.test.name}" - - time_zone_id = "India Standard Time" - task_type = "LabVmsShutdownTask" - - weekly_recurrence { - time = "1100" - week_days = ["Monday", "Tuesday"] - } - - notification_settings { - - } - - tags = { - environment = "Production" - } - } -`, rInt, location, rInt) -} - func testAccAzureRMDevTestLabSchedule_autoShutdownBasic(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { @@ -365,11 +188,11 @@ func testAccAzureRMDevTestLabSchedule_autoShutdownBasic(rInt int, location strin } - resource "azurerm_dev_test_lab_schedule" "test" { + resource "azurerm_dev_test_schedule" "test" { name = "LabVmsShutdown" location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" - dev_test_lab_name = "${azurerm_dev_test_lab.test.name}" + lab_name = "${azurerm_dev_test_lab.test.name}" daily_recurrence { time = "0100" } @@ -400,12 +223,12 @@ resource "azurerm_dev_test_lab" "test" { } -resource "azurerm_dev_test_lab_schedule" "test" { +resource "azurerm_dev_test_schedule" "test" { name = "LabVmsShutdown" status = "Enabled" location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" - dev_test_lab_name = "${azurerm_dev_test_lab.test.name}" + lab_name = "${azurerm_dev_test_lab.test.name}" daily_recurrence { time = "0900" } @@ -438,11 +261,11 @@ func testAccAzureRMDevTestLabSchedule_autoStartupBasic(rInt int, location string } - resource "azurerm_dev_test_lab_schedule" "test" { + resource "azurerm_dev_test_schedule" "test" { name = "LabVmAutoStart" location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" - dev_test_lab_name = "${azurerm_dev_test_lab.test.name}" + lab_name = "${azurerm_dev_test_lab.test.name}" weekly_recurrence { time = "1100" week_days = ["Monday", "Tuesday"] @@ -476,11 +299,11 @@ resource "azurerm_dev_test_lab" "test" { } -resource "azurerm_dev_test_lab_schedule" "test" { +resource "azurerm_dev_test_schedule" "test" { name = "LabVmAutoStart" location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" - dev_test_lab_name = "${azurerm_dev_test_lab.test.name}" + lab_name = "${azurerm_dev_test_lab.test.name}" weekly_recurrence { time = "1000" week_days = ["Wednesday", "Thursday", "Friday"] @@ -517,11 +340,11 @@ func testAccAzureRMDevTestLabSchedule_concurrent(rInt int, location string) stri } - resource "azurerm_dev_test_lab_schedule" "test" { + resource "azurerm_dev_test_schedule" "test" { name = "LabVmAutoStart" location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" - dev_test_lab_name = "${azurerm_dev_test_lab.test.name}" + lab_name = "${azurerm_dev_test_lab.test.name}" weekly_recurrence { time = "1100" week_days = ["Monday", "Tuesday"] @@ -539,11 +362,11 @@ func testAccAzureRMDevTestLabSchedule_concurrent(rInt int, location string) stri } } - resource "azurerm_dev_test_lab_schedule" "test2" { + resource "azurerm_dev_test_schedule" "test2" { name = "LabVmsShutdown" location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" - dev_test_lab_name = "${azurerm_dev_test_lab.test.name}" + lab_name = "${azurerm_dev_test_lab.test.name}" daily_recurrence { time = "0100" } diff --git a/website/docs/r/dev_test_lab_schedule.html.markdown b/website/docs/r/dev_test_lab_schedule.html.markdown index 98e56e5bb908..771a3b5627ac 100644 --- a/website/docs/r/dev_test_lab_schedule.html.markdown +++ b/website/docs/r/dev_test_lab_schedule.html.markdown @@ -1,12 +1,12 @@ --- layout: "azurerm" -page_title: "Azure Resource Manager: azurerm_dev_test_lab_schedule" +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_lab_schedule +# azurerm_dev_test_schedule Manages automated startup and shutdown schedules for Azure Dev Test Lab. @@ -26,11 +26,11 @@ resource "azurerm_dev_test_lab" "test" { } -resource "azurerm_dev_test_lab_schedule" "test" { +resource "azurerm_dev_test_schedule" "test" { name = "LabVmAutoStart" location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" - dev_test_lab_name = "${azurerm_dev_test_lab.test.name}" + lab_name = "${azurerm_dev_test_lab.test.name}" weekly_recurrence { time = "1100" @@ -65,7 +65,7 @@ The following arguments are supported: create the dev test lab schedule. Changing this forces a new resource to be created. -* `dev_test_lab_name` - (Required) The name of the dev test lab. Changing +* `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` @@ -113,8 +113,8 @@ The following attributes are exported: ## Import -Dev Test Lab Schedule can be imported using the `resource id`, e.g. +Dev Test Schedule can be imported using the `resource id`, e.g. ```shell -terraform import azurerm_dev_test_lab_schedule.test /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.DevTestLab/labs/myDevTestLab/schedules/labvmautostart +terraform import azurerm_dev_test_schedule.test /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.DevTestLab/labs/myDevTestLab/schedules/labvmautostart ``` From 6ec331ceba7644e675870827a1ad0d3198e1cdf2 Mon Sep 17 00:00:00 2001 From: manisbindra Date: Thu, 20 Jun 2019 14:37:23 +0530 Subject: [PATCH 4/7] minor change as per review comments --- azurerm/resource_arm_dev_test_lab_schedule.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/resource_arm_dev_test_lab_schedule.go b/azurerm/resource_arm_dev_test_lab_schedule.go index d2ff64f77157..90da6ddfd2e2 100644 --- a/azurerm/resource_arm_dev_test_lab_schedule.go +++ b/azurerm/resource_arm_dev_test_lab_schedule.go @@ -440,7 +440,7 @@ func flattenAzureRmDevTestLabScheduleNotificationSettings(notificationSettings * } if string(notificationSettings.Status) != "" { - result["status"] = notificationSettings.Status + result["status"] = string(notificationSettings.Status) } return []interface{}{result} From 9d050aed8ecbbbba53f25e8c802ace4befe2dac6 Mon Sep 17 00:00:00 2001 From: manisbindra Date: Sun, 28 Jul 2019 12:47:52 +0530 Subject: [PATCH 5/7] merge conflict resolution --- azurerm/internal/services/devtestlabs/client.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/azurerm/internal/services/devtestlabs/client.go b/azurerm/internal/services/devtestlabs/client.go index 951ab353a674..cb500dfca3a5 100644 --- a/azurerm/internal/services/devtestlabs/client.go +++ b/azurerm/internal/services/devtestlabs/client.go @@ -28,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 } From 250430f509fc5fd8ade1f010c1ee13e52601516b Mon Sep 17 00:00:00 2001 From: manisbindra Date: Sun, 28 Jul 2019 18:24:41 +0530 Subject: [PATCH 6/7] incorporated review comments --- azurerm/resource_arm_dev_test_lab_schedule.go | 62 ++++++++++--------- ...resource_arm_dev_test_lab_schedule_test.go | 7 +-- .../r/dev_test_lab_schedule.html.markdown | 39 +++++------- 3 files changed, 51 insertions(+), 57 deletions(-) diff --git a/azurerm/resource_arm_dev_test_lab_schedule.go b/azurerm/resource_arm_dev_test_lab_schedule.go index 90da6ddfd2e2..051177d7dc53 100644 --- a/azurerm/resource_arm_dev_test_lab_schedule.go +++ b/azurerm/resource_arm_dev_test_lab_schedule.go @@ -2,8 +2,7 @@ package azurerm import ( "fmt" - - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" + "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" @@ -11,6 +10,7 @@ import ( "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" ) @@ -26,9 +26,10 @@ func resourceArmDevTestLabSchedules() *schema.Resource { Schema: map[string]*schema.Schema{ "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validate.NoEmptyStrings, }, "location": azure.SchemaLocation(), @@ -66,6 +67,10 @@ func resourceArmDevTestLabSchedules() *schema.Resource { "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": { @@ -97,6 +102,10 @@ func resourceArmDevTestLabSchedules() *schema.Resource { "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", + ), }, }, }, @@ -109,8 +118,9 @@ func resourceArmDevTestLabSchedules() *schema.Resource { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "minute": { - Type: schema.TypeInt, - Required: true, + Type: schema.TypeInt, + Required: true, + ValidateFunc: validation.IntBetween(0, 59), }, }, }, @@ -138,8 +148,9 @@ func resourceArmDevTestLabSchedules() *schema.Resource { }, false), }, "time_in_minutes": { - Type: schema.TypeInt, - Optional: true, + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntAtLeast(1), }, "webhook_url": { Type: schema.TypeString, @@ -201,19 +212,19 @@ func resourceArmDevTestLabSchedulesCreateUpdate(d *schema.ResourceData, meta int schedule.ScheduleProperties.TimeZoneID = &timeZoneId } - if _, ok := d.GetOk("weekly_recurrence"); ok { - weekRecurrence := expandArmDevTestLabScheduleRecurrenceWeekly(d) + if v, ok := d.GetOk("weekly_recurrence"); ok { + weekRecurrence := expandArmDevTestLabScheduleRecurrenceWeekly(v) schedule.WeeklyRecurrence = weekRecurrence } - if _, ok := d.GetOk("daily_recurrence"); ok { - dailyRecurrence := expandArmDevTestLabScheduleRecurrenceDaily(d) + if v, ok := d.GetOk("daily_recurrence"); ok { + dailyRecurrence := expandArmDevTestLabScheduleRecurrenceDaily(v) schedule.DailyRecurrence = dailyRecurrence } - if _, ok := d.GetOk("hourly_recurrence"); ok { - hourlyRecurrence := expandArmDevTestLabScheduleRecurrenceHourly(d) + if v, ok := d.GetOk("hourly_recurrence"); ok { + hourlyRecurrence := expandArmDevTestLabScheduleRecurrenceHourly(v) schedule.HourlyRecurrence = hourlyRecurrence } @@ -223,8 +234,7 @@ func resourceArmDevTestLabSchedulesCreateUpdate(d *schema.ResourceData, meta int schedule.NotificationSettings = notificationSettings } - _, err := client.CreateOrUpdate(ctx, resGroup, devTestLabName, name, schedule) - if err != nil { + if _, err := client.CreateOrUpdate(ctx, resGroup, devTestLabName, name, schedule); err != nil { return err } @@ -270,6 +280,7 @@ func resourceArmDevTestLabSchedulesRead(d *schema.ResourceData, meta interface{} } d.Set("lab_name", devTestLabName) d.Set("resource_group_name", resGroup) + d.Set("task_type", resp.TaskType) if props := resp.ScheduleProperties; props != nil { @@ -319,15 +330,14 @@ func resourceArmDevTestLabSchedulesDelete(d *schema.ResourceData, meta interface return future.WaitForCompletionRef(ctx, client.Client) } -func expandArmDevTestLabScheduleRecurrenceDaily(d *schema.ResourceData) *dtl.DayDetails { - dailyRecurrenceConfigs := d.Get("daily_recurrence").([]interface{}) +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{} { @@ -344,10 +354,9 @@ func flattenAzureRmDevTestLabScheduleRecurrenceDaily(dailyRecurrence *dtl.DayDet return []interface{}{result} } -func expandArmDevTestLabScheduleRecurrenceWeekly(d *schema.ResourceData) *dtl.WeekDetails { - weeklyRecurrenceConfigs := d.Get("weekly_recurrence").([]interface{}) +func expandArmDevTestLabScheduleRecurrenceWeekly(recurrence interface{}) *dtl.WeekDetails { + weeklyRecurrenceConfigs := recurrence.([]interface{}) weeklyRecurrenceConfig := weeklyRecurrenceConfigs[0].(map[string]interface{}) - weeklyTime := weeklyRecurrenceConfig["time"].(string) weekDays := make([]string, 0) @@ -359,7 +368,6 @@ func expandArmDevTestLabScheduleRecurrenceWeekly(d *schema.ResourceData) *dtl.We Time: &weeklyTime, Weekdays: &weekDays, } - } func flattenAzureRmDevTestLabScheduleRecurrenceWeekly(weeklyRecurrence *dtl.WeekDetails) []interface{} { @@ -382,15 +390,14 @@ func flattenAzureRmDevTestLabScheduleRecurrenceWeekly(weeklyRecurrence *dtl.Week return []interface{}{result} } -func expandArmDevTestLabScheduleRecurrenceHourly(d *schema.ResourceData) *dtl.HourDetails { - hourlyRecurrenceConfigs := d.Get("hourly_recurrence").([]interface{}) +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{} { @@ -421,7 +428,6 @@ func expandArmDevTestLabScheduleNotificationSettings(d *schema.ResourceData) *dt TimeInMinutes: &timeInMinutes, Status: notificationStatus, } - } func flattenAzureRmDevTestLabScheduleNotificationSettings(notificationSettings *dtl.NotificationSettings) []interface{} { diff --git a/azurerm/resource_arm_dev_test_lab_schedule_test.go b/azurerm/resource_arm_dev_test_lab_schedule_test.go index 790f72f86b7e..f1dd60ce587b 100644 --- a/azurerm/resource_arm_dev_test_lab_schedule_test.go +++ b/azurerm/resource_arm_dev_test_lab_schedule_test.go @@ -33,10 +33,9 @@ func TestAccAzureRMDevTestLabSchedule_autoShutdownBasic(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"task_type"}, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, }, { Config: postConfig, diff --git a/website/docs/r/dev_test_lab_schedule.html.markdown b/website/docs/r/dev_test_lab_schedule.html.markdown index 771a3b5627ac..901e73a7d4aa 100644 --- a/website/docs/r/dev_test_lab_schedule.html.markdown +++ b/website/docs/r/dev_test_lab_schedule.html.markdown @@ -14,23 +14,23 @@ Manages automated startup and shutdown schedules for Azure Dev Test Lab. ## Example Usage ```hcl -resource "azurerm_resource_group" "test" { +resource "azurerm_resource_group" "sample" { name = "acctestRG" location = "West US" } -resource "azurerm_dev_test_lab" "test" { - name = "acctestDTL" +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" "test" { +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}" + lab_name = "${azurerm_dev_test_lab.test.name}" weekly_recurrence { time = "1100" @@ -54,25 +54,17 @@ resource "azurerm_dev_test_schedule" "test" { 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` +* `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. +* `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. +* `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. +* `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`. +* `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`. +* `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) @@ -84,8 +76,7 @@ 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`. +* `week_days` - A list of days that this schedule takes effect . Possible values include `Monday`, `Tuesday`, `Wednesday`, `Thursday`, `Friday`, `Saturday` and `Sunday`. --- @@ -97,11 +88,9 @@ A `daily_recurrence` - block supports the following: A `notification_settings` - (Required) - block supports the following: -* `status` - The status of the notification. Possible values are `Enabled` - and `Disabled`. Defaults to `Disabled` +* `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. +* `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. From ab00a8535507781792c0c10ba1eb03e54d556690 Mon Sep 17 00:00:00 2001 From: manisbindra Date: Sun, 28 Jul 2019 18:58:16 +0530 Subject: [PATCH 7/7] minor cosmetic change to markdown --- website/docs/r/dev_test_lab_schedule.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/r/dev_test_lab_schedule.html.markdown b/website/docs/r/dev_test_lab_schedule.html.markdown index 901e73a7d4aa..80e4b2ed23ab 100644 --- a/website/docs/r/dev_test_lab_schedule.html.markdown +++ b/website/docs/r/dev_test_lab_schedule.html.markdown @@ -54,7 +54,7 @@ resource "azurerm_dev_test_schedule" "sample" { 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` +* `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. @@ -66,7 +66,7 @@ The following arguments are supported: * `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) +* `time_zone_id` - (Required) The time zone ID (e.g. Pacific Standard time). * `tags` - (Optional) A mapping of tags to assign to the resource.