Skip to content

Commit

Permalink
Removing comments and extra logs
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomarquesc committed Dec 27, 2024
1 parent 00e0e3b commit b782821
Showing 1 changed file with 3 additions and 118 deletions.
121 changes: 3 additions & 118 deletions internal/services/netapp/netapp_snapshot_policy_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,97 +342,6 @@ func resourceNetAppSnapshotPolicyRead(d *pluginsdk.ResourceData, meta interface{
return nil
}

// func resourceNetAppSnapshotPolicyDelete(d *pluginsdk.ResourceData, meta interface{}) error {
// client := meta.(*clients.Client).NetApp.SnapshotPoliciesClient
// ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
// defer cancel()

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

// // Deleting snapshot policy and waiting for it fo fully complete the operation
// if err = client.SnapshotPoliciesDeleteThenPoll(ctx, *id); err != nil {
// return fmt.Errorf("deleting %s: %+v", id, err)
// }

// log.Printf("[DEBUG] Waiting for %s to be deleted", id)
// if err := waitForSnapshotPolicyDeletion(ctx, client, *id, d.Timeout(pluginsdk.TimeoutDelete)); err != nil {
// return err
// }

// return nil
// }

// func resourceNetAppSnapshotPolicyDelete(d *pluginsdk.ResourceData, meta interface{}) error {
// client := meta.(*clients.Client).NetApp.SnapshotPoliciesClient
// volumeClient := meta.(*clients.Client).NetApp.VolumeClient
// ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
// defer cancel()

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

// // Try to delete the snapshot policy
// result, err := client.SnapshotPoliciesDelete(ctx, *id)

// if err != nil {
// // Check if error is 409 with message about being used by volumes
// if result.HttpResponse != nil && result.HttpResponse.StatusCode == 409 && strings.Contains(err.Error(), "SnapshotPolicy is used") {
// // Get all volumes in the account that might be using this snapshot policy
// volumeIds, err := findVolumesUsingSnapshotPolicy(ctx, meta.(*clients.Client), *id)
// if err != nil {
// return fmt.Errorf("finding volumes using snapshot policy %s: %+v", *id, err)
// }

// // Disassociate snapshot policy from each volume
// for _, volumeId := range volumeIds {
// volId, err := volumes.ParseVolumeID(volumeId)
// if err != nil {
// return fmt.Errorf("parsing volume ID %q: %+v", volumeId, err)
// }

// // Update volume to remove snapshot policy
// update := volumes.VolumePatch{
// Properties: &volumes.VolumePatchProperties{
// DataProtection: &volumes.VolumePatchPropertiesDataProtection{
// Snapshot: &volumes.VolumeSnapshotProperties{
// SnapshotPolicyId: nil,
// },
// },
// },
// }

// if err = volumeClient.UpdateThenPoll(ctx, *volId, update); err != nil {
// return fmt.Errorf("removing snapshot policy from volume %s: %+v", *volId, err)
// }

// // Wait for the update to complete
// if err := waitForVolumeCreateOrUpdate(ctx, volumeClient, *volId); err != nil {
// return fmt.Errorf("waiting for snapshot policy removal from volume %s: %+v", *volId, err)
// }
// }

// // Try deleting the snapshot policy again using DeleteThenPoll
// if err = client.SnapshotPoliciesDeleteThenPoll(ctx, *id); err != nil {
// return fmt.Errorf("deleting %s after volume disassociation: %+v", *id, err)
// }
// } else {
// return fmt.Errorf("deleting %s: %+v", *id, err)
// }
// }

// log.Printf("[DEBUG] Waiting for %s to be deleted", *id)
// if err := waitForSnapshotPolicyDeletion(ctx, client, *id, d.Timeout(pluginsdk.TimeoutDelete)); err != nil {
// return err
// }

// return nil
// }

func resourceNetAppSnapshotPolicyDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).NetApp.SnapshotPoliciesClient
volumeClient := meta.(*clients.Client).NetApp.VolumeClient
Expand All @@ -444,32 +353,24 @@ func resourceNetAppSnapshotPolicyDelete(d *pluginsdk.ResourceData, meta interfac
return err
}

