Skip to content

Commit

Permalink
Refresh auth context while polling for data protection resources
Browse files Browse the repository at this point in the history
Signed-off-by: Rahul Naren Pujari <rnarenpujari@gmail.com>
  • Loading branch information
rnarenpujari committed Mar 22, 2024
1 parent 3681696 commit db5c3cd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
5 changes: 5 additions & 0 deletions internal/resources/backupschedule/resource_backup_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,11 @@ func readResourceWait(ctx context.Context, config *authctx.TanzuContext, resourc
resp, err = config.TMCConnection.BackupScheduleService.BackupScheduleResourceServiceGet(resourceFullName)

if err != nil || resp == nil || resp.Schedule == nil {
if clienterrors.IsUnauthorizedError(err) {
authctx.RefreshUserAuthContext(config, clienterrors.IsUnauthorizedError, err)
continue
}

return nil, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ func resourceEnableDataProtectionRead(ctx context.Context, data *schema.Resource
config := m.(authctx.TanzuContext)

err := populateDataFromServer(ctx, config, scopedFullnameData, data)
if err != nil {
if clienterrors.IsNotFoundError(err) {
if !helper.IsContextCallerSet(ctx) {
*data = schema.ResourceData{}

return diags
} else if helper.IsDeleteState(ctx) {
// d.SetId("") is automatically called assuming delete returns no errors, but
// it is added here for explicitness.
_ = schema.RemoveFromState(data, m)

return diags
}
}

return diag.FromErr(err)
}

// remove the existing cluster level resource from state if it is now
// managed at the cluster group level.
Expand All @@ -83,22 +100,6 @@ func resourceEnableDataProtectionRead(ctx context.Context, data *schema.Resource
}
}

if err != nil {
if clienterrors.IsNotFoundError(err) {
if !helper.IsContextCallerSet(ctx) {
*data = schema.ResourceData{}

return diags
} else if helper.IsDeleteState(ctx) {
// d.SetId("") is automatically called assuming delete returns no errors, but
// it is added here for explicitness.
_ = schema.RemoveFromState(data, m)

return diags
}
}
}

return diags
}

Expand Down Expand Up @@ -228,6 +229,11 @@ func populateDataFromServer(ctx context.Context, config authctx.TanzuContext, sc

resp, err := config.TMCConnection.DataProtectionService.DataProtectionResourceServiceList(scopedFullnameData.FullnameCluster)
if err != nil || resp == nil {
if clienterrors.IsUnauthorizedError(err) {
authctx.RefreshUserAuthContext(&config, clienterrors.IsUnauthorizedError, err)
continue
}

return errors.Wrap(err, "list data protections")
}

Expand Down
5 changes: 5 additions & 0 deletions internal/resources/targetlocation/resource_target_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ func readResourceWait(ctx context.Context, config *authctx.TanzuContext, resourc
resp, err = config.TMCConnection.TargetLocationService.TargetLocationResourceServiceGet(resourceFullName)

if err != nil || resp == nil || resp.BackupLocation == nil {
if clienterrors.IsUnauthorizedError(err) {
authctx.RefreshUserAuthContext(config, clienterrors.IsUnauthorizedError, err)
continue
}

return nil, err
}

Expand Down

0 comments on commit db5c3cd

Please sign in to comment.