Skip to content

Commit

Permalink
security-group: show instances in from all zones
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterp committed May 13, 2024
1 parent 4e8f353 commit 66c43ee
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 58 deletions.
3 changes: 2 additions & 1 deletion cmd/config_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/utils"
exo "github.com/exoscale/egoscale/v2"
)

Expand Down Expand Up @@ -120,7 +121,7 @@ func promptAccountInformation() (*account.Account, error) {
account.DefaultZone, err = chooseZone(client, nil)
if err != nil {
for {
defaultZone, err := chooseZone(globalstate.EgoscaleClient, allZones)
defaultZone, err := chooseZone(globalstate.EgoscaleClient, utils.AllZones)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/dbaas_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"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"
)

Expand Down Expand Up @@ -55,7 +56,7 @@ func (c *dbaasServiceListCmd) cmdRun(_ *cobra.Command, _ []string) error {
if c.Zone != "" {
zones = []string{c.Zone}
} else {
zones = allZones
zones = utils.AllZones
}

out := make(dbaasServiceListOutput, 0)
Expand All @@ -68,7 +69,7 @@ func (c *dbaasServiceListCmd) cmdRun(_ *cobra.Command, _ []string) error {
}
done <- struct{}{}
}()
err := forEachZone(zones, func(zone string) error {
err := utils.ForEachZone(zones, func(zone string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))

list, err := globalstate.EgoscaleClient.ListDatabaseServices(ctx, zone)
Expand Down
5 changes: 3 additions & 2 deletions cmd/deploy_target_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"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"
)

Expand Down Expand Up @@ -55,7 +56,7 @@ func (c *deployTargetListCmd) cmdRun(_ *cobra.Command, _ []string) error {
if c.Zone != "" {
zones = []string{c.Zone}
} else {
zones = allZones
zones = utils.AllZones
}

out := make(deployTargetListOutput, 0)
Expand All @@ -68,7 +69,7 @@ func (c *deployTargetListCmd) cmdRun(_ *cobra.Command, _ []string) error {
}
done <- struct{}{}
}()
err := forEachZone(zones, func(zone string) error {
err := utils.ForEachZone(zones, func(zone string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))

list, err := globalstate.EgoscaleClient.ListDeployTargets(ctx, zone)
Expand Down
5 changes: 3 additions & 2 deletions cmd/elastic_ip_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"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"
)

Expand Down Expand Up @@ -54,7 +55,7 @@ func (c *elasticIPListCmd) cmdRun(_ *cobra.Command, _ []string) error {
if c.Zone != "" {
zones = []string{c.Zone}
} else {
zones = allZones
zones = utils.AllZones
}

out := make(elasticIPListOutput, 0)
Expand All @@ -67,7 +68,7 @@ func (c *elasticIPListCmd) cmdRun(_ *cobra.Command, _ []string) error {
}
done <- struct{}{}
}()
err := forEachZone(zones, func(zone string) error {
err := utils.ForEachZone(zones, func(zone string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))

list, err := globalstate.EgoscaleClient.ListElasticIPs(ctx, zone)
Expand Down
4 changes: 2 additions & 2 deletions cmd/instance_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (c *instanceListCmd) cmdRun(_ *cobra.Command, _ []string) error {
if c.Zone != "" {
zones = []string{c.Zone}
} else {
zones = allZones
zones = utils.AllZones
}

out := make(instanceListOutput, 0)
Expand All @@ -79,7 +79,7 @@ func (c *instanceListCmd) cmdRun(_ *cobra.Command, _ []string) error {
}
done <- struct{}{}
}()
err := forEachZone(zones, func(zone string) error {
err := utils.ForEachZone(zones, func(zone string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))

list, err := globalstate.EgoscaleClient.ListInstances(ctx, zone)
Expand Down
5 changes: 3 additions & 2 deletions cmd/instance_pool_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"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"
)

Expand Down Expand Up @@ -56,7 +57,7 @@ func (c *instancePoolListCmd) cmdRun(_ *cobra.Command, _ []string) error {
if c.Zone != "" {
zones = []string{c.Zone}
} else {
zones = allZones
zones = utils.AllZones
}

out := make(instancePoolListOutput, 0)
Expand All @@ -69,7 +70,7 @@ func (c *instancePoolListCmd) cmdRun(_ *cobra.Command, _ []string) error {
}
done <- struct{}{}
}()
err := forEachZone(zones, func(zone string) error {
err := utils.ForEachZone(zones, func(zone string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))

list, err := globalstate.EgoscaleClient.ListInstancePools(ctx, zone)
Expand Down
5 changes: 3 additions & 2 deletions cmd/instance_snapshot_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/utils"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
)
Expand Down Expand Up @@ -58,7 +59,7 @@ func (c *instanceSnapshotListCmd) cmdRun(_ *cobra.Command, _ []string) error {
if c.Zone != "" {
zones = []string{c.Zone}
} else {
zones = allZones
zones = utils.AllZones
}

out := make(instanceSnapshotListOutput, 0)
Expand All @@ -73,7 +74,7 @@ func (c *instanceSnapshotListCmd) cmdRun(_ *cobra.Command, _ []string) error {
}
done <- struct{}{}
}()
err := forEachZone(zones, func(zone string) error {
err := utils.ForEachZone(zones, func(zone string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))

list, err := globalstate.EgoscaleClient.ListSnapshots(ctx, zone)
Expand Down
4 changes: 2 additions & 2 deletions cmd/nlb_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c *nlbListCmd) cmdRun(_ *cobra.Command, _ []string) error {
if c.Zone != "" {
zones = []string{c.Zone}
} else {
zones = allZones
zones = utils.AllZones
}

out := make(nlbListOutput, 0)
Expand All @@ -69,7 +69,7 @@ func (c *nlbListCmd) cmdRun(_ *cobra.Command, _ []string) error {
}
done <- struct{}{}
}()
err := forEachZone(zones, func(zone string) error {
err := utils.ForEachZone(zones, func(zone string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))

list, err := globalstate.EgoscaleClient.ListNetworkLoadBalancers(ctx, zone)
Expand Down
5 changes: 3 additions & 2 deletions cmd/private_network_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"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"
)

Expand Down Expand Up @@ -54,7 +55,7 @@ func (c *privateNetworkListCmd) cmdRun(_ *cobra.Command, _ []string) error {
if c.Zone != "" {
zones = []string{c.Zone}
} else {
zones = allZones
zones = utils.AllZones
}

out := make(privateNetworkListOutput, 0)
Expand All @@ -67,7 +68,7 @@ func (c *privateNetworkListCmd) cmdRun(_ *cobra.Command, _ []string) error {
}
done <- struct{}{}
}()
err := forEachZone(zones, func(zone string) error {
err := utils.ForEachZone(zones, func(zone string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))

list, err := globalstate.EgoscaleClient.ListPrivateNetworks(ctx, zone)
Expand Down
20 changes: 0 additions & 20 deletions cmd/request.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/security_group_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (c *securityGroupShowCmd) cmdRun(_ *cobra.Command, _ []string) error {
}
}

instances, err := utils.GetInstancesInSecurityGroup(ctx, globalstate.EgoscaleClient, *securityGroup.ID, zone)
instances, err := utils.GetInstancesInSecurityGroup(ctx, globalstate.EgoscaleClient, *securityGroup.ID)
if err != nil {
return fmt.Errorf("error retrieving instances in Security Group: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/sks_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"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"
)

Expand Down Expand Up @@ -54,7 +55,7 @@ func (c *sksListCmd) cmdRun(_ *cobra.Command, _ []string) error {
if c.Zone != "" {
zones = []string{c.Zone}
} else {
zones = allZones
zones = utils.AllZones
}

out := make(sksClusterListOutput, 0)
Expand All @@ -67,7 +68,7 @@ func (c *sksListCmd) cmdRun(_ *cobra.Command, _ []string) error {
}
done <- struct{}{}
}()
err := forEachZone(zones, func(zone string) error {
err := utils.ForEachZone(zones, func(zone string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))

list, err := globalstate.EgoscaleClient.ListSKSClusters(ctx, zone)
Expand Down
5 changes: 3 additions & 2 deletions cmd/sks_nodepool_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"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"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ func (c *sksNodepoolListCmd) cmdRun(_ *cobra.Command, _ []string) error {
if c.Zone != "" {
zones = []string{c.Zone}
} else {
zones = allZones
zones = utils.AllZones
}

out := make(sksNodepoolListOutput, 0)
Expand All @@ -70,7 +71,7 @@ func (c *sksNodepoolListCmd) cmdRun(_ *cobra.Command, _ []string) error {
}
done <- struct{}{}
}()
err := forEachZone(zones, func(zone string) error {
err := utils.ForEachZone(zones, func(zone string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))

list, err := globalstate.EgoscaleClient.ListSKSClusters(ctx, zone)
Expand Down
14 changes: 0 additions & 14 deletions cmd/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,6 @@ import (
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/egoscale/v2/oapi"
)

var (
// allZones represents the list of known Exoscale zones, in case we need it without performing API lookup.
allZones = []string{
string(oapi.ZoneNameAtVie1),
string(oapi.ZoneNameAtVie2),
string(oapi.ZoneNameBgSof1),
string(oapi.ZoneNameChDk2),
string(oapi.ZoneNameChGva2),
string(oapi.ZoneNameDeFra1),
string(oapi.ZoneNameDeMuc1),
}
)

type zoneListItemOutput struct {
Expand Down
50 changes: 48 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,26 @@ import (
"strconv"
"strings"

"github.com/hashicorp/go-multierror"

exoapi "github.com/exoscale/egoscale/v2/api"

"github.com/exoscale/cli/pkg/account"
v2 "github.com/exoscale/egoscale/v2"
"github.com/exoscale/egoscale/v2/oapi"
)

var (
// utils.AllZones represents the list of known Exoscale zones, in case we need it without performing API lookup.
AllZones = []string{
string(oapi.ZoneNameAtVie1),
string(oapi.ZoneNameAtVie2),
string(oapi.ZoneNameBgSof1),
string(oapi.ZoneNameChDk2),
string(oapi.ZoneNameChGva2),
string(oapi.ZoneNameDeFra1),
string(oapi.ZoneNameDeMuc1),
}
)

// RandStringBytes Generate random string of n bytes
Expand All @@ -22,8 +41,20 @@ func RandStringBytes(n int) (string, error) {
return base64.StdEncoding.EncodeToString(b), nil
}

func GetInstancesInSecurityGroup(ctx context.Context, client *v2.Client, securityGroupID, zone string) ([]*v2.Instance, error) {
allInstances, err := client.ListInstances(ctx, zone)
func GetInstancesInSecurityGroup(ctx context.Context, client *v2.Client, securityGroupID string) ([]*v2.Instance, error) {
allInstances := make([]*v2.Instance, 0)
err := ForEachZone(AllZones, func(zone string) error {
ctx := exoapi.WithEndpoint(ctx, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, zone))

instances, err := client.ListInstances(ctx, zone)
if err != nil {
return err
}

allInstances = append(allInstances, instances...)

return nil
})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -190,3 +221,18 @@ func VersionIsNewer(old, new string) bool {
func VersionsAreEquivalent(a, b string) bool {
return (VersionMajor(b) == VersionMajor(a) && VersionMinor(b) == VersionMinor(a))
}

// ForEachZone executes the function f for each specified zone, and return a multierror.Error containing all
// errors that may have occurred during execution.
func ForEachZone(zones []string, f func(zone string) error) error {
meg := new(multierror.Group)

for _, zone := range zones {
zone := zone
meg.Go(func() error {
return f(zone)
})
}

return meg.Wait().ErrorOrNil()
}

0 comments on commit 66c43ee

Please sign in to comment.