-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathresource_variable.go
240 lines (189 loc) · 6.42 KB
/
resource_variable.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
package octopusdeploy
import (
"context"
"fmt"
"log"
"strings"
"sync"
"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var mutex = &sync.Mutex{}
func resourceVariable() *schema.Resource {
return &schema.Resource{
CreateContext: resourceVariableCreate,
DeleteContext: resourceVariableDelete,
Description: "This resource manages variables in Octopus Deploy.",
Importer: &schema.ResourceImporter{State: resourceVariableImport},
ReadContext: resourceVariableRead,
Schema: getVariableSchema(),
UpdateContext: resourceVariableUpdate,
}
}
func resourceVariableImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
log.Printf("[INFO] importing variable (%s)", d.Id())
importStrings := strings.Split(d.Id(), ":")
if len(importStrings) != 2 {
return nil, fmt.Errorf("octopusdeploy_variable import must be in the form of OwnerID:VariableID (e.g. Projects-62:0906031f-68ba-4a15-afaa-657c1564e07b")
}
d.Set("owner_id", importStrings[0])
d.SetId(importStrings[1])
return []*schema.ResourceData{d}, nil
}
func resourceVariableCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()
if err := validateVariable(d); err != nil {
return diag.FromErr(err)
}
projectID, projectOk := d.GetOk("project_id")
ownerID, ownerOk := d.GetOk("owner_id")
if !projectOk && !ownerOk {
return diag.Errorf("one of project_id or owner_id must be configured")
}
var variableOwnerID string
if projectOk {
variableOwnerID = projectID.(string)
} else {
variableOwnerID = ownerID.(string)
}
variable := expandVariable(d)
log.Printf("[INFO] creating variable: %#v", variable)
client := m.(*octopusdeploy.Client)
variableSet, err := client.Variables.AddSingle(variableOwnerID, variable)
if err != nil {
return diag.FromErr(err)
}
for _, v := range variableSet.Variables {
if v.Name == variable.Name && v.Type == variable.Type && (v.IsSensitive || v.Value == variable.Value) && v.Description == variable.Description && v.IsSensitive == variable.IsSensitive {
scopeMatches, _, err := client.Variables.MatchesScope(v.Scope, &variable.Scope)
if err != nil {
return diag.FromErr(err)
}
if scopeMatches {
d.SetId(v.ID)
log.Printf("[INFO] variable created (%s)", d.Id())
return nil
}
}
}
d.SetId("")
return diag.Errorf("unable to locate variable for owner ID, %s", variableOwnerID)
}
func resourceVariableRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Printf("[INFO] reading variable (%s)", d.Id())
id := d.Id()
projectID, projectOk := d.GetOk("project_id")
ownerID, ownerOk := d.GetOk("owner_id")
if !projectOk && !ownerOk {
return diag.Errorf("one of project_id or owner_id must be configured")
}
var variableOwnerID string
if projectOk {
variableOwnerID = projectID.(string)
} else {
variableOwnerID = ownerID.(string)
}
client := m.(*octopusdeploy.Client)
variable, err := client.Variables.GetByID(variableOwnerID, id)
if err != nil {
if apiError, ok := err.(*octopusdeploy.APIError); ok {
if apiError.StatusCode == 404 {
log.Printf("[INFO] variable (%s) not found; deleting from state", d.Id())
d.SetId("")
return nil
}
}
return diag.FromErr(err)
}
if err := setVariable(ctx, d, variable); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] variable read (%s)", d.Id())
return nil
}
func resourceVariableUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()
log.Printf("[INFO] updating variable (%s)", d.Id())
if err := validateVariable(d); err != nil {
return diag.FromErr(err)
}
variable := expandVariable(d)
projectID, projectOk := d.GetOk("project_id")
ownerID, ownerOk := d.GetOk("owner_id")
if !projectOk && !ownerOk {
return diag.Errorf("one of project_id or owner_id must be configured")
}
var variableOwnerID string
if projectOk {
variableOwnerID = projectID.(string)
} else {
variableOwnerID = ownerID.(string)
}
client := m.(*octopusdeploy.Client)
variableSet, err := client.Variables.UpdateSingle(variableOwnerID, variable)
if err != nil {
return diag.FromErr(err)
}
for _, v := range variableSet.Variables {
if v.Name == variable.Name && v.Type == variable.Type && (v.IsSensitive || v.Value == variable.Value) && v.Description == variable.Description && v.IsSensitive == variable.IsSensitive {
scopeMatches, _, _ := client.Variables.MatchesScope(v.Scope, &variable.Scope)
if scopeMatches {
if err := setVariable(ctx, d, v); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] variable updated (%s)", d.Id())
return nil
}
}
}
d.SetId("")
return diag.Errorf("unable to locate variable for owner ID, %s", variableOwnerID)
}
func resourceVariableDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
mutex.Lock()
defer mutex.Unlock()
log.Printf("[INFO] deleting variable (%s)", d.Id())
projectID, projectOk := d.GetOk("project_id")
ownerID, ownerOk := d.GetOk("owner_id")
if !projectOk && !ownerOk {
return diag.Errorf("one of project_id or owner_id must be configured")
}
var variableOwnerID string
if projectOk {
variableOwnerID = projectID.(string)
} else {
variableOwnerID = ownerID.(string)
}
client := m.(*octopusdeploy.Client)
_, err := client.Variables.DeleteSingle(variableOwnerID, d.Id())
if err != nil {
if apiError, ok := err.(*octopusdeploy.APIError); ok {
if apiError.StatusCode == 404 {
log.Printf("[INFO] variable (%s) not found; deleting from state", d.Id())
d.SetId("")
return nil
}
}
return diag.FromErr(err)
}
log.Printf("[INFO] variable deleted (%s)", d.Id())
d.SetId("")
return nil
}
// Validating is done in its own function as we need to compare options once the entire
// schema has been parsed, which as far as I can tell we can't do in a normal validation
// function.
func validateVariable(d *schema.ResourceData) error {
tfSensitive := d.Get("is_sensitive").(bool)
tfType := d.Get("type").(string)
if tfSensitive && tfType != "Sensitive" {
return fmt.Errorf("when is_sensitive is set to true, type needs to be 'Sensitive'")
}
if !tfSensitive && tfType == "Sensitive" {
return fmt.Errorf("when type is set to 'Sensitive', is_sensitive needs to be true")
}
return nil
}