-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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_job_schedule
: update jobSchedule resource id format
#22164
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR!
I've taken a look through and left some comments inline. The main ask is that this solution is built on an assumption that the schedules linked to a runbook must have different names, would you please help confirm whether this is the case?
internal/services/automation/migration/automation_job_schedule_migration_v0_to_v1.go
Outdated
Show resolved
Hide resolved
internal/services/automation/automation_runbook_resource_test.go
Outdated
Show resolved
Hide resolved
internal/services/automation/automation_job_schedule_resource_test.go
Outdated
Show resolved
Hide resolved
internal/services/automation/automation_job_schedule_resource.go
Outdated
Show resolved
Hide resolved
internal/services/automation/automation_job_schedule_resource.go
Outdated
Show resolved
Hide resolved
internal/services/automation/automation_job_schedule_resource.go
Outdated
Show resolved
Hide resolved
internal/services/automation/automation_job_schedule_resource.go
Outdated
Show resolved
Hide resolved
The Schedule name should be unique inside the same automation account, and there MUST be only 1 link between the runbook and the schedule. so the new id schema should work. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR!
I have left some additional mostly minor comments that once addressed this should be good to merge 👍
internal/services/automation/automation_job_schedule_resource.go
Outdated
Show resolved
Hide resolved
internal/services/automation/automation_job_schedule_resource.go
Outdated
Show resolved
Hide resolved
internal/services/automation/automation_job_schedule_resource_test.go
Outdated
Show resolved
Hide resolved
internal/services/automation/automation_runbook_resource_test.go
Outdated
Show resolved
Hide resolved
internal/services/automation/automation_runbook_resource_test.go
Outdated
Show resolved
Hide resolved
internal/services/automation/automation_runbook_resource_test.go
Outdated
Show resolved
Hide resolved
@@ -0,0 +1,3 @@ | |||
package automation | |||
|
|||
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=AutomationJobSchedule -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Automation/automationAccounts/account1/runBook/book1/schedule/schedule1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The functionality provided by resourceids.go
shouldn't be used for new resources and will be removed once we've finished migrating off them - instead, since this is already using hashicorp/go-azure-sdk
- the relevant Resource ID from hashicorp/go-azure-sdk
should be used instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the Resource ID in go-azure-sdk
cannot work as expect in the issue of this PR is tring to fix. because the job schedule id will be updated automatically when the runbook resource updated. so I create this new job schedule ID Format to represent the link between the runbook and schedule.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated this PR to fix some logic and tested it locally. Although the ID format of the resource has changed, I verified that this update should not introduce any breaking changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove this now?
9be75ad
to
48e1117
Compare
7713e60
to
b783f72
Compare
@@ -67,5 +71,5 @@ The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/l | |||
Automation Job Schedules can be imported using the `resource id`, e.g. | |||
|
|||
```shell | |||
terraform import azurerm_automation_job_schedule.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Automation/automationAccounts/account1/jobSchedules/10000000-1001-1001-1001-000000000001 | |||
terraform import azurerm_automation_job_schedule.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Automation/automationAccounts/account1/runBook/book1/schedule/schedule1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we are creating a new "fake" resource id instead of creating a composite resource id? ie
/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Automation/automationAccounts/account1| /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Automation/automationAccounts/account1/runbooks/Get-AzureVMTutorial
this is something we are looking to implement first class support for soon and will work with the up coming casing fixes - see https://github.com/hashicorp/go-azure-helpers/pull/208/files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A composite resource ID would definitely be effective. Is there a plan to merge PR 208? I will update this PR accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, hopefully it'll be merged in the next couple weeks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wuxu92 - the above PR has been merged
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated this PR with CompositeResourceID but need new version/tag of go-azure-helpers
to be released. so I'm going to make this PR draft before new version of go-azure-helpers
available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
accountID := jobschedule.NewAutomationAccountID(id.First.SubscriptionId, id.First.ResourceGroupName, id.First.AutomationAccountName) | ||
filter := fmt.Sprintf("properties/schedule/name eq '%s' and properties/runbook/name eq '%s'", id.First.ScheduleName, id.Second.RunbookName) | ||
jsList, err := client.ListByAutomationAccountComplete(ctx, accountID, jobschedule.ListByAutomationAccountOperationOptions{Filter: &filter}) | ||
if err != nil { | ||
return nil, fmt.Errorf("loading Automation Account %q Job Schedule List: %+v", accountID.AutomationAccountName, err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this needed? The Go SDK has a Get
method?
func (c RunbookClient) Get(ctx context.Context, id RunbookId) (result GetOperationResponse, err error) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to get the job schedule here, but we don't know the job schedule id from the composite id, because the job schedule id can be updated when the related account or runbook is updated. So we need the List operation to get the correct job schedule id here.
@@ -0,0 +1,3 @@ | |||
package automation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we remove this file?
runbookID := runbook.NewRunbookID(subscriptionId, resourceGroup, accountName, runbookName) | ||
id := jobschedule.NewJobScheduleID(subscriptionId, resourceGroup, accountName, jobScheduleUUID.String()) | ||
|
||
// tfID := parse.NewAutomationJobScheduleID(id.SubscriptionId, id.ResourceGroupName, id.AutomationAccountName, runbookName, scheduleName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can remove this line now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed.
@@ -0,0 +1,3 @@ | |||
package automation | |||
|
|||
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=AutomationJobSchedule -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Automation/automationAccounts/account1/runBook/book1/schedule/schedule1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove this now?
@@ -10,6 +10,8 @@ description: |- | |||
|
|||
Links an Automation Runbook and Schedule. | |||
|
|||
~> **NOTE** AzureRM provides this stand-alone [azurerm_automation_job_schedule](automation_job_schedule.html.markdown) and an inlined `job_schdule` property in [azurerm_runbook](automation_runbook.html.markdown) to manage the job schedules. At this time you should choose one of them to manage the job schedule resources. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
~> **NOTE** AzureRM provides this stand-alone [azurerm_automation_job_schedule](automation_job_schedule.html.markdown) and an inlined `job_schdule` property in [azurerm_runbook](automation_runbook.html.markdown) to manage the job schedules. At this time you should choose one of them to manage the job schedule resources. | |
~> **NOTE** AzureRM provides this stand-alone [azurerm_automation_job_schedule](automation_job_schedule.html.markdown) and an inlined `job_schdule` property in [azurerm_runbook](automation_runbook.html.markdown) to manage the job schedules. You can only make use of one of these methods to manage a job schedule. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @wuxu92 I had a look though this and left a couple of comments inline, but once those are addressed we can take another look. Thanks!
@@ -107,43 +125,24 @@ func resourceAutomationJobScheduleCreate(d *pluginsdk.ResourceData, meta interfa | |||
jobScheduleUUID = uuid.FromStringOrNil(jobScheduleID.(string)) | |||
} | |||
|
|||
id := jobschedule.NewJobScheduleID(subscriptionId, d.Get("resource_group_name").(string), d.Get("automation_account_name").(string), jobScheduleUUID.String()) | |||
// accountID := automationaccount.NewAutomationAccountID(subscriptionId, resourceGroup, accountName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed.
js, err := GetJobScheduleFromTFID(ctx, client, tfID) | ||
if err != nil { | ||
if response.WasNotFound(resp.HttpResponse) { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("making Read request on %s: %+v", *id, err) | ||
return err | ||
} | ||
if js == nil { | ||
d.SetId("") | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the case where the resource group has been deleted outside of terraform, this will continually give us an error when trying to list the job schedules and it is never marked as gone. We should update this to cover this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated GetJobScheduleFromTFID
to return nil error if API response is 404.
* `draft` - (Optional) A `draft` block as defined below . | ||
* `draft` - (Optional) A `draft` block as defined below. | ||
|
||
* `job_schedule` - (Optional) Onr or more `job_schedule` block as defined below. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* `job_schedule` - (Optional) Onr or more `job_schedule` block as defined below. | |
* `job_schedule` - (Optional) One or more `job_schedule` block as defined below. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making the changes @wuxu92 - I tested this and still got an error in some cases, could you take another look? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing this up @wuxu92 - I spotted a couple of minor things on looking through this again but otherwise this is looking good. Thanks!
@@ -67,5 +71,5 @@ The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/l | |||
Automation Job Schedules can be imported using the `resource id`, e.g. | |||
|
|||
```shell | |||
terraform import azurerm_automation_job_schedule.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Automation/automationAccounts/account1/jobSchedules/10000000-1001-1001-1001-000000000001 | |||
terraform import azurerm_automation_job_schedule.example "/subscriptions/85b3dbca-5974-4067-9669-67a141095a76/resourceGroups/acctestRG-auto-240326135902419761/providers/Microsoft.Automation/automationAccounts/acctestAA-240328091346495956/schedules/acctestAS-240328091346495956|/subscriptions/85b3dbca-5974-4067-9669-67a141095a76/resourceGroups/acctestRG-auto-240326135902419761/providers/Microsoft.Automation/automationAccounts/acctestAA-240328091346495956/runbooks/Output-HelloWorld" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
terraform import azurerm_automation_job_schedule.example "/subscriptions/85b3dbca-5974-4067-9669-67a141095a76/resourceGroups/acctestRG-auto-240326135902419761/providers/Microsoft.Automation/automationAccounts/acctestAA-240328091346495956/schedules/acctestAS-240328091346495956|/subscriptions/85b3dbca-5974-4067-9669-67a141095a76/resourceGroups/acctestRG-auto-240326135902419761/providers/Microsoft.Automation/automationAccounts/acctestAA-240328091346495956/runbooks/Output-HelloWorld" | |
terraform import azurerm_automation_job_schedule.example "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Automation/automationAccounts/account1/schedules/schedule1|/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Automation/automationAccounts/account1/runbooks/runbook1" |
|
||
id, err := jobschedule.ParseJobScheduleID(pointer.From(js.Id)) | ||
if err != nil { | ||
return fmt.Errorf("parse jobSchedule id %s: %v", pointer.From(js.Id), err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return fmt.Errorf("parse jobSchedule id %s: %v", pointer.From(js.Id), err) | |
return err |
The job schdule is a link between a runbook and a schdule. there should be only one such link between the runbook and schedule pair. When we update a runbook, request a PUT of runbook resource, the job schedule id linked to this runbook will be automatically updated, and the old job schedule id should be NOT FOUND then.
The current implementation introduced in #7555 will try to delete all job schedules linked to the runbook and then it need a next
terraform apply
to recreate these job schedules. This is not neccessary at first, and it make theterraform plan
can not report whatterraform apply
really does, it makes users confused when update a runbook.This PR is to replace the
azurerm_automation_job_schedule
resource id with a customized one. The new one contains the runbook name and the schedule resource name, so we can query the job schdule id in runtime and made the id responded from API as computed attributeresource_manager_id
. this PR also introduces a schema migration to make sure not break exsiting resources.the old resource id format:
terraform import azurerm_automation_job_schedule.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Automation/automationAccounts/account1/jobSchedules/10000000-1001-1001-1001-000000000001
the new resourec id format:
/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Automation/automationAccounts/account1/runBook/book1/schedule/schedule1
resolves: #8634
Test Pass in TC