aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorstack72 <public@paulstack.co.uk>2015-10-03 23:11:26 +0100
committerstack72 <public@paulstack.co.uk>2015-11-27 15:03:13 +0000
commit7dec75fcd796fa25f655c1f1cda2d202f117eb4b (patch)
tree5093965628b0d81897d01e53cd12f0f44b94e21d
parentb8f05dfc572fcdd53e58e0a1e3851ccc4db8f17e (diff)
downloadterraform-provider-statuscake-7dec75fcd796fa25f655c1f1cda2d202f117eb4b.tar.gz
terraform-provider-statuscake-7dec75fcd796fa25f655c1f1cda2d202f117eb4b.tar.zst
terraform-provider-statuscake-7dec75fcd796fa25f655c1f1cda2d202f117eb4b.zip
Adding the update and read functionality for the statuscake client
-rw-r--r--provider.go6
-rw-r--r--resource_statuscaketest.go85
2 files changed, 67 insertions, 24 deletions
diff --git a/provider.go b/provider.go
index abca376..7d96e0e 100644
--- a/provider.go
+++ b/provider.go
@@ -1,7 +1,7 @@
1package statuscake 1package statuscake
2 2
3import ( 3import (
4 "github.com/DreamItGetIT/statuscake" 4 wtf "github.com/DreamItGetIT/statuscake"
5 "github.com/hashicorp/terraform/helper/schema" 5 "github.com/hashicorp/terraform/helper/schema"
6 "github.com/hashicorp/terraform/terraform" 6 "github.com/hashicorp/terraform/terraform"
7) 7)
@@ -32,9 +32,9 @@ func Provider() terraform.ResourceProvider {
32} 32}
33 33
34func providerConfigure(d *schema.ResourceData) (interface{}, error) { 34func providerConfigure(d *schema.ResourceData) (interface{}, error) {
35 auth := statuscake.Auth{ 35 auth := wtf.Auth{
36 Username: d.Get("username").(string), 36 Username: d.Get("username").(string),
37 Apikey: d.Get("apikey").(string), 37 Apikey: d.Get("apikey").(string),
38 } 38 }
39 return statuscake.New(auth) 39 return wtf.New(auth)
40} 40}
diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go
index 8eb5401..6834135 100644
--- a/resource_statuscaketest.go
+++ b/resource_statuscaketest.go
@@ -6,7 +6,7 @@ import (
6 6
7 "log" 7 "log"
8 8
9 "github.com/DreamItGetIT/statuscake" 9 wtf "github.com/DreamItGetIT/statuscake"
10 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/hashicorp/terraform/helper/schema"
11) 11)
12 12
@@ -49,29 +49,24 @@ func resourceStatusCakeTest() *schema.Resource {
49 Optional: true, 49 Optional: true,
50 Default: false, 50 Default: false,
51 }, 51 },
52 "timeout": &schema.Schema{
53 Type: schema.TypeInt,
54 Computed: true,
55 },
52 }, 56 },
53 } 57 }
54} 58}
55 59
56func CreateTest(d *schema.ResourceData, meta interface{}) error { 60func CreateTest(d *schema.ResourceData, meta interface{}) error {
57 client := meta.(*statuscake.Client) 61 client := meta.(*wtf.Client)
58 62
59 newTest := &statuscake.Test{ 63 newTest := &wtf.Test{
60 WebsiteName: "posters.dreamitget.it", 64 WebsiteName: d.Get("website_name").(string),
61 WebsiteURL: "https://posters.dreamitget.it", 65 WebsiteURL: d.Get("website_url").(string),
62 TestType: "HTTP", 66 TestType: d.Get("test_type").(string),
63 CheckRate: 500, 67 CheckRate: d.Get("check_rate").(int),
64 } 68 }
65 69
66 // newTest := &statuscake.Test{
67 // WebsiteName: d.Get("website_name").(string),
68 // WebsiteURL: d.Get("website_url").(string),
69 // TestType: d.Get("test_type").(string),
70 // CheckRate: 500,
71 // }
72
73 log.Printf("[DEBUG] Check Rate: %d", d.Get("check_rate").(int))
74 log.Printf("[DEBUG] TestType: %s", d.Get("test_type").(string))
75 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) 70 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string))
76 71
77 response, err := client.Tests().Put(newTest) 72 response, err := client.Tests().Put(newTest)
@@ -82,24 +77,31 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error {
82 d.Set("test_id", fmt.Sprintf("%d", response.TestID)) 77 d.Set("test_id", fmt.Sprintf("%d", response.TestID))
83 d.SetId(fmt.Sprintf("%d", response.TestID)) 78 d.SetId(fmt.Sprintf("%d", response.TestID))
84 79
85 return UpdateTest(d, meta) 80 return ReadTest(d, meta)
86} 81}
87 82
88func UpdateTest(d *schema.ResourceData, meta interface{}) error { 83func UpdateTest(d *schema.ResourceData, meta interface{}) error {
84 client := meta.(*wtf.Client)
85
86 params := getStatusCakeTestInput(d)
87
88 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id())
89 _, err := client.Tests().Put(params)
90 if err != nil {
91 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error())
92 }
89 return nil 93 return nil
90} 94}
91 95
92func DeleteTest(d *schema.ResourceData, meta interface{}) error { 96func DeleteTest(d *schema.ResourceData, meta interface{}) error {
93 client := meta.(*statuscake.Client) 97 client := meta.(*wtf.Client)
94 98
95 testId, parseErr := strconv.Atoi(d.Id()) 99 testId, parseErr := strconv.Atoi(d.Id())
96 if parseErr != nil { 100 if parseErr != nil {
97 return parseErr 101 return parseErr
98 } 102 }
99 testIntId := int(testId)
100
101 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id()) 103 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id())
102 err := client.Tests().Delete(testIntId) 104 err := client.Tests().Delete(testId)
103 if err != nil { 105 if err != nil {
104 return err 106 return err
105 } 107 }
@@ -108,5 +110,46 @@ func DeleteTest(d *schema.ResourceData, meta interface{}) error {
108} 110}
109 111
110func ReadTest(d *schema.ResourceData, meta interface{}) error { 112func ReadTest(d *schema.ResourceData, meta interface{}) error {
113 client := meta.(*wtf.Client)
114
115 testId, parseErr := strconv.Atoi(d.Id())
116 if parseErr != nil {
117 return parseErr
118 }
119 testResp, err := client.Tests().Details(testId)
120 if err != nil {
121 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err)
122 }
123 d.Set("check_rate", testResp.CheckRate)
124
111 return nil 125 return nil
112} 126}
127
128func getStatusCakeTestInput(d *schema.ResourceData) *wtf.Test {
129 testId, parseErr := strconv.Atoi(d.Id())
130 if parseErr != nil {
131 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id())
132 }
133 test := &wtf.Test{
134 TestID: testId,
135 }
136 if v, ok := d.GetOk("website_name"); ok {
137 test.WebsiteName = v.(string)
138 }
139 if v, ok := d.GetOk("website_url"); ok {
140 test.WebsiteURL = v.(string)
141 }
142 if v, ok := d.GetOk("check_rate"); ok {
143 test.CheckRate = v.(int)
144 }
145 if v, ok := d.GetOk("test_type"); ok {
146 test.TestType = v.(string)
147 }
148 if v, ok := d.GetOk("paused"); ok {
149 test.Paused = v.(bool)
150 }
151 if v, ok := d.GetOk("timeout"); ok {
152 test.Timeout = v.(int)
153 }
154 return test
155}