-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1512 from bgelens/automationresources
Automation resources
- Loading branch information
Showing
12 changed files
with
1,207 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
package azurerm | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"log" | ||
"regexp" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/automation/mgmt/2015-10-31/automation" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmAutomationDscConfiguration() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmAutomationDscConfigurationCreateUpdate, | ||
Read: resourceArmAutomationDscConfigurationRead, | ||
Update: resourceArmAutomationDscConfigurationCreateUpdate, | ||
Delete: resourceArmAutomationDscConfigurationDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringMatch( | ||
regexp.MustCompile(`^[a-zA-Z0-9_]{1,64}$`), | ||
`The name length must be from 1 to 64 characters. The name can only contain letters, numbers and underscores.`, | ||
), | ||
}, | ||
|
||
"automation_account_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
|
||
"content_embedded": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
|
||
"resource_group_name": resourceGroupNameSchema(), | ||
|
||
"location": locationSchema(), | ||
|
||
"log_verbose": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
|
||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
|
||
"state": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmAutomationDscConfigurationCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).automationDscConfigurationClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
log.Printf("[INFO] preparing arguments for AzureRM Automation Dsc Configuration creation.") | ||
|
||
name := d.Get("name").(string) | ||
resGroup := d.Get("resource_group_name").(string) | ||
accName := d.Get("automation_account_name").(string) | ||
contentEmbedded := d.Get("content_embedded").(string) | ||
location := azureRMNormalizeLocation(d.Get("location").(string)) | ||
logVerbose := d.Get("log_verbose").(bool) | ||
description := d.Get("description").(string) | ||
|
||
parameters := automation.DscConfigurationCreateOrUpdateParameters{ | ||
DscConfigurationCreateOrUpdateProperties: &automation.DscConfigurationCreateOrUpdateProperties{ | ||
LogVerbose: utils.Bool(logVerbose), | ||
Description: utils.String(description), | ||
Source: &automation.ContentSource{ | ||
Type: automation.EmbeddedContent, | ||
Value: utils.String(contentEmbedded), | ||
}, | ||
}, | ||
Location: utils.String(location), | ||
} | ||
|
||
_, err := client.CreateOrUpdate(ctx, resGroup, accName, name, parameters) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
read, err := client.Get(ctx, resGroup, accName, name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if read.ID == nil { | ||
return fmt.Errorf("Cannot read Automation Dsc Configuration %q (resource group %q) ID", name, resGroup) | ||
} | ||
|
||
d.SetId(*read.ID) | ||
|
||
return resourceArmAutomationDscConfigurationRead(d, meta) | ||
} | ||
|
||
func resourceArmAutomationDscConfigurationRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).automationDscConfigurationClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resGroup := id.ResourceGroup | ||
accName := id.Path["automationAccounts"] | ||
name := id.Path["configurations"] | ||
|
||
resp, err := client.Get(ctx, resGroup, accName, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Error making Read request on AzureRM Automation Dsc Configuration %q: %+v", name, err) | ||
} | ||
|
||
d.Set("name", resp.Name) | ||
d.Set("resource_group_name", resGroup) | ||
d.Set("automation_account_name", accName) | ||
|
||
if location := resp.Location; location != nil { | ||
d.Set("location", azureRMNormalizeLocation(*location)) | ||
} | ||
|
||
if props := resp.DscConfigurationProperties; props != nil { | ||
d.Set("log_verbose", props.LogVerbose) | ||
d.Set("description", props.Description) | ||
d.Set("state", resp.State) | ||
} | ||
|
||
contentresp, err := client.GetContent(ctx, resGroup, accName, name) | ||
if err != nil { | ||
return fmt.Errorf("Error making Read request on AzureRM Automation Dsc Configuration content %q: %+v", name, err) | ||
} | ||
|
||
buf := new(bytes.Buffer) | ||
buf.ReadFrom(contentresp.Body) | ||
content := buf.String() | ||
|
||
d.Set("content_embedded", content) | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmAutomationDscConfigurationDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).automationDscConfigurationClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resGroup := id.ResourceGroup | ||
accName := id.Path["automationAccounts"] | ||
name := id.Path["configurations"] | ||
|
||
resp, err := client.Delete(ctx, resGroup, accName, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp) { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Error issuing AzureRM delete request for Automation Dsc Configuration %q: %+v", name, err) | ||
} | ||
|
||
return nil | ||
} |
135 changes: 135 additions & 0 deletions
135
azurerm/resource_arm_automation_dsc_configuration_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func TestAccAzureRMAutomationDscConfiguration_basic(t *testing.T) { | ||
resourceName := "azurerm_automation_dsc_configuration.test" | ||
ri := acctest.RandInt() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMAutomationDscConfigurationDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAzureRMAutomationDscConfiguration_basic(ri, testLocation()), | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMAutomationDscConfigurationExists(resourceName), | ||
resource.TestCheckResourceAttrSet(resourceName, "location"), | ||
resource.TestCheckResourceAttr(resourceName, "description", "test"), | ||
resource.TestCheckResourceAttrSet(resourceName, "log_verbose"), | ||
resource.TestCheckResourceAttrSet(resourceName, "state"), | ||
resource.TestCheckResourceAttr(resourceName, "content_embedded", "configuration acctest {}"), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testCheckAzureRMAutomationDscConfigurationDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*ArmClient).automationDscConfigurationClient | ||
ctx := testAccProvider.Meta().(*ArmClient).StopContext | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "azurerm_automation_dsc_configuration" { | ||
continue | ||
} | ||
|
||
name := rs.Primary.Attributes["name"] | ||
accName := rs.Primary.Attributes["automation_account_name"] | ||
|
||
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
if !hasResourceGroup { | ||
return fmt.Errorf("Bad: no resource group found in state for Automation Dsc Configuration: '%s'", name) | ||
} | ||
|
||
resp, err := conn.Get(ctx, resourceGroup, accName, name) | ||
|
||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return nil | ||
} | ||
|
||
return err | ||
} | ||
|
||
return fmt.Errorf("Automation Dsc Configuration still exists:\n%#v", resp) | ||
|
||
} | ||
|
||
return nil | ||
} | ||
|
||
func testCheckAzureRMAutomationDscConfigurationExists(name string) resource.TestCheckFunc { | ||
|
||
return func(s *terraform.State) error { | ||
// Ensure we have enough information in state to look up in API | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", name) | ||
} | ||
|
||
name := rs.Primary.Attributes["name"] | ||
accName := rs.Primary.Attributes["automation_account_name"] | ||
|
||
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
if !hasResourceGroup { | ||
return fmt.Errorf("Bad: no resource group found in state for Automation Dsc Configuration: '%s'", name) | ||
} | ||
|
||
conn := testAccProvider.Meta().(*ArmClient).automationDscConfigurationClient | ||
ctx := testAccProvider.Meta().(*ArmClient).StopContext | ||
|
||
resp, err := conn.Get(ctx, resourceGroup, accName, name) | ||
|
||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Automation Dsc Configuration '%s' (resource group: '%s') does not exist", name, resourceGroup) | ||
} | ||
|
||
return fmt.Errorf("Bad: Get on automationDscConfigurationClient: %s\nName: %s, Account name: %s, Resource group: %s OBJECT: %+v", err, name, accName, resourceGroup, rs.Primary) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccAzureRMAutomationDscConfiguration_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 = "acctest-%d" | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
sku { | ||
name = "Basic" | ||
} | ||
} | ||
resource "azurerm_automation_dsc_configuration" "test" { | ||
name = "acctest" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
automation_account_name = "${azurerm_automation_account.test.name}" | ||
location = "${azurerm_resource_group.test.location}" | ||
content_embedded = "configuration acctest {}" | ||
description = "test" | ||
} | ||
`, rInt, location, rInt) | ||
} |
Oops, something went wrong.