diff --git a/CHANGELOG.md b/CHANGELOG.md index 77c09dee2..fc9b3b077 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### To be Released * chore(term): remove `github.com/andrew-d/go-termutil`, use standard library instead +* feat(cmd): addon can be retrieve from addon type, not only UUID ### 1.29.1 diff --git a/cmd/addons.go b/cmd/addons.go index a5db07deb..c49742c16 100644 --- a/cmd/addons.go +++ b/cmd/addons.go @@ -89,7 +89,12 @@ var ( utils.CheckForConsent(c.Context, currentApp) - err := addons.Destroy(c.Context, currentApp, c.Args().First()) + addonUUID, err := utils.GetAddonUUIDFromType(c.Context, currentApp, c.Args().First()) + if err != nil { + errorQuit(err) + } + + err = addons.Destroy(c.Context, currentApp, addonUUID) if err != nil { errorQuit(err) } @@ -119,7 +124,12 @@ var ( return cli.ShowCommandHelp(c, "addons-upgrade") } - err := addons.Upgrade(c.Context, currentApp, c.Args().First(), c.Args().Slice()[1]) + addonUUID, err := utils.GetAddonUUIDFromType(c.Context, currentApp, c.Args().First()) + if err != nil { + errorQuit(err) + } + + err = addons.Upgrade(c.Context, currentApp, addonUUID, c.Args().Slice()[1]) if err != nil { errorQuit(err) } @@ -147,9 +157,14 @@ var ( } currentApp := detect.CurrentApp(c) - currentAddon := c.Args().First() + addonName := c.Args().First() + + addonUUID, err := utils.GetAddonUUIDFromType(c.Context, currentApp, addonName) + if err != nil { + errorQuit(err) + } - err := addons.Info(c.Context, currentApp, currentAddon) + err = addons.Info(c.Context, currentApp, addonUUID) if err != nil { errorQuit(err) } diff --git a/cmd/backups.go b/cmd/backups.go index e8fafd62b..d650a7f96 100644 --- a/cmd/backups.go +++ b/cmd/backups.go @@ -23,7 +23,7 @@ var ( Action: func(c *cli.Context) error { currentApp := detect.CurrentApp(c) - addonName := addonNameFromFlags(c, true) + addonName := addonUUIDFromFlags(c, currentApp, true) err := db.ListBackups(c.Context, currentApp, addonName) if err != nil { @@ -45,7 +45,7 @@ var ( }.Render(), Action: func(c *cli.Context) error { currentApp := detect.CurrentApp(c) - addonName := addonNameFromFlags(c, true) + addonName := addonUUIDFromFlags(c, currentApp, true) utils.CheckForConsent(c.Context, currentApp, utils.ConsentTypeDBs) @@ -82,7 +82,7 @@ var ( }.Render(), Action: func(c *cli.Context) error { currentApp := detect.CurrentApp(c) - addonName := addonNameFromFlags(c, true) + addonName := addonUUIDFromFlags(c, currentApp, true) utils.CheckForConsent(c.Context, currentApp, utils.ConsentTypeDBs) diff --git a/cmd/databases.go b/cmd/databases.go index 41198fd9f..011094fc1 100644 --- a/cmd/databases.go +++ b/cmd/databases.go @@ -37,7 +37,7 @@ var ( Action: func(c *cli.Context) error { currentApp := detect.CurrentApp(c) utils.CheckForConsent(c.Context, currentApp, utils.ConsentTypeDBs) - addonName := addonNameFromFlags(c, true) + addonName := addonUUIDFromFlags(c, currentApp, true) if c.NArg() != 1 { errorQuit(errors.New("feature argument should be specified")) } @@ -67,7 +67,7 @@ var ( Action: func(c *cli.Context) error { currentApp := detect.CurrentApp(c) utils.CheckForConsent(c.Context, currentApp, utils.ConsentTypeDBs) - addonName := addonNameFromFlags(c, true) + addonName := addonUUIDFromFlags(c, currentApp, true) if c.NArg() != 1 { errorQuit(errors.New("feature argument should be specified")) } @@ -104,7 +104,7 @@ var ( Action: func(c *cli.Context) error { currentApp := detect.CurrentApp(c) utils.CheckForConsent(c.Context, currentApp, utils.ConsentTypeDBs) - addonName := addonNameFromFlags(c, true) + addonName := addonUUIDFromFlags(c, currentApp, true) params := scalingo.DatabaseUpdatePeriodicBackupsConfigParams{} scheduleAtFlag := c.String("schedule-at") diff --git a/cmd/flags.go b/cmd/flags.go index 78aa58c92..ab86b0834 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -1,11 +1,12 @@ package cmd import ( - "fmt" "os" "github.com/urfave/cli/v2" + "github.com/Scalingo/cli/io" + "github.com/Scalingo/cli/utils" "github.com/Scalingo/go-scalingo/v6/debug" ) @@ -24,7 +25,7 @@ var ( ) // exitIfMissing is optional. Set to true to show a message requesting for the --addon flag. -func addonNameFromFlags(c *cli.Context, exitIfMissing ...bool) string { +func addonUUIDFromFlags(c *cli.Context, app string, exitIfMissing ...bool) string { var addonName string for _, cliContext := range c.Lineage() { @@ -39,12 +40,19 @@ func addonNameFromFlags(c *cli.Context, exitIfMissing ...bool) string { } if addonName == "" && len(exitIfMissing) > 0 && exitIfMissing[0] { - fmt.Println("Unable to find the addon name, please use --addon flag.") + io.Error("Unable to find the addon name, please use --addon flag.") os.Exit(1) } + var addonUUID string + var err error + addonUUID, err = utils.GetAddonUUIDFromType(c.Context, app, addonName) + if err != nil { + io.Error("Unable to get the addon UUID based on its type:", err) + os.Exit(1) + } debug.Println("[ADDON] Addon name is", addonName) - return addonName + return addonUUID } func regionNameFromFlags(c *cli.Context) string { diff --git a/cmd/log_drains.go b/cmd/log_drains.go index a5784bd4a..b4a36a74c 100644 --- a/cmd/log_drains.go +++ b/cmd/log_drains.go @@ -43,7 +43,7 @@ Use the parameter "--with-addons" to list log drains of all addons connected to utils.CheckForConsent(c.Context, currentApp) - addonID := addonNameFromFlags(c) + addonID := addonUUIDFromFlags(c, currentApp) err := logdrains.List(c.Context, currentApp, logdrains.ListAddonOpts{ WithAddons: c.Bool("with-addons"), @@ -105,7 +105,7 @@ Warning: At the moment, only databases addons are able to forward logs to a drai Action: func(c *cli.Context) error { currentApp := detect.CurrentApp(c) - addonID := addonNameFromFlags(c) + addonID := addonUUIDFromFlags(c, currentApp) utils.CheckForConsent(c.Context, currentApp) @@ -174,7 +174,7 @@ Warning: At the moment, only databases addons are able to forward logs to a drai } drain := c.Args().First() - addonID := addonNameFromFlags(c) + addonID := addonUUIDFromFlags(c, currentApp) if addonID != "" && c.Bool("only-app") { cli.ShowCommandHelp(c, "log-drains-remove") diff --git a/cmd/logs.go b/cmd/logs.go index 7e00aa84e..46258ffd7 100644 --- a/cmd/logs.go +++ b/cmd/logs.go @@ -40,7 +40,7 @@ var ( return nil } - addonName := addonNameFromFlags(c) + addonName := addonUUIDFromFlags(c, currentApp) var err error if addonName == "" { diff --git a/cmd/logs_archives.go b/cmd/logs_archives.go index 609fd4418..69670d566 100644 --- a/cmd/logs_archives.go +++ b/cmd/logs_archives.go @@ -34,7 +34,7 @@ var ( return nil } - addonName := addonNameFromFlags(c) + addonName := addonUUIDFromFlags(c, currentApp) var err error if addonName == "" { diff --git a/db/logs.go b/db/logs.go index 127daee79..076383e66 100644 --- a/db/logs.go +++ b/db/logs.go @@ -2,13 +2,11 @@ package db import ( "context" - "strings" "gopkg.in/errgo.v1" "github.com/Scalingo/cli/config" "github.com/Scalingo/cli/logs" - "github.com/Scalingo/go-scalingo/v6" ) type LogsOpts struct { @@ -19,21 +17,12 @@ type LogsOpts struct { // Logs displays the addon logs. // app may be an app UUID or name. // addon may be a addon UUID or an addon type (e.g. MongoDB). -func Logs(ctx context.Context, app, addon string, opts LogsOpts) error { +func Logs(ctx context.Context, app, addonUUID string, opts LogsOpts) error { c, err := config.ScalingoClient(ctx) if err != nil { return errgo.Notef(err, "fail to get Scalingo client") } - addonUUID := addon - // If addon does not contain a UUID, we consider it contains an addon type (e.g. MongoDB) - if !strings.HasPrefix(addon, "ad-") { - addonUUID, err = getAddonUUIDFromType(ctx, c, app, addon) - if err != nil { - return errgo.Notef(err, "fail to get the addon UUID based on its type") - } - } - url, err := c.AddonLogsURL(ctx, app, addonUUID) if err != nil { return errgo.Notef(err, "fail to get log URL") @@ -52,35 +41,3 @@ func Logs(ctx context.Context, app, addon string, opts LogsOpts) error { } return nil } - -func getAddonUUIDFromType(ctx context.Context, addonsClient scalingo.AddonsService, app, addonType string) (string, error) { - aliases := map[string]string{ - "psql": "postgresql", - "pgsql": "postgresql", - "postgres": "postgresql", - - "mgo": "mongodb", - "mongo": "mongodb", - - "influx": "influxdb", - - "es": "elasticsearch", - } - addonTypeAlias, isAlias := aliases[addonType] - if isAlias { - addonType = addonTypeAlias - } - - addons, err := addonsClient.AddonsList(ctx, app) - if err != nil { - return "", errgo.Notef(err, "fail to list the addons to get the type UUID") - } - - for _, addon := range addons { - if strings.EqualFold(addonType, addon.AddonProvider.Name) { - return addon.ID, nil - } - } - - return "", errgo.Newf("no '%s' addon exists", addonType) -} diff --git a/utils/addon_uuid.go b/utils/addon_uuid.go new file mode 100644 index 000000000..1e092966a --- /dev/null +++ b/utils/addon_uuid.go @@ -0,0 +1,52 @@ +package utils + +import ( + "context" + "strings" + + "github.com/Scalingo/cli/config" + "github.com/Scalingo/go-utils/errors/v2" +) + +func GetAddonUUIDFromType(ctx context.Context, app, addonTypeOrUUID string) (string, error) { + // If addon does not contain a UUID, we consider it contains an addon type (e.g. MongoDB) + if strings.HasPrefix(addonTypeOrUUID, "ad-") { + return addonTypeOrUUID, nil + } + addonType := addonTypeOrUUID + + addonsClient, err := config.ScalingoClient(ctx) + if err != nil { + return "", errors.Wrapf(ctx, err, "unable to get Scalingo client") + } + + aliases := map[string]string{ + "psql": "postgresql", + "pgsql": "postgresql", + "postgres": "postgresql", + + "mgo": "mongodb", + "mongo": "mongodb", + + "influx": "influxdb", + + "es": "elasticsearch", + } + addonTypeFromAlias, isAlias := aliases[addonType] + if isAlias { + addonType = addonTypeFromAlias + } + + addons, err := addonsClient.AddonsList(ctx, app) + if err != nil { + return "", errors.Wrapf(ctx, err, "list the addons to get the type UUID") + } + + for _, addon := range addons { + if strings.EqualFold(addonType, addon.AddonProvider.Name) { + return addon.ID, nil + } + } + + return "", errors.Newf(ctx, "no '%s' addon exists", addonType) +}