-
Notifications
You must be signed in to change notification settings - Fork 5k
azurerm_site_recovery_replicated_vm - remove ForceNew from managed_disk disk type fields
#32158
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
Open
ziyeqf
wants to merge
24
commits into
hashicorp:main
Choose a base branch
from
ziyeqf:fix/asr-disk-type-forcenew
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
51aab64
`azurerm_site_recovery_replicated_vm` - remove incorrect `ForceNew` f…
ziyeqf ac5d4cc
Merge branch 'main' into fix/asr-disk-type-forcenew
ziyeqf 2b52ed0
add poller for diskType change
ziyeqf 60967e5
update for disk type update
ziyeqf 5f4cd6e
Update goenv before installing a version
gerrytan 5ec55bd
Revert "Update goenv before installing a version"
ziyeqf f9fda20
Merge branch 'main' into fix/asr-disk-type-forcenew
ziyeqf 53a61bb
update poller
ziyeqf 6cce03a
Merge branch 'main' into fix/asr-disk-type-forcenew
ziyeqf c931ed8
update poller
ziyeqf ccf807b
fmt
ziyeqf 2a8198e
Merge branch 'main' into fix/asr-disk-type-forcenew
ziyeqf 33ddf4a
update custom poller to monitor disk migration job
ziyeqf 99e72f5
update poller
ziyeqf a99176b
update poller
ziyeqf 105c0ba
fix: enforce new disk type for Site Recovery replicated VMs
ziyeqf d2d14fd
Apply suggestions from code review
ziyeqf c61a71a
fix docs lint and address review feedback
ziyeqf 923c44a
update code
ziyeqf 972469f
update `managed_disk` to typeList
ziyeqf aeb7f65
diffsuppress
ziyeqf 603c256
fix test
ziyeqf ca5dde0
update per comment
ziyeqf e4c4257
Merge branch 'main' into fix/asr-disk-type-forcenew
ziyeqf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
184 changes: 184 additions & 0 deletions
184
...l/services/recoveryservices/custompollers/site_recovery_replicated_vm_disk_type_poller.go
This file contains hidden or 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,184 @@ | ||
| // Copyright IBM Corp. 2014, 2025 | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package custompollers | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log" | ||
| "sort" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/hashicorp/go-azure-helpers/lang/pointer" | ||
| "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" | ||
| "github.com/hashicorp/go-azure-sdk/resource-manager/recoveryservicessiterecovery/2024-04-01/replicationprotecteditems" | ||
| "github.com/hashicorp/go-azure-sdk/sdk/client" | ||
| "github.com/hashicorp/go-azure-sdk/sdk/client/pollers" | ||
| ) | ||
|
|
||
| var _ pollers.PollerType = &SiteRecoveryReplicatedVMDiskTypePoller{} | ||
|
|
||
| // siteRecoveryReplicatedVMDiskTypeRequiredSuccessfulReads is intentionally high. The Site | ||
| // Recovery service is eventually consistent and can briefly report the requested disk type | ||
| // before the value flips back while replication catches up, so we require this many | ||
| // consecutive matching reads before treating the update as complete. Testing showed that a | ||
| // lower threshold (e.g. 5) still produced false positives. | ||
| const siteRecoveryReplicatedVMDiskTypeRequiredSuccessfulReads = 10 | ||
|
|
||
| type SiteRecoveryReplicatedVMDiskTypeUpdate struct { | ||
| DiskId string | ||
| RecoveryTargetDiskType string | ||
| RecoveryReplicaDiskType string | ||
| } | ||
|
|
||
| type SiteRecoveryReplicatedVMDiskTypePoller struct { | ||
| client *replicationprotecteditems.ReplicationProtectedItemsClient | ||
| id replicationprotecteditems.ReplicationProtectedItemId | ||
| updates map[string]SiteRecoveryReplicatedVMDiskTypeUpdate | ||
| consecutiveSuccessfulReads int | ||
| } | ||
|
|
||
| func NewSiteRecoveryReplicatedVMDiskTypePoller(client *replicationprotecteditems.ReplicationProtectedItemsClient, id replicationprotecteditems.ReplicationProtectedItemId, updates []SiteRecoveryReplicatedVMDiskTypeUpdate) *SiteRecoveryReplicatedVMDiskTypePoller { | ||
| updatesByDiskId := make(map[string]SiteRecoveryReplicatedVMDiskTypeUpdate, len(updates)) | ||
| for _, update := range updates { | ||
| updatesByDiskId[normalizeSiteRecoveryReplicatedVMManagedDiskID(update.DiskId)] = update | ||
| } | ||
|
|
||
| return &SiteRecoveryReplicatedVMDiskTypePoller{ | ||
| client: client, | ||
| id: id, | ||
| updates: updatesByDiskId, | ||
| } | ||
| } | ||
|
|
||
| func (p *SiteRecoveryReplicatedVMDiskTypePoller) Poll(ctx context.Context) (*pollers.PollResult, error) { | ||
| resp, err := p.client.Get(ctx, p.id) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("retrieving %s: %+v", p.id, err) | ||
| } | ||
|
|
||
| if resp.Model == nil { | ||
| return nil, fmt.Errorf("retrieving %s: `model` was nil", p.id) | ||
| } | ||
|
|
||
| if resp.Model.Properties == nil { | ||
| return nil, fmt.Errorf("retrieving %s: `properties` was nil", p.id) | ||
| } | ||
|
|
||
| result := &pollers.PollResult{ | ||
| HttpResponse: &client.Response{ | ||
| OData: resp.OData, | ||
| Response: resp.HttpResponse, | ||
| }, | ||
| PollInterval: 30 * time.Second, | ||
| } | ||
|
|
||
| if siteRecoveryReplicatedVMStateIndicatesFailure(pointer.From(resp.Model.Properties.ProtectionState)) { | ||
| return nil, pollers.PollingFailedError{ | ||
| HttpResponse: result.HttpResponse, | ||
| Message: fmt.Sprintf("%s entered protection state %q", p.id, pointer.From(resp.Model.Properties.ProtectionState)), | ||
| } | ||
| } | ||
|
|
||
| a2aDetails, ok := resp.Model.Properties.ProviderSpecificDetails.(replicationprotecteditems.A2AReplicationDetails) | ||
| if !ok { | ||
| return nil, pollers.PollingFailedError{ | ||
| HttpResponse: result.HttpResponse, | ||
| Message: fmt.Sprintf("%s returned provider specific details that were not A2A", p.id), | ||
| } | ||
| } | ||
|
|
||
| if siteRecoveryReplicatedVMStateIndicatesFailure(pointer.From(a2aDetails.VMProtectionState)) { | ||
| return nil, pollers.PollingFailedError{ | ||
| HttpResponse: result.HttpResponse, | ||
| Message: fmt.Sprintf("%s entered VM protection state %q", p.id, pointer.From(a2aDetails.VMProtectionState)), | ||
| } | ||
| } | ||
|
|
||
| pending, err := p.pendingDiskTypeUpdates(a2aDetails, result.HttpResponse) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if len(pending) == 0 { | ||
| p.consecutiveSuccessfulReads++ | ||
| if p.consecutiveSuccessfulReads < siteRecoveryReplicatedVMDiskTypeRequiredSuccessfulReads { | ||
| log.Printf("[DEBUG] waiting for managed disk type updates for %s: matched %d/%d consecutive reads", p.id, p.consecutiveSuccessfulReads, siteRecoveryReplicatedVMDiskTypeRequiredSuccessfulReads) | ||
| result.Status = pollers.PollingStatusInProgress | ||
| return result, nil | ||
| } | ||
|
|
||
| result.Status = pollers.PollingStatusSucceeded | ||
| return result, nil | ||
| } | ||
|
|
||
| p.consecutiveSuccessfulReads = 0 | ||
| sort.Strings(pending) | ||
| log.Printf("[DEBUG] waiting for managed disk type updates for %s: %s", p.id, strings.Join(pending, "; ")) | ||
| result.Status = pollers.PollingStatusInProgress | ||
| return result, nil | ||
| } | ||
|
|
||
| func (p *SiteRecoveryReplicatedVMDiskTypePoller) pendingDiskTypeUpdates(details replicationprotecteditems.A2AReplicationDetails, resp *client.Response) ([]string, error) { | ||
| pending := make([]string, 0) | ||
|
|
||
| protectedDisks := make(map[string]replicationprotecteditems.A2AProtectedManagedDiskDetails) | ||
| if details.ProtectedManagedDisks != nil { | ||
| for _, disk := range *details.ProtectedManagedDisks { | ||
| diskId := normalizeSiteRecoveryReplicatedVMManagedDiskID(pointer.From(disk.DiskId)) | ||
| if _, ok := p.updates[diskId]; ok { | ||
| protectedDisks[diskId] = disk | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for diskId, update := range p.updates { | ||
| disk, ok := protectedDisks[diskId] | ||
| if !ok { | ||
| pending = append(pending, fmt.Sprintf("%s is not present in protected managed disks", update.DiskId)) | ||
| continue | ||
| } | ||
|
|
||
| if siteRecoveryReplicatedVMStateIndicatesFailure(pointer.From(disk.DiskState)) { | ||
| return nil, pollers.PollingFailedError{ | ||
| HttpResponse: resp, | ||
| Message: fmt.Sprintf("%s entered disk state %q", update.DiskId, pointer.From(disk.DiskState)), | ||
| } | ||
| } | ||
|
|
||
| actualTargetDiskType := pointer.From(disk.RecoveryTargetDiskAccountType) | ||
| actualTargetReplicaDiskType := pointer.From(disk.RecoveryReplicaDiskAccountType) | ||
|
|
||
| mismatches := make([]string, 0) | ||
| if actualTargetDiskType != update.RecoveryTargetDiskType { | ||
| mismatches = append(mismatches, fmt.Sprintf("target_disk_type is %q, expected %q", actualTargetDiskType, update.RecoveryTargetDiskType)) | ||
| } | ||
| if actualTargetReplicaDiskType != update.RecoveryReplicaDiskType { | ||
| mismatches = append(mismatches, fmt.Sprintf("target_replica_disk_type is %q, expected %q", actualTargetReplicaDiskType, update.RecoveryReplicaDiskType)) | ||
| } | ||
| if len(mismatches) > 0 { | ||
| pending = append(pending, fmt.Sprintf("%s: %s", update.DiskId, strings.Join(mismatches, ", "))) | ||
| } | ||
| } | ||
|
|
||
| return pending, nil | ||
| } | ||
|
|
||
| func normalizeSiteRecoveryReplicatedVMManagedDiskID(input string) string { | ||
| if parsed, err := commonids.ParseManagedDiskIDInsensitively(input); err == nil { | ||
| return strings.ToLower(parsed.ID()) | ||
| } | ||
|
|
||
| return strings.ToLower(input) | ||
| } | ||
|
|
||
| func siteRecoveryReplicatedVMStateIndicatesFailure(input string) bool { | ||
| input = strings.ToLower(input) | ||
| if input == "" || input == "noerror" { | ||
| return false | ||
| } | ||
|
|
||
| return strings.Contains(input, "failed") || strings.Contains(input, "error") | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 service has an eventual consistency issue and per testing, 5 is not enough so used 10.