Skip to content

Commit ad94b9a

Browse files
committed
refactor(logdrains): update log drain command usage and improve error handling
1 parent dee2968 commit ad94b9a

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

cmd/log_drains.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ Use the parameter "--with-addons" to list log drains of all addons connected to
5858
autocomplete.CmdFlagsAutoComplete(c, "log-drains")
5959
},
6060
}
61-
6261
logDrainsAddCommand = cli.Command{
6362
Name: "log-drains-add",
6463
Category: "Log drains",
@@ -68,7 +67,7 @@ Use the parameter "--with-addons" to list log drains of all addons connected to
6867
&cli.BoolFlag{Name: "with-addons", Usage: "also add the log drains to all addons"},
6968
&cli.BoolFlag{Name: "with-databases", Usage: "also add the log drains to all databases"},
7069
&cli.StringFlag{Name: "type", Usage: "Communication protocol", Required: true},
71-
&cli.StringFlag{Name: "url", Usage: "URL of self hosted ELK"},
70+
&cli.StringFlag{Name: "url", Usage: "URL of self hosted ELK or OpenSearch"},
7271
&cli.StringFlag{Name: "host", Usage: "Host of logs management service"},
7372
&cli.StringFlag{Name: "port", Usage: "Port of logs management service"},
7473
&cli.StringFlag{Name: "token", Usage: "Used by certain vendor for authentication"},
@@ -96,6 +95,7 @@ Warning: At the moment, only databases addons are able to forward logs to a drai
9695
"scalingo --app my-app log-drains-add --type syslog --host custom.logstash.com --port 12345",
9796
"scalingo --app my-app log-drains-add --type syslog --token 123456789abcdef --host custom.logstash.com --port 12345",
9897
"scalingo --app my-app log-drains-add --type elk --url https://my-user:123456789abcdef@logstash-app-name.osc-fr1.scalingo.io",
98+
"scalingo --app my-app log-drains-add --type opensearch --url https://my-user:123456789abcdef@my-app-name.opensearch.osc-fr1.scalingo-dbs.com:30123/yourindex/_doc?pipeline=your_pipeline",
9999
"scalingo --app my-app --addon ad-3c2f8c81-99bd-4667-9791-466799bd4667 log-drains-add --type datadog --token 123456789abcdef --drain-region eu-west-2",
100100
"scalingo --app my-app --with-addons log-drains-add --type datadog --token 123456789abcdef --drain-region eu-west-2",
101101
},

logdrains/remove.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package logdrains
22

33
import (
44
"context"
5-
6-
"gopkg.in/errgo.v1"
5+
"strings"
76

87
"github.com/Scalingo/cli/config"
98
"github.com/Scalingo/cli/io"
9+
"github.com/Scalingo/go-utils/errors/v2"
1010
)
1111

1212
type RemoveAddonOpts struct {
@@ -18,36 +18,41 @@ type RemoveAddonOpts struct {
1818
func Remove(ctx context.Context, app string, opts RemoveAddonOpts) error {
1919
c, err := config.ScalingoClient(ctx)
2020
if err != nil {
21-
return errgo.Notef(err, "fail to get Scalingo client to remove a log drain from the application")
21+
return errors.Wrap(ctx, err, "get Scalingo client")
2222
}
2323

2424
if opts.AddonID != "" {
2525
// addon only
2626
err := c.LogDrainAddonRemove(ctx, app, opts.AddonID, opts.URL)
2727
if err != nil {
28-
return errgo.Notef(err, "fail to remove the log drain from the addon %s", opts.AddonID)
28+
return errors.Wrap(ctx, err, "remove log drain from addon "+opts.AddonID)
2929
}
3030
io.Status("The log drain", opts.URL, "has been deleted from the addon", opts.AddonID)
3131
return nil
3232
}
3333

3434
err = c.LogDrainRemove(ctx, app, opts.URL)
3535
if err != nil {
36-
io.Status("fail to remove the log drain from the application:", app, "\n\t", err)
37-
} else {
38-
io.Status("Log drain", opts.URL, "has been deleted from the application", app)
36+
return errors.Wrap(ctx, err, "remove log drain from application "+app)
3937
}
38+
io.Status("Log drain", opts.URL, "has been deleted from the application", app)
4039

4140
if !opts.OnlyApp {
4241
addons, err := c.AddonsList(ctx, app)
4342
if err != nil {
44-
return errgo.Notef(err, "fail to list addons to remove log drain")
43+
return errors.Wrap(ctx, err, "list addons to remove log drain")
4544
}
4645

4746
for _, addon := range addons {
4847
err := c.LogDrainAddonRemove(ctx, app, addon.ID, opts.URL)
4948
if err != nil {
50-
io.Status("fail to remove the log drain from the addon:", addon.AddonProvider.Name, "\n\t", err)
49+
// Check if this is a "not found" error, which can happen if the log drain
50+
// was already removed by the main API call
51+
if strings.Contains(err.Error(), "not found") {
52+
io.Status("Log drain", opts.URL, "was already removed from the addon", addon.AddonProvider.Name)
53+
} else {
54+
io.Status("Unable to remove the log drain from the addon:", addon.AddonProvider.Name, "\n\t", err)
55+
}
5156
} else {
5257
io.Status("Log drain", opts.URL, "has been deleted from the addon", addon.AddonProvider.Name)
5358
}

0 commit comments

Comments
 (0)