/
organization_tags.go
169 lines (133 loc) · 4.39 KB
/
organization_tags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package tfe
import (
"context"
"errors"
"fmt"
"net/url"
)
var _ OrganizationTags = (*organizationTags)(nil)
type OrganizationTags interface {
// List all tags within an organization
List(ctx context.Context, organization string, options *OrganizationTagsListOptions) (*OrganizationTagsList, error)
// Delete tags from an organization
Delete(ctx context.Context, organization string, options OrganizationTagsDeleteOptions) error
// Associate an organization's workspace with a tag
AddWorkspaces(ctx context.Context, tag string, options AddWorkspacesToTagOptions) error
}
type organizationTags struct {
client *Client
}
// OrganizationTagsList represents a list of organization tags
type OrganizationTagsList struct {
*Pagination
Items []*OrganizationTag
}
// OrganizationTag represents a Terraform Enterprise Organization tag
type OrganizationTag struct {
ID string `jsonapi:"primary,tags"`
Name string `jsonapi:"attr,name,omitempty"`
// Number of workspaces that have this tag
InstanceCount int `jsonapi:"attr,instance-count,omitempty"`
// The org this tag belongs to
Organization *Organization `jsonapi:"relation,organization"`
}
// OrganizationTagsListOptions represents the options for listing organization tags
type OrganizationTagsListOptions struct {
ListOptions
Filter string `url:"filter[exclude][taggable][id],omitempty"`
}
// List all the tags in an organization. You can provide query params through OrganizationTagsListOptions
func (s *organizationTags) List(ctx context.Context, organization string, options *OrganizationTagsListOptions) (*OrganizationTagsList, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
u := fmt.Sprintf("organizations/%s/tags", url.QueryEscape(organization))
req, err := s.client.newRequest("GET", u, options)
if err != nil {
return nil, err
}
tags := &OrganizationTagsList{}
err = s.client.do(ctx, req, tags)
if err != nil {
return nil, err
}
return tags, nil
}
// OrganizationTagsDeleteOptions represents the request body for deleting a tag in an organization
type OrganizationTagsDeleteOptions struct {
IDs []string
}
// this represents a single tag ID sent over the wire
type tagID struct {
ID string `jsonapi:"primary,tags"`
}
func (opts *OrganizationTagsDeleteOptions) valid() error {
if opts.IDs == nil || len(opts.IDs) == 0 {
return errors.New("you must specify at least one tag id to remove")
}
for _, id := range opts.IDs {
if !validStringID(&id) {
errorMsg := fmt.Sprintf("%s is not a valid id value", id)
return errors.New(errorMsg)
}
}
return nil
}
// Delete tags from a Terraform Enterprise organization
func (s *organizationTags) Delete(ctx context.Context, organization string, options OrganizationTagsDeleteOptions) error {
if !validStringID(&organization) {
return ErrInvalidOrg
}
if err := options.valid(); err != nil {
return err
}
u := fmt.Sprintf("organizations/%s/tags", url.QueryEscape(organization))
var tagsToRemove []*tagID
for _, id := range options.IDs {
tagsToRemove = append(tagsToRemove, &tagID{ID: id})
}
req, err := s.client.newRequest("DELETE", u, tagsToRemove)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}
// AddWorkspacesToTagOptions represents the request body to add a workspace to a tag
type AddWorkspacesToTagOptions struct {
WorkspaceIDs []string
}
func (w *AddWorkspacesToTagOptions) valid() error {
if w.WorkspaceIDs == nil || len(w.WorkspaceIDs) == 0 {
return errors.New("you must specify at least one workspace to add tag to")
}
for _, id := range w.WorkspaceIDs {
if !validStringID(&id) {
errorMsg := fmt.Sprintf("%s is not a valid id value", id)
return errors.New(errorMsg)
}
}
return nil
}
// this represents how workspace IDs will be sent over the wire
type workspaceID struct {
ID string `jsonapi:"primary,workspaces"`
}
// Add workspaces to a tag
func (s *organizationTags) AddWorkspaces(ctx context.Context, tag string, options AddWorkspacesToTagOptions) error {
if !validStringID(&tag) {
return errors.New("invalid tag id")
}
if err := options.valid(); err != nil {
return err
}
var workspaces []*workspaceID
for _, id := range options.WorkspaceIDs {
workspaces = append(workspaces, &workspaceID{ID: id})
}
u := fmt.Sprintf("tags/%s/relationships/workspaces", url.QueryEscape(tag))
req, err := s.client.newRequest("POST", u, workspaces)
if err != nil {
return err
}
return s.client.do(ctx, req, nil)
}