-
Notifications
You must be signed in to change notification settings - Fork 8
/
log_drains.go
134 lines (107 loc) · 3.69 KB
/
log_drains.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package scalingo
import (
"context"
"net/http"
"gopkg.in/errgo.v1"
httpclient "github.com/Scalingo/go-scalingo/v7/http"
)
type LogDrainsService interface {
LogDrainsList(ctx context.Context, app string) ([]LogDrain, error)
LogDrainAdd(ctx context.Context, app string, params LogDrainAddParams) (*LogDrainRes, error)
LogDrainRemove(ctx context.Context, app, URL string) error
LogDrainAddonRemove(ctx context.Context, app, addonID string, URL string) error
LogDrainsAddonList(ctx context.Context, app string, addonID string) ([]LogDrain, error)
LogDrainAddonAdd(ctx context.Context, app string, addonID string, params LogDrainAddParams) (*LogDrainRes, error)
}
var _ LogDrainsService = (*Client)(nil)
type LogDrain struct {
AppID string `json:"app_id"`
URL string `json:"url"`
}
type LogDrainRes struct {
Drain LogDrain `json:"drain"`
}
type LogDrainsRes struct {
Drains []LogDrain `json:"drains"`
}
func (c *Client) LogDrainsList(ctx context.Context, app string) ([]LogDrain, error) {
var logDrainsRes LogDrainsRes
err := c.ScalingoAPI().SubresourceList(ctx, "apps", app, "log_drains", nil, &logDrainsRes)
if err != nil {
return nil, errgo.Notef(err, "fail to list the log drains")
}
return logDrainsRes.Drains, nil
}
func (c *Client) LogDrainsAddonList(ctx context.Context, app string, addonID string) ([]LogDrain, error) {
var logDrainsRes LogDrainsRes
err := c.ScalingoAPI().SubresourceList(ctx, "apps", app, "addons/"+addonID+"/log_drains", nil, &logDrainsRes)
if err != nil {
return nil, errgo.Notef(err, "fail to list the log drains of the addon %s", addonID)
}
return logDrainsRes.Drains, nil
}
type LogDrainAddPayload struct {
Drain LogDrainAddParams `json:"drain"`
}
type LogDrainAddParams struct {
Type string `json:"type"`
URL string `json:"url"`
Port string `json:"port"`
Host string `json:"host"`
Token string `json:"token"`
DrainRegion string `json:"drain_region"`
}
func (c *Client) LogDrainAdd(ctx context.Context, app string, params LogDrainAddParams) (*LogDrainRes, error) {
var logDrainRes LogDrainRes
payload := LogDrainAddPayload{
Drain: params,
}
err := c.ScalingoAPI().SubresourceAdd(ctx, "apps", app, "log_drains", payload, &logDrainRes)
if err != nil {
return nil, errgo.Notef(err, "fail to add drain")
}
return &logDrainRes, nil
}
func (c *Client) LogDrainRemove(ctx context.Context, app, URL string) error {
payload := map[string]string{
"url": URL,
}
req := &httpclient.APIRequest{
Method: "DELETE",
Endpoint: "/apps/" + app + "/log_drains",
Expected: httpclient.Statuses{http.StatusNoContent},
Params: payload,
}
err := c.ScalingoAPI().DoRequest(ctx, req, nil)
if err != nil {
return errgo.Notef(err, "fail to delete log drain")
}
return nil
}
func (c *Client) LogDrainAddonRemove(ctx context.Context, 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(ctx, 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(ctx context.Context, app string, addonID string, params LogDrainAddParams) (*LogDrainRes, error) {
var logDrainRes LogDrainRes
payload := LogDrainAddPayload{
Drain: params,
}
err := c.ScalingoAPI().SubresourceAdd(ctx, "apps", app, "addons/"+addonID+"/log_drains", payload, &logDrainRes)
if err != nil {
return nil, errgo.Notef(err, "fail to add log drain to the addon %s", addonID)
}
return &logDrainRes, nil
}