From bb74bd18153a9dc325dc9018377134abbf21865b Mon Sep 17 00:00:00 2001 From: tgrondier <5384645+tgrondier@users.noreply.github.com> Date: Thu, 9 Jan 2025 10:55:42 +0100 Subject: [PATCH] security-group show: fix empty response if an API endpoint is misbehaving (#660) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description `exo compute security-group show` iterates through all zones to retrieve instances associated with it. If a single API endpoint happened to throw an error, the call itself would error out. This fix allows showing that security group while still showing an error ## Checklist (For exoscale contributors) * [x] Changelog updated (under *Unreleased* block) * [x] Testing ## Testing ``` ➜ ~/exo/cli git:(tgrondier/sc-114869/exo-c-sh-show-error-in-preprod) ✗ go run . c sg show edc0d129-2f94-4531-920e-7cf798439cd3 25-01-06 16:40 error while listing instances in security group: 1 error occurred: * Get "https://ppapi-at-vie-2.exoscale.com/v2/instance": dial tcp: lookup ppapi-at-vie-2.exoscale.com on 127.0.0.53:53: server misbehaving ┼──────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼ │ SECURITY GROUP │ │ ┼──────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼ │ ID │ edc0d129-2f94-4531-920e-7cf798439cd3 │ │ Name │ default │ │ Description │ Default Security Group │ │ Ingress Rules │ │ │ │ 08798387-0c7d-4601-a5ed-93367b889314 TCP 0.0.0.0/0 22 │ │ │ │ │ Egress Rules │ - │ │ External Sources │ - │ │ Instances │ │ │ │ VM-ec365352-f303-4fac-9cad-26e4a63b2227 ec365352-f303-4fac-9cad-26e4a63b2227 159.100.241.234 ch-gva-2 │ │ │ VM-79e75e58-34c8-4f99-8168-e37722eb97cc 79e75e58-34c8-4f99-8168-e37722eb97cc 85.217.161.242 ch-gva-2 │ │ │ VM-33e1fdda-d1d4-4fbd-8c7a-4e49971e3975 33e1fdda-d1d4-4fbd-8c7a-4e49971e3975 85.217.161.235 ch-gva-2 │ │ │ │ ┼──────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼ ``` --- CHANGELOG.md | 1 + utils/utils.go | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cbfcf0e2..cf72f49c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - instance update: fixing no err check after creating client #657 - fix(error): Improve error message on snapshot creation (#655) +- security-group show: fix empty response if an API endpoint is misbehaving #660 ## 1.82.0 diff --git a/utils/utils.go b/utils/utils.go index 0292bd68..6c500aad 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -4,6 +4,7 @@ import ( "context" "crypto/rand" "encoding/base64" + "errors" "fmt" "net" "strconv" @@ -49,15 +50,21 @@ func GetInstancesInSecurityGroup(ctx context.Context, client *v2.Client, securit instances, err := client.ListInstances(ctx, zone) if err != nil { - return err + if !errors.Is(err, exoapi.ErrNotFound) { + return err + } + } else { + allInstances = append(allInstances, instances...) } - allInstances = append(allInstances, instances...) - return nil }) if err != nil { - return nil, err + if allInstances == nil { + return nil, err + } else { + fmt.Printf("error while listing instances in security group: %s", err) + } } var instancesInSG []*v2.Instance