Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List Variable Sets associated with workspace #551

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Unreleased

## Enhancements

* Add `ListVariableSets` method to `Workspaces` interface to enable fetching variable sets associated with a workspace. [#551](https://github.com/hashicorp/go-tfe/pull/551)

# v1.10.0

## Enhancements
Expand Down
28 changes: 28 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
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/workspace_mocks.go

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

2 changes: 1 addition & 1 deletion variable_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ 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())
Expand Down
29 changes: 29 additions & 0 deletions workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ type Workspaces interface {

// RemoveTags removes tags from a workspace
RemoveTags(ctx context.Context, workspaceID string, options WorkspaceRemoveTagsOptions) error

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

// workspaces implements Workspaces.
Expand Down Expand Up @@ -1063,6 +1066,32 @@ func (s *workspaces) RemoveTags(ctx context.Context, workspaceID string, options
return req.Do(ctx, nil)
}

// ListVariableSets reads the associated variable sets for a workspace
func (s *workspaces) ListVariableSets(ctx context.Context, workspaceID string, options *VariableSetListOptions) (*VariableSetList, error) {
if !validStringID(&workspaceID) {
return nil, ErrInvalidOrg
}
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
}

func (o WorkspaceCreateOptions) valid() error {
if !validString(o.Name) {
return ErrRequiredName
Expand Down
54 changes: 54 additions & 0 deletions workspace_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1848,6 +1848,60 @@ func TestWorkspaces_RemoveTags(t *testing.T) {
})
}

func TestWorkspacesListVariableSets(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.Workspaces.ListVariableSets(ctx, workspaceTest.ID, nil)
require.NoError(t, err)
require.Len(t, vsl.Items, 2)

ids := []string{vsTest1.ID, vsTest2.ID}
assert.Contains(t, ids, vsl.Items[0].ID)
assert.Contains(t, ids, vsl.Items[1].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.Workspaces.ListVariableSets(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.Workspaces.ListVariableSets(ctx, badIdentifier, nil)
assert.Nil(t, vsl)
assert.EqualError(t, err, ErrInvalidOrg.Error())
})
}

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

Expand Down