Skip to content

Commit ba63b42

Browse files
committed
do not switch client on listing entities per zone
1 parent 842b57f commit ba63b42

File tree

10 files changed

+132
-210
lines changed

10 files changed

+132
-210
lines changed

cmd/compute/deploy_target/deploy_target_list.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import (
1515
)
1616

1717
type deployTargetListItemOutput struct {
18-
Zone string `json:"zone"`
19-
ID string `json:"id"`
20-
Name string `json:"name"`
21-
Type string `json:"type"`
18+
Zone v3.ZoneName `json:"zone"`
19+
ID v3.UUID `json:"id"`
20+
Name string `json:"name"`
21+
Type string `json:"type"`
2222
}
2323

2424
type deployTargetListOutput []deployTargetListItemOutput
@@ -32,7 +32,7 @@ type deployTargetListCmd struct {
3232

3333
_ bool `cli-cmd:"list"`
3434

35-
Zone string `cli-short:"z" cli-usage:"zone to filter results to"`
35+
Zone v3.ZoneName `cli-short:"z" cli-usage:"zone to filter results to"`
3636
}
3737

3838
func (c *deployTargetListCmd) CmdAliases() []string { return nil }
@@ -51,20 +51,21 @@ func (c *deployTargetListCmd) CmdPreRun(cmd *cobra.Command, args []string) error
5151
}
5252

5353
func (c *deployTargetListCmd) CmdRun(_ *cobra.Command, _ []string) error {
54-
var zones []v3.ZoneName
54+
client := globalstate.EgoscaleV3Client
5555
ctx := exocmd.GContext
5656

57+
resp, err := client.ListZones(ctx)
58+
if err != nil {
59+
return err
60+
}
61+
zones := resp.Zones
62+
5763
if c.Zone != "" {
58-
zones = []v3.ZoneName{v3.ZoneName(c.Zone)}
59-
} else {
60-
client, err := exocmd.SwitchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(c.Zone))
61-
if err != nil {
62-
return err
63-
}
64-
zones, err = utils.AllZonesV3(ctx, client)
64+
endpoint, err := client.GetZoneAPIEndpoint(ctx, c.Zone)
6565
if err != nil {
6666
return err
6767
}
68+
zones = []v3.Zone{{APIEndpoint: endpoint}}
6869
}
6970

