/
registry_no_code_module.go
248 lines (196 loc) · 7.79 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
// 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)
// 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
}
// 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"`
}
// 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"`
}
// 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.QueryEscape(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.QueryEscape(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
}
// 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.QueryEscape(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.QueryEscape(noCodeModuleID))
req, err := r.client.NewRequest("DELETE", u, nil)
if err != nil {
return err
}
return req.Do(ctx, 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
}