-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathresource_build_configuration.go
616 lines (559 loc) · 20.5 KB
/
resource_build_configuration.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package main
import (
"errors"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/umweltdk/teamcity/teamcity"
"github.com/umweltdk/teamcity/types"
"log"
"reflect"
)
func resourceBuildStep() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"properties": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
},
},
}
}
func resourceAttachedVcsRoot() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"vcs_root": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: teamcity.ValidateID,
},
"checkout_rules": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceBuildConfiguration() *schema.Resource {
return &schema.Resource{
Create: resourceBuildConfigurationCreate,
Read: resourceBuildConfigurationRead,
Update: resourceBuildConfigurationUpdate,
Delete: resourceBuildConfigurationDelete,
Schema: map[string]*schema.Schema{
"project": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: teamcity.ValidateID,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"template": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: teamcity.ValidateID,
},
"parameter": &schema.Schema{
Type: schema.TypeSet,
Elem: resourceParameter(),
Set: parameterValueHash,
Optional: true,
},
"parameter_values": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
},
"step": &schema.Schema{
Type: schema.TypeList,
Elem: resourceBuildStep(),
Optional: true,
},
"attached_vcs_root": &schema.Schema{
Type: schema.TypeSet,
Elem: resourceAttachedVcsRoot(),
Set: attachedVcsRootValueHash,
Optional: true,
},
},
}
}
func resourceBuildTemplate() *schema.Resource {
return &schema.Resource{
Create: resourceBuildTemplateCreate,
Read: resourceBuildTemplateRead,
Update: resourceBuildTemplateUpdate,
Delete: resourceBuildTemplateDelete,
Schema: map[string]*schema.Schema{
"project": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"parameter": &schema.Schema{
Type: schema.TypeSet,
Elem: resourceParameter(),
Set: parameterValueHash,
Optional: true,
},
"parameter_values": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
},
"step": &schema.Schema{
Type: schema.TypeList,
Elem: resourceBuildStep(),
Optional: true,
},
"attached_vcs_root": &schema.Schema{
Type: schema.TypeSet,
Elem: resourceAttachedVcsRoot(),
Set: attachedVcsRootValueHash,
Optional: true,
},
},
}
}
func resourceBuildConfigurationCreate(d *schema.ResourceData, meta interface{}) error {
return resourceBuildConfigurationCreateInternal(d, meta, false)
}
func resourceBuildConfigurationRead(d *schema.ResourceData, meta interface{}) error {
return resourceBuildConfigurationReadInternal(d, meta, false)
}
func resourceBuildConfigurationUpdate(d *schema.ResourceData, meta interface{}) error {
return resourceBuildConfigurationUpdateInternal(d, meta, false)
}
func resourceBuildConfigurationDelete(d *schema.ResourceData, meta interface{}) error {
return resourceBuildConfigurationDeleteInternal(d, meta, false)
}
func resourceBuildTemplateCreate(d *schema.ResourceData, meta interface{}) error {
return resourceBuildConfigurationCreateInternal(d, meta, true)
}
func resourceBuildTemplateRead(d *schema.ResourceData, meta interface{}) error {
return resourceBuildConfigurationReadInternal(d, meta, true)
}
func resourceBuildTemplateUpdate(d *schema.ResourceData, meta interface{}) error {
return resourceBuildConfigurationUpdateInternal(d, meta, true)
}
func resourceBuildTemplateDelete(d *schema.ResourceData, meta interface{}) error {
return resourceBuildConfigurationDeleteInternal(d, meta, true)
}
/*
ID string `json:"id,omitempty"`
ProjectID string `json:"projectId"`
TemplateFlag bool `json:"templateFlag"`
Template *TemplateID `json:"template,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Settings Properties `json:"settings,omitempty"`
Parameters Properties `json:"parameters,omitempty"`
Steps BuildSteps `json:"steps,omitempty"`
VcsRootEntries VcsRootEntries `json:"vcs-root-entries,omitempty"`
*/
func resourceBuildConfigurationCreateInternal(d *schema.ResourceData, meta interface{}, template bool) error {
client := meta.(*teamcity.Client)
projectID := d.Get("project").(string)
name := d.Get("name").(string)
steps := resourceBuildSteps(d.Get("step").([]interface{}))
d.Partial(true)
templateID := ""
if !template {
templateID = d.Get("template").(string)
}
template_parameters := make(types.Parameters)
if templateID != "" {
if template_config, err := client.GetBuildConfiguration(templateID); err != nil {
return err
} else {
template_parameters = template_config.Parameters
if len(template_config.Steps) > 0 && len(steps) > 0 {
return fmt.Errorf("Can't combine build config steps and template build steps %s", name)
}
}
}
var project_parameters types.Parameters
if project, err := client.GetProject(projectID); err != nil {
return err
} else {
project_parameters = project.Parameters
}
parameters := definitionToParameters(*d.Get("parameter").(*schema.Set))
for name, _ := range parameters {
if project_parameter, ok := project_parameters[name]; ok && project_parameter.Spec != nil {
return fmt.Errorf("Can't redefine project parameter %s", name)
}
if template_parameter, ok := template_parameters[name]; ok && template_parameter.Spec != nil {
return fmt.Errorf("Can't redefine template parameter %s", name)
}
}
config := types.BuildConfiguration{
ProjectID: projectID,
TemplateFlag: template,
TemplateID: types.TemplateId(templateID),
Name: name,
Description: d.Get("description").(string),
Steps: steps,
}
if err := client.CreateBuildConfiguration(&config); err != nil {
return err
}
id := config.ID
d.SetId(id)
d.SetPartial("project")
d.SetPartial("name")
d.SetPartial("description")
if !template {
d.SetPartial("template")
}
d.SetPartial("step")
for name, v := range d.Get("parameter_values").(map[string]interface{}) {
value := v.(string)
parameter, ok := parameters[name]
if !ok {
if parameter, ok = project_parameters[name]; !ok {
if parameter, ok = template_parameters[name]; !ok {
parameter = types.Parameter{
Value: value,
}
}
}
}
parameter.Value = value
parameters[name] = parameter
log.Printf("Parameter value %s => %s", name, parameter.Value)
}
log.Printf("Replace Parameters value %q", parameters)
if err := client.ReplaceAllBuildConfigurationParameters(id, ¶meters); err != nil {
return err
}
d.SetPartial("parameter_values")
d.SetPartial("parameter")
for _, root := range resourceAttachedVcsRoots(*d.Get("attached_vcs_root").(*schema.Set)) {
err := client.AttachBuildConfigurationVcsRoot(id, &root)
if err != nil {
return err
}
}
d.SetPartial("attached_vcs_root")
d.Partial(false)
return nil
}
func resourceBuildConfigurationReadInternal(d *schema.ResourceData, meta interface{}, template bool) error {
log.Printf("Reading resource %q", d.Id())
client := meta.(*teamcity.Client)
config, err := client.GetBuildConfiguration(d.Id())
if err != nil {
return err
}
if config == nil || template != config.TemplateFlag {
d.SetId("")
return nil
}
log.Printf("Reading resource %q\n%q", d.Id(), d.Get("parameters"))
d.Set("project", config.ProjectID)
d.Set("name", config.Name)
d.Set("description", config.Description)
if !template {
d.Set("template", config.TemplateID)
}
templateID := string(config.TemplateID)
template_parameters := make(types.Parameters)
template_steps := make(types.BuildSteps, 0)
template_vcs_roots := make(types.VcsRootEntries, 0)
if templateID != "" {
if template_config, err := client.GetBuildConfiguration(templateID); err != nil {
return err
} else {
template_parameters = template_config.Parameters
template_steps = template_config.Steps
template_vcs_roots = template_config.VcsRootEntries
}
}
steps := make([]map[string]interface{}, 0)
for _, step := range config.Steps {
inTemplate := false
for _, template_step := range template_steps {
if step.ID == template_step.ID {
inTemplate = true
break
}
}
if inTemplate {
continue
}
v := make(map[string]interface{})
v["type"] = step.Type
if step.Name != "" {
v["name"] = step.Name
}
properties := make(map[string]interface{})
for name, prop := range step.Properties {
properties[name] = prop
}
if len(properties) > 0 {
v["properties"] = properties
}
steps = append(steps, v)
}
log.Printf("[INFO] Steps %q\n", steps)
d.Set("step", steps)
var project_parameters types.Parameters
if project, err := client.GetProject(string(config.ProjectID)); err != nil {
return err
} else {
project_parameters = project.Parameters
}
parameters := config.Parameters
values := make(map[string]interface{})
current := d.Get("parameter_values").(map[string]interface{})
for name, parameter := range config.Parameters {
if project_parameter, ok := project_parameters[name]; ok {
if project_parameter.Value != parameter.Value {
values[name] = parameter.Value
}
if project_parameter.Spec != nil || parameter.Spec == nil {
delete(parameters, name)
}
} else if template_parameter, ok := template_parameters[name]; ok {
if template_parameter.Value != parameter.Value {
values[name] = parameter.Value
}
if template_parameter.Spec != nil || parameter.Spec == nil {
delete(parameters, name)
}
} else {
if parameter.Spec == nil {
delete(parameters, name)
}
pwt := types.PasswordType{}
if parameter.Value != "" {
values[name] = parameter.Value
} else if parameter.Spec != nil && parameter.Spec.Type == pwt {
if value, ok := current[name]; ok && value != "" {
values[name] = value
}
}
}
}
d.Set("parameter", parametersToDefinition(parameters))
d.Set("parameter_values", values)
vcs_roots := make([]interface{}, 0)
for _, root := range config.VcsRootEntries {
inTemplate := false
for _, template_vcs_root := range template_vcs_roots {
if root.ID == template_vcs_root.ID {
inTemplate = true
break
}
}
if inTemplate {
continue
}
v := make(map[string]interface{})
v["vcs_root"] = string(root.VcsRootID)
v["checkout_rules"] = root.CheckoutRules
vcs_roots = append(vcs_roots, v)
}
d.Set("attached_vcs_root", schema.NewSet(attachedVcsRootValueHash, vcs_roots))
return nil
}
func resourceBuildSteps(steps []interface{}) types.BuildSteps {
tcSteps := make(types.BuildSteps, 0)
for _, s := range steps {
step := s.(map[string]interface{})
typeName := step["type"].(string)
name := step["name"].(string)
properties := step["properties"].(map[string]interface{})
actualProps := make(types.Properties)
for k, v := range properties {
actualProps[k] = v.(string)
}
tcSteps = append(tcSteps, types.BuildStep{
Type: typeName,
Name: name,
Properties: actualProps,
})
}
return tcSteps
}
func resourceAttachedVcsRoots(vcsRoots schema.Set) types.VcsRootEntries {
keySet := schema.NewSet(attachedVcsRootKeyHash, vcsRoots.List())
tcRoots := make(types.VcsRootEntries, 0)
for _, s := range keySet.List() {
entry := s.(map[string]interface{})
vcsRoot := entry["vcs_root"].(string)
rules := entry["checkout_rules"].(string)
tcRoots = append(tcRoots, types.VcsRootEntry{
VcsRootID: types.VcsRootId(vcsRoot),
CheckoutRules: rules,
})
}
return tcRoots
}
func resourceBuildConfigurationUpdateInternal(d *schema.ResourceData, meta interface{}, template bool) error {
client := meta.(*teamcity.Client)
//var err error
id := d.Id()
d.Partial(true)
steps := resourceBuildSteps(d.Get("step").([]interface{}))
templateID := ""
if !template {
templateID = d.Get("template").(string)
}
template_parameters := make(types.Parameters)
if templateID != "" {
if template_config, err := client.GetBuildConfiguration(templateID); err != nil {
return err
} else {
template_parameters = template_config.Parameters
if len(template_config.Steps) > 0 && len(steps) > 0 {
return fmt.Errorf("Can't combine build config steps and template build steps %s", name)
}
}
}
if !template && d.HasChange("template") {
if err := client.SetBuildConfigurationTemplate(id, ""); err != nil {
return err
}
if templateID == "" {
d.SetPartial("template")
}
}
if d.HasChange("parameter") || (!template && d.HasChange("template")) {
projectID := d.Get("project").(string)
var project_parameters types.Parameters
if project, err := client.GetProject(projectID); err != nil {
return err
} else {
project_parameters = project.Parameters
}
o, n := d.GetChange("parameter")
parameters := definitionToParameters(*n.(*schema.Set))
old := definitionToParameters(*o.(*schema.Set))
replace_parameters := make(types.Parameters)
delete_parameters := old
for name, parameter := range parameters {
if project_parameter, ok := project_parameters[name]; ok && project_parameter.Spec != nil {
return fmt.Errorf("Can't redefine project parameter %s", name)
}
if template_parameter, ok := template_parameters[name]; ok && template_parameter.Spec != nil {
return fmt.Errorf("Can't redefine template parameter %s", name)
}
if !reflect.DeepEqual(parameter, old[name]) {
replace_parameters[name] = parameter
}
delete(delete_parameters, name)
}
for name, v := range d.Get("parameter_values").(map[string]interface{}) {
value := v.(string)
parameter, ok := parameters[name]
if !ok {
if parameter, ok = project_parameters[name]; !ok {
if parameter, ok = template_parameters[name]; !ok {
parameter = types.Parameter{
Value: value,
}
}
}
}
parameter.Value = value
replace_parameters[name] = parameter
}
for name, _ := range delete_parameters {
if err := client.DeleteBuildConfigurationParameter(id, name); err != nil {
return err
}
}
for name, parameter := range replace_parameters {
if err := client.DeleteBuildConfigurationParameter(id, name); err != nil {
return err
}
if err := client.ReplaceBuildConfigurationParameter(id, name, ¶meter); err != nil {
return err
}
}
d.SetPartial("parameter_values")
d.SetPartial("parameter")
}
if d.HasChange("attached_vcs_root") {
old, n := d.GetChange("attached_vcs_root")
existing := make(map[types.VcsRootId]bool)
for _, root := range resourceAttachedVcsRoots(*n.(*schema.Set)) {
err := client.AttachBuildConfigurationVcsRoot(id, &root)
if err != nil {
return err
}
existing[root.VcsRootID] = true
}
for _, root := range resourceAttachedVcsRoots(*old.(*schema.Set)) {
if !existing[root.VcsRootID] {
err := client.DetachBuildConfigurationVcsRoot(id, string(root.VcsRootID))
if err != nil {
return err
}
}
}
d.SetPartial("attached_vcs_root")
}
if d.HasChange("description") {
if err := client.SetBuildConfigurationDescription(d.Id(), d.Get("description").(string)); err != nil {
return err
}
d.SetPartial("description")
}
if d.HasChange("step") {
if err := client.ReplaceAllBuildConfigurationSteps(d.Id(), steps); err != nil {
return err
}
d.SetPartial("step")
}
if !template && d.HasChange("template") && templateID != "" {
if err := client.SetBuildConfigurationTemplate(id, templateID); err != nil {
return err
}
}
d.Partial(false)
return nil
}
func resourceBuildConfigurationDeleteInternal(d *schema.ResourceData, meta interface{}, template bool) error {
client := meta.(*teamcity.Client)
return client.DeleteBuildConfiguration(d.Id())
}
func attachedVcsRootValueHash(v interface{}) int {
rd := v.(map[string]interface{})
vcsRoot := rd["vcs_root"].(string)
checkoutRules := rd["checkout_rules"].(string)
hk := fmt.Sprintf("%s:%s", vcsRoot, checkoutRules)
log.Printf("[DEBUG] TeamCity attachedVcsRootValueHash(%#v): %s: hk=%s,hc=%d", v, vcsRoot, hk, hashcode.String(hk))
return hashcode.String(hk)
}
func attachedVcsRootKeyHash(v interface{}) int {
m := v.(map[string]interface{})
hk := m["vcs_root"].(string)
log.Printf("[DEBUG] TeamCity attachedVcsRootKeyHash(%#v): %s: hk=%s,hc=%d", v, hk, hk, hashcode.String(hk))
return hashcode.String(hk)
}