Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenyGri committed Oct 12, 2023
1 parent 0f1dbaf commit 8f7944b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 36 deletions.
36 changes: 18 additions & 18 deletions cmd/sgroups-tf-v2/internal/provider/collection-resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
)

type (
SingleResource interface {
SingleResource[T any] interface {
ResourceAttributes() map[string]schema.Attribute
Changed(oldState SingleResource) bool
IsDiffer(T) bool
}

subjectOfSync interface {
Expand All @@ -30,13 +30,12 @@ type (
ItemsDescription string
}

CollectionResource[T SingleResource, S subjectOfSync] struct {
suffix string
description Description
client *sgAPI.Client
toProto func(context.Context, map[string]T) (*S, diag.Diagnostics)
read func(context.Context, CollectionResourceModel[T, S], *sgAPI.Client) (CollectionResourceModel[T, S], diag.Diagnostics)
sr SingleResource
CollectionResource[T SingleResource[T], S subjectOfSync] struct {
suffix string
description Description
client *sgAPI.Client
toSubjOfSync func(context.Context, map[string]T) (*S, diag.Diagnostics)
read func(context.Context, CollectionResourceModel[T, S], *sgAPI.Client) (CollectionResourceModel[T, S], diag.Diagnostics)
}
)

Expand All @@ -45,14 +44,15 @@ func (c *CollectionResource[T, S]) Metadata(_ context.Context, req resource.Meta
}

func (c *CollectionResource[T, S]) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
var sr T
resp.Schema = schema.Schema{
Description: c.description.ResourceDescription,
Attributes: map[string]schema.Attribute{
"items": schema.MapNestedAttribute{
Description: c.description.ItemsDescription,
Required: true,
NestedObject: schema.NestedAttributeObject{
Attributes: c.sr.ResourceAttributes(),
Attributes: sr.ResourceAttributes(),
},
},
},
Expand All @@ -69,7 +69,7 @@ func (c *CollectionResource[T, S]) Create(ctx context.Context, req resource.Crea
}

// Convert from Terraform data model into GRPC data model
syncReq, diags := plan.toSyncReq(ctx, protos.SyncReq_Upsert, c.toProto)
syncReq, diags := plan.toSyncReq(ctx, protos.SyncReq_Upsert, c.toSubjOfSync)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand All @@ -78,8 +78,8 @@ func (c *CollectionResource[T, S]) Create(ctx context.Context, req resource.Crea
// Send GRPC request
if _, err := c.client.Sync(ctx, syncReq); err != nil {
resp.Diagnostics.AddError(
"Error creating resource",
"Could not create resource: "+err.Error())
"create "+c.description.ItemsDescription,
err.Error())
return
}

Expand All @@ -101,7 +101,7 @@ func (c *CollectionResource[T, S]) Read(ctx context.Context, req resource.ReadRe
if state, diags = c.read(ctx, state, c.client); diags.HasError() {
for _, diagError := range diags.Errors() {
resp.Diagnostics.AddError(
"Error reading resource state",
"read "+c.description.ItemsDescription,
diagError.Summary()+":"+diagError.Detail())
}
}
Expand Down Expand Up @@ -131,7 +131,7 @@ func (c *CollectionResource[T, S]) Update(ctx context.Context, req resource.Upda
tempModel := CollectionResourceModel[T, S]{
Items: itemsToDelete,
}
delReq, diags := tempModel.toSyncReq(ctx, protos.SyncReq_Delete, c.toProto)
delReq, diags := tempModel.toSyncReq(ctx, protos.SyncReq_Delete, c.toSubjOfSync)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand All @@ -149,7 +149,7 @@ func (c *CollectionResource[T, S]) Update(ctx context.Context, req resource.Upda
for name, itemFeatures := range plan.Items {
// in plan state can have unchanged items which should be ignored
// missing items before and changed items should be updated
if oldItemFeatures, ok := state.Items[name]; !ok || itemFeatures.Changed(oldItemFeatures) {
if oldItemFeatures, ok := state.Items[name]; !ok || itemFeatures.IsDiffer(oldItemFeatures) {
itemsToUpdate[name] = itemFeatures
}
}
Expand All @@ -158,7 +158,7 @@ func (c *CollectionResource[T, S]) Update(ctx context.Context, req resource.Upda
tempModel := CollectionResourceModel[T, S]{
Items: itemsToUpdate,
}
updateReq, diags := tempModel.toSyncReq(ctx, protos.SyncReq_Upsert, c.toProto)
updateReq, diags := tempModel.toSyncReq(ctx, protos.SyncReq_Upsert, c.toSubjOfSync)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand All @@ -182,7 +182,7 @@ func (c *CollectionResource[T, S]) Delete(ctx context.Context, req resource.Dele
return
}

delReq, diags := state.toSyncReq(ctx, protos.SyncReq_Delete, c.toProto)
delReq, diags := state.toSyncReq(ctx, protos.SyncReq_Delete, c.toSubjOfSync)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand Down
17 changes: 6 additions & 11 deletions cmd/sgroups-tf-v2/internal/provider/resource-networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ func NewNetworksResource() resource.Resource {
ItemsDescription: "Networks",
}
return &networksResource{
suffix: "_networks",
description: d,
toProto: networksToProto,
read: listNetworks,
sr: networkItem{},
suffix: "_networks",
description: d,
toSubjOfSync: networksToProto,
read: listNetworks,
}
}

Expand All @@ -49,12 +48,8 @@ func (item networkItem) ResourceAttributes() map[string]schema.Attribute {
}
}

func (item networkItem) Changed(oldState SingleResource) bool {
oldNetwork, ok := oldState.(networkItem)
if !ok {
panic("networkItem type expected")
}
return !item.Cidr.Equal(oldNetwork.Cidr)
func (item networkItem) IsDiffer(oldState networkItem) bool {
return !item.Cidr.Equal(oldState.Cidr)
}

func networksToProto(_ context.Context, items map[string]networkItem) (*protos.SyncNetworks, diag.Diagnostics) {
Expand Down
12 changes: 5 additions & 7 deletions cmd/sgroups-tf-v2/internal/provider/resource-sgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ func NewSgsResource() resource.Resource {
ItemsDescription: "Security Groups",
}
return &sgsResource{
suffix: "_groups",
description: d,
toProto: sgsToProto,
read: listSgs,
sr: sgItem{},
suffix: "_groups",
description: d,
toSubjOfSync: sgsToProto,
read: listSgs,
}
}

Expand Down Expand Up @@ -77,8 +76,7 @@ func (item sgItem) ResourceAttributes() map[string]schema.Attribute {
}
}

func (item sgItem) Changed(oldState SingleResource) bool {
oldSg := oldState.(sgItem)
func (item sgItem) IsDiffer(oldSg sgItem) bool {
return !(item.Logs.Equal(oldSg.Logs) &&
item.Trace.Equal(oldSg.Trace) &&
item.DefaultAction.Equal(oldSg.DefaultAction) &&
Expand Down

0 comments on commit 8f7944b

Please sign in to comment.