From 2efc446c80ca83202ac8caf08475a6cfeb0b9a1a Mon Sep 17 00:00:00 2001 From: Omer Lachish Date: Wed, 16 Oct 2024 14:28:02 +0200 Subject: [PATCH] addressed some PR comments --- .../resources/sharing/resource_share.go | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/internal/providers/pluginfw/resources/sharing/resource_share.go b/internal/providers/pluginfw/resources/sharing/resource_share.go index 081d06993..e00404e4f 100644 --- a/internal/providers/pluginfw/resources/sharing/resource_share.go +++ b/internal/providers/pluginfw/resources/sharing/resource_share.go @@ -81,8 +81,7 @@ func diff(beforeSi sharing.ShareInfo, afterSi sharing.ShareInfo) []sharing.Share changes := []sharing.SharedDataObjectUpdate{} // not in after so remove for _, beforeSdo := range beforeSi.Objects { - _, exists := afterMap[beforeSdo.Name] - if exists { + if _, ok := afterMap[beforeSdo.Name]; ok { continue } changes = append(changes, sharing.SharedDataObjectUpdate{ @@ -94,8 +93,7 @@ func diff(beforeSi sharing.ShareInfo, afterSi sharing.ShareInfo) []sharing.Share // not in before so add // if in before but diff then update for _, afterSdo := range afterSi.Objects { - beforeSdo, exists := beforeMap[afterSdo.Name] - if exists { + if beforeSdo, ok := beforeMap[afterSdo.Name]; ok { if !equal(beforeSdo, afterSdo) { // do not send SharedAs afterSdo.SharedAs = "" @@ -344,26 +342,27 @@ func (r *ShareResource) Update(ctx context.Context, req resource.UpdateRequest, Name: plan.Name.ValueString(), Updates: changes, }) - if err == nil { - resp.Diagnostics.Append(converters.GoSdkToTfSdkStruct(ctx, updatedShareInfo, &state)...) - if resp.Diagnostics.HasError() { - return - } - } else { + + if err != nil { + resp.Diagnostics.AddError("failed to update share", err.Error()) + rollbackShareInfo, rollbackErr := client.Shares.Update(ctx, sharing.UpdateShare{ Name: currentShareInfo.Name, Owner: currentShareInfo.Owner, }) - if rollbackErr == nil { - resp.Diagnostics.Append(converters.GoSdkToTfSdkStruct(ctx, rollbackShareInfo, &state)...) - if resp.Diagnostics.HasError() { - return - } - } else { + if rollbackErr != nil { resp.Diagnostics.AddError("failed to roll back", common.OwnerRollbackError(err, rollbackErr, currentShareInfo.Owner, plan.Owner.ValueString()).Error()) + return } - resp.Diagnostics.AddError("failed to update share", err.Error()) + resp.Diagnostics.Append(converters.GoSdkToTfSdkStruct(ctx, rollbackShareInfo, &state)...) + if resp.Diagnostics.HasError() { + return + } + } + + resp.Diagnostics.Append(converters.GoSdkToTfSdkStruct(ctx, updatedShareInfo, &state)...) + if resp.Diagnostics.HasError() { return } }