This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
resource_cabot_check_jenkins.go
94 lines (77 loc) · 2.45 KB
/
resource_cabot_check_jenkins.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
package main
import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/mrsaints/go-cabot/cabot"
"strconv"
"time"
)
func resourceCabotCheckJenkins() *schema.Resource {
s := map[string]*schema.Schema{
"max_queued_build_time": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"jenkins_config": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
}
return &schema.Resource{
Create: resourceCabotCheckJenkinsCreate,
Read: resourceCabotCheckJenkinsRead,
Update: resourceCabotCheckJenkinsUpdate,
Delete: resourceCabotCheckJenkinsDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: CombineWithBaseCheckSchema(s),
}
}
func getJenkinsCheckFromResourceData(d *schema.ResourceData) *cabot.JenkinsCheck {
checkRequest := &cabot.JenkinsCheck{
MaxQueuedBuildTime: d.Get("max_queued_build_time").(int),
JenkinsConfig: d.Get("jenkins_config").(int),
}
checkRequest.StatusCheck = getStatusCheckFromResourceData(d)
return checkRequest
}
func resourceCabotCheckJenkinsCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
check, err := client.JenkinsChecks.Create(getJenkinsCheckFromResourceData(d))
if err != nil {
return err
}
d.SetId(strconv.Itoa(check.ID))
return resourceCabotCheckJenkinsRead(d, meta)
}
func resourceCabotCheckJenkinsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
id, _ := strconv.Atoi(d.Id())
return resource.Retry(DEFAULT_RETRY_TIMEOUT, func() *resource.RetryError {
check, err := client.JenkinsChecks.Get(id)
if err != nil {
time.Sleep(DEFAULT_GRACE_PERIOD)
return resource.RetryableError(err)
}
d.SetId(strconv.Itoa(check.ID))
setResourceDataForStatusCheck(d, check.StatusCheck)
d.Set("max_queued_build_time", check.MaxQueuedBuildTime)
return nil
})
}
func resourceCabotCheckJenkinsUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
id, _ := strconv.Atoi(d.Id())
check, err := client.JenkinsChecks.Update((id), getJenkinsCheckFromResourceData(d))
if err != nil {
return err
}
d.SetId(strconv.Itoa(check.ID))
return resourceCabotCheckJenkinsRead(d, meta)
}
func resourceCabotCheckJenkinsDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cabot.Client)
id, _ := strconv.Atoi(d.Id())
return client.JenkinsChecks.Delete(id)
}