generated from davidji99/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathenvironments.go
208 lines (174 loc) · 7.41 KB
/
environments.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
package api
import (
"fmt"
"github.com/davidji99/simpleresty"
"strconv"
)
// EnvironmentsService handles communication with the environments related
// methods of the Split.io APIv2.
//
// Reference: https://docs.split.io/reference#environments-overview
type EnvironmentsService service
// Environment reflects a stage in the development process, such as your production application or your internal staging
// environment. During the feature release process, Splits can be promoted through the various environments; allowing for
// a targeted roll out throughout the development process.
type Environment struct {
ID *string `json:"id"`
Name *string `json:"name"`
Production *bool `json:"production"`
}
// EnvironmentSegment represents a segment in an environment.
type EnvironmentSegment struct {
ID *string `json:"id"`
OrgID *string `json:"orgId"`
Environment *string `json:"environment"`
Name *string `json:"name"`
TrafficTypeID *string `json:"trafficTypeId"`
Description *string `json:"description"`
Status *string `json:"status"`
CreationTime *int64 `json:"creationTime"`
LastUpdateTime *int64 `json:"lastUpdateTime"`
TrafficTypeURN *TrafficType `json:"trafficTypeURN"`
Creator *User `json:"creator"`
}
// EnvironmentRequest represents a request modify an environment.
type EnvironmentRequest struct {
Name *string `json:"name,omitempty"`
Production *bool `json:"production,omitempty"`
}
// EnvironmentSegmentKeysRequest represents a request to add/remove segment keys in an environment.
type EnvironmentSegmentKeysRequest struct {
Keys []string `json:"keys"`
Comment string `json:"comment,omitempty"`
Title string `json:"title,omitempty"`
}
// List all environments.
//
// Reference: https://docs.split.io/reference#get-environments
func (e *EnvironmentsService) List(workspaceID string) ([]*Environment, *simpleresty.Response, error) {
var result []*Environment
urlStr := e.client.http.RequestURL("/environments/ws/%s", workspaceID)
response, getErr := e.client.get(urlStr, &result, nil)
return result, response, getErr
}
// ListSegments retrieves segments given an environment.
//
// Reference: https://docs.split.io/reference/list-segments-in-environment
func (e *EnvironmentsService) ListSegments(workspaceID, environmentID string) (*SegmentListResult, *simpleresty.Response, error) {
var result SegmentListResult
urlStr := e.client.http.RequestURL("/segments/ws/%s/environments/%s", workspaceID, environmentID)
response, getErr := e.client.get(urlStr, &result, nil)
return &result, response, getErr
}
// AddSegmentKeys for a given an environment.
//
// Reference: https://docs.split.io/reference/update-segment-keys-in-environment-via-json
func (e *EnvironmentsService) AddSegmentKeys(environmentID, segmentName string, shouldReplace bool, opts *EnvironmentSegmentKeysRequest) (*EnvironmentSegment, *simpleresty.Response, error) {
var result EnvironmentSegment
urlStr := e.client.http.RequestURL("/segments/%s/%s/uploadKeys?replace=%v", environmentID, segmentName, shouldReplace)
response, updateErr := e.client.put(urlStr, &result, opts)
return &result, response, updateErr
}
// GetSegmentKeys retrieves segment keys given an environment.
//
// Reference: https://docs.split.io/reference/get-segment-keys-in-environment
func (e *EnvironmentsService) GetSegmentKeys(environmentID, segmentName string) (*SegmentKeysList, *simpleresty.Response, error) {
var result SegmentKeysList
urlStr := e.client.http.RequestURL("/segments/%s/%s/keys", environmentID, segmentName)
response, getErr := e.client.get(urlStr, &result, nil)
return &result, response, getErr
}
// RemoveSegmentKeys removes segment keys given an environment.
//
// Reference: https://docs.split.io/reference/remove-segment-keys-from-environment
func (e *EnvironmentsService) RemoveSegmentKeys(environmentID, segmentName string, opts *EnvironmentSegmentKeysRequest) (*simpleresty.Response, error) {
urlStr := e.client.http.RequestURL("/segments/%s/%s/removeKeys", environmentID, segmentName)
response, err := e.client.put(urlStr, nil, opts)
return response, err
}
// FindByID retrieves an environment by its ID.
//
// Note: this method uses the List() method to first return all environments and then look for the target environment
// by an ID. The Split APIv2 does not provide a GET#show endpoint for environments unfortunately.
func (e *EnvironmentsService) FindByID(workspaceID, envID string) (*Environment, *simpleresty.Response, error) {
envs, listResponse, listErr := e.List(workspaceID)
if listErr != nil {
return nil, listResponse, listErr
}
for _, e := range envs {
if e.GetID() == envID {
return e, nil, nil
}
}
return nil, nil, fmt.Errorf("environment [%s] not found", envID)
}
// FindByName retrieves an environment by its name.
//
// Note: this method uses the List() method to first return all environments and then look for the target environment
// by an name. The Split APIv2 does not provide a GET#show endpoint for environments unfortunately.
func (e *EnvironmentsService) FindByName(workspaceID, envName string) (*Environment, *simpleresty.Response, error) {
envs, listResponse, listErr := e.List(workspaceID)
if listErr != nil {
return nil, listResponse, listErr
}
for _, e := range envs {
if e.GetName() == envName {
return e, nil, nil
}
}
return nil, nil, fmt.Errorf("environment [%s] not found", envName)
}
// Create an environment.
//
// Reference: https://docs.split.io/reference#create-environment
func (e *EnvironmentsService) Create(workspaceID string, opts *EnvironmentRequest) (*Environment, *simpleresty.Response, error) {
var result Environment
urlStr := e.client.http.RequestURL("/environments/ws/%s", workspaceID)
// Execute the request
response, createErr := e.client.post(urlStr, &result, opts)
return &result, response, createErr
}
type environmentPatchRequest struct {
Operation string `json:"op"`
Path string `json:"path"`
Value string `json:"value"`
}
// Update an environment.
//
// Reference: https://docs.split.io/reference#update-environment
func (e *EnvironmentsService) Update(workspaceID, envID string, opts *EnvironmentRequest) (*Environment, *simpleresty.Response, error) {
var result Environment
urlStr := e.client.http.RequestURL("/environments/ws/%s/%s", workspaceID, envID)
// Construct request body.
reqBody := make([]environmentPatchRequest, 0)
if opts.Name != nil {
reqBody = append(reqBody, environmentPatchRequest{
Operation: "replace",
Path: "/name",
Value: opts.GetName(),
})
}
if opts.Production != nil {
reqBody = append(reqBody, environmentPatchRequest{
Operation: "replace",
Path: "/production",
Value: strconv.FormatBool(opts.GetProduction()),
})
}
// Execute the request
response, getErr := e.client.patch(urlStr, &result, reqBody)
return &result, response, getErr
}
// Delete an environment.
//
// Note: you CANNOT delete an environment unless you first revoke all api keys associated with it.
//
// If deletion request is successful, the response body returns a "true" string.
//
// Reference: https://docs.split.io/reference#delete-environment
func (e *EnvironmentsService) Delete(workspaceID, envID string) (*simpleresty.Response, error) {
urlStr := e.client.http.RequestURL("/environments/ws/%s/%s", workspaceID, envID)
// Execute the request
response, getErr := e.client.delete(urlStr, nil, nil)
return response, getErr
}