forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry_module.go
435 lines (366 loc) · 13 KB
/
registry_module.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package tfe
import (
"context"
"fmt"
"net/url"
)
// Compile-time proof of interface implementation.
var _ RegistryModules = (*registryModules)(nil)
// RegistryModules describes all the registry module related methods that the Terraform
// Enterprise API supports.
//
// TFE API docs: https://www.terraform.io/docs/cloud/api/modules.html
type RegistryModules interface {
// Create a registry module without a VCS repo
Create(ctx context.Context, organization string, options RegistryModuleCreateOptions) (*RegistryModule, error)
// Create a registry module version
CreateVersion(ctx context.Context, moduleID RegistryModuleID, options RegistryModuleCreateVersionOptions) (*RegistryModuleVersion, error)
// Create and publish a registry module with a VCS repo
CreateWithVCSConnection(ctx context.Context, options RegistryModuleCreateWithVCSConnectionOptions) (*RegistryModule, error)
// Read a registry module
Read(ctx context.Context, moduleID RegistryModuleID) (*RegistryModule, error)
// Delete a registry module
Delete(ctx context.Context, organization string, name string) error
// Delete a specific registry module provider
DeleteProvider(ctx context.Context, moduleID RegistryModuleID) error
// Delete a specific registry module version
DeleteVersion(ctx context.Context, moduleID RegistryModuleID, version string) error
// Upload Terraform configuration files for the provided registry module version. It
// requires a path to the configuration files on disk, which will be packaged by
// hashicorp/go-slug before being uploaded.
Upload(ctx context.Context, rmv RegistryModuleVersion, path string) error
}
// registryModules implements RegistryModules.
type registryModules struct {
client *Client
}
// RegistryModuleStatus represents the status of the registry module
type RegistryModuleStatus string
// List of available registry module statuses
const (
RegistryModuleStatusPending RegistryModuleStatus = "pending"
RegistryModuleStatusNoVersionTags RegistryModuleStatus = "no_version_tags"
RegistryModuleStatusSetupFailed RegistryModuleStatus = "setup_failed"
RegistryModuleStatusSetupComplete RegistryModuleStatus = "setup_complete"
)
// RegistryModuleVersionStatus represents the status of a specific version of a registry module
type RegistryModuleVersionStatus string
// List of available registry module version statuses
const (
RegistryModuleVersionStatusPending RegistryModuleVersionStatus = "pending"
RegistryModuleVersionStatusCloning RegistryModuleVersionStatus = "cloning"
RegistryModuleVersionStatusCloneFailed RegistryModuleVersionStatus = "clone_failed"
RegistryModuleVersionStatusRegIngressReqFailed RegistryModuleVersionStatus = "reg_ingress_req_failed"
RegistryModuleVersionStatusRegIngressing RegistryModuleVersionStatus = "reg_ingressing"
RegistryModuleVersionStatusRegIngressFailed RegistryModuleVersionStatus = "reg_ingress_failed"
RegistryModuleVersionStatusOk RegistryModuleVersionStatus = "ok"
)
// RegistryModuleID represents the set of IDs that identify a RegistryModule
type RegistryModuleID struct {
// The organization the module belongs to, see RegistryModule.Organization.Name
Organization string
// The name of the module, see RegistryModule.Name
Name string
// The module's provider, see RegistryModule.Provider
Provider string
}
// RegistryModule represents a registry module
type RegistryModule struct {
ID string `jsonapi:"primary,registry-modules"`
Name string `jsonapi:"attr,name"`
Provider string `jsonapi:"attr,provider"`
Permissions *RegistryModulePermissions `jsonapi:"attr,permissions"`
Status RegistryModuleStatus `jsonapi:"attr,status"`
VCSRepo *VCSRepo `jsonapi:"attr,vcs-repo"`
VersionStatuses []RegistryModuleVersionStatuses `jsonapi:"attr,version-statuses"`
CreatedAt string `jsonapi:"attr,created-at"`
UpdatedAt string `jsonapi:"attr,updated-at"`
// Relations
Organization *Organization `jsonapi:"relation,organization"`
}
// RegistryModuleVersion represents a registry module version
type RegistryModuleVersion struct {
ID string `jsonapi:"primary,registry-module-versions"`
Source string `jsonapi:"attr,source"`
Status RegistryModuleVersionStatus `jsonapi:"attr,status"`
Version string `jsonapi:"attr,version"`
CreatedAt string `jsonapi:"attr,created-at"`
UpdatedAt string `jsonapi:"attr,updated-at"`
// Relations
RegistryModule *RegistryModule `jsonapi:"relation,registry-module"`
// Links
Links map[string]interface{} `jsonapi:"links,omitempty"`
}
type RegistryModulePermissions struct {
CanDelete bool `jsonapi:"attr,can-delete"`
CanResync bool `jsonapi:"attr,can-resync"`
CanRetry bool `jsonapi:"attr,can-retry"`
}
type RegistryModuleVersionStatuses struct {
Version string `jsonapi:"attr,version"`
Status RegistryModuleVersionStatus `jsonapi:"attr,status"`
Error string `jsonapi:"attr,error"`
}
// RegistryModuleCreateOptions is used when creating a registry module without a VCS repo
type RegistryModuleCreateOptions struct {
// Type is a public field utilized by JSON:API to
// set the resource type via the field tag.
// It is not a user-defined value and does not need to be set.
// https://jsonapi.org/format/#crud-creating
Type string `jsonapi:"primary,registry-modules"`
// Required:
Name *string `jsonapi:"attr,name"`
// Required:
Provider *string `jsonapi:"attr,provider"`
}
// RegistryModuleCreateVersionOptions is used when creating a registry module version
type RegistryModuleCreateVersionOptions struct {
// Type is a public field utilized by JSON:API to
// set the resource type via the field tag.
// It is not a user-defined value and does not need to be set.
// https://jsonapi.org/format/#crud-creating
Type string `jsonapi:"primary,registry-module-versions"`
Version *string `jsonapi:"attr,version"`
}
// RegistryModuleCreateWithVCSConnectionOptions is used when creating a registry module with a VCS repo
type RegistryModuleCreateWithVCSConnectionOptions struct {
// Type is a public field utilized by JSON:API to
// set the resource type via the field tag.
// It is not a user-defined value and does not need to be set.
// https://jsonapi.org/format/#crud-creating
Type string `jsonapi:"primary,registry-modules"`
// Required: VCS repository information
VCSRepo *RegistryModuleVCSRepoOptions `jsonapi:"attr,vcs-repo"`
}
type RegistryModuleVCSRepoOptions struct {
Identifier *string `json:"identifier"` // Required
OAuthTokenID *string `json:"oauth-token-id"` // Required
DisplayIdentifier *string `json:"display-identifier"` // Required
}
// Upload uploads Terraform configuration files for the provided registry module version. It
// requires a path to the configuration files on disk, which will be packaged by
// hashicorp/go-slug before being uploaded.
func (r *registryModules) Upload(ctx context.Context, rmv RegistryModuleVersion, path string) error {
uploadURL, ok := rmv.Links["upload"].(string)
if !ok {
return fmt.Errorf("provided RegistryModuleVersion does not contain an upload link")
}
body, err := packContents(path)
if err != nil {
return err
}
req, err := r.client.newRequest("PUT", uploadURL, body)
if err != nil {
return err
}
return r.client.do(ctx, req, nil)
}
// Create a new registry module without a VCS repo
func (r *registryModules) Create(ctx context.Context, organization string, options RegistryModuleCreateOptions) (*RegistryModule, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf(
"organizations/%s/registry-modules",
url.QueryEscape(organization),
)
req, err := r.client.newRequest("POST", u, &options)
if err != nil {
return nil, err
}
rm := &RegistryModule{}
err = r.client.do(ctx, req, rm)
if err != nil {
return nil, err
}
return rm, nil
}
// CreateVersion creates a new registry module version
func (r *registryModules) CreateVersion(ctx context.Context, moduleID RegistryModuleID, options RegistryModuleCreateVersionOptions) (*RegistryModuleVersion, error) {
if err := moduleID.valid(); err != nil {
return nil, err
}
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf(
"registry-modules/%s/%s/%s/versions",
url.QueryEscape(moduleID.Organization),
url.QueryEscape(moduleID.Name),
url.QueryEscape(moduleID.Provider),
)
req, err := r.client.newRequest("POST", u, &options)
if err != nil {
return nil, err
}
rmv := &RegistryModuleVersion{}
err = r.client.do(ctx, req, rmv)
if err != nil {
return nil, err
}
return rmv, nil
}
// CreateWithVCSConnection is used to create and publish a new registry module with a VCS repo
func (r *registryModules) CreateWithVCSConnection(ctx context.Context, options RegistryModuleCreateWithVCSConnectionOptions) (*RegistryModule, error) {
if err := options.valid(); err != nil {
return nil, err
}
req, err := r.client.newRequest("POST", "registry-modules", &options)
if err != nil {
return nil, err
}
rm := &RegistryModule{}
err = r.client.do(ctx, req, rm)
if err != nil {
return nil, err
}
return rm, nil
}
// Read a specific registry module
func (r *registryModules) Read(ctx context.Context, moduleID RegistryModuleID) (*RegistryModule, error) {
if err := moduleID.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf(
"registry-modules/show/%s/%s/%s",
url.QueryEscape(moduleID.Organization),
url.QueryEscape(moduleID.Name),
url.QueryEscape(moduleID.Provider),
)
req, err := r.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
rm := &RegistryModule{}
err = r.client.do(ctx, req, rm)
if err != nil {
return nil, err
}
return rm, nil
}
// Delete is used to delete the entire registry module
func (r *registryModules) Delete(ctx context.Context, organization, name string) error {
if !validStringID(&organization) {
return ErrInvalidOrg
}
if !validString(&name) {
return ErrRequiredName
}
if !validStringID(&name) {
return ErrInvalidName
}
u := fmt.Sprintf(
"registry-modules/actions/delete/%s/%s",
url.QueryEscape(organization),
url.QueryEscape(name),
)
req, err := r.client.newRequest("POST", u, nil)
if err != nil {
return err
}
return r.client.do(ctx, req, nil)
}
// DeleteProvider is used to delete the specific registry module provider
func (r *registryModules) DeleteProvider(ctx context.Context, moduleID RegistryModuleID) error {
if err := moduleID.valid(); err != nil {
return err
}
u := fmt.Sprintf(
"registry-modules/actions/delete/%s/%s/%s",
url.QueryEscape(moduleID.Organization),
url.QueryEscape(moduleID.Name),
url.QueryEscape(moduleID.Provider),
)
req, err := r.client.newRequest("POST", u, nil)
if err != nil {
return err
}
return r.client.do(ctx, req, nil)
}
// DeleteVersion is used to delete the specific registry module version
func (r *registryModules) DeleteVersion(ctx context.Context, moduleID RegistryModuleID, version string) error {
if err := moduleID.valid(); err != nil {
return err
}
if !validString(&version) {
return ErrRequiredVersion
}
if !validStringID(&version) {
return ErrInvalidVersion
}
u := fmt.Sprintf(
"registry-modules/actions/delete/%s/%s/%s/%s",
url.QueryEscape(moduleID.Organization),
url.QueryEscape(moduleID.Name),
url.QueryEscape(moduleID.Provider),
url.QueryEscape(version),
)
req, err := r.client.newRequest("POST", u, nil)
if err != nil {
return err
}
return r.client.do(ctx, req, nil)
}
func (o RegistryModuleID) valid() error {
if !validStringID(&o.Organization) {
return ErrInvalidOrg
}
if !validString(&o.Name) {
return ErrRequiredName
}
if !validStringID(&o.Name) {
return ErrInvalidName
}
if !validString(&o.Provider) {
return ErrRequiredProvider
}
if !validStringID(&o.Provider) {
return ErrInvalidProvider
}
return nil
}
func (o RegistryModuleCreateOptions) valid() error {
if !validString(o.Name) {
return ErrRequiredName
}
if !validStringID(o.Name) {
return ErrInvalidName
}
if !validString(o.Provider) {
return ErrRequiredProvider
}
if !validStringID(o.Provider) {
return ErrInvalidProvider
}
return nil
}
func (o RegistryModuleCreateVersionOptions) valid() error {
if !validString(o.Version) {
return ErrRequiredVersion
}
if !validStringID(o.Version) {
return ErrInvalidVersion
}
return nil
}
func (o RegistryModuleCreateWithVCSConnectionOptions) valid() error {
if o.VCSRepo == nil {
return ErrRequiredVCSRepo
}
return o.VCSRepo.valid()
}
func (o RegistryModuleVCSRepoOptions) valid() error {
if !validString(o.Identifier) {
return ErrRequiredIdentifier
}
if !validString(o.OAuthTokenID) {
return ErrRequiredOauthTokenID
}
if !validString(o.DisplayIdentifier) {
return ErrRequiredDisplayIdentifier
}
return nil
}