/
organization_tags.go
181 lines (144 loc) · 4.72 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
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"context"
"errors"
"fmt"
"net/url"
)
var _ OrganizationTags = (*organizationTags)(nil)
// OrganizationMemberships describes all the list of tags used with all resources across the organization.
//
// TFE API docs:
// https://developer.hashicorp.com/terraform/cloud-docs/api-docs/organization-tags
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
}
// organizationTags implements OrganizationTags.
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"`
// Optional:
Name string `jsonapi:"attr,name,omitempty"`
// Optional: 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
// Optional:
Filter string `url:"filter[exclude][taggable][id],omitempty"`
// Optional: A search query string. Organization tags are searchable by name likeness.
Query string `url:"q,omitempty"`
}
// OrganizationTagsDeleteOptions represents the request body for deleting a tag in an organization
type OrganizationTagsDeleteOptions struct {
IDs []string // Required
}
// AddWorkspacesToTagOptions represents the request body to add a workspace to a tag
type AddWorkspacesToTagOptions struct {
WorkspaceIDs []string // Required
}
// this represents a single tag ID
type tagID struct {
ID string `jsonapi:"primary,tags"`
}
// this represents a single workspace ID
type workspaceID struct {
ID string `jsonapi:"primary,workspaces"`
}
// 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 = req.Do(ctx, tags)
if err != nil {
return nil, err
}
return tags, 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 req.Do(ctx, nil)
}
// Add workspaces to a tag
func (s *organizationTags) AddWorkspaces(ctx context.Context, tag string, options AddWorkspacesToTagOptions) error {
if !validStringID(&tag) {
return ErrInvalidTag
}
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 req.Do(ctx, nil)
}
func (opts *OrganizationTagsDeleteOptions) valid() error {
if opts.IDs == nil || len(opts.IDs) == 0 {
return ErrRequiredTagID
}
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
}
func (w *AddWorkspacesToTagOptions) valid() error {
if w.WorkspaceIDs == nil || len(w.WorkspaceIDs) == 0 {
return ErrRequiredTagWorkspaceID
}
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
}