]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - resource_statuscaketest.go
enable contact-group id in statuscake test
[github/fretlink/terraform-provider-statuscake.git] / resource_statuscaketest.go
1 package statuscake
2
3 import (
4 "fmt"
5 "strconv"
6
7 "log"
8
9 "github.com/DreamItGetIT/statuscake"
10 "github.com/hashicorp/terraform/helper/schema"
11 )
12
13 func resourceStatusCakeTest() *schema.Resource {
14 return &schema.Resource{
15 Create: CreateTest,
16 Update: UpdateTest,
17 Delete: DeleteTest,
18 Read: ReadTest,
19
20 Schema: map[string]*schema.Schema{
21 "test_id": &schema.Schema{
22 Type: schema.TypeString,
23 Computed: true,
24 },
25
26 "website_name": &schema.Schema{
27 Type: schema.TypeString,
28 Required: true,
29 },
30
31 "website_url": &schema.Schema{
32 Type: schema.TypeString,
33 Required: true,
34 },
35
36 "check_rate": &schema.Schema{
37 Type: schema.TypeInt,
38 Optional: true,
39 Default: 300,
40 },
41
42 "test_type": &schema.Schema{
43 Type: schema.TypeString,
44 Required: true,
45 },
46
47 "paused": &schema.Schema{
48 Type: schema.TypeBool,
49 Optional: true,
50 Default: false,
51 },
52 "timeout": &schema.Schema{
53 Type: schema.TypeInt,
54 Computed: true,
55 },
56 "contact_id": &schema.Schema{
57 Type: schema.TypeInt,
58 Optional: true,
59 },
60 },
61 }
62 }
63
64 func CreateTest(d *schema.ResourceData, meta interface{}) error {
65 client := meta.(*statuscake.Client)
66
67 newTest := &statuscake.Test{
68 WebsiteName: d.Get("website_name").(string),
69 WebsiteURL: d.Get("website_url").(string),
70 TestType: d.Get("test_type").(string),
71 CheckRate: d.Get("check_rate").(int),
72 }
73
74 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
75
76 response, err := client.Tests().Update(newTest)
77 if err != nil {
78 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error())
79 }
80
81 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
82 d.SetId(fmt.Sprintf("%d", response.TestID))
83
84 return ReadTest(d, meta)
85 }
86
87 func UpdateTest(d *schema.ResourceData, meta interface{}) error {
88 client := meta.(*statuscake.Client)
89
90 params := getStatusCakeTestInput(d)
91
92 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
93 _, err := client.Tests().Update(params)
94 if err != nil {
95 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
96 }
97 return nil
98 }
99
100 func DeleteTest(d *schema.ResourceData, meta interface{}) error {
101 client := meta.(*statuscake.Client)
102
103 testId, parseErr := strconv.Atoi(d.Id())
104 if parseErr != nil {
105 return parseErr
106 }
107 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
108 err := client.Tests().Delete(testId)
109 if err != nil {
110 return err
111 }
112
113 return nil
114 }
115
116 func ReadTest(d *schema.ResourceData, meta interface{}) error {
117 client := meta.(*statuscake.Client)
118
119 testId, parseErr := strconv.Atoi(d.Id())
120 if parseErr != nil {
121 return parseErr
122 }
123 testResp, err := client.Tests().Detail(testId)
124 if err != nil {
125 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
126 }
127 d.Set("check_rate", testResp.CheckRate)
128
129 return nil
130 }
131
132 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test {
133 testId, parseErr := strconv.Atoi(d.Id())
134 if parseErr != nil {
135 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
136 }
137 test := &statuscake.Test{
138 TestID: testId,
139 }
140 if v, ok := d.GetOk("website_name"); ok {
141 test.WebsiteName = v.(string)
142 }
143 if v, ok := d.GetOk("website_url"); ok {
144 test.WebsiteURL = v.(string)
145 }
146 if v, ok := d.GetOk("check_rate"); ok {
147 test.CheckRate = v.(int)
148 }
149 if v, ok := d.GetOk("test_type"); ok {
150 test.TestType = v.(string)
151 }
152 if v, ok := d.GetOk("paused"); ok {
153 test.Paused = v.(bool)
154 }
155 if v, ok := d.GetOk("timeout"); ok {
156 test.Timeout = v.(int)
157 }
158 if v, ok := d.GetOk("contact_id"); ok {
159 test.ContactID = v.(int)
160 }
161 return test
162 }