Skip to content

Commit

Permalink
refactor operator
Browse files Browse the repository at this point in the history
  • Loading branch information
CDimonaco committed Oct 2, 2024
1 parent 5852f53 commit b08ae45
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 44 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ require github.com/stretchr/testify v1.9.0
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
golang.org/x/sys v0.25.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
42 changes: 34 additions & 8 deletions pkg/operator/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import (
"context"
"errors"
"fmt"

"github.com/sirupsen/logrus"
)

type BaseOption Option[base]

type errUnimplementedPhase struct {
phase OPERATION_PHASES
}
Expand All @@ -14,34 +18,56 @@ func (e errUnimplementedPhase) Error() string {
return fmt.Sprintf("phase function: %s not implemented", e.phase)
}

type Base struct {
func WithLogger(logger *logrus.Logger) BaseOption {
return func(b *base) {
b.logger = logger
}
}

type base struct {
operationID string
arguments OperatorArguments
currentPhase OPERATION_PHASES
planResources map[string]any
logger *logrus.Logger
}

func newBaseOperator(operationID string, arguments OperatorArguments, options ...BaseOption) base {
base := &base{
operationID: operationID,
arguments: arguments,
planResources: make(map[string]any),
logger: logrus.StandardLogger(),
}

for _, opt := range options {
opt(base)
}

return *base
}

func (sa *Base) Plan(_ context.Context) error {
func (sa *base) plan(_ context.Context) error {

Check failure on line 50 in pkg/operator/base.go

View workflow job for this annotation

GitHub Actions / static-analysis

func `(*base).plan` is unused (unused)
return errUnimplementedPhase{phase: PLAN}
}

func (sa *Base) commit(_ context.Context) error {
func (sa *base) commit(_ context.Context) error {
return errUnimplementedPhase{phase: COMMIT}
}

func (sa *Base) verify(_ context.Context) error {
func (sa *base) verify(_ context.Context) error {
return errUnimplementedPhase{phase: VERIFY}
}

func (sa *Base) rollback(_ context.Context) error {
func (sa *base) rollback(_ context.Context) error {
return errUnimplementedPhase{phase: ROLLBACK}
}

func (sa *Base) wrapRollbackError(phaseError error, rollbackError error) error {
func (sa *base) wrapRollbackError(phaseError error, rollbackError error) error {
return errors.Join(rollbackError, phaseError)
}

func (sa *Base) reportError(error error) *ExecutionReport {
func (sa *base) reportError(error error) *ExecutionReport {
return &ExecutionReport{
OperationID: sa.operationID,
Error: &ExecutionError{
Expand All @@ -51,7 +77,7 @@ func (sa *Base) reportError(error error) *ExecutionReport {
}
}

func (sa *Base) reportSuccess(diff map[string]string) *ExecutionReport {
func (sa *base) reportSuccess(diff map[string]string) *ExecutionReport {
return &ExecutionReport{
OperationID: sa.operationID,
Success: &ExecutionSuccess{
Expand Down
26 changes: 26 additions & 0 deletions pkg/operator/execution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package operator

import "fmt"

type ExecutionError struct {
ErrorPhase OPERATION_PHASES
Message string
}

func (e ExecutionError) Error() string {
return fmt.Sprintf(
"error during operator exeuction in phase: %s, reason: %s",
e.ErrorPhase,
e.Message,
)
}

type ExecutionSuccess struct {
Diff map[string]string
}

type ExecutionReport struct {
OperationID string
Success *ExecutionSuccess
Error *ExecutionError
}
31 changes: 6 additions & 25 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package operator

import (
"context"
"fmt"
)

type OPERATION_PHASES string
Expand All @@ -14,33 +13,15 @@ const (
ROLLBACK OPERATION_PHASES = "ROLLBACK"
)

type ExecutionError struct {
ErrorPhase OPERATION_PHASES
Message string
}

func (e ExecutionError) Error() string {
return fmt.Sprintf(
"error during operator exeuction in phase: %s, reason: %s",
e.ErrorPhase,
e.Message,
)
}

type ExecutionSuccess struct {
Diff map[string]string
}

type ExecutionReport struct {
OperationID string
Success *ExecutionSuccess
Error *ExecutionError
}

type OperatorArguments map[string]any

type Operator interface {
Run(ctx context.Context) *ExecutionReport
}

type Option func(Operator)
type Option[T any] func(*T)

type OperatorOptions[T any] struct {
BaseOperatorOptions []BaseOption
OperatorOptions []Option[T]
}
19 changes: 9 additions & 10 deletions pkg/operator/saptuneapply.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"github.com/trento-project/workbench/internal/support"
)

type SaptuneApplyOption func(*SaptuneApply)
type SaptuneApplyOption Option[SaptuneApply]

type SaptuneApply struct {
Base
base
executor support.CmdExecutor
}

func WithCustomSaptuneExecutor(executor support.CmdExecutor) SaptuneApplyOption {
func WithCustomSaptuneExecutor(executor support.CmdExecutor) Option[SaptuneApply] {
return func(o *SaptuneApply) {
o.executor = executor
}
Expand All @@ -22,18 +22,14 @@ func WithCustomSaptuneExecutor(executor support.CmdExecutor) SaptuneApplyOption
func NewSaptuneApply(
arguments OperatorArguments,
operationID string,
options ...SaptuneApplyOption,
options OperatorOptions[SaptuneApply],
) *SaptuneApply {
saptuneApply := &SaptuneApply{
Base: Base{
operationID: operationID,
arguments: arguments,
planResources: make(map[string]any),
},
base: newBaseOperator(operationID, arguments, options.BaseOperatorOptions...),
executor: support.Executor{},
}

for _, opt := range options {
for _, opt := range options.OperatorOptions {
opt(saptuneApply)
}

Expand Down Expand Up @@ -72,5 +68,8 @@ func (sa *SaptuneApply) Run(ctx context.Context) *ExecutionReport {
}

func (sa *SaptuneApply) plan(_ context.Context) error {
sa.logger.Debug(
"printing infos",
)
return nil
}
7 changes: 6 additions & 1 deletion pkg/operator/saptuneapply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/trento-project/workbench/pkg/operator"
)
Expand All @@ -13,7 +14,11 @@ func TestSaptuneApplyRunFailure(t *testing.T) {
"test": 1,
}
opID := "223"
sa := operator.NewSaptuneApply(arguments, opID)
logger := logrus.StandardLogger()
logger.SetLevel(logrus.DebugLevel)
sa := operator.NewSaptuneApply(arguments, opID, operator.OperatorOptions[operator.SaptuneApply]{
BaseOperatorOptions: []operator.BaseOption{operator.WithLogger(logrus.StandardLogger())},
})
res := sa.Run(context.TODO())

assert.NotNil(t, res.Error)
Expand Down

0 comments on commit b08ae45

Please sign in to comment.