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

Add resource azurerm_automation_job_schedule #3386

Merged
merged 23 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from 13 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
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type ArmClient struct {
automationCredentialClient automation.CredentialClient
automationDscConfigurationClient automation.DscConfigurationClient
automationDscNodeConfigurationClient automation.DscNodeConfigurationClient
automationJobScheduleClient automation.JobScheduleClient
automationModuleClient automation.ModuleClient
automationRunbookClient automation.RunbookClient
automationRunbookDraftClient automation.RunbookDraftClient
Expand Down Expand Up @@ -660,6 +661,10 @@ func (c *ArmClient) registerAutomationClients(endpoint, subscriptionId string, a
c.configureClient(&dscNodeConfigurationClient.Client, auth)
c.automationDscNodeConfigurationClient = dscNodeConfigurationClient

jobScheduleClient := automation.NewJobScheduleClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&jobScheduleClient.Client, auth)
c.automationJobScheduleClient = jobScheduleClient

moduleClient := automation.NewModuleClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&moduleClient.Client, auth)
c.automationModuleClient = moduleClient
Expand Down
32 changes: 32 additions & 0 deletions azurerm/helpers/azure/automation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package azure

import (
"regexp"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

// ValidateAutomationAccountName validates Automation Account names
func ValidateAutomationAccountName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile(`^[0-9a-zA-Z][-0-9a-zA-Z]{4,48}[0-9a-zA-Z]$`),
`The account name must start with a letter or number. The account name can contain letters, numbers, and dashes. The final character must be a letter or a number. The account name length must be from 6 to 50 characters.`,
)
}

// ValidateAutomationRunbookName validates Automation Account Runbook names
func ValidateAutomationRunbookName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile(`^[0-9a-zA-Z][-_0-9a-zA-Z]{0,62}$`),
`The name can contain only letters, numbers, underscores and dashes. The name must begin with a letter. The name must be less than 64 characters.`,
)
}

// ValidateAutomationScheduleName validates Automation Account Schedule names
func ValidateAutomationScheduleName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile(`^[^<>*%&:\\?.+\/]{0,127}[^<>*%&:\\?.+\/\s]$`),
`The name length must be from 1 to 128 characters. The name cannot contain special characters < > * % & : \ ? . + / and cannot end with a whitespace character.`,
)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_automation_credential": resourceArmAutomationCredential(),
"azurerm_automation_dsc_configuration": resourceArmAutomationDscConfiguration(),
"azurerm_automation_dsc_nodeconfiguration": resourceArmAutomationDscNodeConfiguration(),
"azurerm_automation_job_schedule": resourceArmAutomationJobSchedule(),
"azurerm_automation_module": resourceArmAutomationModule(),
"azurerm_automation_runbook": resourceArmAutomationRunbook(),
"azurerm_automation_schedule": resourceArmAutomationSchedule(),
Expand Down
209 changes: 209 additions & 0 deletions azurerm/resource_arm_automation_job_schedule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package azurerm

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/automation/mgmt/2015-10-31/automation"
"github.com/hashicorp/terraform/helper/schema"
uuid "github.com/satori/go.uuid"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmAutomationJobSchedule() *schema.Resource {
return &schema.Resource{
Create: resourceArmAutomationJobScheduleCreate,
Read: resourceArmAutomationJobScheduleRead,
Delete: resourceArmAutomationJobScheduleDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{

"resource_group_name": resourceGroupNameSchema(),

"automation_account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateAutomationAccountName(),
},
draggeta marked this conversation as resolved.
Show resolved Hide resolved

"runbook_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateAutomationRunbookName(),
},
draggeta marked this conversation as resolved.
Show resolved Hide resolved

"schedule_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateAutomationScheduleName(),
},
draggeta marked this conversation as resolved.
Show resolved Hide resolved

"parameters": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},

"run_on": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

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

func resourceArmAutomationJobScheduleCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).automationJobScheduleClient
ctx := meta.(*ArmClient).StopContext

log.Printf("[INFO] preparing arguments for AzureRM Automation Job Schedule creation.")

jobScheduleUUID := uuid.NewV4()
resGroup := d.Get("resource_group_name").(string)
accountName := d.Get("automation_account_name").(string)

runbookName := d.Get("runbook_name").(string)
scheduleName := d.Get("schedule_name").(string)

if requireResourcesToBeImported && d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, accountName, jobScheduleUUID)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Automation Job Schedule %q (Account %q / Resource Group %q): %s", jobScheduleUUID, accountName, resGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_automation_job_schedule", *existing.ID)
}
}

parameters := automation.JobScheduleCreateParameters{
JobScheduleCreateProperties: &automation.JobScheduleCreateProperties{
Schedule: &automation.ScheduleAssociationProperty{
Name: &scheduleName,
},
Runbook: &automation.RunbookAssociationProperty{
Name: &runbookName,
},
},
}
properties := parameters.JobScheduleCreateProperties

// parameters to be passed into the runbook
if v, ok := d.GetOk("parameters"); ok {
jsParameters := make(map[string]*string)
for k, v := range v.(map[string]interface{}) {
value := v.(string)
jsParameters[k] = &value
}
properties.Parameters = jsParameters
}

if v, ok := d.GetOk("run_on"); ok {
value := v.(string)
properties.RunOn = &value
}

if _, err := client.Create(ctx, resGroup, accountName, jobScheduleUUID, parameters); err != nil {
return err
}

read, err := client.Get(ctx, resGroup, accountName, jobScheduleUUID)
if err != nil {
return err
}

if read.ID == nil {
return fmt.Errorf("Cannot read Automation Job Schedule '%s' (Account %q / Resource Group %s) ID", jobScheduleUUID, accountName, resGroup)
}

d.SetId(*read.ID)

return resourceArmAutomationJobScheduleRead(d, meta)
}

func resourceArmAutomationJobScheduleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).automationJobScheduleClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

jobScheduleID := id.Path["jobSchedules"]
jobScheduleUUID := uuid.FromStringOrNil(jobScheduleID)
resGroup := id.ResourceGroup
accountName := id.Path["automationAccounts"]

resp, err := client.Get(ctx, resGroup, accountName, jobScheduleUUID)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on AzureRM Automation Job Schedule '%s': %+v", jobScheduleUUID, err)
}

d.Set("job_schedule_id", resp.JobScheduleID)
d.Set("resource_group_name", resGroup)
d.Set("automation_account_name", accountName)
d.Set("runbook_name", resp.JobScheduleProperties.Runbook.Name)
d.Set("schedule_name", resp.JobScheduleProperties.Schedule.Name)

if v := resp.JobScheduleProperties.RunOn; v != nil {
d.Set("run_on", v)
}

if v := resp.JobScheduleProperties.Parameters; v != nil {
jsParameters := make(map[string]interface{})
for key, value := range v {
jsParameters[key] = value
}
d.Set("parameters", jsParameters)
}

return nil
}

func resourceArmAutomationJobScheduleDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).automationJobScheduleClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

jobScheduleID := id.Path["jobSchedules"]
jobScheduleUUID := uuid.FromStringOrNil(jobScheduleID)
resGroup := id.ResourceGroup
accountName := id.Path["automationAccounts"]

resp, err := client.Delete(ctx, resGroup, accountName, jobScheduleUUID)
if err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error issuing AzureRM delete request for Automation Job Schedule '%s': %+v", jobScheduleUUID, err)
}
}

return nil
}
Loading