Skip to content

Commit a8703f6

Browse files
committed
adding the delete sub-command tree logic, still missing the opensearch becuase the openapi does not support delete opensearch acl entry.
1 parent be8f14f commit a8703f6

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

cmd/dbaas_acl_delete.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,102 @@
11
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+
}

cmd/dbaas_acl_delete_kafka.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
v3 "github.com/exoscale/egoscale/v3"
8+
)
9+
10+
// deleteKafkaACL deletes a Kafka ACL entry for the specified username.
11+
func (c *dbaasAclDeleteCmd) deleteKafkaACL(ctx context.Context, client *v3.Client, serviceName, username string) error {
12+
// Fetch Kafka ACLs for the service
13+
acls, err := client.GetDBAASKafkaAclConfig(ctx, serviceName)
14+
if err != nil {
15+
return fmt.Errorf("error fetching Kafka ACL configuration: %w", err)
16+
}
17+
18+
// Find ACL entries for the given username and delete them
19+
var found bool
20+
for _, acl := range acls.TopicAcl {
21+
if acl.Username == username {
22+
found = true
23+
// Use the correct delete function to remove the topic ACL
24+
op, err := client.DeleteDBAASKafkaTopicAclConfig(ctx, serviceName, string(acl.ID))
25+
if err != nil {
26+
return fmt.Errorf("error deleting ACL entry %q for topic %q: %w", acl.ID, acl.Topic, err)
27+
}
28+
29+
// Wait for the operation to complete (if applicable)
30+
_, waitErr := client.Wait(ctx, op, v3.OperationStateSuccess)
31+
if waitErr != nil {
32+
return fmt.Errorf("error waiting for ACL deletion operation: %w", waitErr)
33+
}
34+
}
35+
}
36+
37+
if !found {
38+
return fmt.Errorf("no ACL entry found for username %q in service %q", username, serviceName)
39+
}
40+
41+
return nil
42+
}

cmd/dbaas_acl_delete_opensearch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package cmd

0 commit comments

Comments
 (0)