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

azurerm_automation_runbook - add support for create runbook as draft and then publish #6813

Merged
merged 2 commits into from May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 27 additions & 18 deletions azurerm/internal/services/automation/automation_runbook_resource.go
Expand Up @@ -87,15 +87,18 @@ func resourceArmAutomationRunbook() *schema.Resource {
},

"content": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
AtLeastOneOf: []string{"content", "publish_content_link"},
ValidateFunc: validation.StringIsNotEmpty,
},

"publish_content_link": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
AtLeastOneOf: []string{"content", "publish_content_link"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"uri": {
Expand Down Expand Up @@ -166,21 +169,25 @@ func resourceArmAutomationRunbookCreateUpdate(d *schema.ResourceData, meta inter
logVerbose := d.Get("log_verbose").(bool)
description := d.Get("description").(string)

contentLink := expandContentLink(d)

parameters := automation.RunbookCreateOrUpdateParameters{
RunbookCreateOrUpdateProperties: &automation.RunbookCreateOrUpdateProperties{
LogVerbose: &logVerbose,
LogProgress: &logProgress,
RunbookType: runbookType,
Description: &description,
PublishContentLink: &contentLink,
LogVerbose: &logVerbose,
LogProgress: &logProgress,
RunbookType: runbookType,
Description: &description,
},

Location: &location,
Tags: tags.Expand(t),
}

contentLink := expandContentLink(d.Get("publish_content_link").([]interface{}))
if contentLink != nil {
parameters.RunbookCreateOrUpdateProperties.PublishContentLink = contentLink
} else {
parameters.RunbookCreateOrUpdateProperties.Draft = &automation.RunbookDraft{}
}

if _, err := client.CreateOrUpdate(ctx, resGroup, accName, name, parameters); err != nil {
return fmt.Errorf("Error creating/updating Automation Runbook %q (Account %q / Resource Group %q): %+v", name, accName, resGroup, err)
}
Expand Down Expand Up @@ -298,20 +305,22 @@ func resourceArmAutomationRunbookDelete(d *schema.ResourceData, meta interface{}
return nil
}

func expandContentLink(d *schema.ResourceData) automation.ContentLink {
inputs := d.Get("publish_content_link").([]interface{})
func expandContentLink(inputs []interface{}) *automation.ContentLink {
if len(inputs) == 0 {
katbyte marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

input := inputs[0].(map[string]interface{})
uri := input["uri"].(string)
version := input["version"].(string)

hashes := input["hash"].([]interface{})

if len(hashes) > 0 {
hash := hashes[0].(map[string]interface{})
hashValue := hash["value"].(string)
hashAlgorithm := hash["algorithm"].(string)

return automation.ContentLink{
return &automation.ContentLink{
URI: &uri,
Version: &version,
ContentHash: &automation.ContentHash{
Expand All @@ -321,7 +330,7 @@ func expandContentLink(d *schema.ResourceData) automation.ContentLink {
}
}

return automation.ContentLink{
return &automation.ContentLink{
URI: &uri,
Version: &version,
}
Expand Down
Expand Up @@ -23,7 +23,6 @@ func TestAccAzureRMAutomationRunbook_PSWorkflow(t *testing.T) {
Config: testAccAzureRMAutomationRunbook_PSWorkflow(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMAutomationRunbookExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "runbook_type", "PowerShellWorkflow"),
),
},
data.ImportStep("publish_content_link"),
Expand Down Expand Up @@ -184,11 +183,12 @@ resource "azurerm_automation_runbook" "test" {
log_verbose = "true"
log_progress = "true"
description = "This is a test runbook for terraform acceptance test"
runbook_type = "PowerShellWorkflow"
runbook_type = "PowerShell"

publish_content_link {
uri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/c4935ffb69246a6058eb24f54640f53f69d3ac9f/101-automation-runbook-getvms/Runbooks/Get-AzureVMTutorial.ps1"
}
content = <<CONTENT
# Some test content
# for Terraform acceptance test
CONTENT
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
Expand All @@ -207,11 +207,12 @@ resource "azurerm_automation_runbook" "import" {
log_verbose = "true"
log_progress = "true"
description = "This is a test runbook for terraform acceptance test"
runbook_type = "PowerShellWorkflow"
runbook_type = "PowerShell"

publish_content_link {
uri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/c4935ffb69246a6058eb24f54640f53f69d3ac9f/101-automation-runbook-getvms/Runbooks/Get-AzureVMTutorial.ps1"
}
content = <<CONTENT
# Some test content
# for Terraform acceptance test
CONTENT
}
`, template)
}
Expand Down
8 changes: 1 addition & 7 deletions website/docs/r/automation_runbook.html.markdown
Expand Up @@ -76,10 +76,6 @@ resource "azurerm_automation_runbook" "example" {
description = "This is an example runbook"
runbook_type = "PowerShell"

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

content = data.local_file.example.content
}
```
Expand All @@ -102,16 +98,14 @@ The following arguments are supported:

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

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

* `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`.

* `tags` - (Optional) A mapping of tags to assign to the resource.

`publish_content_link` supports the following:
Expand Down