From 80b21e84a1d4457210211306226df1613971297c Mon Sep 17 00:00:00 2001 From: CURZOLA Pierre Date: Wed, 22 Apr 2020 11:15:39 +0200 Subject: [PATCH 1/8] add log drains --- client.go | 1 + log_drains.go | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 log_drains.go diff --git a/client.go b/client.go index dec3e05c..35601b62 100644 --- a/client.go +++ b/client.go @@ -22,6 +22,7 @@ type API interface { EventsService KeysService LoginService + LogDrainsService LogsArchivesService LogsService NotificationPlatformsService diff --git a/log_drains.go b/log_drains.go new file mode 100644 index 00000000..4a8a9309 --- /dev/null +++ b/log_drains.go @@ -0,0 +1,75 @@ +package scalingo + +import ( + "gopkg.in/errgo.v1" +) + +type LogDrainsService interface { + LogDrainsList(app string) ([]LogDrain, error) + LogDrainAdd(app string, params LogDrainAddParams) (*LogDrain, error) + LogDrainRemove(app string, id string) error +} + +var _ LogDrainsService = (*Client)(nil) + +type LogDrain struct { + ID string `json:"id"` + Type string `json:"drain_type"` + Status string `json:"status"` + TargetURL string `json:"targetURL"` +} + +type LogDrainsRes struct { + LogDrains []LogDrain `json:"log_drains"` +} + +type LogDrainRes struct { + LogDrain LogDrain `json:"log_drain"` +} + +func (c *Client) LogDrainsList(app string) ([]LogDrain, error) { + var logDrainsRes LogDrainsRes + err := c.ScalingoAPI().SubresourceList("apps", app, "log_drains", nil, &logDrainsRes) + if err != nil { + return nil, errgo.Mask(err) + } + return logDrainsRes.LogDrains, nil +} + +type LogDrainAddParams struct { + Type string `json:"drain_type"` + Status string `json:"status"` + TargetURL string `json:"targetURL"` +} + +func (c *Client) LogDrainAdd(app string, params LogDrainAddParams) (*LogDrain, error) { + var logDrainRes LogDrainRes + err := c.ScalingoAPI().SubresourceAdd("apps", app, "log_drains", LogDrainRes{ + LogDrain: LogDrain{ + Type: params.Type, + TargetURL: params.TargetURL, + Status: params.Status, + }, + }, &logDrainRes) + if err != nil { + return nil, errgo.Mask(err) + } + return &logDrainRes.LogDrain, nil +} + +func (c *Client) LogDrainInfo(app, id string) (*LogDrain, error) { + var logDrainRes LogDrainRes + err := c.ScalingoAPI().SubresourceGet("apps", app, "log_drains", id, nil, &logDrainRes) + if err != nil { + return nil, errgo.Mask(err) + } + return &logDrainRes.LogDrain, nil +} + +func (c *Client) LogDrainRemove(app, id string) error { + err := c.ScalingoAPI().SubresourceDelete("apps", app, "log_drains", id) + if err != nil { + return errgo.Mask(err) + } + return nil +} From f12674b80cb5bbae639dbf33231290f66611ea40 Mon Sep 17 00:00:00 2001 From: CURZOLA Pierre Date: Thu, 23 Apr 2020 16:03:03 +0200 Subject: [PATCH 2/8] list works with the current implementation of log drains --- log_drains.go | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/log_drains.go b/log_drains.go index 4a8a9309..7fe662ac 100644 --- a/log_drains.go +++ b/log_drains.go @@ -13,14 +13,17 @@ type LogDrainsService interface { var _ LogDrainsService = (*Client)(nil) type LogDrain struct { - ID string `json:"id"` - Type string `json:"drain_type"` - Status string `json:"status"` - TargetURL string `json:"targetURL"` + // ID string `json:"id"` + // Type string `json:"drain_type"` + // Status string `json:"status"` + // TargetURL string `json:"targetURL"` + + AppID string `json:"app_id"` + URL string `json:"url"` } type LogDrainsRes struct { - LogDrains []LogDrain `json:"log_drains"` + LogDrains []LogDrain } type LogDrainRes struct { @@ -28,27 +31,31 @@ type LogDrainRes struct { } func (c *Client) LogDrainsList(app string) ([]LogDrain, error) { - var logDrainsRes LogDrainsRes + var logDrainsRes []LogDrain err := c.ScalingoAPI().SubresourceList("apps", app, "log_drains", nil, &logDrainsRes) if err != nil { return nil, errgo.Mask(err) } - return logDrainsRes.LogDrains, nil + return logDrainsRes, nil } type LogDrainAddParams struct { - Type string `json:"drain_type"` - Status string `json:"status"` - TargetURL string `json:"targetURL"` + // Type string `json:"drain_type"` + // Status string `json:"status"` + // TargetURL string `json:"targetURL"` + AppID string `json:"app_id"` + URL string `json:"url"` } func (c *Client) LogDrainAdd(app string, params LogDrainAddParams) (*LogDrain, error) { var logDrainRes LogDrainRes err := c.ScalingoAPI().SubresourceAdd("apps", app, "log_drains", LogDrainRes{ LogDrain: LogDrain{ - Type: params.Type, - TargetURL: params.TargetURL, - Status: params.Status, + // Type: params.Type, + // TargetURL: params.TargetURL, + // Status: params.Status, + AppID: params.AppID, + URL: params.URL, }, }, &logDrainRes) if err != nil { From 29a97f719606c2a44333a9dfa855737aa37c59a4 Mon Sep 17 00:00:00 2001 From: CURZOLA Pierre Date: Wed, 6 May 2020 11:19:11 +0200 Subject: [PATCH 3/8] vscode launch config to debug tests --- .vscode/launch.json | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..93909d76 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,19 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch", + "type": "go", + "request": "launch", + "mode": "test", + "program": "${workspaceFolder}", + "env": {}, + "args": [ + "'TestLogDrainsClient'" + ] + } + ] +} \ No newline at end of file From 86720f282e6366b92a97c72c161be085a04ea2dd Mon Sep 17 00:00:00 2001 From: CURZOLA Pierre Date: Wed, 6 May 2020 11:19:31 +0200 Subject: [PATCH 4/8] test listing log drains --- log_drains_test.go | 95 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 log_drains_test.go diff --git a/log_drains_test.go b/log_drains_test.go new file mode 100644 index 00000000..da52a3d0 --- /dev/null +++ b/log_drains_test.go @@ -0,0 +1,95 @@ +package scalingo + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + gomock "github.com/golang/mock/gomock" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestLogDrainsClient(t *testing.T) { + appName := "my-app" + logDrainID := "my-id" + logDrainURL := "tcp+tls://localhost:8080" + + tests := []struct { + action string + testedClientCall func(c LogDrainsService) error + expectedEndpoint string + expectedMethod string + response interface{} + responseStatus int + noBody bool + }{ + { + action: "list", + testedClientCall: func(c LogDrainsService) error { + _, err := c.LogDrainsList(appName) + return err + }, + expectedEndpoint: "/v1/apps/my-app/log_drains", + expectedMethod: "GET", + response: []LogDrain{ + { + AppID: logDrainID, + URL: logDrainURL, + }, + }, + }, + } + + for _, test := range tests { + for msg, run := range map[string]struct { + invalidResponse bool + }{ + "it should fail if it fails to " + test.action + "the subresource": { + invalidResponse: true, + }, + "it should succeed if it succeeds to " + test.action + " the subresource": { + invalidResponse: false, + }, + } { + t.Run(msg, func(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + handler := func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, test.expectedMethod, r.Method) + assert.Equal(t, test.expectedEndpoint, r.URL.Path) + if run.invalidResponse { + w.WriteHeader(500) + w.Write([]byte("INVALID")) + } else { + if test.responseStatus != 0 { + w.WriteHeader(test.responseStatus) + } + if test.response != nil { + err := json.NewEncoder(w).Encode(&test.response) + assert.NoError(t, err) + } + } + } + ts := httptest.NewServer(http.HandlerFunc(handler)) + defer ts.Close() + + c, err := New(ClientConfig{ + APIEndpoint: ts.URL, + APIToken: "test", + }) + require.NoError(t, err) + + c.authClient = MockAuth(ctrl) + + err = test.testedClientCall(c) + if run.invalidResponse { + require.Error(t, err) + } else { + require.NoError(t, err) + } + }) + } + } +} From c12e7ead924cd0d979c9406d9b385d388b980517 Mon Sep 17 00:00:00 2001 From: CURZOLA Pierre Date: Wed, 6 May 2020 11:29:22 +0200 Subject: [PATCH 5/8] clean up comments --- log_drains.go | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/log_drains.go b/log_drains.go index 7fe662ac..c02ba470 100644 --- a/log_drains.go +++ b/log_drains.go @@ -13,11 +13,6 @@ type LogDrainsService interface { var _ LogDrainsService = (*Client)(nil) type LogDrain struct { - // ID string `json:"id"` - // Type string `json:"drain_type"` - // Status string `json:"status"` - // TargetURL string `json:"targetURL"` - AppID string `json:"app_id"` URL string `json:"url"` } @@ -40,9 +35,6 @@ func (c *Client) LogDrainsList(app string) ([]LogDrain, error) { } type LogDrainAddParams struct { - // Type string `json:"drain_type"` - // Status string `json:"status"` - // TargetURL string `json:"targetURL"` AppID string `json:"app_id"` URL string `json:"url"` } @@ -51,9 +43,6 @@ func (c *Client) LogDrainAdd(app string, params LogDrainAddParams) (*LogDrain, e var logDrainRes LogDrainRes err := c.ScalingoAPI().SubresourceAdd("apps", app, "log_drains", LogDrainRes{ LogDrain: LogDrain{ - // Type: params.Type, - // TargetURL: params.TargetURL, - // Status: params.Status, AppID: params.AppID, URL: params.URL, }, From 53ddd9792399e7111e15abe4f9448cf2bbe93799 Mon Sep 17 00:00:00 2001 From: CURZOLA Pierre Date: Wed, 6 May 2020 13:27:05 +0200 Subject: [PATCH 6/8] Add changelog entry. Remove vscode config from git. Remove unused functions --- .gitignore | 1 + .vscode/launch.json | 19 ------------------- log_drains.go | 42 ------------------------------------------ 3 files changed, 1 insertion(+), 61 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.gitignore b/.gitignore index 881fd01b..1e628e75 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ # IDE .idea/ +.vscode/ \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 93909d76..00000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Launch", - "type": "go", - "request": "launch", - "mode": "test", - "program": "${workspaceFolder}", - "env": {}, - "args": [ - "'TestLogDrainsClient'" - ] - } - ] -} \ No newline at end of file diff --git a/log_drains.go b/log_drains.go index c02ba470..4472840f 100644 --- a/log_drains.go +++ b/log_drains.go @@ -6,8 +6,6 @@ import ( type LogDrainsService interface { LogDrainsList(app string) ([]LogDrain, error) - LogDrainAdd(app string, params LogDrainAddParams) (*LogDrain, error) - LogDrainRemove(app string, id string) error } var _ LogDrainsService = (*Client)(nil) @@ -21,10 +19,6 @@ type LogDrainsRes struct { LogDrains []LogDrain } -type LogDrainRes struct { - LogDrain LogDrain `json:"log_drain"` -} - func (c *Client) LogDrainsList(app string) ([]LogDrain, error) { var logDrainsRes []LogDrain err := c.ScalingoAPI().SubresourceList("apps", app, "log_drains", nil, &logDrainsRes) @@ -33,39 +27,3 @@ func (c *Client) LogDrainsList(app string) ([]LogDrain, error) { } return logDrainsRes, nil } - -type LogDrainAddParams struct { - AppID string `json:"app_id"` - URL string `json:"url"` -} - -func (c *Client) LogDrainAdd(app string, params LogDrainAddParams) (*LogDrain, error) { - var logDrainRes LogDrainRes - err := c.ScalingoAPI().SubresourceAdd("apps", app, "log_drains", LogDrainRes{ - LogDrain: LogDrain{ - AppID: params.AppID, - URL: params.URL, - }, - }, &logDrainRes) - if err != nil { - return nil, errgo.Mask(err) - } - return &logDrainRes.LogDrain, nil -} - -func (c *Client) LogDrainInfo(app, id string) (*LogDrain, error) { - var logDrainRes LogDrainRes - err := c.ScalingoAPI().SubresourceGet("apps", app, "log_drains", id, nil, &logDrainRes) - if err != nil { - return nil, errgo.Mask(err) - } - return &logDrainRes.LogDrain, nil -} - -func (c *Client) LogDrainRemove(app, id string) error { - err := c.ScalingoAPI().SubresourceDelete("apps", app, "log_drains", id) - if err != nil { - return errgo.Mask(err) - } - return nil -} From d0e836ceea230271c2a65802f89d09397b7658ce Mon Sep 17 00:00:00 2001 From: CURZOLA Pierre Date: Wed, 6 May 2020 13:29:59 +0200 Subject: [PATCH 7/8] remove unused struct. Change errgo.Mask to errgo.Notef --- log_drains.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/log_drains.go b/log_drains.go index 4472840f..c6132922 100644 --- a/log_drains.go +++ b/log_drains.go @@ -15,15 +15,11 @@ type LogDrain struct { URL string `json:"url"` } -type LogDrainsRes struct { - LogDrains []LogDrain -} - func (c *Client) LogDrainsList(app string) ([]LogDrain, error) { var logDrainsRes []LogDrain err := c.ScalingoAPI().SubresourceList("apps", app, "log_drains", nil, &logDrainsRes) if err != nil { - return nil, errgo.Mask(err) + return nil, errgo.Notef(err, "fail to list the log drains") } return logDrainsRes, nil } From 0b9beb56526ab6b14a1bec361e3713e8cf0a15ca Mon Sep 17 00:00:00 2001 From: CURZOLA Pierre Date: Wed, 6 May 2020 15:02:26 +0200 Subject: [PATCH 8/8] log entry in toBeReleased section --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8f32e23..6021b78c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## ToBeReleased +* Add `LogDrainsList` resource, to call the list of drains of an app from logs-service via public API + +* Add `LogDrainsService` service +* Add `LogDrainsList` resource, to call the list of drains of an app from logs-service via public API ## v4.4.1 * Fix support for `addon_updated` and `start_region_migration` event types