Skip to content

Commit

Permalink
using FindServiceAcrossZones function in list sub-commands instead …
Browse files Browse the repository at this point in the history
…of repeating the same code block in each sub-command for acl management.
  • Loading branch information
elkezza committed Jan 2, 2025
1 parent 1dbcc11 commit 79bb079
Showing 1 changed file with 9 additions and 28 deletions.
37 changes: 9 additions & 28 deletions cmd/dbaas_acl_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,50 +43,31 @@ func (c *dbaasAclListCmd) cmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("both --name and --type flags must be specified")
}

// Fetch all available zones
zones, err := globalstate.EgoscaleV3Client.ListZones(ctx)
// Search for the service in each zone
service, zone, err := FindServiceAcrossZones(ctx, globalstate.EgoscaleV3Client, c.Name)
if err != nil {
return fmt.Errorf("error fetching zones: %w", err)
return fmt.Errorf("error finding service: %w", err)
}

// Iterate through zones to find the service
var found bool
var serviceZone string
var dbType v3.DBAASDatabaseName
for _, zone := range zones.Zones {
db, err := dbaasGetV3(ctx, c.Name, string(zone.Name))
if err == nil {
dbType = v3.DBAASDatabaseName(db.Type)
found = true
serviceZone = string(zone.Name)
break
}
}

// Handle case where service is not found in any zone
if !found {
return fmt.Errorf("service %q not found in any zone", c.Name)
}

client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(serviceZone))
client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(zone))
if err != nil {
return fmt.Errorf("error initializing client for zone %s: %w", serviceZone, err)
return fmt.Errorf("error initializing client for zone %s: %w", zone, err)
}

// Validate the service type
if string(dbType) != c.ServiceType {
return fmt.Errorf("mismatched service type: expected %q but got %q for service %q", c.ServiceType, dbType, c.Name)
if string(service.Type) != c.ServiceType {
return fmt.Errorf("mismatched service type: expected %q but got %q for service %q", c.ServiceType, service.Type, c.Name)
}

// Determine the appropriate listing logic based on the service type
var output output.Outputter
switch dbType {
switch service.Type {
case "kafka":
output, err = c.listKafkaACL(ctx, client, c.Name)
case "opensearch":
output, err = c.listOpenSearchACL(ctx, client, c.Name)
default:
return fmt.Errorf("listing ACL unsupported for service type %q", dbType)
return fmt.Errorf("listing ACL unsupported for service type %q", service.Type)
}

if err != nil {
Expand Down

0 comments on commit 79bb079

Please sign in to comment.