Skip to content

Commit

Permalink
Add support for test_variables#read (#851)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaabdelgany committed Mar 5, 2024
1 parent c40506c commit c8e6fd2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Enhancements
* Adds `description` attribute to `Project` by @netramali [861](https://github.com/hashicorp/go-tfe/pull/861)
* Adds `Read` method to `TestVariables` @aaabdelgany [#851](https://github.com/hashicorp/go-tfe/pull/851)

# v1.46.0

Expand Down
29 changes: 29 additions & 0 deletions test_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type TestVariables interface {
// List all the test variables associated with the given module.
List(ctx context.Context, moduleID RegistryModuleID, options *VariableListOptions) (*VariableList, error)

// Read a test variable by its ID.
Read(ctx context.Context, moduleID RegistryModuleID, variableID string) (*Variable, error)

// Create is used to create a new variable.
Create(ctx context.Context, moduleID RegistryModuleID, options VariableCreateOptions) (*Variable, error)

Expand Down Expand Up @@ -55,6 +58,32 @@ func (s *testVariables) List(ctx context.Context, moduleID RegistryModuleID, opt
return vl, nil
}

// Read a variable by its ID.
func (s *testVariables) Read(ctx context.Context, moduleID RegistryModuleID, variableID string) (*Variable, error) {
if err := moduleID.valid(); err != nil {
return nil, err
}

if !validStringID(&variableID) {
return nil, ErrInvalidVariableID
}

u := fmt.Sprintf("%s/%s", testVarsPath(moduleID), url.QueryEscape(variableID))

req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}

v := &Variable{}
err = req.Do(ctx, v)
if err != nil {
return nil, err
}

return v, err
}

// Create is used to create a new variable.
func (s *testVariables) Create(ctx context.Context, moduleID RegistryModuleID, options VariableCreateOptions) (*Variable, error) {
if err := moduleID.valid(); err != nil {
Expand Down
48 changes: 48 additions & 0 deletions test_variables_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,54 @@ func TestTestVariablesList(t *testing.T) {
})
}

func TestTestVariablesRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()

orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()

rmTest, registryModuleTestCleanup := createBranchBasedRegistryModule(t, client, orgTest)
defer registryModuleTestCleanup()

id := RegistryModuleID{
Organization: orgTest.Name,
Name: rmTest.Name,
Provider: rmTest.Provider,
Namespace: rmTest.Namespace,
RegistryName: rmTest.RegistryName,
}

tv, tvCleanup := createTestVariable(t, client, rmTest)

defer tvCleanup()

t.Run("when the variable exists", func(t *testing.T) {
v, err := client.TestVariables.Read(ctx, id, tv.ID)

require.NoError(t, err)
assert.Equal(t, tv.ID, v.ID)
assert.Equal(t, tv.Category, v.Category)
assert.Equal(t, tv.HCL, v.HCL)
assert.Equal(t, tv.Key, v.Key)
assert.Equal(t, tv.Sensitive, v.Sensitive)
assert.Equal(t, tv.Value, v.Value)
assert.Equal(t, tv.VersionID, v.VersionID)
})

t.Run("when the variable does not exist", func(t *testing.T) {
v, err := client.TestVariables.Read(ctx, id, "nonexisting")
assert.Nil(t, v)
assert.Equal(t, ErrResourceNotFound, err)
})

t.Run("without a valid module ID", func(t *testing.T) {
v, err := client.TestVariables.Read(ctx, RegistryModuleID{}, tv.ID)
assert.Nil(t, v)
assert.EqualError(t, err, ErrInvalidOrg.Error())
})
}

func TestTestVariablesCreate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
Expand Down

0 comments on commit c8e6fd2

Please sign in to comment.