From fb6c34001b1f219940449faaab148474b5803caf Mon Sep 17 00:00:00 2001 From: CURZOLA Pierre Date: Wed, 1 Jul 2020 11:13:11 +0200 Subject: [PATCH 1/5] log drains remove command --- Gopkg.lock | 6 +-- Gopkg.toml | 2 +- cmd/log_drains.go | 37 ++++++++++++++--- log_drains/remove.go | 41 ++++++++++++++++--- .../Scalingo/go-scalingo/log_drains.go | 21 ++++++++++ 5 files changed, 93 insertions(+), 14 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 866085641..41c6cf0bb 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -26,7 +26,8 @@ revision = "8eb48cc6f27eafda8e3a9627edba494a1d229a01" [[projects]] - digest = "1:d58a2d159db515d94824ee99685cb65571e431bf988b638d31de4652866c6094" + branch = "feature/cli/576/remove_log_drain_from_db" + digest = "1:23498d03e2a2117346d5def9486410790b626e5505b7d2ca51fa2e628f412510" name = "github.com/Scalingo/go-scalingo" packages = [ ".", @@ -37,8 +38,7 @@ "io", ] pruneopts = "NUT" - revision = "5af0bef7605dbe28f7c7eafd6cfc27263c9e16d8" - version = "v4.5.7" + revision = "902f939808781aa3aeb27319f0f9895f0c71bb7b" [[projects]] digest = "1:ab7f2d565135ccc3b35eb2427819fbca0501d582eba5091e684275022d3ff3ba" diff --git a/Gopkg.toml b/Gopkg.toml index 093576317..30341654a 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -26,7 +26,7 @@ [[constraint]] name = "github.com/Scalingo/go-scalingo" - version = "^4" + branch = "feature/cli/576/remove_log_drain_from_db" [[constraint]] branch = "master" diff --git a/cmd/log_drains.go b/cmd/log_drains.go index 9e0cae4da..dcf0221da 100644 --- a/cmd/log_drains.go +++ b/cmd/log_drains.go @@ -133,19 +133,46 @@ 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 addons", + Description: `Remove a log drain from an application and all addons connected to the application: + + $ 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 your remove commands to remove a log drain from a specific addon - $ scalingo --app my-app log-drains-remove syslog://custom.logstash.com:12345 + $ scalingo --app my-app --addon ad-3c2f8c81-99bd-4667-9791-466799bd4667 log-drains-remove syslog://custom.logstash.com:12345 + + Remove a log drain only for the application: + Use the parameter: "--only-app" to your remove commands to remove 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) { + // TODO: survey currentApp := appdetect.CurrentApp(c) + + var addonID string + if c.GlobalString("addon") != "" { + addonID = c.GlobalString("addon") + } else if c.String("addon") != "" { + addonID = c.String("addon") + } + 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") } diff --git a/log_drains/remove.go b/log_drains/remove.go index ef3db5fbd..cd28cd304 100644 --- a/log_drains/remove.go +++ b/log_drains/remove.go @@ -6,17 +6,48 @@ import ( "gopkg.in/errgo.v1" ) -func Remove(app string, URL string) error { +type RemoveAddonOpts struct { + WithAddons bool + 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 err != nil { - return errgo.Notef(err, "fail to remove the log drain from the application") + if opts.OnlyApp { + // app only + err = c.LogDrainRemove(app, opts.URL) + if err != nil { + return errgo.Notef(err, "fail to remove the log drain from the application") + } + } else if opts.AddonID != "" { + // addons 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) + } + } else { + // app + addons + addons, err := c.AddonsList(app) + if err != nil { + return errgo.Notef(err, "fail to list addons") + } + + 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") + io.Status("The log drain:", opts.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..9b9dd541b 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 of the addon %s", addonID) + } + + return nil +} + func (c *Client) LogDrainAddonAdd(app string, addonID string, params LogDrainAddParams) (*LogDrainRes, error) { var logDrainRes LogDrainRes payload := LogDrainRes{ From 71ce173192df48dea5972bbe9c882a17da8ca297 Mon Sep 17 00:00:00 2001 From: CURZOLA Pierre Date: Wed, 1 Jul 2020 15:51:37 +0200 Subject: [PATCH 2/5] ask for deletion --- cmd/log_drains.go | 26 +++++++++++++++++++++++++- log_drains/remove.go | 41 +++++++++++++++++++++-------------------- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/cmd/log_drains.go b/cmd/log_drains.go index dcf0221da..5b04bb8ff 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" @@ -156,7 +159,6 @@ var ( # See also commands 'log-drains-add', 'log-drains'`, Action: func(c *cli.Context) { - // TODO: survey currentApp := appdetect.CurrentApp(c) var addonID string @@ -166,6 +168,19 @@ var ( addonID = c.String("addon") } + if addonID != "" && c.Bool("only-app") { + cli.ShowCommandHelp(c, "log-drains-remove") + return + } + + if addonID == "" && !c.Bool("only-app") { + result := askContinue("This operation will delete the log drain " + c.Args()[0] + " for the application and all addons connected to this application") + if !result { + fmt.Println("Aborted") + return + } + } + var err error if len(c.Args()) == 1 { err = log_drains.Remove(currentApp, log_drains.RemoveAddonOpts{ @@ -187,3 +202,12 @@ var ( }, } ) + +func askContinue(message string) bool { + result := false + prompt := &survey.Confirm{ + Message: message + "\n\tContinue ?", + } + survey.AskOne(prompt, &result, nil) + return result +} diff --git a/log_drains/remove.go b/log_drains/remove.go index cd28cd304..ce3a6866f 100644 --- a/log_drains/remove.go +++ b/log_drains/remove.go @@ -7,10 +7,9 @@ import ( ) type RemoveAddonOpts struct { - WithAddons bool - AddonID string - OnlyApp bool - URL string + AddonID string + OnlyApp bool + URL string } func Remove(app string, opts RemoveAddonOpts) error { @@ -19,35 +18,37 @@ func Remove(app string, opts RemoveAddonOpts) error { return errgo.Notef(err, "fail to get Scalingo client to remove a log drain from the application") } - if opts.OnlyApp { - // app only - err = c.LogDrainRemove(app, opts.URL) - if err != nil { - return errgo.Notef(err, "fail to remove the log drain from the application") - } - } else if opts.AddonID != "" { + if opts.AddonID != "" { // addons 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) } else { - // app + addons - addons, err := c.AddonsList(app) + err = c.LogDrainRemove(app, opts.URL) if err != nil { - return errgo.Notef(err, "fail to list addons") + 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) } - for _, addon := range addons { - err := c.LogDrainAddonRemove(app, addon.ID, opts.URL) + if !opts.OnlyApp { + addons, err := c.AddonsList(app) 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) + return errgo.Notef(err, "fail to list addons") + } + + 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:", opts.URL, "has been deleted") return nil } From 573cdb21594906b3478e778f4188dc64652f634f Mon Sep 17 00:00:00 2001 From: CURZOLA Pierre Date: Thu, 2 Jul 2020 13:35:08 +0200 Subject: [PATCH 3/5] typo + ask for deletion in any cases --- Gopkg.lock | 4 +- cmd/log_drains.go | 40 +++++++++++-------- log_drains/remove.go | 37 ++++++++--------- .../Scalingo/go-scalingo/log_drains.go | 2 +- 4 files changed, 46 insertions(+), 37 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 41c6cf0bb..cb9b28f2b 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -27,7 +27,7 @@ [[projects]] branch = "feature/cli/576/remove_log_drain_from_db" - digest = "1:23498d03e2a2117346d5def9486410790b626e5505b7d2ca51fa2e628f412510" + digest = "1:15a57f7ecf2be15fe29f4bebc4128a9105290c237b20f39120afbc5e695e40fc" name = "github.com/Scalingo/go-scalingo" packages = [ ".", @@ -38,7 +38,7 @@ "io", ] pruneopts = "NUT" - revision = "902f939808781aa3aeb27319f0f9895f0c71bb7b" + revision = "2f8650a862e9a2083597cf874a733cde12473321" [[projects]] digest = "1:ab7f2d565135ccc3b35eb2427819fbca0501d582eba5091e684275022d3ff3ba" diff --git a/cmd/log_drains.go b/cmd/log_drains.go index 5b04bb8ff..99c3a90f7 100644 --- a/cmd/log_drains.go +++ b/cmd/log_drains.go @@ -22,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 @@ -31,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 { @@ -85,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. @@ -141,19 +140,19 @@ var ( addonFlag, cli.BoolFlag{Name: "only-app", Usage: "remove the log drains for the application only"}, }, - Usage: "Remove a log drain from an application and addons", - Description: `Remove a log drain from an application and all addons connected to the application: + 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 your remove commands to 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 Remove a log drain only for the application: - Use the parameter: "--only-app" to your remove commands to remove log drain only from 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'`, @@ -173,12 +172,21 @@ var ( return } + message := "This operation will delete the log drain " + c.Args()[0] if addonID == "" && !c.Bool("only-app") { - result := askContinue("This operation will delete the log drain " + c.Args()[0] + " for the application and all addons connected to this application") - if !result { - fmt.Println("Aborted") - return - } + // 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 @@ -206,7 +214,7 @@ var ( func askContinue(message string) bool { result := false prompt := &survey.Confirm{ - Message: message + "\n\tContinue ?", + 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 ce3a6866f..deb4a07e9 100644 --- a/log_drains/remove.go +++ b/log_drains/remove.go @@ -19,33 +19,34 @@ func Remove(app string, opts RemoveAddonOpts) error { } if opts.AddonID != "" { - // addons only + // 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) + 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 { + io.Status("fail to remove the log drain from the application:", app, "\n\t", err) } else { - err = c.LogDrainRemove(app, opts.URL) + io.Status("Log drain", opts.URL, "has been deleted from the application", app) + } + + if !opts.OnlyApp { + addons, err := c.AddonsList(app) if err != nil { - 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) + return errgo.Notef(err, "fail to list addons to remove log drain") } - if !opts.OnlyApp { - addons, err := c.AddonsList(app) + for _, addon := range addons { + err := c.LogDrainAddonRemove(app, addon.ID, opts.URL) if err != nil { - return errgo.Notef(err, "fail to list addons") - } - - 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("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) } } } diff --git a/vendor/github.com/Scalingo/go-scalingo/log_drains.go b/vendor/github.com/Scalingo/go-scalingo/log_drains.go index 9b9dd541b..3591922b0 100644 --- a/vendor/github.com/Scalingo/go-scalingo/log_drains.go +++ b/vendor/github.com/Scalingo/go-scalingo/log_drains.go @@ -119,7 +119,7 @@ func (c *Client) LogDrainAddonRemove(app, addonID string, URL string) error { err := c.ScalingoAPI().DoRequest(req, nil) if err != nil { - return errgo.Notef(err, "fail to delete log drain of the addon %s", addonID) + return errgo.Notef(err, "fail to delete log drain %s from the addon %s", URL, addonID) } return nil From 5575d995fa1e1d8ff4faab81e8c312c367b9d755 Mon Sep 17 00:00:00 2001 From: CURZOLA Pierre Date: Fri, 3 Jul 2020 09:49:06 +0200 Subject: [PATCH 4/5] update deps --- Gopkg.lock | 6 +++--- Gopkg.toml | 2 +- vendor/github.com/Scalingo/go-scalingo/version.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index cb9b28f2b..70047047b 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -26,8 +26,7 @@ revision = "8eb48cc6f27eafda8e3a9627edba494a1d229a01" [[projects]] - branch = "feature/cli/576/remove_log_drain_from_db" - digest = "1:15a57f7ecf2be15fe29f4bebc4128a9105290c237b20f39120afbc5e695e40fc" + digest = "1:dcd5c59297daac978a4a050a585c29b28c745df3d0d8f12e3d4d3689f7918580" name = "github.com/Scalingo/go-scalingo" packages = [ ".", @@ -38,7 +37,8 @@ "io", ] pruneopts = "NUT" - revision = "2f8650a862e9a2083597cf874a733cde12473321" + revision = "fec0faece36ffe70f16bca9834b1894e833e78a2" + version = "v4.5.8" [[projects]] digest = "1:ab7f2d565135ccc3b35eb2427819fbca0501d582eba5091e684275022d3ff3ba" diff --git a/Gopkg.toml b/Gopkg.toml index 30341654a..093576317 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -26,7 +26,7 @@ [[constraint]] name = "github.com/Scalingo/go-scalingo" - branch = "feature/cli/576/remove_log_drain_from_db" + version = "^4" [[constraint]] branch = "master" 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" From 330dcd216d1d8ff28cfbea568ff2a5710be5615e Mon Sep 17 00:00:00 2001 From: CURZOLA Pierre Date: Fri, 3 Jul 2020 10:12:18 +0200 Subject: [PATCH 5/5] add changelog entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) 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)