Skip to content

Commit f219062

Browse files
authoredJul 10, 2023
fix(instance): ssh-config-install invalid zone or all (#3272)
1 parent 3505820 commit f219062

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed
 

‎cmd/scw/testdata/test-all-usage-instance-ssh-install-config-usage.golden

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ USAGE:
77

88
ARGS:
99
[project-id] Project ID to use. If none is passed the default project ID will be used
10-
[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)
10+
[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)
1111

1212
FLAGS:
1313
-h, --help help for install-config

‎docs/commands/instance.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2655,7 +2655,7 @@ scw instance ssh install-config [arg=value ...]
26552655
| Name | | Description |
26562656
|------|---|-------------|
26572657
| project-id | | Project ID to use. If none is passed the default project ID will be used |
2658-
| 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 |
2658+
| 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 |
26592659

26602660

26612661

‎internal/namespaces/instance/v1/custom_ssh_config.go

+24-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"reflect"
7+
"strings"
78

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

36-
type sshConfigRequest struct {
37+
type sshConfigInstallRequest struct {
3738
Zone scw.Zone
3839
ProjectID *string
3940
}
4041

4142
func sshConfigInstallCommand() *core.Command {
43+
availableZones := ((*instance.API)(nil)).Zones()
44+
availableZones = append(availableZones, scw.Zone(core.AllLocalities))
45+
4246
return &core.Command{
4347
Namespace: "instance",
4448
Resource: "ssh",
4549
Verb: "install-config",
4650
Short: `Install a ssh config with all your servers as host
4751
It generate hosts for instance servers, baremetal, apple-silicon and bastions`,
4852
Long: "Path of the config will be $HOME/.ssh/scaleway.config",
49-
ArgsType: reflect.TypeOf(sshConfigRequest{}),
53+
ArgsType: reflect.TypeOf(sshConfigInstallRequest{}),
5054
ArgSpecs: core.ArgSpecs{
5155
core.ProjectIDArgSpec(),
52-
core.ZoneArgSpec(((*instance.API)(nil)).Zones()...),
56+
core.ZoneArgSpec(availableZones...),
5357
},
5458
Run: func(ctx context.Context, argsI interface{}) (interface{}, error) {
55-
args := argsI.(*sshConfigRequest)
59+
args := argsI.(*sshConfigInstallRequest)
5660
homeDir := core.ExtractUserHomeDir(ctx)
5761

5862
// Start server list with instances
5963
servers, err := sshConfigListServers(ctx, args)
6064
if err != nil {
61-
return nil, err
65+
return nil, fmt.Errorf("failed to list instance servers: %w", err)
6266
}
6367

6468
// Add baremetal servers
6569
baremetalServers, err := sshConfigListBaremetalServers(ctx, args)
6670
if err != nil {
67-
return nil, err
71+
return nil, fmt.Errorf("failed to list baremetal servers: %w", err)
6872
}
6973
servers = append(servers, baremetalServers...)
7074

7175
// Add Apple-Silicon servers
7276
siliconServers, err := sshConfigListAppleSiliconServers(ctx, args)
7377
if err != nil {
74-
return nil, err
78+
return nil, fmt.Errorf("failed to list apple-silicon servers: %w", err)
7579
}
7680
servers = append(servers, siliconServers...)
7781

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

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

156160
reqOpts := []scw.RequestOption{scw.WithAllPages()}
157161
if args.Zone == scw.Zone(core.AllLocalities) {
158162
reqOpts = append(reqOpts, scw.WithZones(instanceAPI.Zones()...))
163+
args.Zone = ""
159164
}
160165

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

192-
func sshConfigListBaremetalServers(ctx context.Context, args *sshConfigRequest) ([]sshConfigServer, error) {
197+
func sshConfigListBaremetalServers(ctx context.Context, args *sshConfigInstallRequest) ([]sshConfigServer, error) {
193198
baremetalAPI := baremetal.NewAPI(core.ExtractClient(ctx))
194199
baremetalPNAPI := baremetal.NewPrivateNetworkAPI(core.ExtractClient(ctx))
195200

196201
reqOpts := []scw.RequestOption{scw.WithAllPages()}
197202
if args.Zone == scw.Zone(core.AllLocalities) {
198203
reqOpts = append(reqOpts, scw.WithZones(baremetalAPI.Zones()...))
204+
args.Zone = ""
199205
}
200206

201207
listServers, err := baremetalAPI.ListServers(&baremetal.ListServersRequest{
202208
Zone: args.Zone,
203209
ProjectID: args.ProjectID,
204210
}, reqOpts...)
205211
if err != nil {
212+
if strings.Contains(err.Error(), "unknown service") {
213+
return nil, nil
214+
}
206215
// TODO: check permissions and print warning
207216
return nil, err
208217
}
@@ -239,19 +248,23 @@ func sshConfigListBaremetalServers(ctx context.Context, args *sshConfigRequest)
239248
return servers, nil
240249
}
241250

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

245254
reqOpts := []scw.RequestOption{scw.WithAllPages()}
246255
if args.Zone == scw.Zone(core.AllLocalities) {
247256
reqOpts = append(reqOpts, scw.WithZones(siliconAPI.Zones()...))
257+
args.Zone = ""
248258
}
249259

250260
listServers, err := siliconAPI.ListServers(&applesilicon.ListServersRequest{
251261
Zone: args.Zone,
252262
ProjectID: args.ProjectID,
253263
}, reqOpts...)
254264
if err != nil {
265+
if strings.Contains(err.Error(), "unknown service") {
266+
return nil, nil
267+
}
255268
return nil, err
256269
}
257270

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

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

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

0 commit comments

Comments
 (0)
Please sign in to comment.