Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove log drains from db #577

Merged
merged 5 commits into from Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Expand Up @@ -26,7 +26,7 @@

[[constraint]]
name = "github.com/Scalingo/go-scalingo"
version = "^4"
branch = "feature/cli/576/remove_log_drain_from_db"
curzolapierre marked this conversation as resolved.
Show resolved Hide resolved

[[constraint]]
branch = "master"
Expand Down
61 changes: 56 additions & 5 deletions 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"
Expand Down Expand Up @@ -133,19 +136,58 @@ 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",
curzolapierre marked this conversation as resolved.
Show resolved Hide resolved
Description: `Remove a log drain from an application and all addons connected to the application:
curzolapierre marked this conversation as resolved.
Show resolved Hide resolved

$ scalingo --app my-app log-drains-remove syslog://custom.logstash.com:12345

Remove a log drain from a specific addon:
Use the parameter: "--addon <addon_uuid>" to your remove commands to remove a log drain from a specific addon
curzolapierre marked this conversation as resolved.
Show resolved Hide resolved

$ 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 your remove commands to remove log drain only from the application
curzolapierre marked this conversation as resolved.
Show resolved Hide resolved

$ 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") != "<addon_id>" {
addonID = c.GlobalString("addon")
} else if c.String("addon") != "<addon_id>" {
addonID = c.String("addon")
}

if addonID != "" && c.Bool("only-app") {
cli.ShowCommandHelp(c, "log-drains-remove")
return
}

if addonID == "" && !c.Bool("only-app") {
curzolapierre marked this conversation as resolved.
Show resolved Hide resolved
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, 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")
}
Expand All @@ -160,3 +202,12 @@ var (
},
}
)

func askContinue(message string) bool {
result := false
prompt := &survey.Confirm{
Message: message + "\n\tContinue ?",
curzolapierre marked this conversation as resolved.
Show resolved Hide resolved
}
survey.AskOne(prompt, &result, nil)
return result
}
42 changes: 37 additions & 5 deletions log_drains/remove.go
Expand Up @@ -6,17 +6,49 @@ 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 err != nil {
return errgo.Notef(err, "fail to remove the log drain from the application")
if opts.AddonID != "" {
// addons only
curzolapierre marked this conversation as resolved.
Show resolved Hide resolved
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)
curzolapierre marked this conversation as resolved.
Show resolved Hide resolved
} else {
curzolapierre marked this conversation as resolved.
Show resolved Hide resolved
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 {
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")
curzolapierre marked this conversation as resolved.
Show resolved Hide resolved
}

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
}
21 changes: 21 additions & 0 deletions vendor/github.com/Scalingo/go-scalingo/log_drains.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.