move operation.Options into /pkg/control (#452)
In order for sdk users to build an operation using a repo, without importing /inernal packages, the operation options needs to be available via a /pkg package.
This commit is contained in:
parent
5a68108006
commit
7e60ec5073
@ -150,7 +150,7 @@ func createExchangeCmd(cmd *cobra.Command, args []string) error {
|
||||
|
||||
sel := exchangeBackupCreateSelectors(exchangeAll, user, exchangeData)
|
||||
|
||||
bo, err := r.NewBackup(ctx, sel, options.OperationOptions())
|
||||
bo, err := r.NewBackup(ctx, sel, options.Control())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Failed to initialize Exchange backup")
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package options
|
||||
|
||||
import (
|
||||
"github.com/alcionai/corso/internal/operations"
|
||||
"github.com/alcionai/corso/pkg/control"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@ -17,7 +17,7 @@ func AddOperationFlags(parent *cobra.Command) {
|
||||
cobra.CheckErr(fs.MarkHidden("fast-fail"))
|
||||
}
|
||||
|
||||
// OperationOptions produces the operation options based on the user's flags.
|
||||
func OperationOptions() operations.Options {
|
||||
return operations.NewOptions(fastFail)
|
||||
// Control produces the control options based on the user's flags.
|
||||
func Control() control.Options {
|
||||
return control.NewOptions(fastFail)
|
||||
}
|
||||
|
||||
@ -150,7 +150,7 @@ func restoreExchangeCmd(cmd *cobra.Command, args []string) error {
|
||||
sel.Include(sel.Users(selectors.Any()))
|
||||
}
|
||||
|
||||
ro, err := r.NewRestore(ctx, backupID, sel.Selector, options.OperationOptions())
|
||||
ro, err := r.NewRestore(ctx, backupID, sel.Selector, options.Control())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Failed to initialize Exchange restore")
|
||||
}
|
||||
|
||||
@ -1,12 +0,0 @@
|
||||
package common
|
||||
|
||||
// Policy is a type that defines the actions taken
|
||||
type RestorePolicy int
|
||||
|
||||
//go:generate stringer -type=RestorePolicy
|
||||
const (
|
||||
Unknown RestorePolicy = iota
|
||||
Copy
|
||||
Drop
|
||||
Replace
|
||||
)
|
||||
@ -1,26 +0,0 @@
|
||||
// Code generated by "stringer -type=RestorePolicy"; DO NOT EDIT.
|
||||
|
||||
package common
|
||||
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[Unknown-0]
|
||||
_ = x[Copy-1]
|
||||
_ = x[Drop-2]
|
||||
_ = x[Replace-3]
|
||||
}
|
||||
|
||||
const _RestorePolicy_name = "UnknownCopyDropReplace"
|
||||
|
||||
var _RestorePolicy_index = [...]uint8{0, 7, 11, 15, 22}
|
||||
|
||||
func (i RestorePolicy) String() string {
|
||||
if i < 0 || i >= RestorePolicy(len(_RestorePolicy_index)-1) {
|
||||
return "RestorePolicy(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
return _RestorePolicy_name[_RestorePolicy_index[i]:_RestorePolicy_index[i+1]]
|
||||
}
|
||||
@ -15,6 +15,7 @@ import (
|
||||
"github.com/alcionai/corso/pkg/account"
|
||||
"github.com/alcionai/corso/pkg/backup"
|
||||
"github.com/alcionai/corso/pkg/backup/details"
|
||||
"github.com/alcionai/corso/pkg/control"
|
||||
"github.com/alcionai/corso/pkg/selectors"
|
||||
"github.com/alcionai/corso/pkg/store"
|
||||
)
|
||||
@ -40,7 +41,7 @@ type BackupResults struct {
|
||||
// NewBackupOperation constructs and validates a backup operation.
|
||||
func NewBackupOperation(
|
||||
ctx context.Context,
|
||||
opts Options,
|
||||
opts control.Options,
|
||||
kw *kopia.Wrapper,
|
||||
sw *store.Wrapper,
|
||||
acct account.Account,
|
||||
|
||||
@ -14,6 +14,7 @@ import (
|
||||
"github.com/alcionai/corso/internal/kopia"
|
||||
ctesting "github.com/alcionai/corso/internal/testing"
|
||||
"github.com/alcionai/corso/pkg/account"
|
||||
"github.com/alcionai/corso/pkg/control"
|
||||
"github.com/alcionai/corso/pkg/selectors"
|
||||
"github.com/alcionai/corso/pkg/store"
|
||||
)
|
||||
@ -53,7 +54,7 @@ func (suite *BackupOpSuite) TestBackupOperation_PersistResults() {
|
||||
}
|
||||
)
|
||||
|
||||
op, err := NewBackupOperation(ctx, Options{}, kw, sw, acct, selectors.Selector{})
|
||||
op, err := NewBackupOperation(ctx, control.Options{}, kw, sw, acct, selectors.Selector{})
|
||||
require.NoError(t, err)
|
||||
|
||||
op.persistResults(now, &stats)
|
||||
@ -103,22 +104,22 @@ func (suite *BackupOpIntegrationSuite) TestNewBackupOperation() {
|
||||
|
||||
table := []struct {
|
||||
name string
|
||||
opts Options
|
||||
opts control.Options
|
||||
kw *kopia.Wrapper
|
||||
sw *store.Wrapper
|
||||
acct account.Account
|
||||
targets []string
|
||||
errCheck assert.ErrorAssertionFunc
|
||||
}{
|
||||
{"good", Options{}, kw, sw, acct, nil, assert.NoError},
|
||||
{"missing kopia", Options{}, nil, sw, acct, nil, assert.Error},
|
||||
{"missing modelstore", Options{}, kw, nil, acct, nil, assert.Error},
|
||||
{"good", control.Options{}, kw, sw, acct, nil, assert.NoError},
|
||||
{"missing kopia", control.Options{}, nil, sw, acct, nil, assert.Error},
|
||||
{"missing modelstore", control.Options{}, kw, nil, acct, nil, assert.Error},
|
||||
}
|
||||
for _, test := range table {
|
||||
suite.T().Run(test.name, func(t *testing.T) {
|
||||
_, err := NewBackupOperation(
|
||||
context.Background(),
|
||||
Options{},
|
||||
test.opts,
|
||||
test.kw,
|
||||
test.sw,
|
||||
test.acct,
|
||||
@ -162,7 +163,7 @@ func (suite *BackupOpIntegrationSuite) TestBackup_Run() {
|
||||
|
||||
bo, err := NewBackupOperation(
|
||||
ctx,
|
||||
Options{},
|
||||
control.Options{},
|
||||
kw,
|
||||
sw,
|
||||
acct,
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/alcionai/corso/internal/kopia"
|
||||
"github.com/alcionai/corso/pkg/control"
|
||||
"github.com/alcionai/corso/pkg/store"
|
||||
)
|
||||
|
||||
@ -27,28 +28,16 @@ const (
|
||||
// Specific processes (eg: backups, restores, etc) are expected to wrap operation
|
||||
// with process specific details.
|
||||
type operation struct {
|
||||
CreatedAt time.Time `json:"createdAt"` // datetime of the operation's creation
|
||||
Options Options `json:"options"`
|
||||
Status opStatus `json:"status"`
|
||||
CreatedAt time.Time `json:"createdAt"` // datetime of the operation's creation
|
||||
Options control.Options `json:"options"`
|
||||
Status opStatus `json:"status"`
|
||||
|
||||
kopia *kopia.Wrapper
|
||||
store *store.Wrapper
|
||||
}
|
||||
|
||||
// Options configure some parameters of the operation
|
||||
type Options struct {
|
||||
FailFast bool `json:"failFast"`
|
||||
// todo: collision handling
|
||||
}
|
||||
|
||||
func NewOptions(failFast bool) Options {
|
||||
return Options{
|
||||
FailFast: failFast,
|
||||
}
|
||||
}
|
||||
|
||||
func newOperation(
|
||||
opts Options,
|
||||
opts control.Options,
|
||||
kw *kopia.Wrapper,
|
||||
sw *store.Wrapper,
|
||||
) operation {
|
||||
|
||||
@ -8,6 +8,7 @@ import (
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"github.com/alcionai/corso/internal/kopia"
|
||||
"github.com/alcionai/corso/pkg/control"
|
||||
"github.com/alcionai/corso/pkg/store"
|
||||
)
|
||||
|
||||
@ -21,7 +22,7 @@ func TestOperationSuite(t *testing.T) {
|
||||
|
||||
func (suite *OperationSuite) TestNewOperation() {
|
||||
t := suite.T()
|
||||
op := newOperation(Options{}, nil, nil)
|
||||
op := newOperation(control.Options{}, nil, nil)
|
||||
assert.Greater(t, op.CreatedAt, time.Time{})
|
||||
}
|
||||
|
||||
@ -40,7 +41,7 @@ func (suite *OperationSuite) TestOperation_Validate() {
|
||||
}
|
||||
for _, test := range table {
|
||||
suite.T().Run(test.name, func(t *testing.T) {
|
||||
op := newOperation(Options{}, test.kw, test.sw)
|
||||
op := newOperation(control.Options{}, test.kw, test.sw)
|
||||
test.errCheck(t, op.validate())
|
||||
})
|
||||
}
|
||||
|
||||
@ -14,6 +14,7 @@ import (
|
||||
"github.com/alcionai/corso/internal/model"
|
||||
"github.com/alcionai/corso/internal/stats"
|
||||
"github.com/alcionai/corso/pkg/account"
|
||||
"github.com/alcionai/corso/pkg/control"
|
||||
"github.com/alcionai/corso/pkg/selectors"
|
||||
"github.com/alcionai/corso/pkg/store"
|
||||
)
|
||||
@ -39,7 +40,7 @@ type RestoreResults struct {
|
||||
// NewRestoreOperation constructs and validates a restore operation.
|
||||
func NewRestoreOperation(
|
||||
ctx context.Context,
|
||||
opts Options,
|
||||
opts control.Options,
|
||||
kw *kopia.Wrapper,
|
||||
sw *store.Wrapper,
|
||||
acct account.Account,
|
||||
|
||||
@ -16,6 +16,7 @@ import (
|
||||
"github.com/alcionai/corso/internal/kopia"
|
||||
ctesting "github.com/alcionai/corso/internal/testing"
|
||||
"github.com/alcionai/corso/pkg/account"
|
||||
"github.com/alcionai/corso/pkg/control"
|
||||
"github.com/alcionai/corso/pkg/selectors"
|
||||
"github.com/alcionai/corso/pkg/store"
|
||||
)
|
||||
@ -53,7 +54,7 @@ func (suite *RestoreOpSuite) TestRestoreOperation_PersistResults() {
|
||||
}
|
||||
)
|
||||
|
||||
op, err := NewRestoreOperation(ctx, Options{}, kw, sw, acct, "foo", selectors.Selector{})
|
||||
op, err := NewRestoreOperation(ctx, control.Options{}, kw, sw, acct, "foo", selectors.Selector{})
|
||||
require.NoError(t, err)
|
||||
|
||||
op.persistResults(now, &stats)
|
||||
@ -98,22 +99,22 @@ func (suite *RestoreOpIntegrationSuite) TestNewRestoreOperation() {
|
||||
|
||||
table := []struct {
|
||||
name string
|
||||
opts Options
|
||||
opts control.Options
|
||||
kw *kopia.Wrapper
|
||||
sw *store.Wrapper
|
||||
acct account.Account
|
||||
targets []string
|
||||
errCheck assert.ErrorAssertionFunc
|
||||
}{
|
||||
{"good", Options{}, kw, sw, acct, nil, assert.NoError},
|
||||
{"missing kopia", Options{}, nil, sw, acct, nil, assert.Error},
|
||||
{"missing modelstore", Options{}, kw, nil, acct, nil, assert.Error},
|
||||
{"good", control.Options{}, kw, sw, acct, nil, assert.NoError},
|
||||
{"missing kopia", control.Options{}, nil, sw, acct, nil, assert.Error},
|
||||
{"missing modelstore", control.Options{}, kw, nil, acct, nil, assert.Error},
|
||||
}
|
||||
for _, test := range table {
|
||||
suite.T().Run(test.name, func(t *testing.T) {
|
||||
_, err := NewRestoreOperation(
|
||||
context.Background(),
|
||||
Options{},
|
||||
test.opts,
|
||||
test.kw,
|
||||
test.sw,
|
||||
test.acct,
|
||||
@ -155,7 +156,7 @@ func (suite *RestoreOpIntegrationSuite) TestRestore_Run() {
|
||||
|
||||
bo, err := NewBackupOperation(
|
||||
ctx,
|
||||
Options{},
|
||||
control.Options{},
|
||||
w,
|
||||
sw,
|
||||
acct,
|
||||
@ -169,7 +170,7 @@ func (suite *RestoreOpIntegrationSuite) TestRestore_Run() {
|
||||
|
||||
ro, err := NewRestoreOperation(
|
||||
ctx,
|
||||
Options{},
|
||||
control.Options{},
|
||||
w,
|
||||
sw,
|
||||
acct,
|
||||
@ -218,7 +219,7 @@ func (suite *RestoreOpIntegrationSuite) TestRestore_Run_ErrorNoResults() {
|
||||
|
||||
bo, err := NewBackupOperation(
|
||||
ctx,
|
||||
Options{},
|
||||
control.Options{},
|
||||
w,
|
||||
sw,
|
||||
acct,
|
||||
@ -232,7 +233,7 @@ func (suite *RestoreOpIntegrationSuite) TestRestore_Run_ErrorNoResults() {
|
||||
|
||||
ro, err := NewRestoreOperation(
|
||||
ctx,
|
||||
Options{},
|
||||
control.Options{},
|
||||
w,
|
||||
sw,
|
||||
acct,
|
||||
|
||||
23
src/pkg/control/options.go
Normal file
23
src/pkg/control/options.go
Normal file
@ -0,0 +1,23 @@
|
||||
package control
|
||||
|
||||
// CollisionPolicy describes how the datalayer behaves in case of a collision.
|
||||
type CollisionPolicy int
|
||||
|
||||
const (
|
||||
Unknown CollisionPolicy = iota
|
||||
Copy
|
||||
Skip
|
||||
Replace
|
||||
)
|
||||
|
||||
// Options holds the optional configurations for a process
|
||||
type Options struct {
|
||||
FailFast bool `json:"failFast"`
|
||||
Collision CollisionPolicy `json:"-"`
|
||||
}
|
||||
|
||||
func NewOptions(failFast bool) Options {
|
||||
return Options{
|
||||
FailFast: failFast,
|
||||
}
|
||||
}
|
||||
26
src/pkg/control/options_test.go
Normal file
26
src/pkg/control/options_test.go
Normal file
@ -0,0 +1,26 @@
|
||||
package control_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"github.com/alcionai/corso/pkg/control"
|
||||
)
|
||||
|
||||
type OptionsSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
func TestOptionsSuite(t *testing.T) {
|
||||
suite.Run(t, new(OptionsSuite))
|
||||
}
|
||||
|
||||
func (suite *OptionsSuite) TestNewOptions() {
|
||||
t := suite.T()
|
||||
o1 := control.NewOptions(true)
|
||||
assert.True(t, o1.FailFast, "failFast")
|
||||
o2 := control.NewOptions(false)
|
||||
assert.False(t, o2.FailFast, "failFast")
|
||||
}
|
||||
@ -13,6 +13,7 @@ import (
|
||||
"github.com/alcionai/corso/pkg/account"
|
||||
"github.com/alcionai/corso/pkg/backup"
|
||||
"github.com/alcionai/corso/pkg/backup/details"
|
||||
"github.com/alcionai/corso/pkg/control"
|
||||
"github.com/alcionai/corso/pkg/selectors"
|
||||
"github.com/alcionai/corso/pkg/storage"
|
||||
"github.com/alcionai/corso/pkg/store"
|
||||
@ -129,7 +130,7 @@ func (r *Repository) Close(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// NewBackup generates a BackupOperation runner.
|
||||
func (r Repository) NewBackup(ctx context.Context, selector selectors.Selector, opts operations.Options) (operations.BackupOperation, error) {
|
||||
func (r Repository) NewBackup(ctx context.Context, selector selectors.Selector, opts control.Options) (operations.BackupOperation, error) {
|
||||
return operations.NewBackupOperation(
|
||||
ctx,
|
||||
opts,
|
||||
@ -144,7 +145,7 @@ func (r Repository) NewRestore(
|
||||
ctx context.Context,
|
||||
backupID string,
|
||||
sel selectors.Selector,
|
||||
opts operations.Options,
|
||||
opts control.Options,
|
||||
) (operations.RestoreOperation, error) {
|
||||
return operations.NewRestoreOperation(
|
||||
ctx,
|
||||
|
||||
@ -8,9 +8,9 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"github.com/alcionai/corso/internal/operations"
|
||||
ctesting "github.com/alcionai/corso/internal/testing"
|
||||
"github.com/alcionai/corso/pkg/account"
|
||||
"github.com/alcionai/corso/pkg/control"
|
||||
"github.com/alcionai/corso/pkg/repository"
|
||||
"github.com/alcionai/corso/pkg/selectors"
|
||||
"github.com/alcionai/corso/pkg/storage"
|
||||
@ -172,7 +172,7 @@ func (suite *RepositoryIntegrationSuite) TestNewBackup() {
|
||||
r, err := repository.Initialize(ctx, acct, st)
|
||||
require.NoError(t, err)
|
||||
|
||||
bo, err := r.NewBackup(ctx, selectors.Selector{}, operations.Options{})
|
||||
bo, err := r.NewBackup(ctx, selectors.Selector{}, control.Options{})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, bo)
|
||||
}
|
||||
@ -191,7 +191,7 @@ func (suite *RepositoryIntegrationSuite) TestNewRestore() {
|
||||
r, err := repository.Initialize(ctx, acct, st)
|
||||
require.NoError(t, err)
|
||||
|
||||
ro, err := r.NewRestore(ctx, "backup-id", selectors.Selector{}, operations.Options{})
|
||||
ro, err := r.NewRestore(ctx, "backup-id", selectors.Selector{}, control.Options{})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, ro)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user