-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
azurerm_automation_job_schedule
needs to be recreated after `azurer…
…m_automation_runbook` is updated (#7555) Fix: #7130 Local test the issue scenario with pass. Job_schedule is a link from runbook to schedule. Once runbook has been updated, all its related job_schedule will update all linked job schedule id. First terraform apply, one change to update runbook, terraform apply with success. Second terraform apply, one create to create job_schedule, (Because job schedule id has been updated, the job schedule reading from its elder job schedule id doesn't exist any more. Here we delete its linked job schedule and recreate this job schedule ), terraform apply with success. Co-authored-by: kt <[email protected]>
- Loading branch information
Showing
4 changed files
with
424 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
azurerm/internal/services/automation/helper/automation_job_schedule.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package helper | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/automation/mgmt/2015-10-31/automation" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode" | ||
"github.com/hashicorp/terraform-plugin-sdk/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/utils" | ||
) | ||
|
||
func JobScheduleSchema() *schema.Schema { | ||
return &schema.Schema{ | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Computed: true, | ||
ConfigMode: schema.SchemaConfigModeAttr, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"schedule_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: azure.ValidateAutomationScheduleName(), | ||
}, | ||
|
||
"parameters": { | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
ValidateFunc: func(v interface{}, _ string) (warnings []string, errors []error) { | ||
m := v.(map[string]interface{}) | ||
for k := range m { | ||
if k != strings.ToLower(k) { | ||
errors = append(errors, fmt.Errorf("Due to a bug in the implementation of Runbooks in Azure, the parameter names need to be specified in lowercase only. See: \"https://github.com/Azure/azure-sdk-for-go/issues/4780\" for more information.")) | ||
} | ||
} | ||
|
||
return warnings, errors | ||
}, | ||
}, | ||
|
||
"run_on": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
|
||
"job_schedule_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
Set: resourceAutomationJobScheduleHash, | ||
} | ||
} | ||
|
||
func ExpandAutomationJobSchedule(input []interface{}, runBookName string) map[uuid.UUID]automation.JobScheduleCreateParameters { | ||
res := make(map[uuid.UUID]automation.JobScheduleCreateParameters) | ||
if len(input) == 0 || input[0] == nil { | ||
return res | ||
} | ||
|
||
for _, v := range input { | ||
js := v.(map[string]interface{}) | ||
jobScheduleCreateParameters := automation.JobScheduleCreateParameters{ | ||
JobScheduleCreateProperties: &automation.JobScheduleCreateProperties{ | ||
Schedule: &automation.ScheduleAssociationProperty{ | ||
Name: utils.String(js["schedule_name"].(string)), | ||
}, | ||
Runbook: &automation.RunbookAssociationProperty{ | ||
Name: utils.String(runBookName), | ||
}, | ||
}, | ||
} | ||
|
||
if v, ok := js["parameters"]; ok { | ||
jsParameters := make(map[string]*string) | ||
for k, v := range v.(map[string]interface{}) { | ||
value := v.(string) | ||
jsParameters[k] = &value | ||
} | ||
jobScheduleCreateParameters.JobScheduleCreateProperties.Parameters = jsParameters | ||
} | ||
|
||
if v, ok := js["run_on"]; ok && v.(string) != "" { | ||
value := v.(string) | ||
jobScheduleCreateParameters.JobScheduleCreateProperties.RunOn = &value | ||
} | ||
jobScheduleUUID := uuid.NewV4() | ||
res[jobScheduleUUID] = jobScheduleCreateParameters | ||
} | ||
|
||
return res | ||
} | ||
|
||
func FlattenAutomationJobSchedule(jsMap map[uuid.UUID]automation.JobScheduleProperties) *schema.Set { | ||
res := &schema.Set{ | ||
F: resourceAutomationJobScheduleHash, | ||
} | ||
for jsId, js := range jsMap { | ||
var scheduleName, runOn string | ||
if js.Schedule.Name != nil { | ||
scheduleName = *js.Schedule.Name | ||
} | ||
|
||
if js.RunOn != nil { | ||
runOn = *js.RunOn | ||
} | ||
|
||
res.Add(map[string]interface{}{ | ||
"schedule_name": scheduleName, | ||
"parameters": utils.FlattenMapStringPtrString(js.Parameters), | ||
"run_on": runOn, | ||
"job_schedule_id": jsId.String(), | ||
}) | ||
} | ||
|
||
return res | ||
} | ||
|
||
func resourceAutomationJobScheduleHash(v interface{}) int { | ||
var buf bytes.Buffer | ||
|
||
if m, ok := v.(automation.JobScheduleProperties); ok { | ||
var scheduleName, runOn string | ||
if m.Schedule.Name != nil { | ||
scheduleName = *m.Schedule.Name | ||
} | ||
|
||
if m.RunOn != nil { | ||
runOn = *m.RunOn | ||
} | ||
|
||
buf.WriteString(fmt.Sprintf("%s-%s-%s-%s", scheduleName, utils.FlattenMapStringPtrString(m.Parameters), runOn, *m.JobScheduleID)) | ||
} | ||
|
||
return hashcode.String(buf.String()) | ||
} |
Oops, something went wrong.