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_resource_policy_remediation - Fix deletion failure due to canceling a completed remediation task #16478

Merged
merged 2 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 19 additions & 16 deletions internal/services/policy/remediation_management_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,24 +210,27 @@ func resourceArmManagementGroupPolicyRemediationDelete(d *pluginsdk.ResourceData
}

if existing.RemediationProperties != nil && existing.RemediationProperties.ResourceDiscoveryMode == policyinsights.ReEvaluateCompliance {
log.Printf("[DEBUG] cancelling the remediation first before deleting it when `resource_discovery_mode` is set to `ReEvaluateCompliance`")
if _, err := client.CancelAtManagementGroup(ctx, managementGroupId.Name, id.Name); err != nil {
return fmt.Errorf("cancelling %s: %+v", id.ID(), err)
}
// Remediation can only be canceld when it is in "Evaluating" status, otherwise, API might raise error (e.g. canceling a "Completed" remediation returns 400).
if existing.RemediationProperties.ProvisioningState != nil && *existing.RemediationProperties.ProvisioningState == "Evaluating" {
log.Printf("[DEBUG] cancelling the remediation first before deleting it when `resource_discovery_mode` is set to `ReEvaluateCompliance`")
if _, err := client.CancelAtManagementGroup(ctx, managementGroupId.Name, id.Name); err != nil {
return fmt.Errorf("cancelling %s: %+v", id.ID(), err)
}

log.Printf("[DEBUG] waiting for the %s to be canceled", id.ID())
stateConf := &pluginsdk.StateChangeConf{
Pending: []string{"Cancelling"},
Target: []string{
"Succeeded", "Canceled", "Failed",
},
Refresh: managementGroupPolicyRemediationCancellationRefreshFunc(ctx, client, *id, *managementGroupId),
MinTimeout: 10 * time.Second,
Timeout: d.Timeout(pluginsdk.TimeoutDelete),
}
log.Printf("[DEBUG] waiting for the %s to be canceled", id.ID())
stateConf := &pluginsdk.StateChangeConf{
Pending: []string{"Cancelling"},
Target: []string{
"Succeeded", "Canceled", "Failed",
},
Refresh: managementGroupPolicyRemediationCancellationRefreshFunc(ctx, client, *id, *managementGroupId),
MinTimeout: 10 * time.Second,
Timeout: d.Timeout(pluginsdk.TimeoutDelete),
}

if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for %s to be canceled: %+v", id.ID(), err)
if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for %s to be canceled: %+v", id.ID(), err)
}
}
}

Expand Down
35 changes: 19 additions & 16 deletions internal/services/policy/remediation_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,24 +196,27 @@ func resourceArmResourcePolicyRemediationDelete(d *pluginsdk.ResourceData, meta
}

if existing.RemediationProperties != nil && existing.RemediationProperties.ResourceDiscoveryMode == policyinsights.ReEvaluateCompliance {
log.Printf("[DEBUG] cancelling the remediation first before deleting it when `resource_discovery_mode` is set to `ReEvaluateCompliance`")
if _, err := client.CancelAtResource(ctx, id.ResourceId, id.Name); err != nil {
return fmt.Errorf("cancelling %s: %+v", id.ID(), err)
}
// Remediation can only be canceld when it is in "Evaluating" status, otherwise, API might raise error (e.g. canceling a "Completed" remediation returns 400).
if existing.RemediationProperties.ProvisioningState != nil && *existing.RemediationProperties.ProvisioningState == "Evaluating" {
log.Printf("[DEBUG] cancelling the remediation first before deleting it when `resource_discovery_mode` is set to `ReEvaluateCompliance`")
if _, err := client.CancelAtResource(ctx, id.ResourceId, id.Name); err != nil {
return fmt.Errorf("cancelling %s: %+v", id.ID(), err)
}

log.Printf("[DEBUG] waiting for the %s to be canceled", id.ID())
stateConf := &pluginsdk.StateChangeConf{
Pending: []string{"Cancelling"},
Target: []string{
"Succeeded", "Canceled", "Failed",
},
Refresh: resourcePolicyRemediationCancellationRefreshFunc(ctx, client, *id),
MinTimeout: 10 * time.Second,
Timeout: d.Timeout(pluginsdk.TimeoutDelete),
}
log.Printf("[DEBUG] waiting for the %s to be canceled", id.ID())
stateConf := &pluginsdk.StateChangeConf{
Pending: []string{"Cancelling"},
Target: []string{
"Succeeded", "Canceled", "Failed",
},
Refresh: resourcePolicyRemediationCancellationRefreshFunc(ctx, client, *id),
MinTimeout: 10 * time.Second,
Timeout: d.Timeout(pluginsdk.TimeoutDelete),
}

if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for %s to be canceled: %+v", id.ID(), err)
if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for %s to be canceled: %+v", id.ID(), err)
}
}
}

