-
Notifications
You must be signed in to change notification settings - Fork 3
/
environment.go
250 lines (208 loc) · 7.95 KB
/
environment.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package scalr
import (
"context"
"errors"
"fmt"
"net/url"
"time"
)
// Compile-time proof of interface implementation.
var _ Environments = (*environments)(nil)
// Environments describes all the environment related methods that the
// Scalr IACP API supports.
type Environments interface {
List(ctx context.Context, options EnvironmentListOptions) (*EnvironmentList, error)
Read(ctx context.Context, environmentID string) (*Environment, error)
Create(ctx context.Context, options EnvironmentCreateOptions) (*Environment, error)
Update(ctx context.Context, environmentID string, options EnvironmentUpdateOptions) (*Environment, error)
UpdateDefaultProviderConfigurationOnly(ctx context.Context, environmentID string, options EnvironmentUpdateOptionsDefaultProviderConfigurationOnly) (*Environment, error)
Delete(ctx context.Context, environmentID string) error
}
// environments implements Environments.
type environments struct {
client *Client
}
// EnvironmentStatus represents an environment status.
type EnvironmentStatus string
// List of available environment statuses.
const (
EnvironmentStatusActive EnvironmentStatus = "Active"
EnvironmentStatusInactive EnvironmentStatus = "Inactive"
)
// EnvironmentList represents a list of environments.
type EnvironmentList struct {
*Pagination
Items []*Environment
}
// Environment represents a Scalr environment.
type Environment struct {
ID string `jsonapi:"primary,environments"`
Name string `jsonapi:"attr,name"`
CostEstimationEnabled bool `jsonapi:"attr,cost-estimation-enabled"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
Status EnvironmentStatus `jsonapi:"attr,status"`
// Relations
Account *Account `jsonapi:"relation,account"`
PolicyGroups []*PolicyGroup `jsonapi:"relation,policy-groups"`
DefaultProviderConfigurations []*ProviderConfiguration `jsonapi:"relation,default-provider-configurations"`
ProviderConfigurations []*ProviderConfiguration `jsonapi:"relation,provider-configurations"`
CreatedBy *User `jsonapi:"relation,created-by"`
Tags []*Tag `jsonapi:"relation,tags"`
}
// Organization is Environment included in Workspace - always prefer Environment
type Organization struct {
ID string `jsonapi:"primary,organizations"`
Name string `jsonapi:"attr,name"`
CostEstimationEnabled bool `jsonapi:"attr,cost-estimation-enabled"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
CreatedBy string `jsonapi:"attr,created-by"`
Status EnvironmentStatus `jsonapi:"attr,status"`
// Relations
Account *Account `jsonapi:"relation,account"`
}
// EnvironmentCreateOptions represents the options for creating a new Environment.
type EnvironmentCreateOptions struct {
ID string `jsonapi:"primary,environments"`
Name *string `jsonapi:"attr,name"`
CostEstimationEnabled *bool `jsonapi:"attr,cost-estimation-enabled,omitempty"`
// Relations
Account *Account `jsonapi:"relation,account"`
PolicyGroups []*PolicyGroup `jsonapi:"relation,policy-groups,omitempty"`
DefaultProviderConfigurations []*ProviderConfiguration `jsonapi:"relation,default-provider-configurations,omitempty"`
// Specifies tags assigned to the environment
Tags []*Tag `jsonapi:"relation,tags,omitempty"`
}
func (o EnvironmentCreateOptions) valid() error {
if o.Account == nil {
return errors.New("account is required")
}
if !validStringID(&o.Account.ID) {
return errors.New("invalid value for account ID")
}
if o.Name == nil {
return errors.New("name is required")
}
return nil
}
type EnvironmentListOptions struct {
ListOptions
Include *string `url:"include,omitempty"`
Filter *EnvironmentFilter `url:"filter,omitempty"`
}
// EnvironmentFilter represents the options for filtering environments.
type EnvironmentFilter struct {
Id *string `url:"environment,omitempty"`
Account *string `url:"account,omitempty"`
Name *string `url:"name,omitempty"`
Tag *string `url:"tag,omitempty"`
}
// List all the environmens.
func (s *environments) List(ctx context.Context, options EnvironmentListOptions) (*EnvironmentList, error) {
req, err := s.client.newRequest("GET", "environments", &options)
if err != nil {
return nil, err
}
envl := &EnvironmentList{}
err = s.client.do(ctx, req, envl)
if err != nil {
return nil, err
}
return envl, nil
}
// Create is used to create a new Environment.
func (s *environments) Create(ctx context.Context, options EnvironmentCreateOptions) (*Environment, error) {
if err := options.valid(); err != nil {
return nil, err
}
// Make sure we don't send a user provided ID.
options.ID = ""
req, err := s.client.newRequest("POST", "environments", &options)
if err != nil {
return nil, err
}
environment := &Environment{}
err = s.client.do(ctx, req, environment)
if err != nil {
return nil, err
}
return environment, nil
}
// Read an environment by its ID.
func (s *environments) Read(ctx context.Context, environmentID string) (*Environment, error) {
if !validStringID(&environmentID) {
return nil, errors.New("invalid value for environment ID")
}
options := struct {
Include string `url:"include"`
}{
Include: "created-by",
}
u := fmt.Sprintf("environments/%s", url.QueryEscape(environmentID))
req, err := s.client.newRequest("GET", u, options)
if err != nil {
return nil, err
}
env := &Environment{}
err = s.client.do(ctx, req, env)
if err != nil {
return nil, err
}
return env, nil
}
// EnvironmentUpdateOptions represents the options for updating an environment.
type EnvironmentUpdateOptions struct {
// For internal use only!
ID string `jsonapi:"primary,environments"`
Name *string `jsonapi:"attr,name,omitempty"`
CostEstimationEnabled *bool `jsonapi:"attr,cost-estimation-enabled,omitempty"`
// Relations
PolicyGroups []*PolicyGroup `jsonapi:"relation,policy-groups"`
DefaultProviderConfigurations []*ProviderConfiguration `jsonapi:"relation,default-provider-configurations"`
}
type EnvironmentUpdateOptionsDefaultProviderConfigurationOnly struct {
ID string `jsonapi:"primary,environments"`
// Relations
DefaultProviderConfigurations []*ProviderConfiguration `jsonapi:"relation,default-provider-configurations"`
}
// Update settings of an existing environment.
func (s *environments) Update(ctx context.Context, environmentID string, options EnvironmentUpdateOptions) (*Environment, error) {
// Make sure we don't send a user provided ID.
options.ID = ""
u := fmt.Sprintf("environments/%s", url.QueryEscape(environmentID))
req, err := s.client.newRequest("PATCH", u, &options)
if err != nil {
return nil, err
}
env := &Environment{}
err = s.client.do(ctx, req, env)
if err != nil {
return nil, err
}
return env, nil
}
func (s *environments) UpdateDefaultProviderConfigurationOnly(ctx context.Context, environmentID string, options EnvironmentUpdateOptionsDefaultProviderConfigurationOnly) (*Environment, error) {
options.ID = ""
u := fmt.Sprintf("environments/%s", url.QueryEscape(environmentID))
req, err := s.client.newRequest("PATCH", u, &options)
if err != nil {
return nil, err
}
env := &Environment{}
err = s.client.do(ctx, req, env)
if err != nil {
return nil, err
}
return env, nil
}
// Delete an environment by its ID.
func (s *environments) Delete(ctx context.Context, environmentID string) error {
if !validStringID(&environmentID) {
return errors.New("invalid value for environment ID")
}
u := fmt.Sprintf("environments/%s", url.QueryEscape(environmentID))
req, err := s.client.newRequest("DELETE", u, nil)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}