Skip to content

Commit dd783eb

Browse files
feat(compute): improve error message when instance not found in zone [sc-167368]
Add findInstance helper in cmd/compute/instance/instance_find.go wrapping FindListInstancesResponseInstances. On a not-found error it returns a zone-aware message with a hint to use -z or list all instances: instance "foo" not found in zone ch-gva-2 Hint: use -z <zone> to specify a different zone, or run 'exo compute instance list' to see instances across all zones All 24 call sites across the instance subcommands are migrated. Adds e2e scenario tests/e2e/scenarios/with-api/compute/instance_not_found_error.txtar.
1 parent 9ca3d94 commit dd783eb

27 files changed

Lines changed: 75 additions & 25 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
### Features
66

77
- sks: add `rotate-karpenter-credentials` command #797
8+
9+
### Improvements
10+
11+
- compute instance: enrich "not found" error with the zone searched and a hint to use -z #805
812
- sks: add `active-nodepool-templates` command #797
913

1014
### Bug fixes

cmd/compute/instance/instance_console_url.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (c *instanceConsoleURLCmd) CmdRun(cmd *cobra.Command, _ []string) error {
6060
return err
6161
}
6262

63-
foundInstance, err := resp.FindListInstancesResponseInstances(c.Instance)
63+
foundInstance, err := findInstance(resp, c.Instance, string(c.Zone))
6464
if err != nil {
6565
return err
6666
}

cmd/compute/instance/instance_delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (c *instanceDeleteCmd) CmdRun(_ *cobra.Command, _ []string) error {
5454

5555
instanceToDelete := []v3.UUID{}
5656
for _, i := range c.Instances {
57-
instance, err := instances.FindListInstancesResponseInstances(i)
57+
instance, err := findInstance(instances, i, c.Zone)
5858
if err != nil {
5959
if !c.Force {
6060
return err

cmd/compute/instance/instance_elastic_ip_attach.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ func (c *instanceEIPAttachCmd) CmdRun(_ *cobra.Command, _ []string) error {
5454
if err != nil {
5555
return err
5656
}
57-
instance, err := instancesList.FindListInstancesResponseInstances(c.Instance)
57+
instance, err := findInstance(instancesList, c.Instance, c.Zone)
5858
if err != nil {
59-
return fmt.Errorf("error retrieving Instance: %w", err)
59+
return err
6060
}
6161

6262
elasticIPs, err := client.ListElasticIPS(ctx)

cmd/compute/instance/instance_elastic_ip_detach.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (c *instanceEIPDetachCmd) CmdRun(_ *cobra.Command, _ []string) error {
5454
if err != nil {
5555
return err
5656
}
57-
instance, err := instancesList.FindListInstancesResponseInstances(c.Instance)
57+
instance, err := findInstance(instancesList, c.Instance, c.Zone)
5858
if err != nil {
5959
return fmt.Errorf("error retrieving Instance: %w", err)
6060
}

cmd/compute/instance/instance_enable_tpm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (c *instanceEnableTPMCmd) CmdRun(_ *cobra.Command, _ []string) error {
4545
return err
4646
}
4747

48-
instance, err := resp.FindListInstancesResponseInstances(c.Instance)
48+
instance, err := findInstance(resp, c.Instance, string(c.Zone))
4949
if err != nil {
5050
return err
5151
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package instance
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
v3 "github.com/exoscale/egoscale/v3"
8+
)
9+
10+
// findInstance looks up an instance by name or ID from a ListInstancesResponse
11+
// and enriches the "not found" error with the zone that was searched,
12+
// reminding the user to use -z to target a different zone.
13+
func findInstance(resp *v3.ListInstancesResponse, nameOrID, zone string) (v3.ListInstancesResponseInstances, error) {
14+
instance, err := resp.FindListInstancesResponseInstances(nameOrID)
15+
if err != nil {
16+
if errors.Is(err, v3.ErrNotFound) {
17+
return v3.ListInstancesResponseInstances{}, fmt.Errorf(
18+
"instance %q not found in zone %s\nHint: use -z <zone> to specify a different zone, or run 'exo compute instance list' to see instances across all zones",
19+
nameOrID,
20+
zone,
21+
)
22+
}
23+
return v3.ListInstancesResponseInstances{}, err
24+
}
25+
return instance, nil
26+
}

cmd/compute/instance/instance_private_network_attach.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (c *instancePrivnetAttachCmd) CmdRun(_ *cobra.Command, _ []string) error {
5656
if err != nil {
5757
return err
5858
}
59-
instance, err := instances.FindListInstancesResponseInstances(c.Instance)
59+
instance, err := findInstance(instances, c.Instance, c.Zone)
6060
if err != nil {
6161
return err
6262
}

cmd/compute/instance/instance_private_network_detach.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (c *instancePrivnetDetachCmd) CmdRun(_ *cobra.Command, _ []string) error {
5454
if err != nil {
5555
return err
5656
}
57-
instance, err := instances.FindListInstancesResponseInstances(c.Instance)
57+
instance, err := findInstance(instances, c.Instance, c.Zone)
5858
if err != nil {
5959
return err
6060
}

cmd/compute/instance/instance_private_network_updateip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (c *instancePrivnetUpdateIPCmd) CmdRun(_ *cobra.Command, _ []string) error
5757
if err != nil {
5858
return err
5959
}
60-
instance, err := instances.FindListInstancesResponseInstances(c.Instance)
60+
instance, err := findInstance(instances, c.Instance, c.Zone)
6161
if err != nil {
6262
return err
6363
}

0 commit comments

Comments
 (0)