-
Notifications
You must be signed in to change notification settings - Fork 148
/
resource_tfe_workspace_run_task.go
225 lines (187 loc) · 5.98 KB
/
resource_tfe_workspace_run_task.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
package tfe
import (
"fmt"
"log"
"strings"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func workspaceRunTaskEnforcementLevels() []string {
return []string{
string(tfe.Advisory),
string(tfe.Mandatory),
}
}
func workspaceRunTaskStages() []string {
return []string{
string(tfe.PrePlan),
string(tfe.PostPlan),
}
}
// Helper function to turn a slice of strings into an english sentence for documentation
func sentenceList(items []string, prefix string, suffix string, conjunction string) string {
var b strings.Builder
for i, v := range items {
fmt.Fprint(&b, prefix, v, suffix)
if i < len(items)-1 {
if i < len(items)-2 {
fmt.Fprint(&b, ", ")
} else {
fmt.Fprintf(&b, " %s ", conjunction)
}
}
}
return b.String()
}
func resourceTFEWorkspaceRunTask() *schema.Resource {
return &schema.Resource{
Create: resourceTFEWorkspaceRunTaskCreate,
Read: resourceTFEWorkspaceRunTaskRead,
Delete: resourceTFEWorkspaceRunTaskDelete,
Update: resourceTFEWorkspaceRunTaskUpdate,
Importer: &schema.ResourceImporter{
State: resourceTFEWorkspaceRunTaskImporter,
},
Schema: map[string]*schema.Schema{
"workspace_id": {
Description: "The id of the workspace to associate the Run task to.",
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"task_id": {
Description: "The id of the Run task to associate to the Workspace.",
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"enforcement_level": {
Description: fmt.Sprintf("The enforcement level of the task. Valid values are %s.", sentenceList(
workspaceRunTaskEnforcementLevels(),
"`",
"`",
"and",
)),
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(
workspaceRunTaskEnforcementLevels(),
false,
),
},
"stage": {
Description: fmt.Sprintf("This is currently in BETA. The stage to run the task in. Valid values are %s.", sentenceList(
workspaceRunTaskStages(),
"`",
"`",
"and",
)),
Type: schema.TypeString,
Optional: true,
Default: tfe.PostPlan,
ValidateFunc: validation.StringInSlice(
workspaceRunTaskStages(),
false,
),
},
},
}
}
func resourceTFEWorkspaceRunTaskCreate(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
workspaceID := d.Get("workspace_id").(string)
taskID := d.Get("task_id").(string)
task, err := tfeClient.RunTasks.Read(ctx, taskID)
if err != nil {
return fmt.Errorf(
"Error retrieving task %s: %w", taskID, err)
}
ws, err := tfeClient.Workspaces.ReadByID(ctx, workspaceID)
if err != nil {
return fmt.Errorf(
"Error retrieving workspace %s: %w", workspaceID, err)
}
stage := tfe.Stage(d.Get("stage").(string))
options := tfe.WorkspaceRunTaskCreateOptions{
RunTask: task,
EnforcementLevel: tfe.TaskEnforcementLevel(d.Get("enforcement_level").(string)),
Stage: &stage,
}
log.Printf("[DEBUG] Create task %s in workspace %s", task.ID, ws.ID)
wstask, err := tfeClient.WorkspaceRunTasks.Create(ctx, ws.ID, options)
if err != nil {
return fmt.Errorf("Error creating task %s in workspace %s: %w", task.ID, ws.ID, err)
}
d.SetId(wstask.ID)
return resourceTFEWorkspaceRunTaskRead(d, meta)
}
func resourceTFEWorkspaceRunTaskDelete(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
// Get the workspace
workspaceID := d.Get("workspace_id").(string)
log.Printf("[DEBUG] Delete task %s in workspace %s", d.Id(), workspaceID)
err := tfeClient.WorkspaceRunTasks.Delete(ctx, workspaceID, d.Id())
if err != nil && !isErrResourceNotFound(err) {
return fmt.Errorf("Error deleting task %s in workspace %s: %w", d.Id(), workspaceID, err)
}
return nil
}
func resourceTFEWorkspaceRunTaskUpdate(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
// Get the workspace
workspaceID := d.Get("workspace_id").(string)
// Setup the options struct
options := tfe.WorkspaceRunTaskUpdateOptions{}
if d.HasChange("enforcement_level") {
options.EnforcementLevel = tfe.TaskEnforcementLevel(d.Get("enforcement_level").(string))
}
if d.HasChange("stage") {
stage := tfe.Stage(d.Get("stage").(string))
options.Stage = &stage
}
log.Printf("[DEBUG] Update configuration of task %s in workspace %s", d.Id(), workspaceID)
_, err := tfeClient.WorkspaceRunTasks.Update(ctx, workspaceID, d.Id(), options)
if err != nil {
return fmt.Errorf("Error updating task %s in workspace %s: %w", d.Id(), workspaceID, err)
}
return nil
}
func resourceTFEWorkspaceRunTaskRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
// Get the workspace
workspaceID := d.Get("workspace_id").(string)
wstask, err := tfeClient.WorkspaceRunTasks.Read(ctx, workspaceID, d.Id())
if err != nil {
if isErrResourceNotFound(err) {
log.Printf("[DEBUG] Workspace Task %s does not exist in workspace %s", d.Id(), workspaceID)
d.SetId("")
return nil
}
return fmt.Errorf("Error reading configuration of task %s in workspace %s: %w", d.Id(), workspaceID, err)
}
// Update the config.
d.Set("workspace_id", wstask.Workspace.ID)
d.Set("task_id", wstask.RunTask.ID)
d.Set("enforcement_level", string(wstask.EnforcementLevel))
d.Set("stage", string(wstask.Stage))
return nil
}
func resourceTFEWorkspaceRunTaskImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
tfeClient := meta.(*tfe.Client)
s := strings.Split(d.Id(), "/")
if len(s) != 3 {
return nil, fmt.Errorf(
"invalid task input format: %s (expected <ORGANIZATION>/<WORKSPACE NAME>/<TASK NAME>)",
d.Id(),
)
}
wstask, err := fetchWorkspaceRunTask(s[2], s[1], s[0], tfeClient)
if err != nil {
return nil, err
}
d.Set("workspace_id", wstask.Workspace.ID)
d.Set("task_id", wstask.RunTask.ID)
d.SetId(wstask.ID)
return []*schema.ResourceData{d}, nil
}