-
Notifications
You must be signed in to change notification settings - Fork 8
/
env.go
117 lines (97 loc) · 3.06 KB
/
env.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
package scalingo
import (
"context"
"encoding/json"
"gopkg.in/errgo.v1"
"github.com/Scalingo/go-scalingo/v7/http"
)
type VariablesService interface {
VariablesList(ctx context.Context, app string) (Variables, error)
VariablesListWithoutAlias(ctx context.Context, app string) (Variables, error)
VariableSet(ctx context.Context, app string, name string, value string) (*Variable, int, error)
VariableMultipleSet(ctx context.Context, app string, variables Variables) (Variables, int, error)
VariableUnset(ctx context.Context, app string, id string) error
}
var _ VariablesService = (*Client)(nil)
type Variable struct {
ID string `json:"id"`
Name string `json:"name"`
Value string `json:"value"`
}
type Variables []*Variable
func (vs Variables) Contains(name string) (*Variable, bool) {
for _, v := range vs {
if v.Name == name {
return v, true
}
}
return nil, false
}
type VariablesRes struct {
Variables Variables `json:"variables"`
}
type VariableSetParams struct {
Variable *Variable `json:"variable"`
}
func (c *Client) VariablesList(ctx context.Context, app string) (Variables, error) {
return c.variableList(ctx, app, true)
}
func (c *Client) VariablesListWithoutAlias(ctx context.Context, app string) (Variables, error) {
return c.variableList(ctx, app, false)
}
func (c *Client) variableList(ctx context.Context, app string, aliases bool) (Variables, error) {
var variablesRes VariablesRes
err := c.ScalingoAPI().SubresourceList(ctx, "apps", app, "variables", map[string]bool{"aliases": aliases}, &variablesRes)
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
}
return variablesRes.Variables, nil
}
func (c *Client) VariableSet(ctx context.Context, app string, name string, value string) (*Variable, int, error) {
req := &http.APIRequest{
Method: "POST",
Endpoint: "/apps/" + app + "/variables",
Params: map[string]interface{}{
"variable": map[string]string{
"name": name,
"value": value,
},
},
Expected: http.Statuses{200, 201},
}
res, err := c.ScalingoAPI().Do(ctx, req)
if err != nil {
return nil, 0, errgo.Mask(err, errgo.Any)
}
defer res.Body.Close()
var params VariableSetParams
err = json.NewDecoder(res.Body).Decode(¶ms)
if err != nil {
return nil, 0, errgo.Mask(err, errgo.Any)
}
return params.Variable, res.StatusCode, nil
}
func (c *Client) VariableMultipleSet(ctx context.Context, app string, variables Variables) (Variables, int, error) {
req := &http.APIRequest{
Method: "PUT",
Endpoint: "/apps/" + app + "/variables",
Params: map[string]Variables{
"variables": variables,
},
Expected: http.Statuses{200, 201},
}
res, err := c.ScalingoAPI().Do(ctx, req)
if err != nil {
return nil, 0, errgo.Mask(err, errgo.Any)
}
defer res.Body.Close()
var params VariablesRes
err = json.NewDecoder(res.Body).Decode(¶ms)
if err != nil {
return nil, 0, errgo.Mask(err, errgo.Any)
}
return params.Variables, res.StatusCode, nil
}
func (c *Client) VariableUnset(ctx context.Context, app string, id string) error {
return c.ScalingoAPI().SubresourceDelete(ctx, "apps", app, "variables", id)
}