Skip to content

Commit

Permalink
feat: addon param can be a db type name
Browse files Browse the repository at this point in the history
  • Loading branch information
curzolapierre committed Jul 6, 2023
1 parent 7cf4f23 commit 11cf3e7
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 63 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 19 additions & 4 deletions cmd/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions cmd/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down Expand Up @@ -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"))
}
Expand Down Expand Up @@ -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")
Expand Down
16 changes: 12 additions & 4 deletions cmd/flags.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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() {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions cmd/log_drains.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (
return nil
}

addonName := addonNameFromFlags(c)
addonName := addonUUIDFromFlags(c, currentApp)

var err error
if addonName == "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/logs_archives.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
return nil
}

addonName := addonNameFromFlags(c)
addonName := addonUUIDFromFlags(c, currentApp)

var err error
if addonName == "" {
Expand Down
45 changes: 1 addition & 44 deletions db/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand All @@ -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)
}
52 changes: 52 additions & 0 deletions utils/addon_uuid.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 11cf3e7

Please sign in to comment.