Skip to content

Commit

Permalink
refactor: use option pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Ishan Arya committed Oct 15, 2024
1 parent 1438cf0 commit a093dfd
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 66 deletions.
28 changes: 23 additions & 5 deletions core/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ import (
"github.com/goto/entropy/pkg/errors"
)

func (svc *Service) CreateResource(ctx context.Context, res resource.Resource, dryRun bool) (*resource.Resource, error) {
type Options struct {
DryRun bool
}

func WithDryRun(dryRun bool) Options {
return Options{DryRun: dryRun}
}

func (svc *Service) CreateResource(ctx context.Context, res resource.Resource, resourceOpts ...Options) (*resource.Resource, error) {
if err := res.Validate(true); err != nil {
return nil, err
}
Expand All @@ -22,10 +30,15 @@ func (svc *Service) CreateResource(ctx context.Context, res resource.Resource, d
}
res.Spec.Configs = nil

dryRun := false
for _, opt := range resourceOpts {
dryRun = opt.DryRun
}

return svc.execAction(ctx, res, act, dryRun)
}

func (svc *Service) UpdateResource(ctx context.Context, urn string, req resource.UpdateRequest, dryRun bool) (*resource.Resource, error) {
func (svc *Service) UpdateResource(ctx context.Context, urn string, req resource.UpdateRequest, resourceOpts ...Options) (*resource.Resource, error) {
if len(req.Spec.Dependencies) != 0 {
return nil, errors.ErrUnsupported.WithMsgf("updating dependencies is not supported")
} else if len(req.Spec.Configs) == 0 {
Expand All @@ -37,17 +50,17 @@ func (svc *Service) UpdateResource(ctx context.Context, urn string, req resource
Params: req.Spec.Configs,
Labels: req.Labels,
UserID: req.UserID,
}, dryRun)
}, resourceOpts...)
}

func (svc *Service) DeleteResource(ctx context.Context, urn string) error {
_, actionErr := svc.ApplyAction(ctx, urn, module.ActionRequest{
Name: module.DeleteAction,
}, false)
}, WithDryRun(false))
return actionErr
}

func (svc *Service) ApplyAction(ctx context.Context, urn string, act module.ActionRequest, dryRun bool) (*resource.Resource, error) {
func (svc *Service) ApplyAction(ctx context.Context, urn string, act module.ActionRequest, resourceOpts ...Options) (*resource.Resource, error) {
res, err := svc.GetResource(ctx, urn)
if err != nil {
return nil, err
Expand All @@ -56,6 +69,11 @@ func (svc *Service) ApplyAction(ctx context.Context, urn string, act module.Acti
WithMsgf("cannot perform '%s' on resource in '%s'", act.Name, res.State.Status)
}

dryRun := false
for _, opt := range resourceOpts {
dryRun = opt.DryRun
}

return svc.execAction(ctx, *res, act, dryRun)
}

Expand Down
18 changes: 9 additions & 9 deletions core/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestService_CreateResource(t *testing.T) {
setup func(t *testing.T) *core.Service
res resource.Resource
want *resource.Resource
dryrun bool
options []core.Options
wantErr error
}{
{
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestService_CreateResource(t *testing.T) {
CreatedAt: frozenTime,
UpdatedAt: frozenTime,
},
dryrun: true,
options: []core.Options{core.WithDryRun(true)},
wantErr: nil,
},
}
Expand All @@ -322,7 +322,7 @@ func TestService_CreateResource(t *testing.T) {
t.Parallel()
svc := tt.setup(t)

got, err := svc.CreateResource(context.Background(), tt.res, tt.dryrun)
got, err := svc.CreateResource(context.Background(), tt.res, tt.options...)
if tt.wantErr != nil {
assert.Error(t, err)
assert.True(t, errors.Is(err, tt.wantErr))
Expand Down Expand Up @@ -364,7 +364,7 @@ func TestService_UpdateResource(t *testing.T) {
urn string
update resource.UpdateRequest
want *resource.Resource
dryrun bool
options []core.Options
wantErr error
}{
{
Expand Down Expand Up @@ -569,7 +569,7 @@ func TestService_UpdateResource(t *testing.T) {
Configs: []byte(`{"foo": "bar"}`),
},
},
dryrun: true,
options: []core.Options{core.WithDryRun(true)},
wantErr: nil,
},
}
Expand All @@ -580,7 +580,7 @@ func TestService_UpdateResource(t *testing.T) {
t.Parallel()
svc := tt.setup(t)

got, err := svc.UpdateResource(context.Background(), tt.urn, tt.update, tt.dryrun)
got, err := svc.UpdateResource(context.Background(), tt.urn, tt.update, tt.options...)
if tt.wantErr != nil {
assert.Error(t, err)
assert.True(t, errors.Is(err, tt.wantErr))
Expand Down Expand Up @@ -741,7 +741,7 @@ func TestService_ApplyAction(t *testing.T) {
urn string
action module.ActionRequest
want *resource.Resource
dryrun bool
options []core.Options
wantErr error
}{
{
Expand Down Expand Up @@ -918,7 +918,7 @@ func TestService_ApplyAction(t *testing.T) {
UpdatedAt: frozenTime,
},
wantErr: nil,
dryrun: true,
options: []core.Options{core.WithDryRun(true)},
},
}

Expand All @@ -928,7 +928,7 @@ func TestService_ApplyAction(t *testing.T) {
t.Parallel()
svc := tt.setup(t)

got, err := svc.ApplyAction(context.Background(), tt.urn, tt.action, tt.dryrun)
got, err := svc.ApplyAction(context.Background(), tt.urn, tt.action, tt.options...)
if tt.wantErr != nil {
assert.Error(t, err)
assert.True(t, errors.Is(err, tt.wantErr), cmp.Diff(tt.want, err))
Expand Down
Loading

0 comments on commit a093dfd

Please sign in to comment.