-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding the update sub-command tree logic, still missing the kafka bec…
…ause the openapi does not support update for opensearch acl entry.
- Loading branch information
Showing
4 changed files
with
159 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |