Skip to content

Commit f3328da

Browse files
Merge pull request #979 from hashicorp/no-code-read-registry-module-variables
Add no-code endpoint for fetching registry module variables.
2 parents 0b3525a + e9a34c2 commit f3328da

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Enhancements
2+
3+
* Add support for reading a no-code module's variables by @paladin-devops [#979](https://github.com/hashicorp/go-tfe/pull/979)
4+
15
# v1.67.1
26

37
## Bug Fixes

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

@@ -160,6 +203,16 @@ type RegistryNoCodeModuleReadOptions struct {
160203
Include []RegistryNoCodeModuleIncludeOpt `url:"include,omitempty"`
161204
}
162205

206+
// RegistryNoCodeModuleReadVariablesOptions is used when reading the variables
207+
// for a no-code module.
208+
type RegistryNoCodeModuleReadVariablesOptions struct {
209+
// Type is a public field utilized by JSON:API to
210+
// set the resource type via the field tag.
211+
// It is not a user-defined value and does not need to be set.
212+
// https://jsonapi.org/format/#crud-updating
213+
Type string `jsonapi:"primary,no-code-modules"`
214+
}
215+
163216
// RegistryNoCodeModuleUpdateOptions is used when updating a registry no-code module
164217
type RegistryNoCodeModuleUpdateOptions struct {
165218
// Type is a public field utilized by JSON:API to
@@ -243,6 +296,39 @@ func (r *registryNoCodeModules) Read(ctx context.Context, noCodeModuleID string,
243296
return rm, nil
244297
}
245298

299+
// ReadVariables retrieves the no-code variable options for a version of a
300+
// module.
301+
func (r *registryNoCodeModules) ReadVariables(
302+
ctx context.Context,
303+
noCodeModuleID, noCodeModuleVersion string,
304+
options *RegistryNoCodeModuleReadVariablesOptions,
305+
) (*RegistryModuleVariableList, error) {
306+
if !validStringID(&noCodeModuleID) {
307+
return nil, ErrInvalidModuleID
308+
}
309+
if !validVersion(noCodeModuleVersion) {
310+
return nil, ErrInvalidVersion
311+
}
312+
313+
u := fmt.Sprintf(
314+
"no-code-modules/%s/versions/%s/module-variables",
315+
url.PathEscape(noCodeModuleID),
316+
url.PathEscape(noCodeModuleVersion),
317+
)
318+
req, err := r.client.NewRequest("GET", u, options)
319+
if err != nil {
320+
return nil, err
321+
}
322+
323+
resp := &RegistryModuleVariableList{}
324+
err = req.Do(ctx, resp)
325+
if err != nil {
326+
return nil, err
327+
}
328+
329+
return resp, nil
330+
}
331+
246332
// Update a registry no-code module
247333
func (r *registryNoCodeModules) Update(ctx context.Context, noCodeModuleID string, options RegistryNoCodeModuleUpdateOptions) (*RegistryNoCodeModule, error) {
248334
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 "GITHUB_REGISTRY_NO_CODE_MODULE_IDENTIFIER" 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("GITHUB_REGISTRY_NO_CODE_MODULE_IDENTIFIER")
226+
if ncmID == "" {
227+
t.Skip("Export a valid GITHUB_REGISTRY_NO_CODE_MODULE_IDENTIFIER 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)