Skip to content

Commit

Permalink
clean up resource_sahre
Browse files Browse the repository at this point in the history
  • Loading branch information
mgyucht committed Dec 5, 2024
1 parent 6a39e1b commit e4aff40
Showing 1 changed file with 58 additions and 32 deletions.
90 changes: 58 additions & 32 deletions internal/providers/pluginfw/products/sharing/resource_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,22 +227,13 @@ func (r *ShareResource) Create(ctx context.Context, req resource.CreateRequest,
return
}

newState.SyncEffectiveFieldsDuringCreateOrUpdate(plan.ShareInfo)
planObjects := []sharing_tf.SharedDataObject{}
resp.Diagnostics.Append(plan.Objects.ElementsAs(ctx, &planObjects, true)...)
newStateObjects := []sharing_tf.SharedDataObject{}
resp.Diagnostics.Append(newState.Objects.ElementsAs(ctx, &newStateObjects, true)...)
for i := range newStateObjects {
newStateObjects[i].SyncEffectiveFieldsDuringCreateOrUpdate(planObjects[i])
}
var d diag.Diagnostics
newState.Objects, d = basetypes.NewListValueFrom(ctx, newState.Objects.ElementType(ctx), newStateObjects)
newState, d := r.syncEffectiveFields(ctx, plan, newState, effectiveFieldsActionCreateOrUpdate{})
resp.Diagnostics.Append(d...)

resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
}

func (r *ShareResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
Expand Down Expand Up @@ -292,17 +283,11 @@ func (r *ShareResource) Read(ctx context.Context, req resource.ReadRequest, resp
return
}

newState.SyncEffectiveFieldsDuringRead(existingState.ShareInfo)
var existingObjects []sharing_tf.SharedDataObject
resp.Diagnostics.Append(existingState.Objects.ElementsAs(ctx, &existingObjects, true)...)
var newStateObjects []sharing_tf.SharedDataObject
resp.Diagnostics.Append(newState.Objects.ElementsAs(ctx, &newStateObjects, true)...)
for i := range newStateObjects {
newStateObjects[i].SyncEffectiveFieldsDuringRead(existingObjects[i])
}
var d diag.Diagnostics
newState.Objects, d = basetypes.NewListValueFrom(ctx, newState.Objects.ElementType(ctx), newStateObjects)
newState, d := r.syncEffectiveFields(ctx, existingState, newState, effectiveFieldsActionRead{})
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
}
Expand Down Expand Up @@ -398,17 +383,11 @@ func (r *ShareResource) Update(ctx context.Context, req resource.UpdateRequest,
}
}

state.SyncEffectiveFieldsDuringCreateOrUpdate(plan.ShareInfo)
planObjects := []sharing_tf.SharedDataObject{}
resp.Diagnostics.Append(plan.Objects.ElementsAs(ctx, &planObjects, true)...)
stateObjects := []sharing_tf.SharedDataObject{}
resp.Diagnostics.Append(state.Objects.ElementsAs(ctx, &stateObjects, true)...)
for i := range stateObjects {
stateObjects[i].SyncEffectiveFieldsDuringCreateOrUpdate(planObjects[i])
}
var d diag.Diagnostics
state.Objects, d = basetypes.NewListValueFrom(ctx, state.Objects.ElementType(ctx), stateObjects)
state, d := r.syncEffectiveFields(ctx, plan, state, effectiveFieldsActionCreateOrUpdate{})
resp.Diagnostics.Append(d...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
Expand All @@ -433,3 +412,50 @@ func (r *ShareResource) Delete(ctx context.Context, req resource.DeleteRequest,
return
}
}

type effectiveFieldsAction interface {
resourceLevel(*ShareInfoExtended, sharing_tf.ShareInfo)
objectLevel(*sharing_tf.SharedDataObject, sharing_tf.SharedDataObject)
}

type effectiveFieldsActionCreateOrUpdate struct{}

func (effectiveFieldsActionCreateOrUpdate) resourceLevel(state *ShareInfoExtended, plan sharing_tf.ShareInfo) {
state.SyncEffectiveFieldsDuringCreateOrUpdate(plan)
}

func (effectiveFieldsActionCreateOrUpdate) objectLevel(state *sharing_tf.SharedDataObject, plan sharing_tf.SharedDataObject) {
state.SyncEffectiveFieldsDuringCreateOrUpdate(plan)
}

type effectiveFieldsActionRead struct{}

func (effectiveFieldsActionRead) resourceLevel(state *ShareInfoExtended, plan sharing_tf.ShareInfo) {
state.SyncEffectiveFieldsDuringRead(plan)
}

func (effectiveFieldsActionRead) objectLevel(state *sharing_tf.SharedDataObject, plan sharing_tf.SharedDataObject) {
state.SyncEffectiveFieldsDuringRead(plan)
}

func (r *ShareResource) syncEffectiveFields(ctx context.Context, plan, state ShareInfoExtended, mode effectiveFieldsAction) (ShareInfoExtended, diag.Diagnostics) {
var d diag.Diagnostics
mode.resourceLevel(&state, plan.ShareInfo)
planObjects := []sharing_tf.SharedDataObject{}
d.Append(plan.Objects.ElementsAs(ctx, &planObjects, true)...)
if d.HasError() {
return state, d
}
stateObjects := []sharing_tf.SharedDataObject{}
d.Append(state.Objects.ElementsAs(ctx, &stateObjects, true)...)
if d.HasError() {
return state, d
}
for i := range stateObjects {
mode.objectLevel(&stateObjects[i], planObjects[i])
}
var dd diag.Diagnostics
state.Objects, dd = basetypes.NewListValueFrom(ctx, state.Objects.ElementType(ctx), stateObjects)
d.Append(dd...)
return state, d
}

0 comments on commit e4aff40

Please sign in to comment.