-
Notifications
You must be signed in to change notification settings - Fork 65
/
resource_tag.go
303 lines (239 loc) · 8.25 KB
/
resource_tag.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package octopusdeploy
import (
"context"
"log"
"strings"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/tagsets"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/tenants"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/internal/errors"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/exp/slices"
)
func resourceTag() *schema.Resource {
return &schema.Resource{
CreateContext: resourceTagCreate,
DeleteContext: resourceTagDelete,
Description: "This resource manages tags in Octopus Deploy.",
Importer: getImporter(),
ReadContext: resourceTagRead,
Schema: getTagSchema(),
UpdateContext: resourceTagUpdate,
}
}
func resourceTagCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()
log.Printf("[INFO] creating tag")
return tagCreate(ctx, d, m)
}
func tagCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] creating tag")
tagSetID := d.Get("tag_set_id").(string)
tagSetSpaceID := d.Get("tag_set_space_id").(string)
octopus := m.(*client.Client)
tagSet, err := tagsets.GetByID(octopus, tagSetSpaceID, tagSetID)
if err != nil {
return processUnknownTagSetError(ctx, d, err)
}
name := d.Get("name").(string)
for _, tag := range tagSet.Tags {
if tag.Name == name {
return diag.Errorf(`the tag name '%s' is already in use by another tag in this tag set; tag names must be unique`, name)
}
}
tag := expandTag(d)
if tag.ID != "" {
tag.ID = tagSet.GetID() + "/" + strings.Split(tag.ID, "/")[1]
}
tagSet.Tags = append(tagSet.Tags, tag)
updatedTagSet, err := tagsets.Update(octopus, tagSet)
if err != nil {
return diag.FromErr(err)
}
return findByIdOrNameAndSetTag(ctx, d, tag, updatedTagSet)
}
func resourceTagDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()
tagSetID := d.Get("tag_set_id").(string)
tagSetSpaceID := d.Get("tag_set_space_id").(string)
log.Printf("[INFO] deleting tag (%s)", d.Id())
octopus := m.(*client.Client)
tagSet, err := tagsets.GetByID(octopus, tagSetSpaceID, tagSetID)
if err != nil {
return processUnknownTagSetError(ctx, d, err)
}
tag := expandTag(d)
// verify tag is not associated with a tenant
isUsed, err := isTagUsedByTenants(ctx, octopus, tagSetSpaceID, tag)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
if isUsed {
d.SetId("")
return diag.Errorf("the tag may not be deleted; it is being used by one or more tenant(s)")
}
// tag is known and not associated with a tenant, therefore it may be deleted
for i := 0; i < len(tagSet.Tags); i++ {
if tagSet.Tags[i].ID == d.Id() {
tagSet.Tags = slices.Delete(tagSet.Tags, i, i+1)
if _, err := tagsets.Update(octopus, tagSet); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] tag deleted (%s)", d.Id())
d.SetId("")
return nil
}
}
return errors.DeleteFromState(ctx, d, "tag")
}
func resourceTagRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()
// validate the tag ID
if d.Id() == "" || !strings.Contains(d.Id(), "/") {
return diag.Errorf(`unable to import tag; ID must be "TagSets-{ID}/Tags-{ID}"`)
}
name := d.Get("name").(string)
tagSetID := d.Get("tag_set_id").(string)
tagSetSpaceID := d.Get("tag_set_space_id").(string)
// if name and tag set ID are empty then an import is underway
if name == "" && tagSetID == "" {
log.Printf("[INFO] importing tag (%s)", d.Id())
tagSetID = strings.Split(d.Id(), "/")[0]
} else {
log.Printf("[INFO] reading tag (%s)", d.Id())
}
octopus := m.(*client.Client)
tagSet, err := tagsets.GetByID(octopus, tagSetSpaceID, tagSetID)
if err != nil {
return processUnknownTagSetError(ctx, d, err)
}
tag := expandTag(d)
return findByIdOrNameAndSetTag(ctx, d, tag, tagSet)
}
func resourceTagUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()
name := d.Get("name").(string)
tagSetID := d.Get("tag_set_id").(string)
tagSetSpaceID := d.Get("tag_set_space_id").(string)
log.Printf("[INFO] updating tag (%s)", d.Id())
octopus := m.(*client.Client)
// if the tag is reassigned to another tag set
if d.HasChange("tag_set_id") {
sourceTagSetID, destinationTagSetID := d.GetChange("tag_set_id")
sourceTagSetSpaceID, destinationTagSetSpaceID := d.GetChange("tag_set_space_id")
sourceTagSet, err := tagsets.GetByID(octopus, sourceTagSetSpaceID.(string), sourceTagSetID.(string))
if err != nil {
// if spaceID has change, tag has been deleted, recreate required
if d.HasChange("tag_set_space_id") {
return tagCreate(ctx, d, m)
}
return diag.FromErr(err)
}
destinationTagSet, err := tagsets.GetByID(octopus, destinationTagSetSpaceID.(string), destinationTagSetID.(string))
if err != nil {
return diag.FromErr(err)
}
// check to see if the name already exists in the destination tag set
for _, tag := range destinationTagSet.Tags {
if tag.Name == name {
d.SetId("")
return diag.Errorf(`the tag name '%s' is already in use by another tag in this tag set; tag names must be unique`, name)
}
}
tag := expandTag(d)
// check to see that the tag is not applied to a tenant
isUsed, err := isTagUsedByTenants(ctx, octopus, sourceTagSetSpaceID.(string), tag)
if err != nil {
d.SetId("")
return diag.FromErr(err)
}
if isUsed {
d.SetId("")
return diag.Errorf("the tag may not be transferred; it is being used by one or more tenant(s)")
}
// all requirements are met; it is OK to transfer the tag
// remove the tag from the source tag set and update through the API
for i := 0; i < len(sourceTagSet.Tags); i++ {
if sourceTagSet.Tags[i].ID == d.Id() {
sourceTagSet.Tags = slices.Delete(sourceTagSet.Tags, i, i+1)
if _, err := tagsets.Update(octopus, sourceTagSet); err != nil {
return diag.FromErr(err)
}
}
}
// update and add the tag to the destination tag set
tag.ID = destinationTagSet.GetID() + "/" + strings.Split(tag.ID, "/")[1]
destinationTagSet.Tags = append(destinationTagSet.Tags, tag)
updatedTagSet, err := tagsets.Update(octopus, destinationTagSet)
if err != nil {
return diag.FromErr(err)
}
return findByIdOrNameAndSetTag(ctx, d, tag, updatedTagSet)
}
tagSet, err := tagsets.GetByID(octopus, tagSetSpaceID, tagSetID)
if err != nil {
return processUnknownTagSetError(ctx, d, err)
}
// find and update the tag that matches the one updated in configuration
for i := 0; i < len(tagSet.Tags); i++ {
if tagSet.Tags[i].ID == d.Id() {
tagSet.Tags[i] = expandTag(d)
updatedTagSet, err := tagsets.Update(octopus, tagSet)
if err != nil {
return diag.FromErr(err)
}
return findByIdOrNameAndSetTag(ctx, d, tagSet.Tags[i], updatedTagSet)
}
}
return diag.Errorf("unable to update tag")
}
func isTagUsedByTenants(ctx context.Context, octopus *client.Client, spaceID string, tag *tagsets.Tag) (bool, error) {
tenants, err := tenants.Get(octopus, spaceID, tenants.TenantsQuery{
Tags: []string{tag.ID},
})
if err != nil {
return false, err
}
return len(tenants.Items) > 0, nil
}
func findByIdOrNameAndSetTag(ctx context.Context, d *schema.ResourceData, tag *tagsets.Tag, tagSet *tagsets.TagSet) diag.Diagnostics {
for _, t := range tagSet.Tags {
if t.Name == tag.Name {
if err := setTag(ctx, d, t, tagSet); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] tag (%s)", tag.ID)
return nil
}
}
for _, t := range tagSet.Tags {
if t.ID == tag.ID {
if err := setTag(ctx, d, t, tagSet); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] tag (%s)", tag.ID)
return nil
}
}
return errors.DeleteFromState(ctx, d, "tag")
}
func processUnknownTagSetError(ctx context.Context, d *schema.ResourceData, err error) diag.Diagnostics {
if err == nil {
return nil
}
if apiError, ok := err.(*core.APIError); ok {
if apiError.StatusCode == 404 {
log.Printf("[INFO] tag set (%s) not found; deleting tag from state", d.Id())
d.SetId("")
return nil
}
}
return diag.FromErr(err)
}