Skip to content

Commit

Permalink
adding the update sub-command tree logic, still missing the kafka bec…
Browse files Browse the repository at this point in the history
…ause the openapi does not support update for opensearch acl entry.
  • Loading branch information
elkezza committed Jan 2, 2025
1 parent a8703f6 commit 7884c91
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 5 deletions.
6 changes: 2 additions & 4 deletions cmd/dbaas_acl_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func (c *dbaasAclDeleteCmd) cmdRun(cmd *cobra.Command, args []string) error {
// Iterate through zones to find the service
var found bool
var serviceZone string
var dbType v3.DBAASDatabaseName // Use DBAASDatabaseName for consistency
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) // Save the type for validation
dbType = v3.DBAASDatabaseName(db.Type)
found = true
serviceZone = string(zone.Name)
break
Expand All @@ -81,8 +81,6 @@ func (c *dbaasAclDeleteCmd) cmdRun(cmd *cobra.Command, args []string) error {
switch dbType {
case "kafka":
err = c.deleteKafkaACL(ctx, client, c.Name, c.Username)
case "opensearch": //TODO
//err = c.deleteOpenSearchACL(ctx, client, c.Name, c.Username)
default:
return fmt.Errorf("deleting ACL unsupported for service type %q", dbType)
}
Expand Down
1 change: 0 additions & 1 deletion cmd/dbaas_acl_delete_opensearch.go

This file was deleted.

92 changes: 92 additions & 0 deletions cmd/dbaas_acl_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package cmd

import (
"context"
"fmt"
"github.com/exoscale/cli/pkg/globalstate"
v3 "github.com/exoscale/egoscale/v3"
"github.com/spf13/cobra"
)

type dbaasAclUpdateCmd struct {
cliCommandSettings `cli-cmd:"-"`

_ bool `cli-cmd:"update"`
Name string `cli-flag:"name" cli-usage:"Name of the DBaaS service"`
Username string `cli-flag:"username" cli-usage:"Current username of the ACL entry to update"`
NewUsername string `cli-flag:"new-username" cli-usage:"New username to replace the current one (optional)"`
ServiceType string `cli-flag:"type" cli-short:"t" cli-usage:"Type of the DBaaS service (e.g., opensearch)"`
Index string `cli-flag:"index" cli-usage:"The index pattern for the ACL rule"`
Permission string `cli-flag:"permission" cli-usage:"Permission to apply (should be one of admin, read, readwrite, write, or deny (only for OpenSearch))"`
}

// Command aliases (none in this case)
func (c *dbaasAclUpdateCmd) cmdAliases() []string { return nil }

// Short description for the command
func (c *dbaasAclUpdateCmd) cmdShort() string {
return "Update an ACL entry for a DBaaS service"
}

// Long description for the command
func (c *dbaasAclUpdateCmd) cmdLong() string {
return `This command updates an ACL entry for a specified DBaaS service. You can also update the username with the --new-username flag.`
}

func (c *dbaasAclUpdateCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}

// Main run logic for showing ACL details
func (c *dbaasAclUpdateCmd) cmdRun(cmd *cobra.Command, args []string) error {
ctx := context.Background()

// Validate required flags
if c.Name == "" || c.Username == "" || c.ServiceType == "" {
return fmt.Errorf("both --name, --username, and --type flags must be specified")
}

// Fetch all available zones
zones, err := globalstate.EgoscaleV3Client.ListZones(ctx)
if err != nil {
return fmt.Errorf("error fetching zones: %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)
}

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

// Determine the appropriate update logic based on the service type
switch dbType {
case "opensearch":
return c.updateOpensearch(ctx, serviceZone, c.Name)
default:
return fmt.Errorf("update ACL unsupported for service type %q", dbType)
}
}

func init() {
cobra.CheckErr(registerCLICommand(dbaasAclCmd, &dbaasAclUpdateCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}
65 changes: 65 additions & 0 deletions cmd/dbaas_acl_update_opensearch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

import (
"context"
"fmt"

"github.com/exoscale/cli/pkg/globalstate"
v3 "github.com/exoscale/egoscale/v3"
)

func (c *dbaasAclUpdateCmd) updateOpensearch(ctx context.Context, zone, serviceName string) error {
client, err := switchClientZoneV3(ctx, globalstate.EgoscaleV3Client, v3.ZoneName(zone))
if err != nil {
return fmt.Errorf("error initializing client for zone %s: %w", zone, err)
}

aclsConfig, err := client.GetDBAASOpensearchAclConfig(ctx, serviceName)
if err != nil {
return fmt.Errorf("error fetching ACL configuration for service %q: %w", serviceName, err)
}

// Ensure ACL entry for the specified username exists
var updatedAcls []v3.DBAASOpensearchAclConfigAcls
var updatedEntry *v3.DBAASOpensearchAclConfigAcls
found := false

for _, acl := range aclsConfig.Acls {
if string(acl.Username) == c.Username {
found = true

// Update username if --new-username is provided
newUsername := c.Username
if c.NewUsername != "" {
newUsername = c.NewUsername
}

updatedEntry = &v3.DBAASOpensearchAclConfigAcls{
Username: v3.DBAASUserUsername(newUsername),
Rules: []v3.DBAASOpensearchAclConfigAclsRules{
{Index: c.Index, Permission: v3.EnumOpensearchRulePermission(c.Permission)},
},
}
} else {
updatedAcls = append(updatedAcls, acl)
}
}

if !found {
return fmt.Errorf("ACL entry for username %q not found in service %q", c.Username, serviceName)
}

if updatedEntry != nil {
updatedAcls = append(updatedAcls, *updatedEntry)
}

// Update the configuration
aclsConfig.Acls = updatedAcls
_, err = client.UpdateDBAASOpensearchAclConfig(ctx, serviceName, *aclsConfig)
if err != nil {
return fmt.Errorf("error updating ACL configuration for service %q: %w", serviceName, err)
}

fmt.Printf("ACL entry for username %q updated successfully in service %q\n", c.Username, serviceName)
return nil
}

0 comments on commit 7884c91

Please sign in to comment.