Skip to content

Commit

Permalink
Merge pull request #979 from hashicorp/no-code-read-registry-module-v…
Browse files Browse the repository at this point in the history
…ariables

Add no-code endpoint for fetching registry module variables.
  • Loading branch information
paladin-devops authored Sep 30, 2024
2 parents 0b3525a + e9a34c2 commit f3328da
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Enhancements

* Add support for reading a no-code module's variables by @paladin-devops [#979](https://github.com/hashicorp/go-tfe/pull/979)

# v1.67.1

## Bug Fixes
Expand Down
15 changes: 15 additions & 0 deletions mocks/registry_no_code_module_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions registry_no_code_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ type RegistryNoCodeModules interface {
// **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)
Expand All @@ -41,6 +45,45 @@ type RegistryNoCodeModules interface {
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"`

Expand Down Expand Up @@ -160,6 +203,16 @@ type RegistryNoCodeModuleReadOptions struct {
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
Expand Down Expand Up @@ -243,6 +296,39 @@ func (r *registryNoCodeModules) Read(ctx context.Context, noCodeModuleID string,
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) {
Expand Down
28 changes: 28 additions & 0 deletions registry_no_code_module_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,34 @@ func TestRegistryNoCodeModulesRead(t *testing.T) {
})
}

// TestRegistryNoCodeModuleReadVariables tests the ReadVariables method of the
// RegistryNoCodeModules service.
//
// This test requires that the environment variable "GITHUB_REGISTRY_NO_CODE_MODULE_IDENTIFIER" is set
// with the ID of an existing no-code module that has variables.
func TestRegistryNoCodeModulesReadVariables(t *testing.T) {
skipUnlessBeta(t)
client := testClient(t)
ctx := context.Background()
r := require.New(t)

ncmID := os.Getenv("GITHUB_REGISTRY_NO_CODE_MODULE_IDENTIFIER")
if ncmID == "" {
t.Skip("Export a valid GITHUB_REGISTRY_NO_CODE_MODULE_IDENTIFIER before running this test")
}

ncm, err := client.RegistryNoCodeModules.Read(ctx, ncmID, nil)
r.NoError(err)
r.NotNil(ncm)

t.Run("happy path", func(t *testing.T) {
vars, err := client.RegistryNoCodeModules.ReadVariables(ctx, ncm.ID, ncm.VersionPin, &RegistryNoCodeModuleReadVariablesOptions{})
r.NoError(err)
r.NotNil(vars)
r.NotEmpty(vars)
})
}

func TestRegistryNoCodeModulesUpdate(t *testing.T) {
skipUnlessBeta(t)
client := testClient(t)
Expand Down

0 comments on commit f3328da

Please sign in to comment.