From 829b8ac63a17eb2902063bc0daafed53eae5e422 Mon Sep 17 00:00:00 2001 From: Jerome Quere Date: Tue, 7 Jul 2020 20:40:43 +0200 Subject: [PATCH 1/2] refactor(rdb): group function by resource --- ...custom_engine_list.go => custom_engine.go} | 0 ...ine_list_test.go => custom_engine_test.go} | 0 internal/namespaces/rdb/v1/custom_instance.go | 55 +++++++++++++++++++ .../rdb/v1/custom_instance_clone.go | 23 -------- .../rdb/v1/custom_instance_clone_test.go | 21 ------- .../rdb/v1/custom_instance_create.go | 34 ------------ .../rdb/v1/custom_instance_create_test.go | 17 ------ .../rdb/v1/custom_instance_get_test.go | 21 ------- .../namespaces/rdb/v1/custom_instance_test.go | 47 ++++++++++++++++ .../rdb/v1/custom_instance_upgrade.go | 25 --------- .../rdb/v1/custom_instance_upgrade_test.go | 28 ---------- internal/namespaces/rdb/v1/helper_test.go | 25 +++++++++ 12 files changed, 127 insertions(+), 169 deletions(-) rename internal/namespaces/rdb/v1/{custom_engine_list.go => custom_engine.go} (100%) rename internal/namespaces/rdb/v1/{custom_engine_list_test.go => custom_engine_test.go} (100%) delete mode 100644 internal/namespaces/rdb/v1/custom_instance_clone.go delete mode 100644 internal/namespaces/rdb/v1/custom_instance_clone_test.go delete mode 100644 internal/namespaces/rdb/v1/custom_instance_create.go delete mode 100644 internal/namespaces/rdb/v1/custom_instance_create_test.go delete mode 100644 internal/namespaces/rdb/v1/custom_instance_get_test.go create mode 100644 internal/namespaces/rdb/v1/custom_instance_test.go delete mode 100644 internal/namespaces/rdb/v1/custom_instance_upgrade.go delete mode 100644 internal/namespaces/rdb/v1/custom_instance_upgrade_test.go create mode 100644 internal/namespaces/rdb/v1/helper_test.go diff --git a/internal/namespaces/rdb/v1/custom_engine_list.go b/internal/namespaces/rdb/v1/custom_engine.go similarity index 100% rename from internal/namespaces/rdb/v1/custom_engine_list.go rename to internal/namespaces/rdb/v1/custom_engine.go diff --git a/internal/namespaces/rdb/v1/custom_engine_list_test.go b/internal/namespaces/rdb/v1/custom_engine_test.go similarity index 100% rename from internal/namespaces/rdb/v1/custom_engine_list_test.go rename to internal/namespaces/rdb/v1/custom_engine_test.go diff --git a/internal/namespaces/rdb/v1/custom_instance.go b/internal/namespaces/rdb/v1/custom_instance.go index b6bf2da980..de10087797 100644 --- a/internal/namespaces/rdb/v1/custom_instance.go +++ b/internal/namespaces/rdb/v1/custom_instance.go @@ -3,6 +3,7 @@ package rdb import ( "context" "reflect" + "strings" "time" "github.com/scaleway/scaleway-cli/internal/core" @@ -76,6 +77,60 @@ func backupScheduleMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, return str, nil } +func instanceCloneBuilder(c *core.Command) *core.Command { + c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) { + api := rdb.NewAPI(core.ExtractClient(ctx)) + return api.WaitForInstance(&rdb.WaitForInstanceRequest{ + InstanceID: respI.(*rdb.Instance).ID, + Region: respI.(*rdb.Instance).Region, + Timeout: scw.TimeDurationPtr(instanceActionTimeout), + RetryInterval: core.DefaultRetryInterval, + }) + } + + return c +} + +func instanceCreateBuilder(c *core.Command) *core.Command { + c.ArgSpecs.GetByName("node-type").Default = core.DefaultValueSetter("DB-DEV-S") + c.ArgSpecs.GetByName("node-type").EnumValues = nodeTypes + + c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) { + api := rdb.NewAPI(core.ExtractClient(ctx)) + return api.WaitForInstance(&rdb.WaitForInstanceRequest{ + InstanceID: respI.(*rdb.Instance).ID, + Region: respI.(*rdb.Instance).Region, + Timeout: scw.TimeDurationPtr(instanceActionTimeout), + RetryInterval: core.DefaultRetryInterval, + }) + } + + // Waiting for API to accept uppercase node-type + c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) { + args := argsI.(*rdb.CreateInstanceRequest) + args.NodeType = strings.ToLower(args.NodeType) + return runner(ctx, args) + } + + return c +} + +func instanceUpgradeBuilder(c *core.Command) *core.Command { + c.ArgSpecs.GetByName("node-type").EnumValues = nodeTypes + + c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) { + api := rdb.NewAPI(core.ExtractClient(ctx)) + return api.WaitForInstance(&rdb.WaitForInstanceRequest{ + InstanceID: respI.(*rdb.Instance).ID, + Region: respI.(*rdb.Instance).Region, + Timeout: scw.TimeDurationPtr(instanceActionTimeout), + RetryInterval: core.DefaultRetryInterval, + }) + } + + return c +} + func instanceWaitCommand() *core.Command { return &core.Command{ Short: `Wait for an instance to reach a stable state`, diff --git a/internal/namespaces/rdb/v1/custom_instance_clone.go b/internal/namespaces/rdb/v1/custom_instance_clone.go deleted file mode 100644 index f9485a1ddf..0000000000 --- a/internal/namespaces/rdb/v1/custom_instance_clone.go +++ /dev/null @@ -1,23 +0,0 @@ -package rdb - -import ( - "context" - - "github.com/scaleway/scaleway-cli/internal/core" - "github.com/scaleway/scaleway-sdk-go/api/rdb/v1" - "github.com/scaleway/scaleway-sdk-go/scw" -) - -func instanceCloneBuilder(c *core.Command) *core.Command { - c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) { - api := rdb.NewAPI(core.ExtractClient(ctx)) - return api.WaitForInstance(&rdb.WaitForInstanceRequest{ - InstanceID: respI.(*rdb.Instance).ID, - Region: respI.(*rdb.Instance).Region, - Timeout: scw.TimeDurationPtr(instanceActionTimeout), - RetryInterval: core.DefaultRetryInterval, - }) - } - - return c -} diff --git a/internal/namespaces/rdb/v1/custom_instance_clone_test.go b/internal/namespaces/rdb/v1/custom_instance_clone_test.go deleted file mode 100644 index 4f8a969e9c..0000000000 --- a/internal/namespaces/rdb/v1/custom_instance_clone_test.go +++ /dev/null @@ -1,21 +0,0 @@ -package rdb - -import ( - "fmt" - "testing" - - "github.com/scaleway/scaleway-cli/internal/core" -) - -func Test_CloneInstance(t *testing.T) { - t.Run("Simple", core.Test(&core.TestConfig{ - Commands: GetCommands(), - BeforeFunc: core.ExecStoreBeforeCmd( - "StartServer", - fmt.Sprintf("scw rdb instance create node-type=DB-DEV-S is-ha-cluster=false name=%s engine=%s user-name=%s password=%s --wait", name, engine, user, password), - ), - Cmd: "scw rdb instance clone {{ .StartServer.ID }} node-type=DB-DEV-M name=foobar --wait", - Check: core.TestCheckGolden(), - AfterFunc: core.ExecAfterCmd("scw rdb instance delete {{ .StartServer.ID }}"), - })) -} diff --git a/internal/namespaces/rdb/v1/custom_instance_create.go b/internal/namespaces/rdb/v1/custom_instance_create.go deleted file mode 100644 index 20d8c11efc..0000000000 --- a/internal/namespaces/rdb/v1/custom_instance_create.go +++ /dev/null @@ -1,34 +0,0 @@ -package rdb - -import ( - "context" - "strings" - - "github.com/scaleway/scaleway-cli/internal/core" - "github.com/scaleway/scaleway-sdk-go/api/rdb/v1" - "github.com/scaleway/scaleway-sdk-go/scw" -) - -func instanceCreateBuilder(c *core.Command) *core.Command { - c.ArgSpecs.GetByName("node-type").Default = core.DefaultValueSetter("DB-DEV-S") - c.ArgSpecs.GetByName("node-type").EnumValues = nodeTypes - - c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) { - api := rdb.NewAPI(core.ExtractClient(ctx)) - return api.WaitForInstance(&rdb.WaitForInstanceRequest{ - InstanceID: respI.(*rdb.Instance).ID, - Region: respI.(*rdb.Instance).Region, - Timeout: scw.TimeDurationPtr(instanceActionTimeout), - RetryInterval: core.DefaultRetryInterval, - }) - } - - // Waiting for API to accept uppercase node-type - c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) { - args := argsI.(*rdb.CreateInstanceRequest) - args.NodeType = strings.ToLower(args.NodeType) - return runner(ctx, args) - } - - return c -} diff --git a/internal/namespaces/rdb/v1/custom_instance_create_test.go b/internal/namespaces/rdb/v1/custom_instance_create_test.go deleted file mode 100644 index df0d517867..0000000000 --- a/internal/namespaces/rdb/v1/custom_instance_create_test.go +++ /dev/null @@ -1,17 +0,0 @@ -package rdb - -import ( - "fmt" - "testing" - - "github.com/scaleway/scaleway-cli/internal/core" -) - -func Test_CreateInstance(t *testing.T) { - t.Run("Simple", core.Test(&core.TestConfig{ - Commands: GetCommands(), - Cmd: fmt.Sprintf("scw rdb instance create node-type=DB-DEV-S is-ha-cluster=false name=%s engine=%s user-name=%s password=%s --wait", name, engine, user, password), - Check: core.TestCheckGolden(), - AfterFunc: core.ExecAfterCmd("scw rdb instance delete {{ .CmdResult.ID }}"), - })) -} diff --git a/internal/namespaces/rdb/v1/custom_instance_get_test.go b/internal/namespaces/rdb/v1/custom_instance_get_test.go deleted file mode 100644 index d1bb120559..0000000000 --- a/internal/namespaces/rdb/v1/custom_instance_get_test.go +++ /dev/null @@ -1,21 +0,0 @@ -package rdb - -import ( - "fmt" - "testing" - - "github.com/scaleway/scaleway-cli/internal/core" -) - -func Test_GetInstance(t *testing.T) { - t.Run("Simple", core.Test(&core.TestConfig{ - Commands: GetCommands(), - BeforeFunc: core.ExecStoreBeforeCmd( - "StartServer", - fmt.Sprintf("scw rdb instance create node-type=DB-DEV-S is-ha-cluster=false name=%s engine=%s user-name=%s password=%s --wait", name, engine, user, password), - ), - Cmd: "scw rdb instance get {{ .StartServer.ID }}", - Check: core.TestCheckGolden(), - AfterFunc: core.ExecAfterCmd("scw rdb instance delete {{ .StartServer.ID }}"), - })) -} diff --git a/internal/namespaces/rdb/v1/custom_instance_test.go b/internal/namespaces/rdb/v1/custom_instance_test.go new file mode 100644 index 0000000000..e522df6dce --- /dev/null +++ b/internal/namespaces/rdb/v1/custom_instance_test.go @@ -0,0 +1,47 @@ +package rdb + +import ( + "fmt" + "testing" + + "github.com/scaleway/scaleway-cli/internal/core" +) + +func Test_CloneInstance(t *testing.T) { + t.Run("Simple", core.Test(&core.TestConfig{ + Commands: GetCommands(), + BeforeFunc: createInstance(), + Cmd: "scw rdb instance clone {{ .Instance.ID }} node-type=DB-DEV-M name=foobar --wait", + Check: core.TestCheckGolden(), + AfterFunc: deleteInstance(), + })) +} + +func Test_CreateInstance(t *testing.T) { + t.Run("Simple", core.Test(&core.TestConfig{ + Commands: GetCommands(), + Cmd: fmt.Sprintf("scw rdb instance create node-type=DB-DEV-S is-ha-cluster=false name=%s engine=%s user-name=%s password=%s --wait", name, engine, user, password), + Check: core.TestCheckGolden(), + AfterFunc: core.ExecAfterCmd("scw rdb instance delete {{ .CmdResult.ID }}"), + })) +} + +func Test_GetInstance(t *testing.T) { + t.Run("Simple", core.Test(&core.TestConfig{ + Commands: GetCommands(), + BeforeFunc: createInstance(), + Cmd: "scw rdb instance get {{ .StartServer.ID }}", + Check: core.TestCheckGolden(), + AfterFunc: deleteInstance(), + })) +} + +func Test_UpgradeInstance(t *testing.T) { + t.Run("Simple", core.Test(&core.TestConfig{ + Commands: GetCommands(), + BeforeFunc: createInstance(), + Cmd: "scw rdb instance upgrade {{ .StartServer.ID }} node-type=DB-DEV-M --wait", + Check: core.TestCheckGolden(), + AfterFunc: deleteInstance(), + })) +} diff --git a/internal/namespaces/rdb/v1/custom_instance_upgrade.go b/internal/namespaces/rdb/v1/custom_instance_upgrade.go deleted file mode 100644 index a824069b58..0000000000 --- a/internal/namespaces/rdb/v1/custom_instance_upgrade.go +++ /dev/null @@ -1,25 +0,0 @@ -package rdb - -import ( - "context" - - "github.com/scaleway/scaleway-cli/internal/core" - "github.com/scaleway/scaleway-sdk-go/api/rdb/v1" - "github.com/scaleway/scaleway-sdk-go/scw" -) - -func instanceUpgradeBuilder(c *core.Command) *core.Command { - c.ArgSpecs.GetByName("node-type").EnumValues = nodeTypes - - c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) { - api := rdb.NewAPI(core.ExtractClient(ctx)) - return api.WaitForInstance(&rdb.WaitForInstanceRequest{ - InstanceID: respI.(*rdb.Instance).ID, - Region: respI.(*rdb.Instance).Region, - Timeout: scw.TimeDurationPtr(instanceActionTimeout), - RetryInterval: core.DefaultRetryInterval, - }) - } - - return c -} diff --git a/internal/namespaces/rdb/v1/custom_instance_upgrade_test.go b/internal/namespaces/rdb/v1/custom_instance_upgrade_test.go deleted file mode 100644 index 192a04ac4e..0000000000 --- a/internal/namespaces/rdb/v1/custom_instance_upgrade_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package rdb - -import ( - "fmt" - "testing" - - "github.com/scaleway/scaleway-cli/internal/core" -) - -const ( - name = "cli-test" - user = "foobar" - password = "{4xdl*#QOoP+&3XRkGA)]" - engine = "PostgreSQL-12" -) - -func Test_UpgradeInstance(t *testing.T) { - t.Run("Simple", core.Test(&core.TestConfig{ - Commands: GetCommands(), - BeforeFunc: core.ExecStoreBeforeCmd( - "StartServer", - fmt.Sprintf("scw rdb instance create node-type=DB-DEV-S is-ha-cluster=false name=%s engine=%s user-name=%s password=%s --wait", name, engine, user, password), - ), - Cmd: "scw rdb instance upgrade {{ .StartServer.ID }} node-type=DB-DEV-M --wait", - Check: core.TestCheckGolden(), - AfterFunc: core.ExecAfterCmd("scw rdb instance delete {{ .StartServer.ID }}"), - })) -} diff --git a/internal/namespaces/rdb/v1/helper_test.go b/internal/namespaces/rdb/v1/helper_test.go new file mode 100644 index 0000000000..ee45f05669 --- /dev/null +++ b/internal/namespaces/rdb/v1/helper_test.go @@ -0,0 +1,25 @@ +package rdb + +import ( + "fmt" + + "github.com/scaleway/scaleway-cli/internal/core" +) + +const ( + name = "cli-test" + user = "foobar" + password = "{4xdl*#QOoP+&3XRkGA)]" + engine = "PostgreSQL-12" +) + +func createInstance() core.BeforeFunc { + return core.ExecStoreBeforeCmd( + "Instance", + fmt.Sprintf("scw rdb instance create node-type=DB-DEV-S is-ha-cluster=false name=%s engine=%s user-name=%s password=%s --wait", name, engine, user, password), + ) +} + +func deleteInstance() core.AfterFunc { + return core.ExecAfterCmd("scw rdb instance delete {{ .Instance.ID }}") +} From 7ba65de0f2129db98fab8fbda164331c1c941f0b Mon Sep 17 00:00:00 2001 From: Jerome Quere Date: Tue, 7 Jul 2020 23:00:22 +0200 Subject: [PATCH 2/2] fix --- internal/namespaces/rdb/v1/custom_instance_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/namespaces/rdb/v1/custom_instance_test.go b/internal/namespaces/rdb/v1/custom_instance_test.go index e522df6dce..ee1ffcdc53 100644 --- a/internal/namespaces/rdb/v1/custom_instance_test.go +++ b/internal/namespaces/rdb/v1/custom_instance_test.go @@ -30,7 +30,7 @@ func Test_GetInstance(t *testing.T) { t.Run("Simple", core.Test(&core.TestConfig{ Commands: GetCommands(), BeforeFunc: createInstance(), - Cmd: "scw rdb instance get {{ .StartServer.ID }}", + Cmd: "scw rdb instance get {{ .Instance.ID }}", Check: core.TestCheckGolden(), AfterFunc: deleteInstance(), })) @@ -40,7 +40,7 @@ func Test_UpgradeInstance(t *testing.T) { t.Run("Simple", core.Test(&core.TestConfig{ Commands: GetCommands(), BeforeFunc: createInstance(), - Cmd: "scw rdb instance upgrade {{ .StartServer.ID }} node-type=DB-DEV-M --wait", + Cmd: "scw rdb instance upgrade {{ .Instance.ID }} node-type=DB-DEV-M --wait", Check: core.TestCheckGolden(), AfterFunc: deleteInstance(), }))