]>
Commit | Line | Data |
---|---|---|
478e1338 | 1 | package statuscake |
2 | ||
b8f05dfc | 3 | import ( |
4 | "fmt" | |
5 | "strconv" | |
6 | ||
7 | "log" | |
8 | ||
4eeeab64 | 9 | "github.com/DreamItGetIT/statuscake" |
b8f05dfc | 10 | "github.com/hashicorp/terraform/helper/schema" |
11 | ) | |
478e1338 | 12 | |
13 | func resourceStatusCakeTest() *schema.Resource { | |
14 | return &schema.Resource{ | |
15 | Create: CreateTest, | |
16 | Update: UpdateTest, | |
17 | Delete: DeleteTest, | |
18 | Read: ReadTest, | |
b8f05dfc | 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 | }, | |
7dec75fc | 52 | "timeout": &schema.Schema{ |
53 | Type: schema.TypeInt, | |
54 | Computed: true, | |
55 | }, | |
51688607 | 56 | "contact_id": &schema.Schema{ |
57 | Type: schema.TypeInt, | |
58 | Optional: true, | |
59 | }, | |
b8f05dfc | 60 | }, |
478e1338 | 61 | } |
62 | } | |
63 | ||
64 | func CreateTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 65 | client := meta.(*statuscake.Client) |
b8f05dfc | 66 | |
4eeeab64 | 67 | newTest := &statuscake.Test{ |
7dec75fc | 68 | WebsiteName: d.Get("website_name").(string), |
69 | WebsiteURL: d.Get("website_url").(string), | |
7dec75fc | 70 | CheckRate: d.Get("check_rate").(int), |
c0701076 RG |
71 | TestType: d.Get("test_type").(string), |
72 | Paused: d.Get("paused").(bool), | |
73 | Timeout: d.Get("timeout").(int), | |
74 | ContactID: d.Get("contact_id").(int), | |
b8f05dfc | 75 | } |
76 | ||
b8f05dfc | 77 | log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) |
78 | ||
32574941 | 79 | response, err := client.Tests().Update(newTest) |
b8f05dfc | 80 | if err != nil { |
81 | return fmt.Errorf("Error creating StatusCake Test: %s", err.Error()) | |
82 | } | |
83 | ||
84 | d.Set("test_id", fmt.Sprintf("%d", response.TestID)) | |
85 | d.SetId(fmt.Sprintf("%d", response.TestID)) | |
86 | ||
7dec75fc | 87 | return ReadTest(d, meta) |
478e1338 | 88 | } |
89 | ||
90 | func UpdateTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 91 | client := meta.(*statuscake.Client) |
7dec75fc | 92 | |
93 | params := getStatusCakeTestInput(d) | |
94 | ||
95 | log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id()) | |
32574941 | 96 | _, err := client.Tests().Update(params) |
7dec75fc | 97 | if err != nil { |
98 | return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error()) | |
99 | } | |
478e1338 | 100 | return nil |
101 | } | |
102 | ||
103 | func DeleteTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 104 | client := meta.(*statuscake.Client) |
b8f05dfc | 105 | |
106 | testId, parseErr := strconv.Atoi(d.Id()) | |
107 | if parseErr != nil { | |
108 | return parseErr | |
109 | } | |
b8f05dfc | 110 | log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id()) |
7dec75fc | 111 | err := client.Tests().Delete(testId) |
b8f05dfc | 112 | if err != nil { |
113 | return err | |
114 | } | |
115 | ||
478e1338 | 116 | return nil |
117 | } | |
118 | ||
119 | func ReadTest(d *schema.ResourceData, meta interface{}) error { | |
4eeeab64 | 120 | client := meta.(*statuscake.Client) |
7dec75fc | 121 | |
122 | testId, parseErr := strconv.Atoi(d.Id()) | |
123 | if parseErr != nil { | |
124 | return parseErr | |
125 | } | |
32574941 | 126 | testResp, err := client.Tests().Detail(testId) |
7dec75fc | 127 | if err != nil { |
128 | return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err) | |
129 | } | |
c0701076 RG |
130 | d.Set("website_name", testResp.WebsiteName) |
131 | d.Set("website_url", testResp.WebsiteURL) | |
7dec75fc | 132 | d.Set("check_rate", testResp.CheckRate) |
c0701076 RG |
133 | d.Set("test_type", testResp.TestType) |
134 | d.Set("paused", testResp.Paused) | |
135 | d.Set("timeout", testResp.Timeout) | |
136 | d.Set("contact_id", testResp.ContactID) | |
7dec75fc | 137 | |
478e1338 | 138 | return nil |
139 | } | |
7dec75fc | 140 | |
4eeeab64 | 141 | func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { |
7dec75fc | 142 | testId, parseErr := strconv.Atoi(d.Id()) |
143 | if parseErr != nil { | |
144 | log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id()) | |
145 | } | |
4eeeab64 | 146 | test := &statuscake.Test{ |
7dec75fc | 147 | TestID: testId, |
148 | } | |
149 | if v, ok := d.GetOk("website_name"); ok { | |
150 | test.WebsiteName = v.(string) | |
151 | } | |
152 | if v, ok := d.GetOk("website_url"); ok { | |
153 | test.WebsiteURL = v.(string) | |
154 | } | |
155 | if v, ok := d.GetOk("check_rate"); ok { | |
156 | test.CheckRate = v.(int) | |
157 | } | |
158 | if v, ok := d.GetOk("test_type"); ok { | |
159 | test.TestType = v.(string) | |
160 | } | |
161 | if v, ok := d.GetOk("paused"); ok { | |
162 | test.Paused = v.(bool) | |
163 | } | |
164 | if v, ok := d.GetOk("timeout"); ok { | |
165 | test.Timeout = v.(int) | |
166 | } | |
51688607 | 167 | if v, ok := d.GetOk("contact_id"); ok { |
168 | test.ContactID = v.(int) | |
169 | } | |
7dec75fc | 170 | return test |
171 | } |