Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automation Runbook: support for setting custom content #1696

Merged
merged 11 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ type ArmClient struct {

cosmosDBClient documentdb.DatabaseAccountsClient

automationAccountClient automation.AccountClient
automationRunbookClient automation.RunbookClient
automationCredentialClient automation.CredentialClient
automationScheduleClient automation.ScheduleClient
automationAccountClient automation.AccountClient
automationRunbookClient automation.RunbookClient
automationCredentialClient automation.CredentialClient
automationScheduleClient automation.ScheduleClient
automationRunbookDraftClient automation.RunbookDraftClient

dnsClient dns.RecordSetsClient
zonesClient dns.ZonesClient
Expand Down Expand Up @@ -472,6 +473,10 @@ func (c *ArmClient) registerAutomationClients(endpoint, subscriptionId string, a
scheduleClient := automation.NewScheduleClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&scheduleClient.Client, auth)
c.automationScheduleClient = scheduleClient

runbookDraftClient := automation.NewRunbookDraftClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&runbookDraftClient.Client, auth)
c.automationRunbookDraftClient = runbookDraftClient
}

func (c *ArmClient) registerAuthentication(endpoint, graphEndpoint, subscriptionId, tenantId string, auth, graphAuth autorest.Authorizer, sender autorest.Sender) {
Expand Down
45 changes: 41 additions & 4 deletions azurerm/resource_arm_automation_runbook.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package azurerm

import (
"bytes"
"fmt"
"io/ioutil"
"log"

"github.com/Azure/azure-sdk-for-go/services/automation/mgmt/2015-10-31/automation"
Expand Down Expand Up @@ -68,6 +70,12 @@ func resourceArmAutomationRunbook() *schema.Resource {
Optional: true,
},

"content": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"publish_content_link": {
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -144,16 +152,31 @@ func resourceArmAutomationRunbookCreateUpdate(d *schema.ResourceData, meta inter

_, err := client.CreateOrUpdate(ctx, resGroup, accName, name, parameters)
if err != nil {
return err
return fmt.Errorf("Error creating/updating Automation Runbook %q (Account %q / Resource Group %q): %+v", name, accName, resGroup, err)
}

if v, ok := d.GetOk("content"); ok {
content := v.(string)
reader := ioutil.NopCloser(bytes.NewBufferString(content))
draftClient := meta.(*ArmClient).automationRunbookDraftClient
_, err := draftClient.ReplaceContent(ctx, resGroup, accName, name, reader)
if err != nil {
return fmt.Errorf("Error setting the draft Automation Runbook %q (Account %q / Resource Group %q): %+v", name, accName, resGroup, err)
}

_, err = draftClient.Publish(ctx, resGroup, accName, name)
if err != nil {
return fmt.Errorf("Error publishing the updated Automation Runbook %q (Account %q / Resource Group %q): %+v", name, accName, resGroup, err)
}
}

read, err := client.Get(ctx, resGroup, accName, name)
if err != nil {
return err
return fmt.Errorf("Error retrieving Automation Runbook %q (Account %q / Resource Group %q): %+v", name, accName, resGroup, err)
}

if read.ID == nil {
return fmt.Errorf("Cannot read Automation Runbook '%s' (resource group %s) ID", name, resGroup)
return fmt.Errorf("Cannot read Automation Runbook %q (Account %q / Resource Group %q) ID", name, accName, resGroup)
}

d.SetId(*read.ID)
Expand All @@ -180,7 +203,7 @@ func resourceArmAutomationRunbookRead(d *schema.ResourceData, meta interface{})
return nil
}

return fmt.Errorf("Error making Read request on AzureRM Automation Runbook '%s': %+v", name, err)
return fmt.Errorf("Error making Read request on AzureRM Automation Runbook %q (Account %q / Resource Group %q): %+v", name, accName, resGroup, err)
}

d.Set("name", resp.Name)
Expand All @@ -201,6 +224,20 @@ func resourceArmAutomationRunbookRead(d *schema.ResourceData, meta interface{})
flattenAndSetTags(d, tags)
}

response, err := client.GetContent(ctx, resGroup, accName, name)
if err != nil {
return fmt.Errorf("Error retrieving content for Automation Runbook %q (Account %q / Resource Group %q): %+v", name, accName, resGroup, err)
}

if v := response.Value; v != nil {
if contentBytes := *response.Value; contentBytes != nil {
buf := new(bytes.Buffer)
buf.ReadFrom(contentBytes)
content := buf.String()
d.Set("content", content)
}
}

return nil
}

