Skip to content

Commit

Permalink
list call to egoscale v3.
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Oct 29, 2024
1 parent a37aab5 commit 45a8128
Showing 1 changed file with 37 additions and 40 deletions.
77 changes: 37 additions & 40 deletions cmd/private_network_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ import (

"github.com/spf13/cobra"

"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/utils"
exoapi "github.com/exoscale/egoscale/v2/api"
v3 "github.com/exoscale/egoscale/v3"
)

type privateNetworkListItemOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Zone string `json:"zone"`
ID v3.UUID `json:"id"`
Name string `json:"name"`
Zone v3.ZoneName `json:"zone"`
}

type privateNetworkListOutput []privateNetworkListItemOutput
Expand All @@ -31,7 +29,7 @@ type privateNetworkListCmd struct {

_ bool `cli-cmd:"list"`

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

func (c *privateNetworkListCmd) cmdAliases() []string { return gListAlias }
Expand All @@ -50,50 +48,49 @@ func (c *privateNetworkListCmd) cmdPreRun(cmd *cobra.Command, args []string) err
}

func (c *privateNetworkListCmd) cmdRun(_ *cobra.Command, _ []string) error {
var zones []string
client := globalstate.EgoscaleV3Client
ctx := gContext

resp, err := client.ListZones(ctx)
if err != nil {
return err
}
zones := resp.Zones

if c.Zone != "" {
zones = []string{c.Zone}
} else {
zones = utils.AllZones
endpoint, err := client.GetZoneAPIEndpoint(ctx, c.Zone)
if err != nil {
return err
}

zones = []v3.Zone{{APIEndpoint: endpoint}}
}

out := make(privateNetworkListOutput, 0)
res := make(chan privateNetworkListItemOutput)
done := make(chan struct{})

go func() {
for nlb := range res {
out = append(out, nlb)
}
done <- struct{}{}
}()
err := utils.ForEachZone(zones, func(zone string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))
var responseError error
var errorZones []string

list, err := globalstate.EgoscaleClient.ListPrivateNetworks(ctx, zone)
if err != nil {
return fmt.Errorf("unable to list Private Networks in zone %s: %w", zone, err)
}
for _, zone := range zones {
c := client.WithEndpoint(zone.APIEndpoint)

for _, p := range list {
res <- privateNetworkListItemOutput{
ID: *p.ID,
Name: *p.Name,
Zone: zone,
resp, err := c.ListPrivateNetworks(ctx)
if err != nil {
responseError = err
errorZones = append(errorZones, string(zone.Name))
} else {
for _, p := range resp.PrivateNetworks {
out = append(out, privateNetworkListItemOutput{
ID: p.ID,
Name: p.Name,
Zone: zone.Name,
})
}
}

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

close(res)
<-done

if responseError != nil {
fmt.Fprintf(os.Stderr, "warning: error during listing private networks in %s zones, results might be incomplete:\n%s\n", errorZones, responseError) // nolint:golint
}
return c.outputFunc(&out, nil)
}

Expand Down

0 comments on commit 45a8128

Please sign in to comment.