Expand Down
35 changes: 19 additions & 16 deletions internal/services/policy/remediation_resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,27 @@ func resourceArmResourceGroupPolicyRemediationDelete(d *pluginsdk.ResourceData,
}

if existing.RemediationProperties != nil && existing.RemediationProperties.ResourceDiscoveryMode == policyinsights.ReEvaluateCompliance {
log.Printf("[DEBUG] cancelling the remediation first before deleting it when `resource_discovery_mode` is set to `ReEvaluateCompliance`")
if _, err := client.CancelAtResourceGroup(ctx, id.SubscriptionId, id.ResourceGroup, id.RemediationName); err != nil {
return fmt.Errorf("cancelling %s: %+v", id.ID(), err)
}
// Remediation can only be canceld when it is in "Evaluating" status, otherwise, API might raise error (e.g. canceling a "Completed" remediation returns 400).
if existing.RemediationProperties.ProvisioningState != nil && *existing.RemediationProperties.ProvisioningState == "Evaluating" {
log.Printf("[DEBUG] cancelling the remediation first before deleting it when `resource_discovery_mode` is set to `ReEvaluateCompliance`")
if _, err := client.CancelAtResourceGroup(ctx, id.SubscriptionId, id.ResourceGroup, id.RemediationName); err != nil {
return fmt.Errorf("cancelling %s: %+v", id.ID(), err)
}

log.Printf("[DEBUG] waiting for the %s to be canceled", id.ID())
stateConf := &pluginsdk.StateChangeConf{
Pending: []string{"Cancelling"},
Target: []string{
"Succeeded", "Canceled", "Failed",
},
Refresh: resourceGroupPolicyRemediationCancellationRefreshFunc(ctx, client, *id),
MinTimeout: 10 * time.Second,
Timeout: d.Timeout(pluginsdk.TimeoutDelete),
}
log.Printf("[DEBUG] waiting for the %s to be canceled", id.ID())
stateConf := &pluginsdk.StateChangeConf{
Pending: []string{"Cancelling"},
Target: []string{
"Succeeded", "Canceled", "Failed",
},
Refresh: resourceGroupPolicyRemediationCancellationRefreshFunc(ctx, client, *id),
MinTimeout: 10 * time.Second,
Timeout: d.Timeout(pluginsdk.TimeoutDelete),
}

if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for %s to be canceled: %+v", id.ID(), err)
if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for %s to be canceled: %+v", id.ID(), err)
}
}
}

Expand Down
35 changes: 19 additions & 16 deletions internal/services/policy/remediation_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,27 @@ func resourceArmSubscriptionPolicyRemediationDelete(d *pluginsdk.ResourceData, m
}

if existing.RemediationProperties != nil && existing.RemediationProperties.ResourceDiscoveryMode == policyinsights.ReEvaluateCompliance {
log.Printf("[DEBUG] cancelling the remediation first before deleting it when `resource_discovery_mode` is set to `ReEvaluateCompliance`")
if _, err := client.CancelAtSubscription(ctx, id.SubscriptionId, id.RemediationName); err != nil {
return fmt.Errorf("cancelling %s: %+v", id.ID(), err)
}
// Remediation can only be canceld when it is in "Evaluating" status, otherwise, API might raise error (e.g. canceling a "Completed" remediation returns 400).
if existing.RemediationProperties.ProvisioningState != nil && *existing.RemediationProperties.ProvisioningState == "Evaluating" {
log.Printf("[DEBUG] cancelling the remediation first before deleting it when `resource_discovery_mode` is set to `ReEvaluateCompliance`")
if _, err := client.CancelAtSubscription(ctx, id.SubscriptionId, id.RemediationName); err != nil {
return fmt.Errorf("cancelling %s: %+v", id.ID(), err)
}

log.Printf("[DEBUG] waiting for the %s to be canceled", id.ID())
stateConf := &pluginsdk.StateChangeConf{
Pending: []string{"Cancelling"},
Target: []string{
"Succeeded", "Canceled", "Failed",
},
Refresh: subscriptionPolicyRemediationCancellationRefreshFunc(ctx, client, *id),
MinTimeout: 10 * time.Second,
Timeout: d.Timeout(pluginsdk.TimeoutDelete),
}
log.Printf("[DEBUG] waiting for the %s to be canceled", id.ID())
stateConf := &pluginsdk.StateChangeConf{
Pending: []string{"Cancelling"},
Target: []string{
"Succeeded", "Canceled", "Failed",
},
Refresh: subscriptionPolicyRemediationCancellationRefreshFunc(ctx, client, *id),
MinTimeout: 10 * time.Second,
Timeout: d.Timeout(pluginsdk.TimeoutDelete),
}

if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for %s to be canceled: %+v", id.ID(), err)
if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for %s to be canceled: %+v", id.ID(), err)
}
}
}

Expand Down