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

Add method to list variable sets by workspace #520

Closed
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
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)
sebasslash marked this conversation as resolved.
Show resolved Hide resolved

// 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, 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
}

// 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
52 changes: 51 additions & 1 deletion variable_set_test.go
Expand Up @@ -49,13 +49,63 @@ 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) {
client := testClient(t)
ctx := context.Background()

orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use t.Cleanup(orgTestCleanup) in favor of defer.

workspaceTest, workspaceTestCleanup := createWorkspace(t, client, orgTest)
defer workspaceTestCleanup()

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

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

t.Skip("paging not supported yet in API")
assert.Equal(t, 1, vsl.CurrentPage)
assert.Equal(t, 2, vsl.TotalCount)
})

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, ErrInvalidOrg.Error())
})
}

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