diff --git a/cmd/dbaas_acl_show.go b/cmd/dbaas_acl_show.go index 0289fae4..be71b487 100644 --- a/cmd/dbaas_acl_show.go +++ b/cmd/dbaas_acl_show.go @@ -2,6 +2,8 @@ package cmd import ( "fmt" + "github.com/exoscale/cli/pkg/globalstate" + v3 "github.com/exoscale/egoscale/v3" "os" "github.com/exoscale/cli/pkg/output" @@ -36,7 +38,6 @@ type dbaasAclShowCmd struct { Name string `cli-flag:"name" cli-usage:"Name of the DBaaS service"` Username string `cli-flag:"username" cli-usage:"Username of the ACL entry"` ServiceType string `cli-flag:"type" cli-short:"t" cli-usage:"type of the DBaaS service (e.g., kafka, opensearch)"` - Zone string `cli-flag:"zone" cli-short:"z" cli-usage:"Database Service zone"` } // Command aliases (none in this case) @@ -52,7 +53,6 @@ func (c *dbaasAclShowCmd) cmdLong() string { // Pre-run validation for required flags and default zone setting func (c *dbaasAclShowCmd) cmdPreRun(cmd *cobra.Command, args []string) error { - cmdSetZoneFlagFromDefault(cmd) // Set the default zone if not specified return cliCommandDefaultPreRun(c, cmd, args) // Run default validations } @@ -65,26 +65,32 @@ func (c *dbaasAclShowCmd) cmdRun(cmd *cobra.Command, args []string) error { return fmt.Errorf("both --name, --username and --type flags must be specified") } - // Fetch DBaaS service details - db, err := dbaasGetV3(ctx, c.Name, c.Zone) + // Search for the service in each zone + service, zone, err := FindServiceAcrossZones(ctx, globalstate.EgoscaleV3Client, c.Name) if err != nil { - return fmt.Errorf("error retrieving DBaaS service %q in zone %q: %w", c.Name, c.Zone, err) + return fmt.Errorf("error finding service: %w", err) } - // Validate that the service type matches the expected type - if string(db.Type) != c.ServiceType { - return fmt.Errorf("mismatched service type: expected %q but got %q for service %q", c.ServiceType, db.Type, c.Name) + // Switch client to the appropriate zone + client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(zone)) + if err != nil { + return fmt.Errorf("error initializing client for zone %s: %w", zone, err) + } + + // Validate the service type + if string(service.Type) != c.ServiceType { + return fmt.Errorf("service type mismatch: expected %q but got %q for service %q", c.ServiceType, service.Type, c.Name) } // Call the appropriate method based on the service type var output output.Outputter - switch db.Type { + switch service.Type { case "kafka": - output, err = c.showKafka(ctx, c.Name) + output, err = c.showKafka(ctx, client, c.Name) case "opensearch": - output, err = c.showOpensearch(ctx, c.Name) + output, err = c.showOpensearch(ctx, client, c.Name) default: - return fmt.Errorf("listing ACL unsupported for service of type %q", db.Type) + return fmt.Errorf("listing ACL unsupported for service of type %q", service.Type) } if err != nil { diff --git a/cmd/dbaas_acl_show_kafka.go b/cmd/dbaas_acl_show_kafka.go index c4ca8def..2f3310c7 100644 --- a/cmd/dbaas_acl_show_kafka.go +++ b/cmd/dbaas_acl_show_kafka.go @@ -3,17 +3,12 @@ package cmd import ( "context" "fmt" - "github.com/exoscale/cli/pkg/globalstate" "github.com/exoscale/cli/pkg/output" v3 "github.com/exoscale/egoscale/v3" ) // Fetch OpenSearch ACL configuration and process its details -func (c *dbaasAclShowCmd) showKafka(ctx context.Context, serviceName string) (output.Outputter, error) { - client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(c.Zone)) - if err != nil { - return nil, fmt.Errorf("error initializing client for zone %s: %w", c.Zone, err) - } +func (c *dbaasAclShowCmd) showKafka(ctx context.Context, client *v3.Client, serviceName string) (output.Outputter, error) { // Fetch Kafka ACLs for the specified service acls, err := client.GetDBAASKafkaAclConfig(ctx, serviceName) diff --git a/cmd/dbaas_acl_show_opensearch.go b/cmd/dbaas_acl_show_opensearch.go index 4a09c200..85fb6d84 100644 --- a/cmd/dbaas_acl_show_opensearch.go +++ b/cmd/dbaas_acl_show_opensearch.go @@ -5,7 +5,6 @@ import ( "fmt" "os" - "github.com/exoscale/cli/pkg/globalstate" "github.com/exoscale/cli/pkg/output" "github.com/exoscale/cli/table" v3 "github.com/exoscale/egoscale/v3" @@ -39,12 +38,7 @@ func (o *dbaasAclShowOpensearchOutput) ToTable() { } // Fetch OpenSearch ACL configuration and process its details -func (c *dbaasAclShowCmd) showOpensearch(ctx context.Context, serviceName string) (output.Outputter, error) { - // Switch to the appropriate client for the specified zone - client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(c.Zone)) - if err != nil { - return nil, fmt.Errorf("error initializing client for zone %s: %w", c.Zone, err) - } +func (c *dbaasAclShowCmd) showOpensearch(ctx context.Context, client *v3.Client, serviceName string) (output.Outputter, error) { // Fetch OpenSearch ACL configuration for the specified service aclsConfig, err := client.GetDBAASOpensearchAclConfig(ctx, serviceName)