Skip to content

Commit d332cb2

Browse files
feat: Add no-code endpoint for fetching registry module variables.
1 parent 0b3525a commit d332cb2

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
* Fixes a bug in `NewRequest` that did not allow query parameters to be specified in the first parameter, which broke several methods: `RegistryModules ReadVersion`, `VariableSets UpdateWorkspaces`, and `Workspaces Readme` by @brandonc [#982](https://github.com/hashicorp/go-tfe/pull/982)
66

7+
## Enhancements
8+
9+
* Add support for reading a no-code module's variables by @paladin-devops [#979](https://github.com/hashicorp/go-tfe/pull/979)
10+
711
# v1.67.0
812

913
## Enhancements

mocks/registry_no_code_module_mocks.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

registry_no_code_module.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ type RegistryNoCodeModules interface {
2626
// **Note: This API is still in BETA and subject to change.**
2727
Read(ctx context.Context, noCodeModuleID string, options *RegistryNoCodeModuleReadOptions) (*RegistryNoCodeModule, error)
2828

29+
// ReadVariables returns the variables for a version of a no-code module
30+
// **Note: This API is still in BETA and subject to change.**
31+
ReadVariables(ctx context.Context, noCodeModuleID, noCodeModuleVersion string, options *RegistryNoCodeModuleReadVariablesOptions) (*RegistryModuleVariableList, error)
32+
2933
// Update a registry no-code module
3034
// **Note: This API is still in BETA and subject to change.**
3135
Update(ctx context.Context, noCodeModuleID string, options RegistryNoCodeModuleUpdateOptions) (*RegistryNoCodeModule, error)
@@ -41,6 +45,44 @@ type RegistryNoCodeModules interface {
4145
UpgradeWorkspace(ctx context.Context, noCodeModuleID string, workspaceID string, options *RegistryNoCodeModuleUpgradeWorkspaceOptions) (*WorkspaceUpgrade, error)
4246
}
4347

48+
// RegistryModuleVariableList is a list of registry module variables.
49+
type RegistryModuleVariableList struct {
50+
Items []*RegistryModuleVariable
51+
52+
// NOTE: At the time of authoring this comment, the API endpoint to fetch
53+
// registry module variables does not support pagination. This field is
54+
// included to satisfy jsonapi unmarshaler implementation here:
55+
// https://github.com/hashicorp/go-tfe/blob/3d29602707fa4b10469d1a02685644bd159d3ccc/tfe.go#L859
56+
*Pagination
57+
}
58+
59+
// RegistryModuleVariable represents a registry module variable.
60+
type RegistryModuleVariable struct {
61+
// ID is the ID of the variable.
62+
ID string `jsonapi:"primary,registry-module-variables"`
63+
64+
// Name is the name of the variable.
65+
Name string `jsonapi:"attr,name"`
66+
67+
// VariableType is the type of the variable.
68+
VariableType string `jsonapi:"attr,type"`
69+
70+
// Description is the description of the variable.
71+
Description string `jsonapi:"attr,description"`
72+
73+
// Required is a boolean indicating if the variable is required.
74+
Required bool `jsonapi:"attr,required"`
75+
76+
// Sensitive is a boolean indicating if the variable is sensitive.
77+
Sensitive bool `jsonapi:"attr,sensitive"`
78+
79+
// Options is a slice of strings representing the options for the variable.
80+
Options []string `jsonapi:"attr,options"`
81+
82+
// HasGlobal is a boolean indicating if the variable is global.
83+
HasGlobal bool `jsonapi:"attr,has-global"`
84+
}
85+
4486
type RegistryNoCodeModuleCreateWorkspaceOptions struct {
4587
Type string `jsonapi:"primary,no-code-module-workspace"`
4688

@@ -160,6 +202,14 @@ type RegistryNoCodeModuleReadOptions struct {
160202
Include []RegistryNoCodeModuleIncludeOpt `url:"include,omitempty"`
161203
}
162204

205+
type RegistryNoCodeModuleReadVariablesOptions struct {
206+
// Type is a public field utilized by JSON:API to
207+
// set the resource type via the field tag.
208+
// It is not a user-defined value and does not need to be set.
209+
// https://jsonapi.org/format/#crud-updating
210+
Type string `jsonapi:"primary,no-code-modules"`
211+
}
212+
163213
// RegistryNoCodeModuleUpdateOptions is used when updating a registry no-code module
164214
type RegistryNoCodeModuleUpdateOptions struct {
165215
// Type is a public field utilized by JSON:API to
@@ -243,6 +293,39 @@ func (r *registryNoCodeModules) Read(ctx context.Context, noCodeModuleID string,
243293
return rm, nil
244294
}
245295

296+
// ReadVariables retrieves the no-code variable options for a version of a
297+
// module.
298+
func (r *registryNoCodeModules) ReadVariables(
299+
ctx context.Context,
300+
noCodeModuleID, noCodeModuleVersion string,
301+
options *RegistryNoCodeModuleReadVariablesOptions,
302+
) (*RegistryModuleVariableList, error) {
303+
if !validStringID(&noCodeModuleID) {
304+
return nil, ErrInvalidModuleID
305+
}
306+
if !validVersion(noCodeModuleVersion) {
307+
return nil, ErrInvalidVersion
308+
}
309+
310+
u := fmt.Sprintf(
311+
"no-code-modules/%s/versions/%s/module-variables",
312+
url.PathEscape(noCodeModuleID),
313+
url.PathEscape(noCodeModuleVersion),
314+
)
315+
req, err := r.client.NewRequest("GET", u, options)
316+
if err != nil {
317+
return nil, err
318+
}
319+
320+
resp := &RegistryModuleVariableList{}
321+
err = req.Do(ctx, resp)
322+
if err != nil {
323+
return nil, err
324+
}
325+
326+
return resp, nil
327+
}
328+
246329
// Update a registry no-code module
247330
func (r *registryNoCodeModules) Update(ctx context.Context, noCodeModuleID string, options RegistryNoCodeModuleUpdateOptions) (*RegistryNoCodeModule, error) {
248331
if !validString(&noCodeModuleID) {

registry_no_code_module_integration_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,34 @@ func TestRegistryNoCodeModulesRead(t *testing.T) {
211211
})
212212
}
213213

214+
// TestRegistryNoCodeModuleReadVariables tests the ReadVariables method of the
215+
// RegistryNoCodeModules service.
216+
//
217+
// This test requires that the environment variable "NO_CODE_MODULE_ID" is set
218+
// with the ID of an existing no-code module that has variables.
219+
func TestRegistryNoCodeModulesReadVariables(t *testing.T) {
220+
skipUnlessBeta(t)
221+
client := testClient(t)
222+
ctx := context.Background()
223+
r := require.New(t)
224+
225+
ncmID := os.Getenv("NO_CODE_MODULE_ID")
226+
if ncmID == "" {
227+
t.Skip("Export a valid NO_CODE_MODULE_ID before running this test")
228+
}
229+
230+
ncm, err := client.RegistryNoCodeModules.Read(ctx, ncmID, nil)
231+
r.NoError(err)
232+
r.NotNil(ncm)
233+
234+
t.Run("happy path", func(t *testing.T) {
235+
vars, err := client.RegistryNoCodeModules.ReadVariables(ctx, ncm.ID, ncm.VersionPin, &RegistryNoCodeModuleReadVariablesOptions{})
236+
r.NoError(err)
237+
r.NotNil(vars)
238+
r.NotEmpty(vars)
239+
})
240+
}
241+
214242
func TestRegistryNoCodeModulesUpdate(t *testing.T) {
215243
skipUnlessBeta(t)
216244
client := testClient(t)

0 commit comments

Comments
 (0)