7071
out := make(deployTargetListOutput, 0)
@@ -77,24 +78,20 @@ func (c *deployTargetListCmd) CmdRun(_ *cobra.Command, _ []string) error {
7778
}
7879
done <- struct{}{}
7980
}()
80-
err := utils.ForEachZone(zones, func(zone v3.ZoneName) error {
81-
82-
client, err := exocmd.SwitchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(zone))
83-
if err != nil {
84-
return err
85-
}
8681

87-
list, err := client.ListDeployTargets(ctx)
82+
err = utils.ForEveryZone(zones, func(zone v3.Zone) error {
83+
c := client.WithEndpoint(zone.APIEndpoint)
84+
list, err := c.ListDeployTargets(ctx)
8885
if err != nil {
8986
return fmt.Errorf("unable to list Deploy Targets in zone %s: %w", zone, err)
9087
}
9188

9289
for _, dt := range list.DeployTargets {
9390
res <- deployTargetListItemOutput{
94-
ID: dt.ID.String(),
91+
ID: dt.ID,
9592
Name: dt.Name,
9693
Type: string(dt.Type),
97-
Zone: string(zone),
94+
Zone: zone.Name,
9895
}
9996
}
10097

cmd/compute/elastic_ip/elastic_ip_list.go

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import (
1515
)
1616

1717
type elasticIPListItemOutput struct {
18-
ID string `json:"id"`
19-
IPAddress string `json:"ip_address"`
20-
Zone string `json:"zone"`
18+
ID v3.UUID `json:"id"`
19+
IPAddress string `json:"ip_address"`
20+
Zone v3.ZoneName `json:"zone"`
2121
}
2222

2323
type elasticIPListOutput []elasticIPListItemOutput
@@ -31,7 +31,7 @@ type elasticIPListCmd struct {
3131

3232
_ bool `cli-cmd:"list"`
3333

34-
Zone string `cli-short:"z" cli-usage:"zone to filter results to"`
34+
Zone v3.ZoneName `cli-short:"z" cli-usage:"zone to filter results to"`
3535
}
3636

3737
func (c *elasticIPListCmd) CmdAliases() []string { return exocmd.GListAlias }
@@ -50,20 +50,12 @@ func (c *elasticIPListCmd) CmdPreRun(cmd *cobra.Command, args []string) error {
5050
}
5151

5252
func (c *elasticIPListCmd) CmdRun(_ *cobra.Command, _ []string) error {
53-
var zones []v3.ZoneName
53+
client := globalstate.EgoscaleV3Client
5454
ctx := exocmd.GContext
5555

56-
if c.Zone != "" {
57-
zones = []v3.ZoneName{v3.ZoneName(c.Zone)}
58-
} else {
59-
client, err := exocmd.SwitchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(c.Zone))
60-
if err != nil {
61-
return err
62-
}
63-
zones, err = utils.AllZonesV3(ctx, client)
64-
if err != nil {
65-
return err
66-
}
56+
zones, err := utils.AllZonesV3(ctx, client, c.Zone)
57+
if err != nil {
58+
return err
6759
}
6860

6961
out := make(elasticIPListOutput, 0)
@@ -76,23 +68,20 @@ func (c *elasticIPListCmd) CmdRun(_ *cobra.Command, _ []string) error {
7668
}
7769
done <- struct{}{}
7870
}()
79-
err := utils.ForEachZone(zones, func(zone v3.ZoneName) error {
80-
client, err := exocmd.SwitchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(zone))
81-
if err != nil {
82-
return err
83-
}
71+
err = utils.ForEveryZone(zones, func(zone v3.Zone) error {
72+
c := client.WithEndpoint(zone.APIEndpoint)
73+
list, err := c.ListElasticIPS(ctx)
8474

85-
list, err := client.ListElasticIPS(ctx)
8675
if err != nil {
8776
return fmt.Errorf("unable to list Elastic IP addresses in zone %s: %w", zone, err)
8877
}
8978

9079
if list != nil {
9180
for _, e := range list.ElasticIPS {
9281
res <- elasticIPListItemOutput{
93-
ID: e.ID.String(),
82+
ID: e.ID,
9483
IPAddress: e.IP,
95-
Zone: string(zone),
84+
Zone: zone.Name,
9685
}
9786
}
9887

cmd/compute/instance/instance_list.go

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import (
1717
)
1818

1919
type instanceListItemOutput struct {
20-
ID string `json:"id"`
21-
Name string `json:"name"`
22-
Zone string `json:"zone"`
23-
Type string `json:"type"`
24-
IPAddress string `json:"ip_address"`
25-
IPv6Address string `json:"ipv6_address"`
26-
State string `json:"state"`
20+
ID v3.UUID `json:"id"`
21+
Name string `json:"name"`
22+
Zone v3.ZoneName `json:"zone"`
23+
Type string `json:"type"`
24+
IPAddress string `json:"ip_address"`
25+
IPv6Address string `json:"ipv6_address"`
26+
State string `json:"state"`
2727
}
2828

2929
type instanceListOutput []instanceListItemOutput
@@ -46,9 +46,9 @@ func (o *instanceListOutput) ToTable() {
4646

4747
for _, instance := range *o {
4848
t.Append([]string{
49-
instance.ID,
49+
string(instance.ID),
5050
instance.Name,
51-
instance.Zone,
51+
string(instance.Zone),
5252
instance.Type,
5353
instance.IPAddress,
5454
instance.IPv6Address,
@@ -62,7 +62,7 @@ type instanceListCmd struct {
6262

6363
_ bool `cli-cmd:"list"`
6464

65-
Zone string `cli-short:"z" cli-usage:"zone to filter results to"`
65+
Zone v3.ZoneName `cli-short:"z" cli-usage:"zone to filter results to"`
6666
}
6767

6868
func (c *instanceListCmd) CmdAliases() []string { return exocmd.GListAlias }
@@ -81,20 +81,12 @@ func (c *instanceListCmd) CmdPreRun(cmd *cobra.Command, args []string) error {
8181
}
8282

8383
func (c *instanceListCmd) CmdRun(_ *cobra.Command, _ []string) error {
84-
var zones []v3.ZoneName
84+
client := globalstate.EgoscaleV3Client
8585
ctx := exocmd.GContext
8686

87-
if c.Zone != "" {
88-
zones = []v3.ZoneName{v3.ZoneName(c.Zone)}
89-
} else {
90-
client, err := exocmd.SwitchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(c.Zone))
91-
if err != nil {
92-
return err
93-
}
94-
zones, err = utils.AllZonesV3(ctx, client)
95-
if err != nil {
96-
return err
97-
}
87+
zones, err := utils.AllZonesV3(ctx, client, c.Zone)
88+
if err != nil {
89+
return err
9890
}
9991

10092
out := make(instanceListOutput, 0)
@@ -109,14 +101,11 @@ func (c *instanceListCmd) CmdRun(_ *cobra.Command, _ []string) error {
109101
}
110102
done <- struct{}{}
111103
}()
112-
err := utils.ForEachZone(zones, func(zone v3.ZoneName) error {
113-
ctx := exocmd.GContext
114-
client, err := exocmd.SwitchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(zone))
115-
if err != nil {
116-
return err
117-
}
104+
err = utils.ForEveryZone(zones, func(zone v3.Zone) error {
105+
106+
c := client.WithEndpoint(zone.APIEndpoint)
107+
instances, err := c.ListInstances(ctx)
118108

119-
instances, err := client.ListInstances(ctx)
120109
if err != nil {
121110
return err
122111
}
@@ -138,9 +127,9 @@ func (c *instanceListCmd) CmdRun(_ *cobra.Command, _ []string) error {
138127
}
139128

140129
res <- instanceListItemOutput{
141-
ID: i.ID.String(),
130+
ID: i.ID,
142131
Name: i.Name,
143-
Zone: string(zone),
132+
Zone: zone.Name,
144133
Type: fmt.Sprintf("%s.%s", instanceType.Family, instanceType.Size),
145134
IPAddress: utils.DefaultIP(&i.PublicIP, utils.EmptyIPAddressVisualization),
146135
IPv6Address: utils.DefaultIP(i.Ipv6Address, utils.EmptyIPAddressVisualization),
@@ -150,6 +139,7 @@ func (c *instanceListCmd) CmdRun(_ *cobra.Command, _ []string) error {
150139

151140
return nil
152141
})
142+
153143
if err != nil {
154144
_, _ = fmt.Fprintf(os.Stderr,
155145
"warning: errors during listing, results might be incomplete.\n%s\n", err) // nolint:golint

cmd/compute/instance/instance_snapshot_list.go

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import (
1616
)
1717

1818
type instanceSnapshotListItemOutput struct {
19-
ID string `json:"id"`
20-
CreationDate string `json:"creation_date"`
21-
Instance string `json:"instance"`
22-
State string `json:"state"`
23-
Zone string `json:"zone"`
19+
ID v3.UUID `json:"id"`
20+
CreationDate string `json:"creation_date"`
21+
Instance string `json:"instance"`
22+
State string `json:"state"`
23+
Zone v3.ZoneName `json:"zone"`
2424
}
2525

2626
type instanceSnapshotListOutput []instanceSnapshotListItemOutput
@@ -34,7 +34,7 @@ type instanceSnapshotListCmd struct {
3434

3535
_ bool `cli-cmd:"list"`
3636

37-
Zone string `cli-short:"z" cli-usage:"zone to filter results to"`
37+
Zone v3.ZoneName `cli-short:"z" cli-usage:"zone to filter results to"`
3838
}
3939

4040
func (c *instanceSnapshotListCmd) CmdAliases() []string { return nil }
@@ -53,20 +53,12 @@ func (c *instanceSnapshotListCmd) CmdPreRun(cmd *cobra.Command, args []string) e
5353
}
5454

5555
func (c *instanceSnapshotListCmd) CmdRun(_ *cobra.Command, _ []string) error {
56-
var zones []v3.ZoneName
56+
client := globalstate.EgoscaleV3Client
5757
ctx := exocmd.GContext
5858

59-
if c.Zone != "" {
60-
zones = []v3.ZoneName{v3.ZoneName(c.Zone)}
61-
} else {
62-
client, err := exocmd.SwitchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(c.Zone))
63-
if err != nil {
64-
return err
65-
}
66-
zones, err = utils.AllZonesV3(ctx, client)
67-
if err != nil {
68-
return err
69-
}
59+
zones, err := utils.AllZonesV3(ctx, client, c.Zone)
60+
if err != nil {
61+
return err
7062
}
7163

7264
out := make(instanceSnapshotListOutput, 0)
@@ -81,14 +73,9 @@ func (c *instanceSnapshotListCmd) CmdRun(_ *cobra.Command, _ []string) error {
8173
}
8274
done <- struct{}{}
8375
}()
84-
err := utils.ForEachZone(zones, func(zone v3.ZoneName) error {
85-
ctx := exocmd.GContext
86-
client, err := exocmd.SwitchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(zone))
87-
if err != nil {
88-
return err
89-
}
90-
91-
snapshots, err := client.ListSnapshots(ctx)
76+
err = utils.ForEveryZone(zones, func(zone v3.Zone) error {
77+
c := client.WithEndpoint(zone.APIEndpoint)
78+
snapshots, err := c.ListSnapshots(ctx)
9279
if err != nil {
9380
return err
9481
}
@@ -107,11 +94,11 @@ func (c *instanceSnapshotListCmd) CmdRun(_ *cobra.Command, _ []string) error {
10794
}
10895

10996
res <- instanceSnapshotListItemOutput{
110-
ID: s.ID.String(),
97+
ID: s.ID,
11198
CreationDate: s.CreatedAT.String(),
11299
Instance: instance.Name,
113100
State: string(s.State),
114-
Zone: string(zone),
101+
Zone: zone.Name,
115102
}
116103
}
117104

0 commit comments

Comments
 (0)