Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.9
github.com/pkg/errors v0.9.1 // indirect
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.5.0.20200211104822-047c88bb15c4
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.5.0.20200212160027-75ab2f6aeda3
github.com/sergi/go-diff v1.0.0 // indirect
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNue
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.5.0.20200211104822-047c88bb15c4 h1:7MhGZPXmbEqx97kIrBA7Xen1obsqQPnR0HKErxh76S4=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.5.0.20200211104822-047c88bb15c4/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.5.0.20200212160027-75ab2f6aeda3 h1:BQb9gbsenI8ZwZmfN7O93VrlXW8eiDlL7Mz+glsK/os=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.5.0.20200212160027-75ab2f6aeda3/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
Expand Down
9 changes: 2 additions & 7 deletions internal/namespaces/instance/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,8 @@ func GetCommands() *core.Commands {
//
// User Data
//
cmds.Merge(core.NewCommands(
userDataCommand(),
userDataListCommand(),
userDataSetCommand(),
userDataDeleteCommand(),
userDataGetCommand(),
))
cmds.MustFind("instance", "user-data", "set").Override(userDataSetBuilder)
cmds.MustFind("instance", "user-data", "get").Override(userDataGetBuilder)

return cmds
}
Expand Down
139 changes: 25 additions & 114 deletions internal/namespaces/instance/v1/custom_user_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,135 +2,46 @@ package instance

import (
"context"
"reflect"
"fmt"
"net/http"

"github.com/scaleway/scaleway-cli/internal/core"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

//
// Commands
//

func userDataCommand() *core.Command {
return &core.Command{
Namespace: "instance",
Resource: "user-data",
func userDataSetBuilder(c *core.Command) *core.Command {
*c.ArgSpecs.GetByName("content.name") = core.ArgSpec{
Name: "content",
Short: "Content of the user data",
Required: true,
}
}

func userDataListCommand() *core.Command {
return &core.Command{
Short: `List user data`,
Long: `List user data for the given server.`,
Namespace: "instance",
Resource: "user-data",
Verb: "list",
ArgsType: reflect.TypeOf(instance.ListServerUserDataRequest{}),
ArgSpecs: core.ArgSpecs{
core.ZoneArgSpec(),
{
Name: "server-id",
Short: `ID of a server`,
Required: true,
},
},
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
return instance.NewAPI(core.ExtractClient(ctx)).ListServerUserData(argsI.(*instance.ListServerUserDataRequest))
},
}
c.ArgSpecs.DeleteByName("content.content-type")
c.ArgSpecs.DeleteByName("content.content")
return c
}

func userDataDeleteCommand() *core.Command {
return &core.Command{
Short: `Delete user data by key`,
Long: `Delete user data key for the given server.`,
Namespace: "instance",
Resource: "user-data",
Verb: "delete",
ArgsType: reflect.TypeOf(instance.DeleteServerUserDataRequest{}),
ArgSpecs: core.ArgSpecs{
core.ZoneArgSpec(),
{
Name: "server-id",
Short: `ID of a server`,
Required: true,
},
{
Name: "key",
Short: `Key of the user data`,
Required: true,
},
},
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
err := instance.NewAPI(core.ExtractClient(ctx)).DeleteServerUserData(argsI.(*instance.DeleteServerUserDataRequest))
if err != nil {
return nil, err
func userDataGetBuilder(c *core.Command) *core.Command {
originalRun := c.Run
c.Run = func(ctx context.Context, argsI interface{}) (interface{}, error) {
req := argsI.(*instance.GetServerUserDataRequest)
res, err := originalRun(ctx, argsI)
if err != nil {
if resErr, ok := err.(*scw.ResponseError); ok {
if resErr.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("'%s' key does not exists", req.Key)
}
}
return &core.SuccessResult{}, nil
},
}
}
return nil, err
}

func userDataGetCommand() *core.Command {
return &core.Command{
Short: `Get user data key`,
Long: `Get user data key for the given server.`,
Namespace: "instance",
Resource: "user-data",
Verb: "get",
ArgsType: reflect.TypeOf(instance.GetServerUserDataRequest{}),
ArgSpecs: core.ArgSpecs{
core.ZoneArgSpec(),
{
Name: "server-id",
Short: `ID of a server`,
Required: true,
},
{
Name: "key",
Short: `Key of the user data`,
Required: true,
},
},
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
return instance.NewAPI(core.ExtractClient(ctx)).GetServerUserData(argsI.(*instance.GetServerUserDataRequest))
},
return res, nil
}
}

func userDataSetCommand() *core.Command {
return &core.Command{
Short: `Set a user data`,
Long: `Set a user data for the given server.`,
Namespace: "instance",
Resource: "user-data",
Verb: "set",
ArgsType: reflect.TypeOf(instance.SetServerUserDataRequest{}),
ArgSpecs: core.ArgSpecs{
core.ZoneArgSpec(),
{
Name: "server-id",
Short: `ID of a server`,
Required: true,
},
{
Name: "key",
Short: `Key of the user data`,
Required: true,
},
{
Name: "content",
Short: `Content of the user data`,
Required: true,
},
},
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
err := instance.NewAPI(core.ExtractClient(ctx)).SetServerUserData(argsI.(*instance.SetServerUserDataRequest))
if err != nil {
return nil, err
}
return &core.SuccessResult{}, nil
},
}
return c
}
55 changes: 55 additions & 0 deletions internal/namespaces/instance/v1/custom_user_data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package instance

import (
"testing"

"github.com/scaleway/scaleway-cli/internal/core"
)

func Test_UserDataGet(t *testing.T) {
t.Run("Usage", core.Test(&core.TestConfig{
Commands: GetCommands(),
Cmd: "scw instance user-data get -h",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
}))

t.Run("Get an existing key", core.Test(&core.TestConfig{
BeforeFunc: func(ctx *core.BeforeFuncCtx) error {
ctx.Meta["Server"] = ctx.ExecuteCmd("scw instance server create image=ubuntu_bionic stopped")
ctx.ExecuteCmd("scw instance user-data set server-id={{.Server.ID}} key=happy content=true")
return nil
},
Commands: GetCommands(),
Cmd: "scw instance user-data get server-id={{.Server.ID}} key=happy",
AfterFunc: deleteVanillaServer,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
}))

t.Run("Get an nonexistent key", core.Test(&core.TestConfig{
BeforeFunc: createVanillaServer,
Commands: GetCommands(),
Cmd: "scw instance user-data get server-id={{.Server.ID}} key=happy",
AfterFunc: deleteVanillaServer,
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(1),
),
}))
}

func Test_UserDataSet(t *testing.T) {
t.Run("Usage", core.Test(&core.TestConfig{
Commands: GetCommands(),
Cmd: "scw instance user-data set -h",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
}))
}
Loading