Skip to content

Commit a8b7788

Browse files
committed
Adds PATCH tag-bindings
1 parent 0385759 commit a8b7788

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

errors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ var (
382382

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

385+
ErrRequiredTagBindings = errors.New("TagBindings are required")
386+
385387
ErrInvalidTestRunID = errors.New("invalid value for test run id")
386388

387389
ErrTerraformVersionValidForPlanOnly = errors.New("setting terraform-version is only valid when plan-only is set to true")

workspace.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ type Workspaces interface {
134134

135135
// ListTagBindings lists all tag bindings associated with the workspace.
136136
ListTagBindings(ctx context.Context, workspaceID string) ([]*TagBinding, error)
137+
138+
// AddTagBindings adds or modifies the value of existing tag binding keys for a workspace.
139+
AddTagBindings(ctx context.Context, workspaceID string, options WorkspaceAddTagBindingsOptions) ([]*TagBinding, error)
137140
}
138141

139142
// workspaces implements Workspaces.
@@ -147,6 +150,12 @@ type WorkspaceList struct {
147150
Items []*Workspace
148151
}
149152

153+
// WorkspaceAddTagBindingsOptions represents the options for adding tag bindings
154+
// to a workspace.
155+
type WorkspaceAddTagBindingsOptions struct {
156+
TagBindings []*TagBinding
157+
}
158+
150159
// LockedByChoice is a choice type struct that represents the possible values
151160
// within a polymorphic relation. If a value is available, exactly one field
152161
// will be non-nil.
@@ -760,6 +769,31 @@ func (s *workspaces) ListTagBindings(ctx context.Context, workspaceID string) ([
760769
return list.Items, nil
761770
}
762771

772+
// AddTagBindings adds or modifies the value of existing tag binding keys for a workspace.
773+
func (s *workspaces) AddTagBindings(ctx context.Context, workspaceID string, options WorkspaceAddTagBindingsOptions) ([]*TagBinding, error) {
774+
if !validStringID(&workspaceID) {
775+
return nil, ErrInvalidWorkspaceID
776+
}
777+
778+
if err := options.valid(); err != nil {
779+
return nil, err
780+
}
781+
782+
u := fmt.Sprintf("workspaces/%s/tag-bindings", url.PathEscape(workspaceID))
783+
req, err := s.client.NewRequest("PATCH", u, options.TagBindings)
784+
if err != nil {
785+
return nil, err
786+
}
787+
788+
var response = struct {
789+
*Pagination
790+
Items []*TagBinding
791+
}{}
792+
err = req.Do(ctx, &response)
793+
794+
return response.Items, err
795+
}
796+
763797
// Create is used to create a new workspace.
764798
func (s *workspaces) Create(ctx context.Context, organization string, options WorkspaceCreateOptions) (*Workspace, error) {
765799
if !validStringID(&organization) {
@@ -1465,6 +1499,14 @@ func (s *workspaces) DeleteDataRetentionPolicy(ctx context.Context, workspaceID
14651499
return req.Do(ctx, nil)
14661500
}
14671501

1502+
func (o WorkspaceAddTagBindingsOptions) valid() error {
1503+
if len(o.TagBindings) == 0 {
1504+
return ErrRequiredTagBindings
1505+
}
1506+
1507+
return nil
1508+
}
1509+
14681510
func (o WorkspaceCreateOptions) valid() error {
14691511
if !validString(o.Name) {
14701512
return ErrRequiredName

workspace_integration_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,68 @@ func TestWorkspacesReadByID(t *testing.T) {
11731173
})
11741174
}
11751175

1176+
func TestWorkspaceAddTagBindings(t *testing.T) {
1177+
client := testClient(t)
1178+
ctx := context.Background()
1179+
1180+
wTest, wCleanup := createWorkspace(t, client, nil)
1181+
t.Cleanup(wCleanup)
1182+
1183+
t.Run("when adding tag bindings to a workspace", func(t *testing.T) {
1184+
tagBindings := []*TagBinding{
1185+
{Key: "foo", Value: "bar"},
1186+
{Key: "baz", Value: "qux"},
1187+
}
1188+
1189+
bindings, err := client.Workspaces.AddTagBindings(ctx, wTest.ID, WorkspaceAddTagBindingsOptions{
1190+
TagBindings: tagBindings,
1191+
})
1192+
require.NoError(t, err)
1193+
1194+
assert.Len(t, bindings, 2)
1195+
assert.Equal(t, tagBindings[0].Key, bindings[0].Key)
1196+
assert.Equal(t, tagBindings[0].Value, bindings[0].Value)
1197+
assert.Equal(t, tagBindings[1].Key, bindings[1].Key)
1198+
assert.Equal(t, tagBindings[1].Value, bindings[1].Value)
1199+
})
1200+
1201+
t.Run("when adding 26 tags", func(t *testing.T) {
1202+
tagBindings := []*TagBinding{
1203+
{Key: "alpha"},
1204+
{Key: "bravo"},
1205+
{Key: "charlie"},
1206+
{Key: "delta"},
1207+
{Key: "echo"},
1208+
{Key: "foxtrot"},
1209+
{Key: "golf"},
1210+
{Key: "hotel"},
1211+
{Key: "india"},
1212+
{Key: "juliet"},
1213+
{Key: "kilo"},
1214+
{Key: "lima"},
1215+
{Key: "mike"},
1216+
{Key: "november"},
1217+
{Key: "oscar"},
1218+
{Key: "papa"},
1219+
{Key: "quebec"},
1220+
{Key: "romeo"},
1221+
{Key: "sierra"},
1222+
{Key: "tango"},
1223+
{Key: "uniform"},
1224+
{Key: "victor"},
1225+
{Key: "whiskey"},
1226+
{Key: "xray"},
1227+
{Key: "yankee"},
1228+
{Key: "zulu"},
1229+
}
1230+
1231+
_, err := client.Workspaces.AddTagBindings(ctx, wTest.ID, WorkspaceAddTagBindingsOptions{
1232+
TagBindings: tagBindings,
1233+
})
1234+
require.Error(t, err, "cannot exceed 10 bindings per resource")
1235+
})
1236+
}
1237+
11761238
func TestWorkspacesUpdate(t *testing.T) {
11771239
client := testClient(t)
11781240
ctx := context.Background()

0 commit comments

Comments
 (0)