Skip to content

Commit

Permalink
Merge pull request #1512 from bgelens/automationresources
Browse files Browse the repository at this point in the history
Automation resources
  • Loading branch information
katbyte authored Oct 17, 2018
2 parents 6d20786 + 71503c8 commit 1c47f25
Show file tree
Hide file tree
Showing 12 changed files with 1,207 additions and 5 deletions.
25 changes: 20 additions & 5 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ type ArmClient struct {

cosmosDBClient documentdb.DatabaseAccountsClient

automationAccountClient automation.AccountClient
automationRunbookClient automation.RunbookClient
automationCredentialClient automation.CredentialClient
automationScheduleClient automation.ScheduleClient
automationRunbookDraftClient automation.RunbookDraftClient
automationAccountClient automation.AccountClient
automationCredentialClient automation.CredentialClient
automationDscConfigurationClient automation.DscConfigurationClient
automationDscNodeConfigurationClient automation.DscNodeConfigurationClient
automationModuleClient automation.ModuleClient
automationRunbookClient automation.RunbookClient
automationRunbookDraftClient automation.RunbookDraftClient
automationScheduleClient automation.ScheduleClient

dnsClient dns.RecordSetsClient
zonesClient dns.ZonesClient
Expand Down Expand Up @@ -534,6 +537,18 @@ func (c *ArmClient) registerAutomationClients(endpoint, subscriptionId string, a
c.configureClient(&credentialClient.Client, auth)
c.automationCredentialClient = credentialClient

dscConfigurationClient := automation.NewDscConfigurationClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&dscConfigurationClient.Client, auth)
c.automationDscConfigurationClient = dscConfigurationClient

dscNodeConfigurationClient := automation.NewDscNodeConfigurationClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&dscNodeConfigurationClient.Client, auth)
c.automationDscNodeConfigurationClient = dscNodeConfigurationClient

moduleClient := automation.NewModuleClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&moduleClient.Client, auth)
c.automationModuleClient = moduleClient

runbookClient := automation.NewRunbookClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&runbookClient.Client, auth)
c.automationRunbookClient = runbookClient
Expand Down
3 changes: 3 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ func Provider() terraform.ResourceProvider {
"azurerm_app_service_slot": resourceArmAppServiceSlot(),
"azurerm_automation_account": resourceArmAutomationAccount(),
"azurerm_automation_credential": resourceArmAutomationCredential(),
"azurerm_automation_dsc_configuration": resourceArmAutomationDscConfiguration(),
"azurerm_automation_dsc_nodeconfiguration": resourceArmAutomationDscNodeConfiguration(),
"azurerm_automation_module": resourceArmAutomationModule(),
"azurerm_automation_runbook": resourceArmAutomationRunbook(),
"azurerm_automation_schedule": resourceArmAutomationSchedule(),
"azurerm_autoscale_setting": resourceArmAutoScaleSetting(),
Expand Down
190 changes: 190 additions & 0 deletions azurerm/resource_arm_automation_dsc_configuration.go
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 azurerm/resource_arm_automation_dsc_configuration_test.go
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)
}
Loading

0 comments on commit 1c47f25

Please sign in to comment.