Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(baremetal): add option to add all ssh keys of an org during install #1016

Merged
merged 13 commits into from
May 27, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ ARGS:
server-id Server ID to install
os-id ID of the OS to install on the server
hostname Hostname of the server
ssh-key-ids.{index} SSH key IDs authorized on the server
[all-ssh-keys] Add all SSH keys on your baremetal instance (cannot be used with ssh-key-ids)
ssh-key-ids.{index} SSH key IDs authorized on the server (cannot be used with all-ssh-keys)
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config (fr-par-2)

FLAGS:
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200511151306-5e35a6b50424 h1:daVeC8IxL9EA3ASo38uwQm2nGcprn0L0XMb8Sj5DL4Y=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200511151306-5e35a6b50424/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/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
Expand Down
1 change: 1 addition & 0 deletions internal/core/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ func getHTTPRecoder(t *testing.T, update bool) (client *http.Client, cleanup fun
r.AddFilter(func(i *cassette.Interaction) error {
delete(i.Request.Headers, "x-auth-token")
delete(i.Request.Headers, "X-Auth-Token")
i.Request.URL = regexp.MustCompile("organization_id=[0-9a-f-]{36}").ReplaceAllString(i.Request.URL, "organization_id=11111111-1111-1111-1111-111111111111")
return nil
})

Expand Down
45 changes: 44 additions & 1 deletion internal/namespaces/baremetal/v1alpha1/custom_server_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,60 @@ package baremetal

import (
"context"
"reflect"

"github.com/scaleway/scaleway-cli/internal/core"
account "github.com/scaleway/scaleway-sdk-go/api/account/v2alpha1"
baremetal "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func serverInstallBuilder(c *core.Command) *core.Command {

type baremetalInstallServerRequestCustom struct {
baremetal.InstallServerRequest
AllSSHKeys *bool
}

c.ArgsType = reflect.TypeOf(baremetalInstallServerRequestCustom{})

c.ArgSpecs.AddBefore("ssh-key-ids.{index}", &core.ArgSpec{
Name: "all-ssh-keys",
Short: "Add all SSH keys on your baremetal instance (cannot be used with ssh-key-ids)",
OneOfGroup: "ssh",
})

c.ArgSpecs.GetByName("ssh-key-ids.{index}").OneOfGroup = "ssh"
c.ArgSpecs.GetByName("ssh-key-ids.{index}").Short = "SSH key IDs authorized on the server (cannot be used with all-ssh-keys)"

c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
tmpRequest := argsI.(*baremetalInstallServerRequestCustom)

// SSH keys management
if tmpRequest.AllSSHKeys != nil && *tmpRequest.AllSSHKeys {
client := core.ExtractClient(ctx)
accountapi := account.NewAPI(client)
orgId, _ := client.GetDefaultOrganizationID()
listKeys, err := accountapi.ListSSHKeys(&account.ListSSHKeysRequest{
OrganizationID: &orgId,
}, scw.WithAllPages())
if err != nil {
return nil, err
}
var keyIDs []string
for _, key := range listKeys.SSHKeys {
keyIDs = append(keyIDs, key.ID)
}
tmpRequest.SSHKeyIDs = keyIDs
}

return runner(ctx, &tmpRequest.InstallServerRequest)
}

c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
api := baremetal.NewAPI(core.ExtractClient(ctx))
return api.WaitForServerInstall(&baremetal.WaitForServerInstallRequest{
Zone: argsI.(*baremetal.InstallServerRequest).Zone,
Zone: argsI.(*baremetalInstallServerRequestCustom).Zone,
ServerID: respI.(*baremetal.Server).ID,
Timeout: serverActionTimeout,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,24 @@ func Test_InstallServer(t *testing.T) {
),
DefaultZone: scw.ZoneFrPar2,
}))

t.Run("All SSH keys", core.Test(&core.TestConfig{
Commands: cmds,
BeforeFunc: core.BeforeFuncCombine(
addSSH("key", sshKey),
createServerAndWait("Server"),
),
Cmd: "scw baremetal server install {{ .Server.ID }} hostname=test-install-server all-ssh-keys=true os-id=" + osID + " -w",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: core.AfterFuncCombine(
deleteSSH("key"),
deleteServer("Server"),
),
DefaultZone: scw.ZoneFrPar2,
}))
})

}
Loading