|
1 | 1 | package cmd
|
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/exoscale/cli/pkg/globalstate" |
| 8 | + v3 "github.com/exoscale/egoscale/v3" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +type dbaasAclDeleteCmd struct { |
| 13 | + cliCommandSettings `cli-cmd:"-"` |
| 14 | + |
| 15 | + _ bool `cli-cmd:"delete"` |
| 16 | + Name string `cli-flag:"name" cli-usage:"Name of the DBaaS service"` |
| 17 | + ServiceType string `cli-flag:"type" cli-short:"t" cli-usage:"DBaaS service type (e.g., kafka, opensearch)"` |
| 18 | + Username string `cli-flag:"username" cli-usage:"Username of the ACL entry"` |
| 19 | +} |
| 20 | + |
| 21 | +// Command aliases (none in this case) |
| 22 | +func (c *dbaasAclDeleteCmd) cmdAliases() []string { return nil } |
| 23 | + |
| 24 | +// Short description for the command |
| 25 | +func (c *dbaasAclDeleteCmd) cmdShort() string { return "Delete an ACL entry for a DBaaS service" } |
| 26 | + |
| 27 | +// Long description for the command |
| 28 | +func (c *dbaasAclDeleteCmd) cmdLong() string { |
| 29 | + return `This command deletes a specified ACL entry for a DBaaS service, such as Kafka or OpenSearch, across all available zones.` |
| 30 | +} |
| 31 | + |
| 32 | +func (c *dbaasAclDeleteCmd) cmdPreRun(cmd *cobra.Command, args []string) error { |
| 33 | + return cliCommandDefaultPreRun(c, cmd, args) // Default validations |
| 34 | +} |
| 35 | + |
| 36 | +// Main run logic for showing ACL details |
| 37 | +func (c *dbaasAclDeleteCmd) cmdRun(cmd *cobra.Command, args []string) error { |
| 38 | + ctx := context.Background() |
| 39 | + |
| 40 | + // Validate required flags |
| 41 | + if c.Name == "" || c.ServiceType == "" || c.Username == "" { |
| 42 | + return fmt.Errorf("all flags --name, --type, and --username must be specified") |
| 43 | + } |
| 44 | + |
| 45 | + // Fetch all available zones |
| 46 | + zones, err := globalstate.EgoscaleV3Client.ListZones(ctx) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("error fetching zones: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + // Iterate through zones to find the service |
| 52 | + var found bool |
| 53 | + var serviceZone string |
| 54 | + var dbType v3.DBAASDatabaseName // Use DBAASDatabaseName for consistency |
| 55 | + for _, zone := range zones.Zones { |
| 56 | + db, err := dbaasGetV3(ctx, c.Name, string(zone.Name)) |
| 57 | + if err == nil { |
| 58 | + dbType = v3.DBAASDatabaseName(db.Type) // Save the type for validation |
| 59 | + found = true |
| 60 | + serviceZone = string(zone.Name) |
| 61 | + break |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Handle case where service is not found in any zone |
| 66 | + if !found { |
| 67 | + return fmt.Errorf("service %q not found in any zone", c.Name) |
| 68 | + } |
| 69 | + |
| 70 | + client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(serviceZone)) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("error initializing client for zone %s: %w", serviceZone, err) |
| 73 | + } |
| 74 | + |
| 75 | + // Validate the service type |
| 76 | + if string(dbType) != c.ServiceType { |
| 77 | + return fmt.Errorf("mismatched service type: expected %q but got %q for service %q", c.ServiceType, dbType, c.Name) |
| 78 | + } |
| 79 | + |
| 80 | + // Call the appropriate delete logic based on the service type |
| 81 | + switch dbType { |
| 82 | + case "kafka": |
| 83 | + err = c.deleteKafkaACL(ctx, client, c.Name, c.Username) |
| 84 | + case "opensearch": //TODO |
| 85 | + //err = c.deleteOpenSearchACL(ctx, client, c.Name, c.Username) |
| 86 | + default: |
| 87 | + return fmt.Errorf("deleting ACL unsupported for service type %q", dbType) |
| 88 | + } |
| 89 | + |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + cmd.Println(fmt.Sprintf("Successfully deleted ACL entry for username %q in service %q.", c.Username, c.Name)) |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func init() { |
| 99 | + cobra.CheckErr(registerCLICommand(dbaasAclCmd, &dbaasAclDeleteCmd{ |
| 100 | + cliCommandSettings: defaultCLICmdSettings(), |
| 101 | + })) |
| 102 | +} |
0 commit comments