Skip to content

Commit

Permalink
Adds PATCH tag-bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonc committed Oct 26, 2024
1 parent 0385759 commit a8b7788
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ var (

ErrRequiredRegistryModule = errors.New("registry module is required")

ErrRequiredTagBindings = errors.New("TagBindings are required")

ErrInvalidTestRunID = errors.New("invalid value for test run id")

ErrTerraformVersionValidForPlanOnly = errors.New("setting terraform-version is only valid when plan-only is set to true")
Expand Down
42 changes: 42 additions & 0 deletions workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ type Workspaces interface {

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

// AddTagBindings adds or modifies the value of existing tag binding keys for a workspace.
AddTagBindings(ctx context.Context, workspaceID string, options WorkspaceAddTagBindingsOptions) ([]*TagBinding, error)
}

// workspaces implements Workspaces.
Expand All @@ -147,6 +150,12 @@ type WorkspaceList struct {
Items []*Workspace
}

// WorkspaceAddTagBindingsOptions represents the options for adding tag bindings
// to a workspace.
type WorkspaceAddTagBindingsOptions struct {
TagBindings []*TagBinding
}

// LockedByChoice is a choice type struct that represents the possible values
// within a polymorphic relation. If a value is available, exactly one field
// will be non-nil.
Expand Down Expand Up @@ -760,6 +769,31 @@ func (s *workspaces) ListTagBindings(ctx context.Context, workspaceID string) ([
return list.Items, nil
}

// AddTagBindings adds or modifies the value of existing tag binding keys for a workspace.
func (s *workspaces) AddTagBindings(ctx context.Context, workspaceID string, options WorkspaceAddTagBindingsOptions) ([]*TagBinding, error) {
if !validStringID(&workspaceID) {
return nil, ErrInvalidWorkspaceID
}

if err := options.valid(); err != nil {
return nil, err
}

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

var response = struct {
*Pagination
Items []*TagBinding
}{}
err = req.Do(ctx, &response)

return response.Items, err
}

// 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 Expand Up @@ -1465,6 +1499,14 @@ func (s *workspaces) DeleteDataRetentionPolicy(ctx context.Context, workspaceID
return req.Do(ctx, nil)
}

func (o WorkspaceAddTagBindingsOptions) valid() error {
if len(o.TagBindings) == 0 {
return ErrRequiredTagBindings
}

return nil
}

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

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

wTest, wCleanup := createWorkspace(t, client, nil)
t.Cleanup(wCleanup)

t.Run("when adding tag bindings to a workspace", func(t *testing.T) {
tagBindings := []*TagBinding{
{Key: "foo", Value: "bar"},
{Key: "baz", Value: "qux"},
}

bindings, err := client.Workspaces.AddTagBindings(ctx, wTest.ID, WorkspaceAddTagBindingsOptions{
TagBindings: tagBindings,
})
require.NoError(t, err)

assert.Len(t, bindings, 2)
assert.Equal(t, tagBindings[0].Key, bindings[0].Key)
assert.Equal(t, tagBindings[0].Value, bindings[0].Value)
assert.Equal(t, tagBindings[1].Key, bindings[1].Key)
assert.Equal(t, tagBindings[1].Value, bindings[1].Value)
})

t.Run("when adding 26 tags", func(t *testing.T) {
tagBindings := []*TagBinding{
{Key: "alpha"},
{Key: "bravo"},
{Key: "charlie"},
{Key: "delta"},
{Key: "echo"},
{Key: "foxtrot"},
{Key: "golf"},
{Key: "hotel"},
{Key: "india"},
{Key: "juliet"},
{Key: "kilo"},
{Key: "lima"},
{Key: "mike"},
{Key: "november"},
{Key: "oscar"},
{Key: "papa"},
{Key: "quebec"},
{Key: "romeo"},
{Key: "sierra"},
{Key: "tango"},
{Key: "uniform"},
{Key: "victor"},
{Key: "whiskey"},
{Key: "xray"},
{Key: "yankee"},
{Key: "zulu"},
}

_, err := client.Workspaces.AddTagBindings(ctx, wTest.ID, WorkspaceAddTagBindingsOptions{
TagBindings: tagBindings,
})
require.Error(t, err, "cannot exceed 10 bindings per resource")
})
}

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

0 comments on commit a8b7788

Please sign in to comment.