Skip to content

Commit

Permalink
List tag bindings by resource
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonc committed Oct 16, 2024
1 parent bd2deab commit e058e9c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
31 changes: 31 additions & 0 deletions project.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type Projects interface {

// Delete a project.
Delete(ctx context.Context, projectID string) error

// ListTagBindings lists all tag bindings associated with the project.
ListTagBindings(ctx context.Context, projectID string) ([]*TagBinding, error)
}

// projects implements Projects
Expand Down Expand Up @@ -104,6 +107,10 @@ type ProjectUpdateOptions struct {

// Optional: A description for the project.
Description *string `jsonapi:"attr,description,omitempty"`

// Associated TagBindings of the project. Note that this will replace
// all existing tag bindings.
TagBindings []*TagBinding `jsonapi:"relation,tag-bindings,omitempty"`
}

// List all projects.
Expand Down Expand Up @@ -178,6 +185,30 @@ func (s *projects) Read(ctx context.Context, projectID string) (*Project, error)
return p, nil
}

func (s *projects) ListTagBindings(ctx context.Context, projectID string) ([]*TagBinding, error) {
if !validStringID(&projectID) {
return nil, ErrInvalidProjectID
}

u := fmt.Sprintf("projects/%s/tag-bindings", url.PathEscape(projectID))
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}

var list struct {
*Pagination
Items []*TagBinding
}

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

return list.Items, nil
}

// Update a project by its ID
func (s *projects) Update(ctx context.Context, projectID string, options ProjectUpdateOptions) (*Project, error) {
if !validStringID(&projectID) {
Expand Down
18 changes: 14 additions & 4 deletions projects_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,22 +197,32 @@ func TestProjectsUpdate(t *testing.T) {
client := testClient(t)
ctx := context.Background()

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

Check failure on line 201 in projects_integration_test.go

View workflow job for this annotation

GitHub Actions / Lint

commentedOutCode: may want to remove commented-out code (gocritic)

t.Run("with valid options", func(t *testing.T) {
kBefore, kTestCleanup := createProject(t, client, orgTest)
defer kTestCleanup()
kBefore, _ := createProject(t, client, orgTest)
// defer kTestCleanup()

Check failure on line 205 in projects_integration_test.go

View workflow job for this annotation

GitHub Actions / Lint

commentedOutCode: may want to remove commented-out code (gocritic)

kAfter, err := client.Projects.Update(ctx, kBefore.ID, ProjectUpdateOptions{
Name: String("new project name"),
Description: String("updated description"),
TagBindings: []*TagBinding{
{Key: "foo", Value: "bar"},
},
})
require.NoError(t, err)

assert.Equal(t, kBefore.ID, kAfter.ID)
assert.NotEqual(t, kBefore.Name, kAfter.Name)
assert.NotEqual(t, kBefore.Description, kAfter.Description)

bindings, err := client.Projects.ListTagBindings(ctx, kAfter.ID)
require.NoError(t, err)

assert.Len(t, bindings, 1)
assert.Equal(t, "foo", bindings[0].Key)
assert.Equal(t, "bar", bindings[0].Value)
})

t.Run("when updating with invalid name", func(t *testing.T) {
Expand Down
27 changes: 27 additions & 0 deletions workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ type Workspaces interface {
// DeleteDataRetentionPolicy deletes a workspace's data retention policy
// **Note: This functionality is only available in Terraform Enterprise.**
DeleteDataRetentionPolicy(ctx context.Context, workspaceID string) error

// ListTagBindings lists all tag bindings associated with the workspace.
ListTagBindings(ctx context.Context, workspaceID string) ([]*TagBinding, error)
}

// workspaces implements Workspaces.
Expand Down Expand Up @@ -733,6 +736,30 @@ func (s *workspaces) List(ctx context.Context, organization string, options *Wor
return wl, nil
}

func (s *workspaces) ListTagBindings(ctx context.Context, workspaceID string) ([]*TagBinding, error) {
if !validStringID(&workspaceID) {
return nil, ErrInvalidWorkspaceID
}

u := fmt.Sprintf("workspaces/%s/tag-bindings", url.PathEscape(workspaceID))
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}

var list struct {
*Pagination
Items []*TagBinding
}

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

return list.Items, nil
}

// Create is used to create a new workspace.
func (s *workspaces) Create(ctx context.Context, organization string, options WorkspaceCreateOptions) (*Workspace, error) {
if !validStringID(&organization) {
Expand Down
10 changes: 7 additions & 3 deletions workspace_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1298,10 +1298,14 @@ func TestWorkspacesUpdate(t *testing.T) {
assert.Equal(t, *options.TerraformVersion, item.TerraformVersion)
assert.Equal(t, options.TriggerPrefixes, item.TriggerPrefixes)
assert.Equal(t, *options.WorkingDirectory, item.WorkingDirectory)
assert.Len(t, item.TagBindings, 1)
assert.Equal(t, "foo", item.TagBindings[0].Key)
assert.Equal(t, "bar", item.TagBindings[0].Value)
}

bindings, err := client.Workspaces.ListTagBindings(ctx, wTest.ID)
require.NoError(t, err)

assert.Len(t, bindings, 1)
assert.Equal(t, "foo", bindings[0].Key)
assert.Equal(t, "bar", bindings[0].Value)
})

t.Run("when options includes both an operations value and an enforcement mode value", func(t *testing.T) {
Expand Down

0 comments on commit e058e9c

Please sign in to comment.