forked from mauromedda/terraform-provider-awx
/
resource_project.go
280 lines (265 loc) · 8.82 KB
/
resource_project.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
package awx
import (
"fmt"
"strconv"
"time"
awxgo "github.com/davidfischer-ch/awx-go"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceProjectObject() *schema.Resource {
return &schema.Resource{
Create: resourceProjectCreate,
Read: resourceProjectRead,
Delete: resourceProjectDelete,
Update: resourceProjectUpdate,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Name of this project",
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Optional description of this project.",
},
// local_path is not writable - tested 2020-04-07
"scm_type": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Specifies the source control system used to store the project (one of '', git, hg, svn, insights).",
},
"scm_url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The location where the project is stored.",
},
"scm_branch": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Specific branch, tag or commit to checkout.",
},
"scm_refspec": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "For git projects, an additional refspec to fetch.",
},
"scm_clean": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Discard any local changes before syncing the project.",
},
"scm_delete_on_update": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Delete the project before syncing.",
},
"credential_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Description: "Numeric ID of the project credential",
},
"timeout": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 0,
Description: "The amount of time (in seconds) to run before the task is canceled.",
},
"organization_id": &schema.Schema{
Type: schema.TypeInt,
Required: true,
Description: "Numeric ID of the project organization",
},
"scm_update_on_launch": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Update the project when a job is launched that uses the project.",
},
"scm_update_cache_timeout": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 0,
Description: "The number of seconds after the last project update ran that a new project update will be launched as a job dependency.",
},
"allow_override": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Allow changing the SCM branch or revision in a job template that uses this project.",
},
"custom_virtualenv": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: nil,
Description: "Local absolute file path containing a custom Python virtualenv to use",
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(1 * time.Minute),
Update: schema.DefaultTimeout(1 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},
}
}
func resourceProjectCreate(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.ProjectService
_, res, err := awxService.ListProjects(map[string]string{
"name": d.Get("name").(string),
"organization": strconv.Itoa(d.Get("organization_id").(int))},
)
if err != nil {
return err
}
if len(res.Results) >= 1 {
return fmt.Errorf("Project with name %s already exists in the organization %d",
d.Get("name").(string), d.Get("organization_id").(int))
}
result, err := awxService.CreateProject(map[string]interface{}{
"name": d.Get("name").(string),
"description": d.Get("description").(string),
"scm_type": d.Get("scm_type").(string),
"scm_url": d.Get("scm_url").(string),
"scm_branch": d.Get("scm_branch").(string),
"scm_refspec": d.Get("scm_refspec").(string),
"scm_clean": d.Get("scm_clean").(bool),
"scm_delete_on_update": d.Get("scm_delete_on_update").(bool),
"credential": d.Get("credential_id").(int),
"timeout": d.Get("timeout").(int),
"organization": d.Get("organization_id").(int),
"scm_update_on_launch": d.Get("scm_update_on_launch").(bool),
"scm_update_cache_timeout": d.Get("scm_update_cache_timeout").(int),
"allow_override": d.Get("allow_override").(bool),
"custom_virtualenv": d.Get("custom_virtualenv").(string),
}, map[string]string{})
if err != nil {
return err
}
d.SetId(strconv.Itoa(result.ID))
return resourceProjectRead(d, m)
}
func resourceProjectUpdate(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.ProjectService
_, res, err := awxService.ListProjects(map[string]string{
"id": d.Id(),
"organization": strconv.Itoa(d.Get("organization_id").(int))},
)
if err != nil {
return err
}
if len(res.Results) == 0 {
return fmt.Errorf("Project with name %s doesn't exists in the organization %s",
d.Get("name").(string), d.Get("organization_id").(string))
}
id, err := strconv.Atoi(d.Id())
if err != nil {
return err
}
_, err = awxService.UpdateProject(id, map[string]interface{}{
"name": d.Get("name").(string),
"description": d.Get("description").(string),
"scm_type": d.Get("scm_type").(string),
"scm_url": d.Get("scm_url").(string),
"scm_branch": d.Get("scm_branch").(string),
"scm_refspec": d.Get("scm_refspec").(string),
"scm_clean": d.Get("scm_clean").(bool),
"scm_delete_on_update": d.Get("scm_delete_on_update").(bool),
"credential": d.Get("credential_id").(int),
"timeout": d.Get("timeout").(int),
"organization": d.Get("organization_id").(int),
"scm_update_on_launch": d.Get("scm_update_on_launch").(bool),
"scm_update_cache_timeout": d.Get("scm_update_cache_timeout").(int),
"allow_override": d.Get("allow_override").(bool),
"custom_virtualenv": d.Get("custom_virtualenv").(string),
}, map[string]string{})
if err != nil {
return err
}
return resourceProjectRead(d, m)
}
func resourceProjectRead(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.ProjectService
_, res, err := awxService.ListProjects(map[string]string{
"name": d.Get("name").(string)})
if err != nil {
return err
}
if len(res.Results) == 0 {
return nil
}
d = setProjectResourceData(d, res.Results[0])
return nil
}
func resourceProjectDelete(d *schema.ResourceData, m interface{}) error {
awx := m.(*awxgo.AWX)
awxService := awx.ProjectService
id, err := strconv.Atoi(d.Id())
if err != nil {
return err
}
var jobID int
var finished time.Time
_, res, err := awxService.ListProjects(map[string]string{
"name": d.Get("name").(string),
"id": d.Id()},
)
if err != nil {
return err
}
if len(res.Results) == 0 {
d.SetId("")
return nil
}
if res.Results[0].SummaryFields.CurrentJob["id"] != nil {
jobID = int(res.Results[0].SummaryFields.CurrentJob["id"].(float64))
} else if res.Results[0].SummaryFields.LastJob["id"] != nil {
jobID = int(res.Results[0].SummaryFields.LastJob["id"].(float64))
}
if jobID != 0 {
_, err = awx.ProjectUpdatesService.ProjectUpdateCancel(jobID)
if err != nil {
return err
}
}
// check if finished is 0
for finished.IsZero() {
prj, _ := awx.ProjectUpdatesService.ProjectUpdateGet(jobID)
finished = prj.Finished
time.Sleep(1 * time.Second)
}
if _, err = awxService.DeleteProject(id); err != nil {
return err
}
d.SetId("")
return nil
}
func setProjectResourceData(d *schema.ResourceData, r *awxgo.Project) *schema.ResourceData {
d.Set("name", r.Name)
d.Set("description", r.Description)
d.Set("scm_type", r.ScmType)
d.Set("scm_url", r.ScmURL)
d.Set("scm_branch", r.ScmBranch)
d.Set("scm_refspec", r.ScmRefSpec)
d.Set("scm_clean", r.ScmClean)
d.Set("scm_delete_on_update", r.ScmDeleteOnUpdate)
d.Set("credential_id", r.Credential)
d.Set("timeout", r.Timeout)
d.Set("organization_id", r.Organization)
d.Set("scm_update_on_launch", r.ScmUpdateOnLaunch)
d.Set("scm_update_cache_timeout", r.ScmUpdateCacheTimeout)
d.Set("allow_override", r.AllowOverride)
d.Set("custom_virtualenv", r.CustomVirtualenv)
return d
}