Expand Down
67 changes: 67 additions & 0 deletions azurerm/resource_arm_automation_runbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ func TestAccAzureRMAutomationRunbook_PSWorkflowWithHash(t *testing.T) {
})
}

func TestAccAzureRMAutomationRunbook_PSWithContent(t *testing.T) {
resourceName := "azurerm_automation_runbook.test"
ri := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMAutomationRunbookDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMAutomationRunbook_PSWithContent(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAutomationRunbookExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "runbook_type", "PowerShell"),
resource.TestCheckResourceAttr(resourceName, "content", "# Some test content\n# for Terraform acceptance test\n"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"publish_content_link"},
},
},
})
}

func testCheckAzureRMAutomationRunbookDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).automationRunbookClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext
Expand Down Expand Up @@ -199,3 +226,43 @@ resource "azurerm_automation_runbook" "test" {
}
`, rInt, location, rInt)
}

func testAccAzureRMAutomationRunbook_PSWithContent(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_runbook" "test" {
name = "Get-AzureVMTutorial"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

account_name = "${azurerm_automation_account.test.name}"
log_verbose = "true"
log_progress = "true"
description = "This is a test runbook for terraform acceptance test"
runbook_type = "PowerShell"

publish_content_link {
uri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-automation-runbook-getvms/Runbooks/Get-AzureVMTutorial.ps1"
}

content = <<CONTENT
# Some test content
# for Terraform acceptance test
CONTENT
}
`, rInt, location, rInt)
}
4 changes: 2 additions & 2 deletions azurerm/resource_arm_key_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ func resourceArmKeyVaultRead(d *schema.ResourceData, meta interface{}) error {
d.Set("enabled_for_disk_encryption", props.EnabledForDiskEncryption)
d.Set("enabled_for_template_deployment", props.EnabledForTemplateDeployment)
if err := d.Set("sku", flattenKeyVaultSku(props.Sku)); err != nil {
return fmt.Errorf("Error flattening `sku` for KeyVault %s: %+v", *resp.Name, err)
return fmt.Errorf("Error flattening `sku` for KeyVault %q: %+v", *resp.Name, err)
}

flattenedPolicies := azure.FlattenKeyVaultAccessPolicies(props.AccessPolicies)
if err := d.Set("access_policy", flattenedPolicies); err != nil {
return fmt.Errorf("Error flattening `access_policy` for KeyVault %s: %+v", *resp.Name, err)
return fmt.Errorf("Error flattening `access_policy` for KeyVault %q: %+v", *resp.Name, err)
}
d.Set("vault_uri", props.VaultURI)
}
Expand Down
47 changes: 45 additions & 2 deletions website/docs/r/automation_runbook.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,43 @@ resource "azurerm_automation_runbook" "example" {
}
```

## Example Usage - custom content

```hcl
resource "azurerm_resource_group" "example" {
name = "resourceGroup1"
location = "West Europe"
}

resource "azurerm_automation_account" "example" {
name = "account1"
location = "${azurerm_resource_group.example.location}"
resource_group_name = "${azurerm_resource_group.example.name}"
sku {
name = "Basic"
}
}

data "local_file" "example" {
filename = "${path.module}/example.ps1"
}

resource "azurerm_automation_runbook" "example" {
name = "Get-AzureVMTutorial"
location = "${azurerm_resource_group.example.location}"
resource_group_name = "${azurerm_resource_group.example.name}"
account_name = "${azurerm_automation_account.example.name}"
log_verbose = "true"
log_progress = "true"
description = "This is an example runbook"
runbook_type = "PowerShell"
publish_content_link {
uri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-automation-runbook-getvms/Runbooks/Get-AzureVMTutorial.ps1"
}
content = "${data.local_file.example.content}"
}
```

## Argument Reference

The following arguments are supported:
Expand All @@ -58,11 +95,17 @@ The following arguments are supported:

* `log_progress` - (Required) Progress log option.

* `log_verbose` - (Required) Verbose log option.
* `log_verbose` - (Required) Verbose log option.

* `publish_content_link` - (Required) The published runbook content link.

* `description` - (Optional) A description for this credential.
* `description` - (Optional) A description for this credential.

* `content` - (Optional) The desired content of the runbook.

~> **NOTE** The Azure API requires a `publish_content_link` to be supplied even when specifying your own `content`.

~> **NOTE** Setting `content` to an empty string will revert the runbook to the `publish_content_link`.

`publish_content_link` supports the following:

Expand Down