/
resource_tfe_organization_run_task.go
194 lines (161 loc) · 4.87 KB
/
resource_tfe_organization_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
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 resourceTFEOrganizationRunTask() *schema.Resource {
return &schema.Resource{
Create: resourceTFEOrganizationRunTaskCreate,
Read: resourceTFEOrganizationRunTaskRead,
Delete: resourceTFEOrganizationRunTaskDelete,
Update: resourceTFEOrganizationRunTaskUpdate,
Importer: &schema.ResourceImporter{
State: resourceTFEOrganizationRunTaskImporter,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"organization": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"url": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
},
"category": {
Type: schema.TypeString,
Default: "task",
Optional: true,
},
"hmac_key": {
Type: schema.TypeString,
Sensitive: true,
Default: "",
Optional: true,
},
"enabled": {
Type: schema.TypeBool,
Default: true,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceTFEOrganizationRunTaskCreate(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
// Get the task name and organization.
name := d.Get("name").(string)
organization := d.Get("organization").(string)
// Create a new options struct.
options := tfe.RunTaskCreateOptions{
Name: name,
URL: d.Get("url").(string),
Category: d.Get("category").(string),
HMACKey: tfe.String(d.Get("hmac_key").(string)),
Enabled: tfe.Bool(d.Get("enabled").(bool)),
Description: tfe.String(d.Get("description").(string)),
}
log.Printf("[DEBUG] Create task %s for organization: %s", name, organization)
task, err := tfeClient.RunTasks.Create(ctx, organization, options)
if err != nil {
return fmt.Errorf(
"Error creating task %s for organization %s: %w", name, organization, err)
}
d.SetId(task.ID)
return resourceTFEOrganizationRunTaskRead(d, meta)
}
func resourceTFEOrganizationRunTaskDelete(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
log.Printf("[DEBUG] Delete task: %s", d.Id())
err := tfeClient.RunTasks.Delete(ctx, d.Id())
if err != nil {
if isErrResourceNotFound(err) {
return nil
}
return fmt.Errorf("Error deleting task %s: %w", d.Id(), err)
}
return nil
}
func resourceTFEOrganizationRunTaskUpdate(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
// Setup the options struct
options := tfe.RunTaskUpdateOptions{}
if d.HasChange("name") {
options.Name = tfe.String(d.Get("name").(string))
}
if d.HasChange("url") {
options.URL = tfe.String(d.Get("url").(string))
}
if d.HasChange("category") {
options.Category = tfe.String(d.Get("category").(string))
}
if d.HasChange("enabled") {
options.Enabled = tfe.Bool(d.Get("enabled").(bool))
}
if d.HasChange("hmac_key") {
options.HMACKey = tfe.String(d.Get("hmac_key").(string))
}
if d.HasChange("description") {
options.Description = tfe.String(d.Get("description").(string))
}
log.Printf("[DEBUG] Update configuration of task: %s", d.Id())
task, err := tfeClient.RunTasks.Update(ctx, d.Id(), options)
if err != nil {
return fmt.Errorf("Error updating task %s: %w", d.Id(), err)
}
d.SetId(task.ID)
return resourceTFEOrganizationRunTaskRead(d, meta)
}
func resourceTFEOrganizationRunTaskRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
log.Printf("[DEBUG] Read configuration of task: %s", d.Id())
task, err := tfeClient.RunTasks.Read(ctx, d.Id())
if err != nil {
if isErrResourceNotFound(err) {
log.Printf("[DEBUG] Task %s does not exist", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading configuration of task %s: %w", d.Id(), err)
}
// Update the config.
d.Set("name", task.Name)
d.Set("url", task.URL)
d.Set("category", task.Category)
d.Set("enabled", task.Enabled)
// The HMAC Key is always empty from the API so all we can do is
// echo the request's key to the response
d.Set("hmac_key", tfe.String(d.Get("hmac_key").(string)))
d.Set("description", task.Description)
return nil
}
func resourceTFEOrganizationRunTaskImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
tfeClient := meta.(*tfe.Client)
s := strings.Split(d.Id(), "/")
if len(s) != 2 {
return nil, fmt.Errorf(
"invalid task input format: %s (expected <ORGANIZATION>/<TASK NAME>)",
d.Id(),
)
}
task, err := fetchOrganizationRunTask(s[1], s[0], tfeClient)
if err != nil {
return nil, err
}
d.Set("organization", task.Organization.Name)
d.SetId(task.ID)
return []*schema.ResourceData{d}, nil
}