diff --git a/variable_set.go b/variable_set.go index 3a9a1f9ec..11caf3665 100644 --- a/variable_set.go +++ b/variable_set.go @@ -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) @@ -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) { diff --git a/variable_set_test.go b/variable_set_test.go index bab062a72..f97a1e80a 100644 --- a/variable_set_test.go +++ b/variable_set_test.go @@ -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() + 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()