/
resource_tfe_variable_set.go
198 lines (163 loc) · 5.72 KB
/
resource_tfe_variable_set.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"fmt"
"log"
"regexp"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var variableSetIDRegexp = regexp.MustCompile("varset-[a-zA-Z0-9]{16}$")
func resourceTFEVariableSet() *schema.Resource {
return &schema.Resource{
Create: resourceTFEVariableSetCreate,
Read: resourceTFEVariableSetRead,
Update: resourceTFEVariableSetUpdate,
Delete: resourceTFEVariableSetDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"global": {
Type: schema.TypeBool,
Optional: true,
Default: false,
ConflictsWith: []string{"workspace_ids"},
},
"organization": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"workspace_ids": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func resourceTFEVariableSetCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(ConfiguredClient)
// Get the name and organization.
name := d.Get("name").(string)
organization, err := config.schemaOrDefaultOrganization(d)
if err != nil {
return err
}
// Create a new options struct.
options := tfe.VariableSetCreateOptions{
Name: tfe.String(name),
Global: tfe.Bool(d.Get("global").(bool)),
}
if description, descriptionSet := d.GetOk("description"); descriptionSet {
options.Description = tfe.String(description.(string))
}
variableSet, err := config.Client.VariableSets.Create(ctx, organization, &options)
if err != nil {
return fmt.Errorf(
"Error creating variable set %s, for organization: %s: %w", name, organization, err)
}
d.SetId(variableSet.ID)
if workspaceIDs, workspacesSet := d.GetOk("workspace_ids"); !*options.Global && workspacesSet {
log.Printf("[DEBUG] Apply variable set %s to workspaces %v", name, workspaceIDs)
warnWorkspaceIdsDeprecation()
applyOptions := tfe.VariableSetUpdateWorkspacesOptions{}
for _, workspaceID := range workspaceIDs.(*schema.Set).List() {
if val, ok := workspaceID.(string); ok {
applyOptions.Workspaces = append(applyOptions.Workspaces, &tfe.Workspace{ID: val})
}
}
_, err := config.Client.VariableSets.UpdateWorkspaces(ctx, variableSet.ID, &applyOptions)
if err != nil {
return fmt.Errorf(
"Error applying variable set %s (%s) to given workspaces: %w", name, variableSet.ID, err)
}
}
return resourceTFEVariableSetRead(d, meta)
}
func resourceTFEVariableSetRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(ConfiguredClient)
log.Printf("[DEBUG] Read configuration of variable set: %s", d.Id())
variableSet, err := config.Client.VariableSets.Read(ctx, d.Id(), &tfe.VariableSetReadOptions{
Include: &[]tfe.VariableSetIncludeOpt{tfe.VariableSetWorkspaces},
})
if err != nil {
if err == tfe.ErrResourceNotFound {
log.Printf("[DEBUG] Variable set %s no longer exists", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading configuration of variable set %s: %w", d.Id(), err)
}
// Update the config.
d.Set("name", variableSet.Name)
d.Set("description", variableSet.Description)
d.Set("global", variableSet.Global)
d.Set("organization", variableSet.Organization.Name)
var wids []interface{}
for _, workspace := range variableSet.Workspaces {
wids = append(wids, workspace.ID)
}
d.Set("workspace_ids", wids)
return nil
}
func resourceTFEVariableSetUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(ConfiguredClient)
if d.HasChange("name") || d.HasChange("description") || d.HasChange("global") {
options := tfe.VariableSetUpdateOptions{
Name: tfe.String(d.Get("name").(string)),
Description: tfe.String(d.Get("description").(string)),
Global: tfe.Bool(d.Get("global").(bool)),
}
log.Printf("[DEBUG] Update variable set: %s", d.Id())
_, err := config.Client.VariableSets.Update(ctx, d.Id(), &options)
if err != nil {
return fmt.Errorf("Error updateing variable %s: %w", d.Id(), err)
}
}
if d.HasChanges("workspace_ids") {
workspaceIDs := d.Get("workspace_ids")
applyOptions := tfe.VariableSetUpdateWorkspacesOptions{}
applyOptions.Workspaces = []*tfe.Workspace{}
for _, workspaceID := range workspaceIDs.(*schema.Set).List() {
if val, ok := workspaceID.(string); ok {
applyOptions.Workspaces = append(applyOptions.Workspaces, &tfe.Workspace{ID: val})
}
}
log.Printf("[DEBUG] Apply variable set %s to workspaces %v", d.Id(), workspaceIDs)
warnWorkspaceIdsDeprecation()
_, err := config.Client.VariableSets.UpdateWorkspaces(ctx, d.Id(), &applyOptions)
if err != nil {
return fmt.Errorf(
"Error applying variable set %s to given workspaces: %w", d.Id(), err)
}
}
return resourceTFEVariableSetRead(d, meta)
}
func resourceTFEVariableSetDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(ConfiguredClient)
log.Printf("[DEBUG] Delete variable set: %s", d.Id())
err := config.Client.VariableSets.Delete(ctx, d.Id())
if err != nil {
if err == tfe.ErrResourceNotFound {
return nil
}
return fmt.Errorf("Error deleting variable set %s: %w", d.Id(), err)
}
return nil
}
func warnWorkspaceIdsDeprecation() {
log.Printf("[WARN] The workspace_ids field of tfe_variable_set is deprecated as of release 0.33.0 and may be removed in a future version. The preferred method of associating a variable set to a workspace is by using the tfe_workspace_variable_set resource.")
}