log.Printf("[INFO] Attempting to delete Snapshot Policy %s", *id)
// Try to delete the snapshot policy using DeleteThenPoll
err = client.SnapshotPoliciesDeleteThenPoll(ctx, *id)
if err != nil {
// Check if error is about snapshot policy being in use
if strings.Contains(err.Error(), "SnapshotPolicy is used") {
log.Printf("[INFO] Snapshot Policy %s is in use, finding volumes using this policy...", *id)

// Get all volumes in the account that might be using this snapshot policy
volumeIds, err := findVolumesUsingSnapshotPolicy(ctx, meta.(*clients.Client), *id)
if err != nil {
return fmt.Errorf("finding volumes using snapshot policy %s: %+v", *id, err)
}

log.Printf("[INFO] Found %d volumes using Snapshot Policy %s", len(volumeIds), *id)

// Disassociate snapshot policy from each volume
for i, volumeId := range volumeIds {
log.Printf("[INFO] Processing volume %d of %d: %s", i+1, len(volumeIds), volumeId)

for _, volumeId := range volumeIds {
volId, err := volumes.ParseVolumeID(volumeId)
if err != nil {
return fmt.Errorf("parsing volume ID %q: %+v", volumeId, err)
}

log.Printf("[INFO] Removing snapshot policy from volume %s", *volId)
// Update volume to remove snapshot policy
update := volumes.VolumePatch{
Properties: &volumes.VolumePatchProperties{
Expand All @@ -488,7 +389,6 @@ func resourceNetAppSnapshotPolicyDelete(d *pluginsdk.ResourceData, meta interfac
return fmt.Errorf("removing snapshot policy from volume %s: %+v", *volId, err)
}

log.Printf("[INFO] Waiting for snapshot policy removal to complete for volume %s", *volId)
// Wait for the update to complete
if err := waitForVolumeCreateOrUpdate(ctx, volumeClient, *volId); err != nil {
locks.UnlockByID(volumeId)
Expand All @@ -498,7 +398,6 @@ func resourceNetAppSnapshotPolicyDelete(d *pluginsdk.ResourceData, meta interfac
locks.UnlockByID(volumeId)
}

log.Printf("[INFO] All volumes processed, attempting to delete Snapshot Policy %s again", *id)
// Try deleting the snapshot policy again
if err = client.SnapshotPoliciesDeleteThenPoll(ctx, *id); err != nil {
return fmt.Errorf("deleting %s after volume disassociation: %+v", *id, err)
Expand All @@ -508,69 +407,55 @@ func resourceNetAppSnapshotPolicyDelete(d *pluginsdk.ResourceData, meta interfac
}
}

log.Printf("[INFO] Waiting for final confirmation of deletion for Snapshot Policy %s", *id)
if err := waitForSnapshotPolicyDeletion(ctx, client, *id, d.Timeout(pluginsdk.TimeoutDelete)); err != nil {
return err
}

log.Printf("[INFO] Successfully deleted Snapshot Policy %s", *id)
return nil
}

func findVolumesUsingSnapshotPolicy(ctx context.Context, client *clients.Client, snapshotPolicyId snapshotpolicy.SnapshotPolicyId) ([]string, error) {
const logPrefix = "[findVolumesUsingSnapshotPolicy]"
log.Printf("[INFO] %s Starting search for volumes using snapshot policy %s", logPrefix, snapshotPolicyId.ID())

volumeIds := make([]string, 0)

poolClient := client.NetApp.PoolClient
accountId := capacitypools.NewNetAppAccountID(snapshotPolicyId.SubscriptionId, snapshotPolicyId.ResourceGroupName, snapshotPolicyId.NetAppAccountName)

log.Printf("[INFO] %s Listing capacity pools in account %s", logPrefix, snapshotPolicyId.NetAppAccountName)
poolsResult, err := poolClient.PoolsList(ctx, accountId)
if err != nil {
return nil, fmt.Errorf("listing capacity pools in account %s: %+v", snapshotPolicyId.NetAppAccountName, err)
}

if model := poolsResult.Model; model != nil {
volumeClient := client.NetApp.VolumeClient
log.Printf("[INFO] %s Found %d pools to check", logPrefix, len(*model))

for i, pool := range *model {
for _, pool := range *model {
if pool.Name == nil {
continue
}

poolNameParts := strings.Split(pointer.From(pool.Name), "/")
poolName := poolNameParts[len(poolNameParts)-1]

log.Printf("[INFO] %s Processing pool %d of %d: %s", logPrefix, i+1, len(*model), poolName)

volumeId := volumes.NewCapacityPoolID(snapshotPolicyId.SubscriptionId, snapshotPolicyId.ResourceGroupName, snapshotPolicyId.NetAppAccountName, poolName)

volumesResult, err := volumeClient.List(ctx, volumeId)
if err != nil {
return nil, fmt.Errorf("listing volumes in pool %s: %+v", poolName, err)
}

if volumesModel := volumesResult.Model; volumesModel != nil {
log.Printf("[INFO] %s Found %d volumes in pool %s", logPrefix, len(*volumesModel), poolName)

for _, volume := range *volumesModel {
if volume.Id == nil || volume.Properties.DataProtection == nil ||
volume.Properties.DataProtection.Snapshot == nil || volume.Properties.DataProtection.Snapshot.SnapshotPolicyId == nil {
continue
}

if strings.EqualFold(*volume.Properties.DataProtection.Snapshot.SnapshotPolicyId, snapshotPolicyId.ID()) {
log.Printf("[INFO] %s Found volume using the snapshot policy: %s", logPrefix, *volume.Id)
volumeIds = append(volumeIds, *volume.Id)
}
}
}
}
}

log.Printf("[INFO] %s Completed search, found %d volumes using the snapshot policy", logPrefix, len(volumeIds))
return volumeIds, nil
}

Expand Down

0 comments on commit b782821

Please sign in to comment.