From b629687523a4618836898c8a3b8d175670295df1 Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Wed, 18 Jul 2018 06:25:42 +0100 Subject: [PATCH 01/11] add content param --- azurerm/config.go | 16 ++++++++++++---- azurerm/resource_arm_automation_runbook.go | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/azurerm/config.go b/azurerm/config.go index f263bae528cc..15a48584a1f9 100644 --- a/azurerm/config.go +++ b/azurerm/config.go @@ -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 @@ -472,6 +473,13 @@ 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) + setUserAgent(&runbookDraftClient.Client) + runbookDraftClient.Authorizer = auth + runbookDraftClient.Sender = sender + runbookDraftClient.SkipResourceProviderRegistration = c.skipProviderRegistration + c.automationRunbookDraftClient = runbookDraftClient } func (c *ArmClient) registerAuthentication(endpoint, graphEndpoint, subscriptionId, tenantId string, auth, graphAuth autorest.Authorizer, sender autorest.Sender) { diff --git a/azurerm/resource_arm_automation_runbook.go b/azurerm/resource_arm_automation_runbook.go index 914cddf5a7d7..ceb5feed84c1 100644 --- a/azurerm/resource_arm_automation_runbook.go +++ b/azurerm/resource_arm_automation_runbook.go @@ -68,6 +68,11 @@ func resourceArmAutomationRunbook() *schema.Resource { Optional: true, }, + "content": { + Type: schema.TypeString, + Optional: true, + }, + "publish_content_link": { Type: schema.TypeList, Required: true, @@ -147,6 +152,19 @@ func resourceArmAutomationRunbookCreateUpdate(d *schema.ResourceData, meta inter return err } + if v, ok := d.GetOk("content"); ok { + content := v.(string) + draftClient := meta.(*ArmClient).automationRunbookDraftClient + _, err := draftClient.ReplaceContent(ctx, resGroup, accName, name, content) + if err != nil { + return err + } + _, err = draftClient.Publish(ctx, resGroup, accName, name) + if err != nil { + return err + } + } + read, err := client.Get(ctx, resGroup, accName, name) if err != nil { return err From 0365acc29f97819cc1c58dc876c0c0ca9a631270 Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Wed, 18 Jul 2018 13:10:45 +0100 Subject: [PATCH 02/11] read content --- azurerm/resource_arm_automation_runbook.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/azurerm/resource_arm_automation_runbook.go b/azurerm/resource_arm_automation_runbook.go index ceb5feed84c1..5d42fe0b4324 100644 --- a/azurerm/resource_arm_automation_runbook.go +++ b/azurerm/resource_arm_automation_runbook.go @@ -219,6 +219,12 @@ func resourceArmAutomationRunbookRead(d *schema.ResourceData, meta interface{}) flattenAndSetTags(d, tags) } + content, err := client.GetContent(ctx, resGroup, accName, name) + if err != nil { + return err + } + d.Set("content", content) + return nil } From 8d97e9609995f942cd4798dcf815985922e1b1cf Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Tue, 31 Jul 2018 15:51:28 +0100 Subject: [PATCH 03/11] update sdk; convert content bytes to string --- azurerm/resource_arm_automation_runbook.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/azurerm/resource_arm_automation_runbook.go b/azurerm/resource_arm_automation_runbook.go index 5d42fe0b4324..0b5894c35bdc 100644 --- a/azurerm/resource_arm_automation_runbook.go +++ b/azurerm/resource_arm_automation_runbook.go @@ -1,6 +1,7 @@ package azurerm import ( + "bytes" "fmt" "log" @@ -219,11 +220,16 @@ func resourceArmAutomationRunbookRead(d *schema.ResourceData, meta interface{}) flattenAndSetTags(d, tags) } - content, err := client.GetContent(ctx, resGroup, accName, name) + response, err := client.GetContent(ctx, resGroup, accName, name) if err != nil { return err } - d.Set("content", content) + if contentBytes := *response.Value; contentBytes != nil { + buf := new(bytes.Buffer) + buf.ReadFrom(contentBytes) + content := buf.String() + d.Set("content", content) + } return nil } From 46929dfe2267387a2046813c237788a8a4278a5c Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Wed, 1 Aug 2018 09:46:17 +0100 Subject: [PATCH 04/11] fix fmt errors --- azurerm/resource_arm_key_vault.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azurerm/resource_arm_key_vault.go b/azurerm/resource_arm_key_vault.go index f7179cda8d24..fd1c5ee8ef84 100644 --- a/azurerm/resource_arm_key_vault.go +++ b/azurerm/resource_arm_key_vault.go @@ -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) } From 0373e9106a19f32e48fe792dcdfe335821a551a5 Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Wed, 1 Aug 2018 09:48:46 +0100 Subject: [PATCH 05/11] use latest automation sdk --- azurerm/resource_arm_automation_runbook.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/azurerm/resource_arm_automation_runbook.go b/azurerm/resource_arm_automation_runbook.go index 0b5894c35bdc..94202ea83038 100644 --- a/azurerm/resource_arm_automation_runbook.go +++ b/azurerm/resource_arm_automation_runbook.go @@ -3,6 +3,7 @@ package azurerm import ( "bytes" "fmt" + "io/ioutil" "log" "github.com/Azure/azure-sdk-for-go/services/automation/mgmt/2015-10-31/automation" @@ -155,8 +156,9 @@ func resourceArmAutomationRunbookCreateUpdate(d *schema.ResourceData, meta inter 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, content) + _, err := draftClient.ReplaceContent(ctx, resGroup, accName, name, reader) if err != nil { return err } From 13584f9c7f545e74746bdb711516a8d1b795afec Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Wed, 1 Aug 2018 09:48:57 +0100 Subject: [PATCH 06/11] add test --- .../resource_arm_automation_runbook_test.go | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/azurerm/resource_arm_automation_runbook_test.go b/azurerm/resource_arm_automation_runbook_test.go index 27686417f540..14fd140789a0 100644 --- a/azurerm/resource_arm_automation_runbook_test.go +++ b/azurerm/resource_arm_automation_runbook_test.go @@ -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 @@ -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 = < Date: Wed, 1 Aug 2018 09:58:31 +0100 Subject: [PATCH 07/11] docs --- website/docs/r/automation_runbook.html.markdown | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/website/docs/r/automation_runbook.html.markdown b/website/docs/r/automation_runbook.html.markdown index 4ee9e8d512a4..0dc126b9923e 100644 --- a/website/docs/r/automation_runbook.html.markdown +++ b/website/docs/r/automation_runbook.html.markdown @@ -58,11 +58,15 @@ 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`. `publish_content_link` supports the following: From ad6d35986614a8c481705d118b3fe584cd9fa747 Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Wed, 1 Aug 2018 10:28:57 +0100 Subject: [PATCH 08/11] docs --- website/docs/r/automation_runbook.html.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/docs/r/automation_runbook.html.markdown b/website/docs/r/automation_runbook.html.markdown index 0dc126b9923e..f117e10a2a2d 100644 --- a/website/docs/r/automation_runbook.html.markdown +++ b/website/docs/r/automation_runbook.html.markdown @@ -68,6 +68,8 @@ The following arguments are supported: ~> **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: * `uri` - (Required) The uri of the runbook content. From ba1263170e20f70c10979fc2b3fc5ba9e3bc0b54 Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Wed, 1 Aug 2018 10:36:42 +0100 Subject: [PATCH 09/11] docs - add example --- .../docs/r/automation_runbook.html.markdown | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/website/docs/r/automation_runbook.html.markdown b/website/docs/r/automation_runbook.html.markdown index f117e10a2a2d..064f139395aa 100644 --- a/website/docs/r/automation_runbook.html.markdown +++ b/website/docs/r/automation_runbook.html.markdown @@ -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: From 139d5039378d64f4fa2c53565fe0d372c082f612 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Fri, 7 Sep 2018 10:20:27 +0200 Subject: [PATCH 10/11] Automation Runbook: minor refactoring --- azurerm/config.go | 5 +--- azurerm/resource_arm_automation_runbook.go | 28 ++++++++++++---------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/azurerm/config.go b/azurerm/config.go index 15a48584a1f9..08c4706dfd6d 100644 --- a/azurerm/config.go +++ b/azurerm/config.go @@ -475,10 +475,7 @@ func (c *ArmClient) registerAutomationClients(endpoint, subscriptionId string, a c.automationScheduleClient = scheduleClient runbookDraftClient := automation.NewRunbookDraftClientWithBaseURI(endpoint, subscriptionId) - setUserAgent(&runbookDraftClient.Client) - runbookDraftClient.Authorizer = auth - runbookDraftClient.Sender = sender - runbookDraftClient.SkipResourceProviderRegistration = c.skipProviderRegistration + c.configureClient(&runbookDraftClient.Client, auth) c.automationRunbookDraftClient = runbookDraftClient } diff --git a/azurerm/resource_arm_automation_runbook.go b/azurerm/resource_arm_automation_runbook.go index 94202ea83038..4ce951edde19 100644 --- a/azurerm/resource_arm_automation_runbook.go +++ b/azurerm/resource_arm_automation_runbook.go @@ -151,7 +151,7 @@ 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 { @@ -160,21 +160,22 @@ func resourceArmAutomationRunbookCreateUpdate(d *schema.ResourceData, meta inter draftClient := meta.(*ArmClient).automationRunbookDraftClient _, err := draftClient.ReplaceContent(ctx, resGroup, accName, name, reader) if err != nil { - return err + 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 err + 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) @@ -201,7 +202,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) @@ -224,13 +225,16 @@ func resourceArmAutomationRunbookRead(d *schema.ResourceData, meta interface{}) response, err := client.GetContent(ctx, resGroup, accName, name) if err != nil { - return err + return fmt.Errorf("Error retrieving content for Automation Runbook %q (Account %q / Resource Group %q): %+v", name, accName, resGroup, err) } - if contentBytes := *response.Value; contentBytes != nil { - buf := new(bytes.Buffer) - buf.ReadFrom(contentBytes) - content := buf.String() - d.Set("content", content) + + 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 From 7e3e123e1f1d5645711c0838bd8709cbf6e0886b Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Fri, 7 Sep 2018 11:15:04 +0200 Subject: [PATCH 11/11] Making the `content` field computed --- azurerm/resource_arm_automation_runbook.go | 1 + 1 file changed, 1 insertion(+) diff --git a/azurerm/resource_arm_automation_runbook.go b/azurerm/resource_arm_automation_runbook.go index 4ce951edde19..a6ccbf8ac6ad 100644 --- a/azurerm/resource_arm_automation_runbook.go +++ b/azurerm/resource_arm_automation_runbook.go @@ -73,6 +73,7 @@ func resourceArmAutomationRunbook() *schema.Resource { "content": { Type: schema.TypeString, Optional: true, + Computed: true, }, "publish_content_link": {