forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry_no_code_module.go
463 lines (367 loc) · 14.7 KB
/
registry_no_code_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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"context"
"fmt"
"net/url"
)
// Compile-time proof of interface implementation.
var _ RegistryNoCodeModules = (*registryNoCodeModules)(nil)
// RegistryNoCodeModules describes all the registry no-code module related methods that the Terraform
// Enterprise API supports.
//
// TFE API docs: (TODO: Add link to API docs)
type RegistryNoCodeModules interface {
// Create a registry no-code module
// **Note: This API is still in BETA and subject to change.**
Create(ctx context.Context, organization string, options RegistryNoCodeModuleCreateOptions) (*RegistryNoCodeModule, error)
// Read a registry no-code module
// **Note: This API is still in BETA and subject to change.**
Read(ctx context.Context, noCodeModuleID string, options *RegistryNoCodeModuleReadOptions) (*RegistryNoCodeModule, error)
// ReadVariables returns the variables for a version of a no-code module
// **Note: This API is still in BETA and subject to change.**
ReadVariables(ctx context.Context, noCodeModuleID, noCodeModuleVersion string, options *RegistryNoCodeModuleReadVariablesOptions) (*RegistryModuleVariableList, error)
// Update a registry no-code module
// **Note: This API is still in BETA and subject to change.**
Update(ctx context.Context, noCodeModuleID string, options RegistryNoCodeModuleUpdateOptions) (*RegistryNoCodeModule, error)
// Delete a registry no-code module
// **Note: This API is still in BETA and subject to change.**
Delete(ctx context.Context, ID string) error
// CreateWorkspace creates a workspace using a no-code module.
CreateWorkspace(ctx context.Context, noCodeModuleID string, options *RegistryNoCodeModuleCreateWorkspaceOptions) (*Workspace, error)
// UpgradeWorkspace initiates an upgrade of an existing no-code module workspace.
UpgradeWorkspace(ctx context.Context, noCodeModuleID string, workspaceID string, options *RegistryNoCodeModuleUpgradeWorkspaceOptions) (*WorkspaceUpgrade, error)
}
// RegistryModuleVariableList is a list of registry module variables.
// **Note: This API is still in BETA and subject to change.**
type RegistryModuleVariableList struct {
Items []*RegistryModuleVariable
// NOTE: At the time of authoring this comment, the API endpoint to fetch
// registry module variables does not support pagination. This field is
// included to satisfy jsonapi unmarshaler implementation here:
// https://github.com/hashicorp/go-tfe/blob/3d29602707fa4b10469d1a02685644bd159d3ccc/tfe.go#L859
*Pagination
}
// RegistryModuleVariable represents a registry module variable.
type RegistryModuleVariable struct {
// ID is the ID of the variable.
ID string `jsonapi:"primary,registry-module-variables"`
// Name is the name of the variable.
Name string `jsonapi:"attr,name"`
// VariableType is the type of the variable.
VariableType string `jsonapi:"attr,type"`
// Description is the description of the variable.
Description string `jsonapi:"attr,description"`
// Required is a boolean indicating if the variable is required.
Required bool `jsonapi:"attr,required"`
// Sensitive is a boolean indicating if the variable is sensitive.
Sensitive bool `jsonapi:"attr,sensitive"`
// Options is a slice of strings representing the options for the variable.
Options []string `jsonapi:"attr,options"`
// HasGlobal is a boolean indicating if the variable is global.
HasGlobal bool `jsonapi:"attr,has-global"`
}
type RegistryNoCodeModuleCreateWorkspaceOptions struct {
Type string `jsonapi:"primary,no-code-module-workspace"`
// Name is the name of the workspace, which can only include letters,
// numbers, and _. This will be used as an identifier and must be unique in
// the organization.
Name string `jsonapi:"attr,name"`
// Description is a description for the workspace.
Description *string `jsonapi:"attr,description,omitempty"`
AutoApply *bool `jsonapi:"attr,auto-apply,omitempty"`
// Project is the associated project with the workspace. If not provided,
// default project of the organization will be assigned to the workspace.
Project *Project `jsonapi:"relation,project,omitempty"`
// Variables is the slice of variables to be configured for the no-code
// workspace.
Variables []*Variable `jsonapi:"relation,vars,omitempty"`
// SourceName is the name of the source of the workspace.
SourceName *string `jsonapi:"attr,source-name,omitempty"`
// SourceUrl is the URL of the source of the workspace.
SourceURL *string `jsonapi:"attr,source-url,omitempty"`
// ExecutionMode is the execution mode of the workspace.
ExecutionMode *string `jsonapi:"attr,execution-mode,omitempty"`
// AgentPoolId is the ID of the agent pool to use for the workspace.
// This is required when execution mode is set to "agent".
// This must not be specified when execution mode is set to "remote".
AgentPoolID *string `jsonapi:"attr,agent-pool-id,omitempty"`
}
type RegistryNoCodeModuleUpgradeWorkspaceOptions struct {
Type string `jsonapi:"primary,no-code-module-workspace"`
// Variables is the slice of variables to be configured for the no-code
// workspace.
Variables []*Variable `jsonapi:"relation,vars,omitempty"`
}
// registryNoCodeModules implements RegistryNoCodeModules.
type registryNoCodeModules struct {
client *Client
}
// RegistryNoCodeModule represents a registry no-code module
type RegistryNoCodeModule struct {
ID string `jsonapi:"primary,no-code-modules"`
VersionPin string `jsonapi:"attr,version-pin"`
Enabled bool `jsonapi:"attr,enabled"`
// Relations
Organization *Organization `jsonapi:"relation,organization"`
RegistryModule *RegistryModule `jsonapi:"relation,registry-module"`
VariableOptions []*NoCodeVariableOption `jsonapi:"relation,variable-options"`
}
// NoCodeVariableOption represents a registry no-code module variable and its
// options.
type NoCodeVariableOption struct {
// Type is a public field utilized by JSON:API to
// set the resource type via the field tag.
Type string `jsonapi:"primary,variable-options"`
// Required: The variable name
VariableName string `jsonapi:"attr,variable-name"`
// Required: The variable type
VariableType string `jsonapi:"attr,variable-type"`
// Optional: The options for the variable
Options []string `jsonapi:"attr,options"`
}
// RegistryNoCodeModuleCreateOptions is used when creating a registry no-code module
type RegistryNoCodeModuleCreateOptions 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,no-code-modules"`
// Required: the registry module to use for the no-code module (only the ID is used)
RegistryModule *RegistryModule `jsonapi:"relation,registry-module"`
// Optional: whether no-code is enabled for the module
Enabled *bool `jsonapi:"attr,enabled,omitempty"`
// Optional: the version pin for the module. valid values are "latest" or a semver string
VersionPin string `jsonapi:"attr,version-pin,omitempty"`
// Optional: the variable options for the registry module
VariableOptions []*NoCodeVariableOption `jsonapi:"relation,variable-options,omitempty"`
}
// RegistryNoCodeModuleIncludeOpt represents the available options for include query params.
type RegistryNoCodeModuleIncludeOpt string
var (
// RegistryNoCodeIncludeVariableOptions is used to include variable options in the response
RegistryNoCodeIncludeVariableOptions RegistryNoCodeModuleIncludeOpt = "variable-options"
)
// RegistryNoCodeModuleReadOptions is used when reading a registry no-code module
type RegistryNoCodeModuleReadOptions 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-updating
Type string `jsonapi:"primary,no-code-modules"`
// Optional: Include is used to specify the related resources to include in the response.
Include []RegistryNoCodeModuleIncludeOpt `url:"include,omitempty"`
}
// RegistryNoCodeModuleReadVariablesOptions is used when reading the variables
// for a no-code module.
type RegistryNoCodeModuleReadVariablesOptions 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-updating
Type string `jsonapi:"primary,no-code-modules"`
}
// RegistryNoCodeModuleUpdateOptions is used when updating a registry no-code module
type RegistryNoCodeModuleUpdateOptions 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-updating
Type string `jsonapi:"primary,no-code-modules"`
// Required: the registry module to use for the no-code module (only the ID is used)
RegistryModule *RegistryModule `jsonapi:"relation,registry-module"`
// Optional: the version pin for the module. valid values are "latest" or a semver string
VersionPin string `jsonapi:"attr,version-pin,omitempty"`
// Optional: whether no-code is enabled for the module
Enabled *bool `jsonapi:"attr,enabled,omitempty"`
// Optional: are the variable options for the module
VariableOptions []*NoCodeVariableOption `jsonapi:"relation,variable-options,omitempty"`
}
// WorkspaceUpgrade contains the data returned by the no-code workspace upgrade
// API endpoint.
type WorkspaceUpgrade struct {
// Status is the status of the run of the upgrade
Status string `jsonapi:"attr,status"`
// PlanURL is the URL to the plan of the upgrade
PlanURL string `jsonapi:"attr,plan-url"`
// Message is the message returned by the API when an upgrade is not available.
Message string `jsonapi:"attr,message"`
}
// Create a new registry no-code module
func (r *registryNoCodeModules) Create(ctx context.Context, organization string, options RegistryNoCodeModuleCreateOptions) (*RegistryNoCodeModule, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf("organizations/%s/no-code-modules", url.PathEscape(organization))
req, err := r.client.NewRequest("POST", u, &options)
if err != nil {
return nil, err
}
rm := &RegistryNoCodeModule{}
err = req.Do(ctx, rm)
if err != nil {
return nil, err
}
return rm, nil
}
// Read a registry no-code module
func (r *registryNoCodeModules) Read(ctx context.Context, noCodeModuleID string, options *RegistryNoCodeModuleReadOptions) (*RegistryNoCodeModule, error) {
if !validStringID(&noCodeModuleID) {
return nil, ErrInvalidModuleID
}
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf("no-code-modules/%s", url.PathEscape(noCodeModuleID))
req, err := r.client.NewRequest("GET", u, options)
if err != nil {
return nil, err
}
rm := &RegistryNoCodeModule{}
err = req.Do(ctx, rm)
if err != nil {
return nil, err
}
return rm, nil
}
// ReadVariables retrieves the no-code variable options for a version of a
// module.
func (r *registryNoCodeModules) ReadVariables(
ctx context.Context,
noCodeModuleID, noCodeModuleVersion string,
options *RegistryNoCodeModuleReadVariablesOptions,
) (*RegistryModuleVariableList, error) {
if !validStringID(&noCodeModuleID) {
return nil, ErrInvalidModuleID
}
if !validVersion(noCodeModuleVersion) {
return nil, ErrInvalidVersion
}
u := fmt.Sprintf(
"no-code-modules/%s/versions/%s/module-variables",
url.PathEscape(noCodeModuleID),
url.PathEscape(noCodeModuleVersion),
)
req, err := r.client.NewRequest("GET", u, options)
if err != nil {
return nil, err
}
resp := &RegistryModuleVariableList{}
err = req.Do(ctx, resp)
if err != nil {
return nil, err
}
return resp, nil
}
// Update a registry no-code module
func (r *registryNoCodeModules) Update(ctx context.Context, noCodeModuleID string, options RegistryNoCodeModuleUpdateOptions) (*RegistryNoCodeModule, error) {
if !validString(&noCodeModuleID) {
return nil, ErrInvalidModuleID
}
if !validStringID(&noCodeModuleID) {
return nil, ErrInvalidModuleID
}
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf("no-code-modules/%s", url.PathEscape(noCodeModuleID))
req, err := r.client.NewRequest("PATCH", u, &options)
if err != nil {
return nil, err
}
rm := &RegistryNoCodeModule{}
err = req.Do(ctx, rm)
if err != nil {
return nil, err
}
return rm, nil
}
// Delete is used to delete the registry no-code module
func (r *registryNoCodeModules) Delete(ctx context.Context, noCodeModuleID string) error {
if !validStringID(&noCodeModuleID) {
return ErrInvalidModuleID
}
u := fmt.Sprintf("no-code-modules/%s", url.PathEscape(noCodeModuleID))
req, err := r.client.NewRequest("DELETE", u, nil)
if err != nil {
return err
}
return req.Do(ctx, nil)
}
// CreateWorkspace creates a no-code workspace using a no-code module.
func (r *registryNoCodeModules) CreateWorkspace(
ctx context.Context,
noCodeModuleID string,
options *RegistryNoCodeModuleCreateWorkspaceOptions,
) (*Workspace, error) {
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf("no-code-modules/%s/workspaces", url.PathEscape(noCodeModuleID))
req, err := r.client.NewRequest("POST", u, options)
if err != nil {
return nil, err
}
w := &Workspace{}
err = req.Do(ctx, w)
if err != nil {
return nil, err
}
return w, nil
}
// UpgradeWorkspace initiates an upgrade of an existing no-code module workspace.
func (r *registryNoCodeModules) UpgradeWorkspace(
ctx context.Context,
noCodeModuleID string,
workspaceID string,
options *RegistryNoCodeModuleUpgradeWorkspaceOptions,
) (*WorkspaceUpgrade, error) {
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf("no-code-modules/%s/workspaces/%s/upgrade",
url.PathEscape(noCodeModuleID),
workspaceID,
)
req, err := r.client.NewRequest("POST", u, options)
if err != nil {
return nil, err
}
wu := &WorkspaceUpgrade{}
err = req.Do(ctx, wu)
if err != nil {
return nil, err
}
return wu, nil
}
func (o RegistryNoCodeModuleCreateOptions) valid() error {
if o.RegistryModule == nil || o.RegistryModule.ID == "" {
return ErrRequiredRegistryModule
}
return nil
}
func (o *RegistryNoCodeModuleUpdateOptions) valid() error {
if o == nil {
return nil // nothing to validate
}
if o.RegistryModule == nil || o.RegistryModule.ID == "" {
return ErrRequiredRegistryModule
}
return nil
}
func (o *RegistryNoCodeModuleReadOptions) valid() error {
return nil
}
func (o *RegistryNoCodeModuleCreateWorkspaceOptions) valid() error {
if !validString(&o.Name) {
return ErrRequiredName
}
return nil
}
func (o *RegistryNoCodeModuleUpgradeWorkspaceOptions) valid() error {
return nil
}