diff --git a/azurerm/automation_variable.go b/azurerm/automation_variable.go new file mode 100644 index 000000000000..6f04a19e9c13 --- /dev/null +++ b/azurerm/automation_variable.go @@ -0,0 +1,299 @@ +package azurerm + +import ( + "fmt" + "log" + "regexp" + "strconv" + "strings" + "time" + + "github.com/Azure/azure-sdk-for-go/services/automation/mgmt/2015-10-31/automation" + "github.com/hashicorp/terraform/helper/schema" + "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 parseAzureAutomationVariableValue(resource string, input *string) (interface{}, error) { + if input == nil { + if resource != "azurerm_automation_variable_null" { + return nil, fmt.Errorf("Expected value \"nil\" to be %q, actual type is \"azurerm_automation_variable_null\"", resource) + } + return nil, nil + } + + var value interface{} + var err error + actualResource := "Unknown" + datePattern := regexp.MustCompile(`"\\/Date\((-?[0-9]+)\)\\/"`) + matches := datePattern.FindStringSubmatch(*input) + + if len(matches) == 2 && matches[0] == *input { + if ticks, err := strconv.ParseInt(matches[1], 10, 64); err == nil { + value = time.Unix(ticks/1000, ticks%1000*1000000).In(time.UTC) + actualResource = "azurerm_automation_variable_datetime" + } + } else if value, err = strconv.Unquote(*input); err == nil { + actualResource = "azurerm_automation_variable_string" + } else if value, err = strconv.ParseBool(*input); err == nil { + actualResource = "azurerm_automation_variable_bool" + } else if value, err = strconv.ParseInt(*input, 10, 32); err == nil { + value = int32(value.(int64)) + actualResource = "azurerm_automation_variable_int" + } + + if actualResource != resource { + return nil, fmt.Errorf("Expected value %q to be %q, actual type is %q", *input, resource, actualResource) + } + return value, nil +} + +func resourceAutomationVariableCommonSchema(attType schema.ValueType, validateFunc schema.SchemaValidateFunc) map[string]*schema.Schema { + return map[string]*schema.Schema{ + "resource_group_name": resourceGroupNameSchema(), + + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validate.NoEmptyStrings, + }, + + "automation_account_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validate.NoEmptyStrings, + }, + + "description": { + Type: schema.TypeString, + Optional: true, + }, + + "encrypted": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "value": { + Type: attType, + Optional: true, + ValidateFunc: validateFunc, + }, + } +} + +func datasourceAutomationVariableCommonSchema(attType schema.ValueType) map[string]*schema.Schema { + return map[string]*schema.Schema{ + "resource_group_name": resourceGroupNameSchema(), + + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validate.NoEmptyStrings, + }, + + "automation_account_name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validate.NoEmptyStrings, + }, + + "description": { + Type: schema.TypeString, + Computed: true, + }, + + "encrypted": { + Type: schema.TypeBool, + Computed: true, + }, + + "value": { + Type: attType, + Computed: true, + }, + } +} + +func resourceAutomationVariableCreateUpdate(d *schema.ResourceData, meta interface{}, varType string) error { + client := meta.(*ArmClient).automationVariableClient + ctx := meta.(*ArmClient).StopContext + + name := d.Get("name").(string) + resourceGroup := d.Get("resource_group_name").(string) + accountName := d.Get("automation_account_name").(string) + varTypeLower := strings.ToLower(varType) + + if requireResourcesToBeImported { + resp, err := client.Get(ctx, resourceGroup, accountName, name) + if err != nil { + if !utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Error checking for present of existing Automation %s Variable %q (Automation Account Name %q / Resource Group %q): %+v", varType, name, accountName, resourceGroup, err) + } + } + + if !utils.ResponseWasNotFound(resp.Response) { + return tf.ImportAsExistsError(fmt.Sprintf("azurerm_automation_variable_%s", varTypeLower), *resp.ID) + } + } + + description := d.Get("description").(string) + encrypted := d.Get("encrypted").(bool) + value := "" + + if varTypeLower == "datetime" { + vTime, parseErr := time.Parse(time.RFC3339, d.Get("value").(string)) + if parseErr != nil { + return fmt.Errorf("Error invalid time format: %+v", parseErr) + } + value = fmt.Sprintf("\"\\/Date(%d)\\/\"", vTime.UnixNano()/1000000) + } else if varTypeLower == "bool" { + value = strconv.FormatBool(d.Get("value").(bool)) + } else if varTypeLower == "int" { + value = strconv.Itoa(d.Get("value").(int)) + } else if varTypeLower == "string" { + value = strconv.Quote(d.Get("value").(string)) + } + + parameters := automation.VariableCreateOrUpdateParameters{ + Name: utils.String(name), + VariableCreateOrUpdateProperties: &automation.VariableCreateOrUpdateProperties{ + Description: utils.String(description), + IsEncrypted: utils.Bool(encrypted), + }, + } + + if varTypeLower != "null" { + parameters.VariableCreateOrUpdateProperties.Value = utils.String(value) + } + + if _, err := client.CreateOrUpdate(ctx, resourceGroup, accountName, name, parameters); err != nil { + return fmt.Errorf("Error creating Automation %s Variable %q (Automation Account Name %q / Resource Group %q): %+v", varType, name, accountName, resourceGroup, err) + } + + resp, err := client.Get(ctx, resourceGroup, accountName, name) + if err != nil { + return fmt.Errorf("Error retrieving Automation %s Variable %q (Automation Account Name %q / Resource Group %q): %+v", varType, name, accountName, resourceGroup, err) + } + if resp.ID == nil { + return fmt.Errorf("Cannot read Automation %s Variable %q (Automation Account Name %q / Resource Group %q) ID", varType, name, accountName, resourceGroup) + } + d.SetId(*resp.ID) + + return resourceAutomationVariableRead(d, meta, varType) +} + +func resourceAutomationVariableRead(d *schema.ResourceData, meta interface{}, varType string) error { + client := meta.(*ArmClient).automationVariableClient + ctx := meta.(*ArmClient).StopContext + + id, err := parseAzureResourceID(d.Id()) + if err != nil { + return err + } + + resourceGroup := id.ResourceGroup + accountName := id.Path["automationAccounts"] + name := id.Path["variables"] + varTypeLower := strings.ToLower(varType) + + resp, err := client.Get(ctx, resourceGroup, accountName, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + log.Printf("[INFO] Automation %s Variable %q does not exist - removing from state", varType, d.Id()) + d.SetId("") + return nil + } + return fmt.Errorf("Error reading Automation %s Variable %q (Automation Account Name %q / Resource Group %q): %+v", varType, name, accountName, resourceGroup, err) + } + + d.Set("name", resp.Name) + d.Set("resource_group_name", resourceGroup) + d.Set("automation_account_name", accountName) + if properties := resp.VariableProperties; properties != nil { + d.Set("description", properties.Description) + d.Set("encrypted", properties.IsEncrypted) + if !d.Get("encrypted").(bool) { + value, err := parseAzureAutomationVariableValue(fmt.Sprintf("azurerm_automation_variable_%s", varTypeLower), properties.Value) + if err != nil { + return err + } + + if varTypeLower == "datetime" { + d.Set("value", value.(time.Time).Format("2006-01-02T15:04:05.999Z")) + } else if varTypeLower != "null" { + d.Set("value", value) + } + } + } + + return nil +} + +func datasourceAutomationVariableRead(d *schema.ResourceData, meta interface{}, varType string) error { + client := meta.(*ArmClient).automationVariableClient + ctx := meta.(*ArmClient).StopContext + + resourceGroup := d.Get("resource_group_name").(string) + accountName := d.Get("automation_account_name").(string) + name := d.Get("name").(string) + varTypeLower := strings.ToLower(varType) + + resp, err := client.Get(ctx, resourceGroup, accountName, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + log.Printf("[INFO] Automation %s Variable %q does not exist - removing from state", varType, d.Id()) + d.SetId("") + return nil + } + return fmt.Errorf("Error reading Automation %s Variable %q (Automation Account Name %q / Resource Group %q): %+v", varType, name, accountName, resourceGroup, err) + } + + d.SetId(*resp.ID) + + d.Set("name", resp.Name) + d.Set("resource_group_name", resourceGroup) + d.Set("automation_account_name", accountName) + if properties := resp.VariableProperties; properties != nil { + d.Set("description", properties.Description) + d.Set("encrypted", properties.IsEncrypted) + if !d.Get("encrypted").(bool) { + value, err := parseAzureAutomationVariableValue(fmt.Sprintf("azurerm_automation_variable_%s", varTypeLower), properties.Value) + if err != nil { + return err + } + + if varTypeLower == "datetime" { + d.Set("value", value.(time.Time).Format("2006-01-02T15:04:05.999Z")) + } else if varTypeLower != "null" { + d.Set("value", value) + } + } + } + + return nil +} + +func resourceAutomationVariableDelete(d *schema.ResourceData, meta interface{}, varType string) error { + client := meta.(*ArmClient).automationVariableClient + ctx := meta.(*ArmClient).StopContext + + id, err := parseAzureResourceID(d.Id()) + if err != nil { + return err + } + + resourceGroup := id.ResourceGroup + accountName := id.Path["automationAccounts"] + name := id.Path["variables"] + + if _, err := client.Delete(ctx, resourceGroup, accountName, name); err != nil { + return fmt.Errorf("Error deleting Automation %s Variable %q (Automation Account Name %q / Resource Group %q): %+v", varType, name, accountName, resourceGroup, err) + } + + return nil +} diff --git a/azurerm/automation_variable_test.go b/azurerm/automation_variable_test.go new file mode 100644 index 000000000000..c3ca3a9b5cd6 --- /dev/null +++ b/azurerm/automation_variable_test.go @@ -0,0 +1,132 @@ +package azurerm + +import ( + "fmt" + "strings" + "testing" + "time" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func TestParseAzureRmAutomationVariableValue(t *testing.T) { + type ExpectFunc func(interface{}) bool + cases := []struct { + Name string + Resource string + IsNil bool + Value string + HasError bool + ExpectValue interface{} + Expect ExpectFunc + }{ + { + Name: "string variable", + Resource: "azurerm_automation_variable_string", + Value: "\"Test String\"", + HasError: false, + ExpectValue: "Test String", + Expect: func(v interface{}) bool { return v.(string) == "Test String" }, + }, + { + Name: "integer variable", + Resource: "azurerm_automation_variable_int", + Value: "135", + HasError: false, + ExpectValue: 135, + Expect: func(v interface{}) bool { return v.(int32) == 135 }, + }, + { + Name: "boolean variable", + Resource: "azurerm_automation_variable_bool", + Value: "true", + HasError: false, + ExpectValue: true, + Expect: func(v interface{}) bool { return v.(bool) == true }, + }, + { + Name: "datetime variable", + Resource: "azurerm_automation_variable_datetime", + Value: "\"\\/Date(1556142054074)\\/\"", + HasError: false, + ExpectValue: time.Date(2019, time.April, 24, 21, 40, 54, 74000000, time.UTC), + Expect: func(v interface{}) bool { + return v.(time.Time) == time.Date(2019, time.April, 24, 21, 40, 54, 74000000, time.UTC) + }, + }, + } + + for _, tc := range cases { + t.Run(tc.Name, func(t *testing.T) { + value := &tc.Value + if tc.IsNil { + value = nil + } + actual, err := parseAzureAutomationVariableValue(tc.Resource, value) + if tc.HasError && err == nil { + t.Fatalf("Expect parseAzureAutomationVariableValue to return error for resource %q and value %s", tc.Resource, tc.Value) + } + if !tc.HasError { + if err != nil { + t.Fatalf("Expect parseAzureAutomationVariableValue to return no error for resource %q and value %s, err: %+v", tc.Resource, tc.Value, err) + } else if !tc.Expect(actual) { + t.Fatalf("Expect parseAzureAutomationVariableValue to return %v instead of %v for resource %q and value %s", tc.ExpectValue, actual, tc.Resource, tc.Value) + } + } + }) + } +} + +func testCheckAzureRMAutomationVariableExists(resourceName string, varType string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Automation %s Variable not found: %s", varType, resourceName) + } + + name := rs.Primary.Attributes["name"] + resourceGroup := rs.Primary.Attributes["resource_group_name"] + accountName := rs.Primary.Attributes["automation_account_name"] + + client := testAccProvider.Meta().(*ArmClient).automationVariableClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + if resp, err := client.Get(ctx, resourceGroup, accountName, name); err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Bad: Automation %s Variable %q (Automation Account Name %q / Resource Group %q) does not exist", varType, name, accountName, resourceGroup) + } + return fmt.Errorf("Bad: Get on automationVariableClient: %+v", err) + } + + return nil + } +} + +func testCheckAzureRMAutomationVariableDestroy(s *terraform.State, varType string) error { + client := testAccProvider.Meta().(*ArmClient).automationVariableClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + resourceName := fmt.Sprintf("azurerm_automation_variable_%s", strings.ToLower(varType)) + + for _, rs := range s.RootModule().Resources { + if rs.Type != resourceName { + continue + } + + name := rs.Primary.Attributes["name"] + resourceGroup := rs.Primary.Attributes["resource_group_name"] + accountName := rs.Primary.Attributes["automation_account_name"] + + if resp, err := client.Get(ctx, resourceGroup, accountName, name); err != nil { + if !utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Bad: Get on automationVariableClient: %+v", err) + } + } + + return nil + } + + return nil +} diff --git a/azurerm/config.go b/azurerm/config.go index 5c5353119356..3e97b3f18fa5 100644 --- a/azurerm/config.go +++ b/azurerm/config.go @@ -106,6 +106,7 @@ type ArmClient struct { automationRunbookClient automation.RunbookClient automationRunbookDraftClient automation.RunbookDraftClient automationScheduleClient automation.ScheduleClient + automationVariableClient automation.VariableClient dnsClient dns.RecordSetsClient zonesClient dns.ZonesClient @@ -675,6 +676,10 @@ func (c *ArmClient) registerAutomationClients(endpoint, subscriptionId string, a runbookDraftClient := automation.NewRunbookDraftClientWithBaseURI(endpoint, subscriptionId) c.configureClient(&runbookDraftClient.Client, auth) c.automationRunbookDraftClient = runbookDraftClient + + variableClient := automation.NewVariableClientWithBaseURI(endpoint, subscriptionId) + c.configureClient(&variableClient.Client, auth) + c.automationVariableClient = variableClient } func (c *ArmClient) registerAuthentication(endpoint, graphEndpoint, subscriptionId, tenantId string, auth, graphAuth autorest.Authorizer) { diff --git a/azurerm/data_source_automation_variable_bool.go b/azurerm/data_source_automation_variable_bool.go new file mode 100644 index 000000000000..f133a8315369 --- /dev/null +++ b/azurerm/data_source_automation_variable_bool.go @@ -0,0 +1,17 @@ +package azurerm + +import ( + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceArmAutomationVariableBool() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmAutomationVariableBoolRead, + + Schema: datasourceAutomationVariableCommonSchema(schema.TypeBool), + } +} + +func dataSourceArmAutomationVariableBoolRead(d *schema.ResourceData, meta interface{}) error { + return datasourceAutomationVariableRead(d, meta, "Bool") +} diff --git a/azurerm/data_source_automation_variable_bool_test.go b/azurerm/data_source_automation_variable_bool_test.go new file mode 100644 index 000000000000..ea3893f0b8bd --- /dev/null +++ b/azurerm/data_source_automation_variable_bool_test.go @@ -0,0 +1,41 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" +) + +func TestAccDataSourceAzureRMAutomationVariableBool_basic(t *testing.T) { + dataSourceName := "data.azurerm_automation_variable_bool.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAutomationVariableBool_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "value", "false"), + ), + }, + }, + }) +} + +func testAccDataSourceAutomationVariableBool_basic(rInt int, location string) string { + config := testAccAzureRMAutomationVariableBool_basic(rInt, location) + return fmt.Sprintf(` +%s + +data "azurerm_automation_variable_bool" "test" { + name = "${azurerm_automation_variable_bool.test.name}" + resource_group_name = "${azurerm_automation_variable_bool.test.resource_group_name}" + automation_account_name = "${azurerm_automation_variable_bool.test.automation_account_name}" +} +`, config) +} diff --git a/azurerm/data_source_automation_variable_datetime.go b/azurerm/data_source_automation_variable_datetime.go new file mode 100644 index 000000000000..575413adb7dc --- /dev/null +++ b/azurerm/data_source_automation_variable_datetime.go @@ -0,0 +1,17 @@ +package azurerm + +import ( + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceArmAutomationVariableDateTime() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmAutomationVariableDateTimeRead, + + Schema: datasourceAutomationVariableCommonSchema(schema.TypeString), + } +} + +func dataSourceArmAutomationVariableDateTimeRead(d *schema.ResourceData, meta interface{}) error { + return datasourceAutomationVariableRead(d, meta, "Datetime") +} diff --git a/azurerm/data_source_automation_variable_datetime_test.go b/azurerm/data_source_automation_variable_datetime_test.go new file mode 100644 index 000000000000..7f89e8c198fa --- /dev/null +++ b/azurerm/data_source_automation_variable_datetime_test.go @@ -0,0 +1,41 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" +) + +func TestAccDataSourceAzureRMAutomationVariableDateTime_basic(t *testing.T) { + dataSourceName := "data.azurerm_automation_variable_datetime.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAutomationVariableDateTime_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "value", "2019-04-24T21:40:54.074Z"), + ), + }, + }, + }) +} + +func testAccDataSourceAutomationVariableDateTime_basic(rInt int, location string) string { + config := testAccAzureRMAutomationVariableDateTime_basic(rInt, location) + return fmt.Sprintf(` +%s + +data "azurerm_automation_variable_datetime" "test" { + name = "${azurerm_automation_variable_datetime.test.name}" + resource_group_name = "${azurerm_automation_variable_datetime.test.resource_group_name}" + automation_account_name = "${azurerm_automation_variable_datetime.test.automation_account_name}" +} +`, config) +} diff --git a/azurerm/data_source_automation_variable_int.go b/azurerm/data_source_automation_variable_int.go new file mode 100644 index 000000000000..84cb82667dde --- /dev/null +++ b/azurerm/data_source_automation_variable_int.go @@ -0,0 +1,17 @@ +package azurerm + +import ( + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceArmAutomationVariableInt() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmAutomationVariableIntRead, + + Schema: datasourceAutomationVariableCommonSchema(schema.TypeInt), + } +} + +func dataSourceArmAutomationVariableIntRead(d *schema.ResourceData, meta interface{}) error { + return datasourceAutomationVariableRead(d, meta, "Int") +} diff --git a/azurerm/data_source_automation_variable_int_test.go b/azurerm/data_source_automation_variable_int_test.go new file mode 100644 index 000000000000..4bf5b417f8b4 --- /dev/null +++ b/azurerm/data_source_automation_variable_int_test.go @@ -0,0 +1,41 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" +) + +func TestAccDataSourceAzureRMAutomationVariableInt_basic(t *testing.T) { + dataSourceName := "data.azurerm_automation_variable_int.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAutomationVariableInt_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "value", "1234"), + ), + }, + }, + }) +} + +func testAccDataSourceAutomationVariableInt_basic(rInt int, location string) string { + config := testAccAzureRMAutomationVariableInt_basic(rInt, location) + return fmt.Sprintf(` +%s + +data "azurerm_automation_variable_int" "test" { + name = "${azurerm_automation_variable_int.test.name}" + resource_group_name = "${azurerm_automation_variable_int.test.resource_group_name}" + automation_account_name = "${azurerm_automation_variable_int.test.automation_account_name}" +} +`, config) +} diff --git a/azurerm/data_source_automation_variable_string.go b/azurerm/data_source_automation_variable_string.go new file mode 100644 index 000000000000..6fb94381c592 --- /dev/null +++ b/azurerm/data_source_automation_variable_string.go @@ -0,0 +1,17 @@ +package azurerm + +import ( + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceArmAutomationVariableString() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmAutomationVariableStringRead, + + Schema: datasourceAutomationVariableCommonSchema(schema.TypeString), + } +} + +func dataSourceArmAutomationVariableStringRead(d *schema.ResourceData, meta interface{}) error { + return datasourceAutomationVariableRead(d, meta, "String") +} diff --git a/azurerm/data_source_automation_variable_string_test.go b/azurerm/data_source_automation_variable_string_test.go new file mode 100644 index 000000000000..68a40d6513a5 --- /dev/null +++ b/azurerm/data_source_automation_variable_string_test.go @@ -0,0 +1,41 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" +) + +func TestAccDataSourceAzureRMAutomationVariableString_basic(t *testing.T) { + dataSourceName := "data.azurerm_automation_variable_string.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAutomationVariableString_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "value", "Hello, Terraform Basic Test."), + ), + }, + }, + }) +} + +func testAccDataSourceAutomationVariableString_basic(rInt int, location string) string { + config := testAccAzureRMAutomationVariableString_basic(rInt, location) + return fmt.Sprintf(` +%s + +data "azurerm_automation_variable_string" "test" { + name = "${azurerm_automation_variable_string.test.name}" + resource_group_name = "${azurerm_automation_variable_string.test.resource_group_name}" + automation_account_name = "${azurerm_automation_variable_string.test.automation_account_name}" +} +`, config) +} diff --git a/azurerm/helpers/azure/automation_variable.go b/azurerm/helpers/azure/automation_variable.go new file mode 100644 index 000000000000..906b62c1b0ed --- /dev/null +++ b/azurerm/helpers/azure/automation_variable.go @@ -0,0 +1,42 @@ +package azure + +import ( + "fmt" + "regexp" + "strconv" + "time" +) + +func ParseAzureRmAutomationVariableValue(resource string, input *string) (interface{}, error) { + if input == nil { + if resource != "azurerm_automation_variable_null" { + return nil, fmt.Errorf("Expected value \"nil\" to be %q, actual type is \"azurerm_automation_variable_null\"", resource) + } + return nil, nil + } + + var value interface{} + var err error + actualResource := "Unknown" + datePattern := regexp.MustCompile(`"\\/Date\((-?[0-9]+)\)\\/"`) + matches := datePattern.FindStringSubmatch(*input) + + if len(matches) == 2 && matches[0] == *input { + if ticks, err := strconv.ParseInt(matches[1], 10, 64); err == nil { + value = time.Unix(ticks/1000, ticks%1000*1000000).In(time.UTC) + actualResource = "azurerm_automation_variable_datetime" + } + } else if value, err = strconv.Unquote(*input); err == nil { + actualResource = "azurerm_automation_variable_string" + } else if value, err = strconv.ParseBool(*input); err == nil { + actualResource = "azurerm_automation_variable_bool" + } else if value, err = strconv.ParseInt(*input, 10, 32); err == nil { + value = int32(value.(int64)) + actualResource = "azurerm_automation_variable_int" + } + + if actualResource != resource { + return nil, fmt.Errorf("Expected value %q to be %q, actual type is %q", *input, resource, actualResource) + } + return value, nil +} diff --git a/azurerm/provider.go b/azurerm/provider.go index 33c9475f0137..8c839777bd8b 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -107,6 +107,10 @@ func Provider() terraform.ResourceProvider { "azurerm_app_service": dataSourceArmAppService(), "azurerm_application_insights": dataSourceArmApplicationInsights(), "azurerm_application_security_group": dataSourceArmApplicationSecurityGroup(), + "azurerm_automation_variable_bool": dataSourceArmAutomationVariableBool(), + "azurerm_automation_variable_datetime": dataSourceArmAutomationVariableDateTime(), + "azurerm_automation_variable_int": dataSourceArmAutomationVariableInt(), + "azurerm_automation_variable_string": dataSourceArmAutomationVariableString(), "azurerm_availability_set": dataSourceArmAvailabilitySet(), "azurerm_azuread_application": dataSourceArmAzureADApplication(), "azurerm_azuread_service_principal": dataSourceArmActiveDirectoryServicePrincipal(), @@ -210,6 +214,10 @@ func Provider() terraform.ResourceProvider { "azurerm_automation_module": resourceArmAutomationModule(), "azurerm_automation_runbook": resourceArmAutomationRunbook(), "azurerm_automation_schedule": resourceArmAutomationSchedule(), + "azurerm_automation_variable_bool": resourceArmAutomationVariableBool(), + "azurerm_automation_variable_datetime": resourceArmAutomationVariableDateTime(), + "azurerm_automation_variable_int": resourceArmAutomationVariableInt(), + "azurerm_automation_variable_string": resourceArmAutomationVariableString(), "azurerm_autoscale_setting": resourceArmAutoScaleSetting(), "azurerm_availability_set": resourceArmAvailabilitySet(), "azurerm_azuread_application": resourceArmActiveDirectoryApplication(), diff --git a/azurerm/resource_arm_automation_variable_bool.go b/azurerm/resource_arm_automation_variable_bool.go new file mode 100644 index 000000000000..6b1bc9c2770d --- /dev/null +++ b/azurerm/resource_arm_automation_variable_bool.go @@ -0,0 +1,32 @@ +package azurerm + +import ( + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceArmAutomationVariableBool() *schema.Resource { + return &schema.Resource{ + Create: resourceArmAutomationVariableBoolCreateUpdate, + Read: resourceArmAutomationVariableBoolRead, + Update: resourceArmAutomationVariableBoolCreateUpdate, + Delete: resourceArmAutomationVariableBoolDelete, + + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: resourceAutomationVariableCommonSchema(schema.TypeBool, nil), + } +} + +func resourceArmAutomationVariableBoolCreateUpdate(d *schema.ResourceData, meta interface{}) error { + return resourceAutomationVariableCreateUpdate(d, meta, "Bool") +} + +func resourceArmAutomationVariableBoolRead(d *schema.ResourceData, meta interface{}) error { + return resourceAutomationVariableRead(d, meta, "Bool") +} + +func resourceArmAutomationVariableBoolDelete(d *schema.ResourceData, meta interface{}) error { + return resourceAutomationVariableDelete(d, meta, "Bool") +} diff --git a/azurerm/resource_arm_automation_variable_bool_test.go b/azurerm/resource_arm_automation_variable_bool_test.go new file mode 100644 index 000000000000..f01d31f7bd69 --- /dev/null +++ b/azurerm/resource_arm_automation_variable_bool_test.go @@ -0,0 +1,160 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" +) + +func TestAccAzureRMAutomationVariableBool_basic(t *testing.T) { + resourceName := "azurerm_automation_variable_bool.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAutomationVariableBoolDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAutomationVariableBool_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableBoolExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "value", "false"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMAutomationVariableBool_complete(t *testing.T) { + resourceName := "azurerm_automation_variable_bool.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAutomationVariableBoolDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAutomationVariableBool_complete(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableBoolExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "description", "This variable is created by Terraform acceptance test."), + resource.TestCheckResourceAttr(resourceName, "value", "true"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMAutomationVariableBool_basicCompleteUpdate(t *testing.T) { + resourceName := "azurerm_automation_variable_bool.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAutomationVariableBoolDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAutomationVariableBool_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableBoolExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "value", "false"), + ), + }, + { + Config: testAccAzureRMAutomationVariableBool_complete(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableBoolExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "description", "This variable is created by Terraform acceptance test."), + resource.TestCheckResourceAttr(resourceName, "value", "true"), + ), + }, + { + Config: testAccAzureRMAutomationVariableBool_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableBoolExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "value", "false"), + ), + }, + }, + }) +} + +func testCheckAzureRMAutomationVariableBoolExists(resourceName string) resource.TestCheckFunc { + return testCheckAzureRMAutomationVariableExists(resourceName, "Bool") +} + +func testCheckAzureRMAutomationVariableBoolDestroy(s *terraform.State) error { + return testCheckAzureRMAutomationVariableDestroy(s, "Bool") +} + +func testAccAzureRMAutomationVariableBool_basic(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_automation_account" "test" { + name = "acctestAutoAcct-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + sku { + name = "Basic" + } +} + +resource "azurerm_automation_variable_bool" "test" { + name = "acctestAutoVar-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + automation_account_name = "${azurerm_automation_account.test.name}" + value = false +} +`, rInt, location, rInt, rInt) +} + +func testAccAzureRMAutomationVariableBool_complete(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_automation_account" "test" { + name = "acctestAutoAcct-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + sku { + name = "Basic" + } +} + +resource "azurerm_automation_variable_bool" "test" { + name = "acctestAutoVar-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + automation_account_name = "${azurerm_automation_account.test.name}" + description = "This variable is created by Terraform acceptance test." + value = true +} +`, rInt, location, rInt, rInt) +} diff --git a/azurerm/resource_arm_automation_variable_datetime.go b/azurerm/resource_arm_automation_variable_datetime.go new file mode 100644 index 000000000000..8343052c029d --- /dev/null +++ b/azurerm/resource_arm_automation_variable_datetime.go @@ -0,0 +1,33 @@ +package azurerm + +import ( + "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" +) + +func resourceArmAutomationVariableDateTime() *schema.Resource { + return &schema.Resource{ + Create: resourceArmAutomationVariableDateTimeCreateUpdate, + Read: resourceArmAutomationVariableDateTimeRead, + Update: resourceArmAutomationVariableDateTimeCreateUpdate, + Delete: resourceArmAutomationVariableDateTimeDelete, + + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: resourceAutomationVariableCommonSchema(schema.TypeString, validate.RFC3339Time), + } +} + +func resourceArmAutomationVariableDateTimeCreateUpdate(d *schema.ResourceData, meta interface{}) error { + return resourceAutomationVariableCreateUpdate(d, meta, "Datetime") +} + +func resourceArmAutomationVariableDateTimeRead(d *schema.ResourceData, meta interface{}) error { + return resourceAutomationVariableRead(d, meta, "Datetime") +} + +func resourceArmAutomationVariableDateTimeDelete(d *schema.ResourceData, meta interface{}) error { + return resourceAutomationVariableDelete(d, meta, "Datetime") +} diff --git a/azurerm/resource_arm_automation_variable_datetime_test.go b/azurerm/resource_arm_automation_variable_datetime_test.go new file mode 100644 index 000000000000..935d006a77d7 --- /dev/null +++ b/azurerm/resource_arm_automation_variable_datetime_test.go @@ -0,0 +1,160 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" +) + +func TestAccAzureRMAutomationVariableDateTime_basic(t *testing.T) { + resourceName := "azurerm_automation_variable_datetime.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAutomationVariableDateTimeDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAutomationVariableDateTime_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableDateTimeExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "value", "2019-04-24T21:40:54.074Z"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMAutomationVariableDateTime_complete(t *testing.T) { + resourceName := "azurerm_automation_variable_datetime.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAutomationVariableDateTimeDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAutomationVariableDateTime_complete(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableDateTimeExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "description", "This variable is created by Terraform acceptance test."), + resource.TestCheckResourceAttr(resourceName, "value", "2019-04-20T08:40:04.02Z"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMAutomationVariableDateTime_basicCompleteUpdate(t *testing.T) { + resourceName := "azurerm_automation_variable_datetime.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAutomationVariableDateTimeDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAutomationVariableDateTime_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableDateTimeExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "value", "2019-04-24T21:40:54.074Z"), + ), + }, + { + Config: testAccAzureRMAutomationVariableDateTime_complete(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableDateTimeExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "description", "This variable is created by Terraform acceptance test."), + resource.TestCheckResourceAttr(resourceName, "value", "2019-04-20T08:40:04.02Z"), + ), + }, + { + Config: testAccAzureRMAutomationVariableDateTime_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableDateTimeExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "value", "2019-04-24T21:40:54.074Z"), + ), + }, + }, + }) +} + +func testCheckAzureRMAutomationVariableDateTimeExists(resourceName string) resource.TestCheckFunc { + return testCheckAzureRMAutomationVariableExists(resourceName, "Datetime") +} + +func testCheckAzureRMAutomationVariableDateTimeDestroy(s *terraform.State) error { + return testCheckAzureRMAutomationVariableDestroy(s, "Datetime") +} + +func testAccAzureRMAutomationVariableDateTime_basic(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_automation_account" "test" { + name = "acctestAutoAcct-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + sku { + name = "Basic" + } +} + +resource "azurerm_automation_variable_datetime" "test" { + name = "acctestAutoVar-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + automation_account_name = "${azurerm_automation_account.test.name}" + value = "2019-04-24T21:40:54.074Z" +} +`, rInt, location, rInt, rInt) +} + +func testAccAzureRMAutomationVariableDateTime_complete(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_automation_account" "test" { + name = "acctestAutoAcct-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + sku { + name = "Basic" + } +} + +resource "azurerm_automation_variable_datetime" "test" { + name = "acctestAutoVar-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + automation_account_name = "${azurerm_automation_account.test.name}" + description = "This variable is created by Terraform acceptance test." + value = "2019-04-20T08:40:04.02Z" +} +`, rInt, location, rInt, rInt) +} diff --git a/azurerm/resource_arm_automation_variable_int.go b/azurerm/resource_arm_automation_variable_int.go new file mode 100644 index 000000000000..ea2c5992f235 --- /dev/null +++ b/azurerm/resource_arm_automation_variable_int.go @@ -0,0 +1,32 @@ +package azurerm + +import ( + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceArmAutomationVariableInt() *schema.Resource { + return &schema.Resource{ + Create: resourceArmAutomationVariableIntCreateUpdate, + Read: resourceArmAutomationVariableIntRead, + Update: resourceArmAutomationVariableIntCreateUpdate, + Delete: resourceArmAutomationVariableIntDelete, + + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: resourceAutomationVariableCommonSchema(schema.TypeInt, nil), + } +} + +func resourceArmAutomationVariableIntCreateUpdate(d *schema.ResourceData, meta interface{}) error { + return resourceAutomationVariableCreateUpdate(d, meta, "Int") +} + +func resourceArmAutomationVariableIntRead(d *schema.ResourceData, meta interface{}) error { + return resourceAutomationVariableRead(d, meta, "Int") +} + +func resourceArmAutomationVariableIntDelete(d *schema.ResourceData, meta interface{}) error { + return resourceAutomationVariableDelete(d, meta, "Int") +} diff --git a/azurerm/resource_arm_automation_variable_int_test.go b/azurerm/resource_arm_automation_variable_int_test.go new file mode 100644 index 000000000000..4cb43fb41cd3 --- /dev/null +++ b/azurerm/resource_arm_automation_variable_int_test.go @@ -0,0 +1,160 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" +) + +func TestAccAzureRMAutomationVariableInt_basic(t *testing.T) { + resourceName := "azurerm_automation_variable_int.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAutomationVariableIntDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAutomationVariableInt_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableIntExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "value", "1234"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMAutomationVariableInt_complete(t *testing.T) { + resourceName := "azurerm_automation_variable_int.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAutomationVariableIntDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAutomationVariableInt_complete(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableIntExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "description", "This variable is created by Terraform acceptance test."), + resource.TestCheckResourceAttr(resourceName, "value", "12345"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMAutomationVariableInt_basicCompleteUpdate(t *testing.T) { + resourceName := "azurerm_automation_variable_int.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAutomationVariableIntDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAutomationVariableInt_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableIntExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "value", "1234"), + ), + }, + { + Config: testAccAzureRMAutomationVariableInt_complete(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableIntExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "description", "This variable is created by Terraform acceptance test."), + resource.TestCheckResourceAttr(resourceName, "value", "12345"), + ), + }, + { + Config: testAccAzureRMAutomationVariableInt_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableIntExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "value", "1234"), + ), + }, + }, + }) +} + +func testCheckAzureRMAutomationVariableIntExists(resourceName string) resource.TestCheckFunc { + return testCheckAzureRMAutomationVariableExists(resourceName, "Int") +} + +func testCheckAzureRMAutomationVariableIntDestroy(s *terraform.State) error { + return testCheckAzureRMAutomationVariableDestroy(s, "Int") +} + +func testAccAzureRMAutomationVariableInt_basic(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_automation_account" "test" { + name = "acctestAutoAcct-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + sku { + name = "Basic" + } +} + +resource "azurerm_automation_variable_int" "test" { + name = "acctestAutoVar-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + automation_account_name = "${azurerm_automation_account.test.name}" + value = 1234 +} +`, rInt, location, rInt, rInt) +} + +func testAccAzureRMAutomationVariableInt_complete(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_automation_account" "test" { + name = "acctestAutoAcct-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + sku { + name = "Basic" + } +} + +resource "azurerm_automation_variable_int" "test" { + name = "acctestAutoVar-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + automation_account_name = "${azurerm_automation_account.test.name}" + description = "This variable is created by Terraform acceptance test." + value = 12345 +} +`, rInt, location, rInt, rInt) +} diff --git a/azurerm/resource_arm_automation_variable_string.go b/azurerm/resource_arm_automation_variable_string.go new file mode 100644 index 000000000000..b52b84e4eb26 --- /dev/null +++ b/azurerm/resource_arm_automation_variable_string.go @@ -0,0 +1,33 @@ +package azurerm + +import ( + "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" +) + +func resourceArmAutomationVariableString() *schema.Resource { + return &schema.Resource{ + Create: resourceArmAutomationVariableStringCreateUpdate, + Read: resourceArmAutomationVariableStringRead, + Update: resourceArmAutomationVariableStringCreateUpdate, + Delete: resourceArmAutomationVariableStringDelete, + + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: resourceAutomationVariableCommonSchema(schema.TypeString, validate.NoEmptyStrings), + } +} + +func resourceArmAutomationVariableStringCreateUpdate(d *schema.ResourceData, meta interface{}) error { + return resourceAutomationVariableCreateUpdate(d, meta, "String") +} + +func resourceArmAutomationVariableStringRead(d *schema.ResourceData, meta interface{}) error { + return resourceAutomationVariableRead(d, meta, "String") +} + +func resourceArmAutomationVariableStringDelete(d *schema.ResourceData, meta interface{}) error { + return resourceAutomationVariableDelete(d, meta, "String") +} diff --git a/azurerm/resource_arm_automation_variable_string_test.go b/azurerm/resource_arm_automation_variable_string_test.go new file mode 100644 index 000000000000..403f204811ef --- /dev/null +++ b/azurerm/resource_arm_automation_variable_string_test.go @@ -0,0 +1,160 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" +) + +func TestAccAzureRMAutomationVariableString_basic(t *testing.T) { + resourceName := "azurerm_automation_variable_string.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAutomationVariableStringDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAutomationVariableString_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableStringExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "value", "Hello, Terraform Basic Test."), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMAutomationVariableString_complete(t *testing.T) { + resourceName := "azurerm_automation_variable_string.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAutomationVariableStringDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAutomationVariableString_complete(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableStringExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "description", "This variable is created by Terraform acceptance test."), + resource.TestCheckResourceAttr(resourceName, "value", "Hello, Terraform Complete Test."), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMAutomationVariableString_basicCompleteUpdate(t *testing.T) { + resourceName := "azurerm_automation_variable_string.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAutomationVariableStringDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAutomationVariableString_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableStringExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "value", "Hello, Terraform Basic Test."), + ), + }, + { + Config: testAccAzureRMAutomationVariableString_complete(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableStringExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "description", "This variable is created by Terraform acceptance test."), + resource.TestCheckResourceAttr(resourceName, "value", "Hello, Terraform Complete Test."), + ), + }, + { + Config: testAccAzureRMAutomationVariableString_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationVariableStringExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "value", "Hello, Terraform Basic Test."), + ), + }, + }, + }) +} + +func testCheckAzureRMAutomationVariableStringExists(resourceName string) resource.TestCheckFunc { + return testCheckAzureRMAutomationVariableExists(resourceName, "String") +} + +func testCheckAzureRMAutomationVariableStringDestroy(s *terraform.State) error { + return testCheckAzureRMAutomationVariableDestroy(s, "String") +} + +func testAccAzureRMAutomationVariableString_basic(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_automation_account" "test" { + name = "acctestAutoAcct-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + sku { + name = "Basic" + } +} + +resource "azurerm_automation_variable_string" "test" { + name = "acctestAutoVar-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + automation_account_name = "${azurerm_automation_account.test.name}" + value = "Hello, Terraform Basic Test." +} +`, rInt, location, rInt, rInt) +} + +func testAccAzureRMAutomationVariableString_complete(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_automation_account" "test" { + name = "acctestAutoAcct-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + sku { + name = "Basic" + } +} + +resource "azurerm_automation_variable_string" "test" { + name = "acctestAutoVar-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + automation_account_name = "${azurerm_automation_account.test.name}" + description = "This variable is created by Terraform acceptance test." + value = "Hello, Terraform Complete Test." +} +`, rInt, location, rInt, rInt) +} diff --git a/website/azurerm.erb b/website/azurerm.erb index 139ca184d138..56df2558ff06 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -95,6 +95,22 @@ azurerm_application_insights +