Skip to content

Commit

Permalink
Merge pull request #552 from hashicorp/list-workspace-variable-sets
Browse files Browse the repository at this point in the history
List workspace variable sets
  • Loading branch information
sebasslash committed Oct 17, 2022
2 parents 27c5ea1 + 5d4da4e commit 5c31a0c
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,7 +1,9 @@
# (Unreleased)

## Enhancements

* Adds `Query` and `Status` fields to `OrganizationMembershipListOptions` to allow filtering memberships by status or username by @sebasslash [#550](https://github.com/hashicorp/go-tfe/pull/550)
* Add `ListForWorkspace` method to `VariableSets` interface to enable fetching variable sets associated with a workspace. [#551](https://github.com/hashicorp/go-tfe/pull/551)

# v1.10.0

Expand Down
28 changes: 28 additions & 0 deletions helper_test.go
Expand Up @@ -1742,6 +1742,34 @@ func createVariableSet(t *testing.T, client *Client, org *Organization, options
}
}

func applyVariableSetToWorkspace(t *testing.T, client *Client, vsID string, wsID string) {
if vsID == "" {
t.Fatal("variable set ID must not be empty")
}

if wsID == "" {
t.Fatal("workspace ID must not be empty")
}

opts := &VariableSetApplyToWorkspacesOptions{}
opts.Workspaces = append(opts.Workspaces, &Workspace{ID: wsID})

ctx := context.Background()
if err := client.VariableSets.ApplyToWorkspaces(ctx, vsID, opts); err != nil {
t.Fatalf("Error applying variable set %s to workspace %s: %v", vsID, wsID, err)
}

t.Cleanup(func() {
removeOpts := &VariableSetRemoveFromWorkspacesOptions{}
removeOpts.Workspaces = append(removeOpts.Workspaces, &Workspace{ID: wsID})
if err := client.VariableSets.RemoveFromWorkspaces(ctx, vsID, removeOpts); err != nil {
t.Errorf("Error removing variable set from workspace! WARNING: Dangling resources\n"+
"may exist! The full error is shown below.\n\n"+
"VariableSet ID: %s\nError: %s", vsID, err)
}
})
}

func createVariableSetVariable(t *testing.T, client *Client, vs *VariableSet, options VariableSetVariableCreateOptions) (*VariableSetVariable, func()) {
var vsCleanup func()

Expand Down
15 changes: 15 additions & 0 deletions mocks/variable_set_mocks.go

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

29 changes: 29 additions & 0 deletions variable_set.go
Expand Up @@ -17,6 +17,9 @@ type VariableSets interface {
// List all the variable sets within an organization.
List(ctx context.Context, organization string, options *VariableSetListOptions) (*VariableSetList, error)

// ListForWorkspace gets the associated variable sets for a workspace.
ListForWorkspace(ctx context.Context, workspaceID string, options *VariableSetListOptions) (*VariableSetList, error)

// Create is used to create a new variable set.
Create(ctx context.Context, organization string, options *VariableSetCreateOptions) (*VariableSet, error)

Expand Down Expand Up @@ -179,6 +182,32 @@ func (s *variableSets) List(ctx context.Context, organization string, options *V
return vl, nil
}

// ListForWorkspace gets the associated variable sets for a workspace.
func (s *variableSets) ListForWorkspace(ctx context.Context, workspaceID string, options *VariableSetListOptions) (*VariableSetList, error) {
if !validStringID(&workspaceID) {
return nil, ErrInvalidWorkspaceID
}
if options != nil {
if err := options.valid(); err != nil {
return nil, err
}
}

u := fmt.Sprintf("workspaces/%s/varsets", url.QueryEscape(workspaceID))
req, err := s.client.NewRequest("GET", u, options)
if err != nil {
return nil, err
}

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

return vl, nil
}

// Create is used to create a new variable set.
func (s *variableSets) Create(ctx context.Context, organization string, options *VariableSetCreateOptions) (*VariableSet, error) {
if !validStringID(&organization) {
Expand Down
56 changes: 55 additions & 1 deletion variable_set_test.go
Expand Up @@ -51,13 +51,67 @@ func TestVariableSetsList(t *testing.T) {
assert.Equal(t, 2, vsl.TotalCount)
})

t.Run("when Organization name is invalid ID", func(t *testing.T) {
t.Run("when Organization name is an invalid ID", func(t *testing.T) {
vsl, err := client.VariableSets.List(ctx, badIdentifier, nil)
assert.Nil(t, vsl)
assert.EqualError(t, err, ErrInvalidOrg.Error())
})
}

func TestVariableSetsListForWorkspace(t *testing.T) {
skipIfNotCINode(t)

client := testClient(t)
ctx := context.Background()

orgTest, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)
workspaceTest, workspaceTestCleanup := createWorkspace(t, client, orgTest)
t.Cleanup(workspaceTestCleanup)

vsTest1, vsTestCleanup1 := createVariableSet(t, client, orgTest, VariableSetCreateOptions{})
t.Cleanup(vsTestCleanup1)
vsTest2, vsTestCleanup2 := createVariableSet(t, client, orgTest, VariableSetCreateOptions{})
t.Cleanup(vsTestCleanup2)

applyVariableSetToWorkspace(t, client, vsTest1.ID, workspaceTest.ID)
applyVariableSetToWorkspace(t, client, vsTest2.ID, workspaceTest.ID)

t.Run("without list options", func(t *testing.T) {
vsl, err := client.VariableSets.ListForWorkspace(ctx, workspaceTest.ID, nil)
require.NoError(t, err)
require.Len(t, vsl.Items, 2)

ids := []string{vsTest1.ID, vsTest2.ID}
for _, varset := range vsl.Items {
assert.Contains(t, ids, varset.ID)
}
})

t.Run("with list options", func(t *testing.T) {
t.Skip("paging not supported yet in API")
// Request a page number which is out of range. The result should
// be successful, but return no results if the paging options are
// properly passed along.
vsl, err := client.VariableSets.ListForWorkspace(ctx, workspaceTest.ID, &VariableSetListOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
})
require.NoError(t, err)
assert.Empty(t, vsl.Items)
assert.Equal(t, 999, vsl.CurrentPage)
assert.Equal(t, 2, vsl.TotalCount)
})

t.Run("when Workspace ID is an invalid ID", func(t *testing.T) {
vsl, err := client.VariableSets.ListForWorkspace(ctx, badIdentifier, nil)
assert.Nil(t, vsl)
assert.EqualError(t, err, ErrInvalidWorkspaceID.Error())
})
}

func TestVariableSetsCreate(t *testing.T) {
skipIfNotCINode(t)

Expand Down

0 comments on commit 5c31a0c

Please sign in to comment.