Skip to content

Commit

Permalink
feat: Add no-code endpoint for fetching registry module variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
paladin-devops committed Sep 23, 2024
1 parent ec9ee35 commit 4a7bf25
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
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.

90 changes: 90 additions & 0 deletions registry_no_code_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"context"
"fmt"
"net/url"

"github.com/hashicorp/go-hclog"
)

// Compile-time proof of interface implementation.
Expand All @@ -26,6 +28,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 +47,44 @@ type RegistryNoCodeModules interface {
UpgradeWorkspace(ctx context.Context, noCodeModuleID string, workspaceID string, options *RegistryNoCodeModuleUpgradeWorkspaceOptions) (*WorkspaceUpgrade, error)
}

// RegistryModuleVariableList is a list of registry module variables.
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 +204,14 @@ type RegistryNoCodeModuleReadOptions struct {
Include []RegistryNoCodeModuleIncludeOpt `url:"include,omitempty"`
}

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 +295,44 @@ 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) {
log := hclog.FromContext(ctx)

Check failure on line 305 in registry_no_code_module.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: hclog (typecheck)
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 {
log.Error("error reading no-code module variables",
"error", err,
"request", req,
"response", resp)
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

0 comments on commit 4a7bf25

Please sign in to comment.