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

fix(instance): ssh-config-install invalid zone or all #3272

Merged
merged 2 commits into from
Jul 10, 2023
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,7 @@ USAGE:

ARGS:
[project-id] Project ID to use. If none is passed the default project ID will be used
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config (fr-par-1 | fr-par-2 | fr-par-3 | nl-ams-1 | nl-ams-2 | nl-ams-3 | pl-waw-1 | pl-waw-2)
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config (fr-par-1 | fr-par-2 | fr-par-3 | nl-ams-1 | nl-ams-2 | nl-ams-3 | pl-waw-1 | pl-waw-2 | all)

FLAGS:
-h, --help help for install-config
Expand Down
2 changes: 1 addition & 1 deletion docs/commands/instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -2655,7 +2655,7 @@ scw instance ssh install-config [arg=value ...]
| Name | | Description |
|------|---|-------------|
| project-id | | Project ID to use. If none is passed the default project ID will be used |
| zone | Default: `fr-par-1`<br />One of: `fr-par-1`, `fr-par-2`, `fr-par-3`, `nl-ams-1`, `nl-ams-2`, `nl-ams-3`, `pl-waw-1`, `pl-waw-2` | Zone to target. If none is passed will use default zone from the config |
| zone | Default: `fr-par-1`<br />One of: `fr-par-1`, `fr-par-2`, `fr-par-3`, `nl-ams-1`, `nl-ams-2`, `nl-ams-3`, `pl-waw-1`, `pl-waw-2`, `all` | Zone to target. If none is passed will use default zone from the config |



Expand Down
35 changes: 24 additions & 11 deletions internal/namespaces/instance/v1/custom_ssh_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"reflect"
"strings"

"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
Expand Down Expand Up @@ -33,45 +34,48 @@ func (s sshConfigServer) InPrivateNetwork(id string) bool {
return false
}

type sshConfigRequest struct {
type sshConfigInstallRequest struct {
Zone scw.Zone
ProjectID *string
}

func sshConfigInstallCommand() *core.Command {
availableZones := ((*instance.API)(nil)).Zones()
availableZones = append(availableZones, scw.Zone(core.AllLocalities))

return &core.Command{
Namespace: "instance",
Resource: "ssh",
Verb: "install-config",
Short: `Install a ssh config with all your servers as host
It generate hosts for instance servers, baremetal, apple-silicon and bastions`,
Long: "Path of the config will be $HOME/.ssh/scaleway.config",
ArgsType: reflect.TypeOf(sshConfigRequest{}),
ArgsType: reflect.TypeOf(sshConfigInstallRequest{}),
ArgSpecs: core.ArgSpecs{
core.ProjectIDArgSpec(),
core.ZoneArgSpec(((*instance.API)(nil)).Zones()...),
core.ZoneArgSpec(availableZones...),
},
Run: func(ctx context.Context, argsI interface{}) (interface{}, error) {
args := argsI.(*sshConfigRequest)
args := argsI.(*sshConfigInstallRequest)
homeDir := core.ExtractUserHomeDir(ctx)

// Start server list with instances
servers, err := sshConfigListServers(ctx, args)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to list instance servers: %w", err)
}

// Add baremetal servers
baremetalServers, err := sshConfigListBaremetalServers(ctx, args)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to list baremetal servers: %w", err)
}
servers = append(servers, baremetalServers...)

// Add Apple-Silicon servers
siliconServers, err := sshConfigListAppleSiliconServers(ctx, args)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to list apple-silicon servers: %w", err)
}
servers = append(servers, siliconServers...)

Expand Down Expand Up @@ -150,12 +154,13 @@ Do you want the include statement to be added at the beginning of your file ?`,
}
}

func sshConfigListServers(ctx context.Context, args *sshConfigRequest) ([]sshConfigServer, error) {
func sshConfigListServers(ctx context.Context, args *sshConfigInstallRequest) ([]sshConfigServer, error) {
instanceAPI := instance.NewAPI(core.ExtractClient(ctx))

reqOpts := []scw.RequestOption{scw.WithAllPages()}
if args.Zone == scw.Zone(core.AllLocalities) {
reqOpts = append(reqOpts, scw.WithZones(instanceAPI.Zones()...))
args.Zone = ""
}

listServers, err := instanceAPI.ListServers(&instance.ListServersRequest{
Expand Down Expand Up @@ -189,20 +194,24 @@ func sshConfigListServers(ctx context.Context, args *sshConfigRequest) ([]sshCon
return servers, nil
}

func sshConfigListBaremetalServers(ctx context.Context, args *sshConfigRequest) ([]sshConfigServer, error) {
func sshConfigListBaremetalServers(ctx context.Context, args *sshConfigInstallRequest) ([]sshConfigServer, error) {
baremetalAPI := baremetal.NewAPI(core.ExtractClient(ctx))
baremetalPNAPI := baremetal.NewPrivateNetworkAPI(core.ExtractClient(ctx))

reqOpts := []scw.RequestOption{scw.WithAllPages()}
if args.Zone == scw.Zone(core.AllLocalities) {
reqOpts = append(reqOpts, scw.WithZones(baremetalAPI.Zones()...))
args.Zone = ""
}

listServers, err := baremetalAPI.ListServers(&baremetal.ListServersRequest{
Zone: args.Zone,
ProjectID: args.ProjectID,
}, reqOpts...)
if err != nil {
if strings.Contains(err.Error(), "unknown service") {
return nil, nil
}
// TODO: check permissions and print warning
return nil, err
}
Expand Down Expand Up @@ -239,19 +248,23 @@ func sshConfigListBaremetalServers(ctx context.Context, args *sshConfigRequest)
return servers, nil
}

func sshConfigListAppleSiliconServers(ctx context.Context, args *sshConfigRequest) ([]sshConfigServer, error) {
func sshConfigListAppleSiliconServers(ctx context.Context, args *sshConfigInstallRequest) ([]sshConfigServer, error) {
siliconAPI := applesilicon.NewAPI(core.ExtractClient(ctx))

reqOpts := []scw.RequestOption{scw.WithAllPages()}
if args.Zone == scw.Zone(core.AllLocalities) {
reqOpts = append(reqOpts, scw.WithZones(siliconAPI.Zones()...))
args.Zone = ""
}

listServers, err := siliconAPI.ListServers(&applesilicon.ListServersRequest{
Zone: args.Zone,
ProjectID: args.ProjectID,
}, reqOpts...)
if err != nil {
if strings.Contains(err.Error(), "unknown service") {
return nil, nil
}
return nil, err
}

Expand All @@ -267,7 +280,7 @@ func sshConfigListAppleSiliconServers(ctx context.Context, args *sshConfigReques
return servers, nil
}

func sshConfigBastionHosts(ctx context.Context, args *sshConfigRequest, servers []sshConfigServer) ([]sshconfig.Host, error) {
func sshConfigBastionHosts(ctx context.Context, args *sshConfigInstallRequest, servers []sshConfigServer) ([]sshconfig.Host, error) {
gwAPI := vpcgw.NewAPI(core.ExtractClient(ctx))

reqOpts := []scw.RequestOption{scw.WithAllPages()}
Expand Down