diff --git a/CHANGELOG.md b/CHANGELOG.md index 32b49ac4d..53d3eda39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ### To be Released +* Remove a log drain from an addon + [#577](https://github.com/Scalingo/cli/pull/577) + [go-scalingo #175](https://github.com/Scalingo/go-scalingo/pull/175) * Add a log drain to an addon [#575](https://github.com/Scalingo/cli/pull/575) [go-scalingo #174](https://github.com/Scalingo/go-scalingo/pull/174) diff --git a/Gopkg.lock b/Gopkg.lock index 866085641..70047047b 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -26,7 +26,7 @@ revision = "8eb48cc6f27eafda8e3a9627edba494a1d229a01" [[projects]] - digest = "1:d58a2d159db515d94824ee99685cb65571e431bf988b638d31de4652866c6094" + digest = "1:dcd5c59297daac978a4a050a585c29b28c745df3d0d8f12e3d4d3689f7918580" name = "github.com/Scalingo/go-scalingo" packages = [ ".", @@ -37,8 +37,8 @@ "io", ] pruneopts = "NUT" - revision = "5af0bef7605dbe28f7c7eafd6cfc27263c9e16d8" - version = "v4.5.7" + revision = "fec0faece36ffe70f16bca9834b1894e833e78a2" + version = "v4.5.8" [[projects]] digest = "1:ab7f2d565135ccc3b35eb2427819fbca0501d582eba5091e684275022d3ff3ba" diff --git a/cmd/log_drains.go b/cmd/log_drains.go index 9e0cae4da..99c3a90f7 100644 --- a/cmd/log_drains.go +++ b/cmd/log_drains.go @@ -1,6 +1,9 @@ package cmd import ( + "fmt" + + "github.com/AlecAivazis/survey" "github.com/Scalingo/cli/appdetect" "github.com/Scalingo/cli/cmd/autocomplete" "github.com/Scalingo/cli/log_drains" @@ -19,8 +22,8 @@ var ( Usage: "List the log drains of an application", Description: `List all the log drains of an application: - Use the parameter: "--addon " to list log drains of a specific addon - Use the parameter: "--with-addons" to list log drains of all addons connected to the application + Use the parameter "--addon " to list log drains of a specific addon + Use the parameter "--with-addons" to list log drains of all addons connected to the application Examples: $ scalingo --app my-app log-drains @@ -28,7 +31,6 @@ var ( $ scalingo --app my-app log-drains --with-addons # See also commands 'log-drains-add', 'log-drains-remove'`, - Action: func(c *cli.Context) { currentApp := appdetect.CurrentApp(c) if len(c.Args()) != 0 { @@ -82,8 +84,8 @@ var ( Add a log drain to an addon: - Use the parameter: "--addon " to your add command to add a log drain to a specific addon - Use the parameter: "--with-addons" to list log drains of all addons connected to the application. + Use the parameter "--addon " to your add command to add a log drain to a specific addon + Use the parameter "--with-addons" to list log drains of all addons connected to the application. Warning: At the moment, only databases addons are able to throw logs. @@ -133,19 +135,67 @@ var ( logDrainsRemoveCommand = cli.Command{ Name: "log-drains-remove", Category: "Log drains", - Flags: []cli.Flag{appFlag}, - Usage: "Remove a log drain from an application", - Description: `Remove a log drain from an application: + Flags: []cli.Flag{ + appFlag, + addonFlag, + cli.BoolFlag{Name: "only-app", Usage: "remove the log drains for the application only"}, + }, + Usage: "Remove a log drain from an application and its associated addons", + Description: `Remove a log drain from an application and all its addons: + + $ scalingo --app my-app log-drains-remove syslog://custom.logstash.com:12345 + + Remove a log drain from a specific addon: + Use the parameter "--addon " to remove a log drain from a specific addon + + $ scalingo --app my-app --addon ad-3c2f8c81-99bd-4667-9791-466799bd4667 log-drains-remove syslog://custom.logstash.com:12345 - $ scalingo --app my-app log-drains-remove syslog://custom.logstash.com:12345 + Remove a log drain only for the application: + Use the parameter "--only-app" to remove a log drain only from the application + + $ scalingo --app my-app --only-app log-drains-remove syslog://custom.logstash.com:12345 # See also commands 'log-drains-add', 'log-drains'`, Action: func(c *cli.Context) { currentApp := appdetect.CurrentApp(c) + + var addonID string + if c.GlobalString("addon") != "" { + addonID = c.GlobalString("addon") + } else if c.String("addon") != "" { + addonID = c.String("addon") + } + + if addonID != "" && c.Bool("only-app") { + cli.ShowCommandHelp(c, "log-drains-remove") + return + } + + message := "This operation will delete the log drain " + c.Args()[0] + if addonID == "" && !c.Bool("only-app") { + // addons + app + message += " for the application and all its addons" + } else if addonID != "" && !c.Bool("only-app") { + // addon only + message += " for the addon " + addonID + } else { + // app only + message += " for the application " + currentApp + } + result := askContinue(message) + if !result { + fmt.Println("Aborted") + return + } + var err error if len(c.Args()) == 1 { - err = log_drains.Remove(currentApp, c.Args()[0]) + err = log_drains.Remove(currentApp, log_drains.RemoveAddonOpts{ + AddonID: addonID, + OnlyApp: c.Bool("only-app"), + URL: c.Args()[0], + }) } else { cli.ShowCommandHelp(c, "log-drains-remove") } @@ -160,3 +210,12 @@ var ( }, } ) + +func askContinue(message string) bool { + result := false + prompt := &survey.Confirm{ + Message: message + "\n\tConfirm deletion ?", + } + survey.AskOne(prompt, &result, nil) + return result +} diff --git a/log_drains/remove.go b/log_drains/remove.go index ef3db5fbd..deb4a07e9 100644 --- a/log_drains/remove.go +++ b/log_drains/remove.go @@ -6,17 +6,50 @@ import ( "gopkg.in/errgo.v1" ) -func Remove(app string, URL string) error { +type RemoveAddonOpts struct { + AddonID string + OnlyApp bool + URL string +} + +func Remove(app string, opts RemoveAddonOpts) error { c, err := config.ScalingoClient() if err != nil { return errgo.Notef(err, "fail to get Scalingo client to remove a log drain from the application") } - err = c.LogDrainRemove(app, URL) + if opts.AddonID != "" { + // addon only + err := c.LogDrainAddonRemove(app, opts.AddonID, opts.URL) + if err != nil { + return errgo.Notef(err, "fail to remove the log drain from the addon %s", opts.AddonID) + } + io.Status("The log drain", opts.URL, "has been deleted from the addon", opts.AddonID) + return nil + } + + err = c.LogDrainRemove(app, opts.URL) if err != nil { - return errgo.Notef(err, "fail to remove the log drain from the application") + io.Status("fail to remove the log drain from the application:", app, "\n\t", err) + } else { + io.Status("Log drain", opts.URL, "has been deleted from the application", app) + } + + if !opts.OnlyApp { + addons, err := c.AddonsList(app) + if err != nil { + return errgo.Notef(err, "fail to list addons to remove log drain") + } + + for _, addon := range addons { + err := c.LogDrainAddonRemove(app, addon.ID, opts.URL) + if err != nil { + io.Status("fail to remove the log drain from the addon:", addon.AddonProvider.Name, "\n\t", err) + } else { + io.Status("Log drain", opts.URL, "has been deleted from the addon", addon.AddonProvider.Name) + } + } } - io.Status("The log drain:", URL, "has been deleted") return nil } diff --git a/vendor/github.com/Scalingo/go-scalingo/log_drains.go b/vendor/github.com/Scalingo/go-scalingo/log_drains.go index 46fa941ee..3591922b0 100644 --- a/vendor/github.com/Scalingo/go-scalingo/log_drains.go +++ b/vendor/github.com/Scalingo/go-scalingo/log_drains.go @@ -11,6 +11,7 @@ type LogDrainsService interface { LogDrainsList(app string) ([]LogDrain, error) LogDrainAdd(app string, params LogDrainAddParams) (*LogDrainRes, error) LogDrainRemove(app, URL string) error + LogDrainAddonRemove(app, addonID string, URL string) error LogDrainsAddonList(app string, addonID string) (LogDrainsRes, error) LogDrainAddonAdd(app string, addonID string, params LogDrainAddParams) (*LogDrainRes, error) } @@ -104,6 +105,26 @@ func (c *Client) LogDrainRemove(app, URL string) error { return nil } +func (c *Client) LogDrainAddonRemove(app, addonID string, URL string) error { + payload := map[string]string{ + "url": URL, + } + + req := &httpclient.APIRequest{ + Method: "DELETE", + Endpoint: "/apps/" + app + "/addons/" + addonID + "/log_drains", + Expected: httpclient.Statuses{http.StatusNoContent}, + Params: payload, + } + + err := c.ScalingoAPI().DoRequest(req, nil) + if err != nil { + return errgo.Notef(err, "fail to delete log drain %s from the addon %s", URL, addonID) + } + + return nil +} + func (c *Client) LogDrainAddonAdd(app string, addonID string, params LogDrainAddParams) (*LogDrainRes, error) { var logDrainRes LogDrainRes payload := LogDrainRes{ diff --git a/vendor/github.com/Scalingo/go-scalingo/version.go b/vendor/github.com/Scalingo/go-scalingo/version.go index e46bf1218..9bee752e7 100644 --- a/vendor/github.com/Scalingo/go-scalingo/version.go +++ b/vendor/github.com/Scalingo/go-scalingo/version.go @@ -1,3 +1,3 @@ package scalingo -var Version = "4.5.7" +var Version = "4.5.8"