github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/statuscake/resource_statuscaketest.go (about) 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 "contact_id": &schema.Schema{ 37 Type: schema.TypeInt, 38 Optional: true, 39 }, 40 41 "check_rate": &schema.Schema{ 42 Type: schema.TypeInt, 43 Optional: true, 44 Default: 300, 45 }, 46 47 "test_type": &schema.Schema{ 48 Type: schema.TypeString, 49 Required: true, 50 }, 51 52 "paused": &schema.Schema{ 53 Type: schema.TypeBool, 54 Optional: true, 55 Default: false, 56 }, 57 "timeout": &schema.Schema{ 58 Type: schema.TypeInt, 59 Computed: true, 60 }, 61 "confirmations": &schema.Schema{ 62 Type: schema.TypeInt, 63 Optional: true, 64 }, 65 }, 66 } 67 } 68 69 func CreateTest(d *schema.ResourceData, meta interface{}) error { 70 client := meta.(*statuscake.Client) 71 72 newTest := &statuscake.Test{ 73 WebsiteName: d.Get("website_name").(string), 74 WebsiteURL: d.Get("website_url").(string), 75 CheckRate: d.Get("check_rate").(int), 76 TestType: d.Get("test_type").(string), 77 Paused: d.Get("paused").(bool), 78 Timeout: d.Get("timeout").(int), 79 ContactID: d.Get("contact_id").(int), 80 Confirmation: d.Get("confirmations").(int), 81 } 82 83 log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) 84 85 response, err := client.Tests().Update(newTest) 86 if err != nil { 87 return fmt.Errorf("Error creating StatusCake Test: %s", err.Error()) 88 } 89 90 d.Set("test_id", fmt.Sprintf("%d", response.TestID)) 91 d.SetId(fmt.Sprintf("%d", response.TestID)) 92 93 return ReadTest(d, meta) 94 } 95 96 func UpdateTest(d *schema.ResourceData, meta interface{}) error { 97 client := meta.(*statuscake.Client) 98 99 params := getStatusCakeTestInput(d) 100 101 log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id()) 102 _, err := client.Tests().Update(params) 103 if err != nil { 104 return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error()) 105 } 106 return nil 107 } 108 109 func DeleteTest(d *schema.ResourceData, meta interface{}) error { 110 client := meta.(*statuscake.Client) 111 112 testId, parseErr := strconv.Atoi(d.Id()) 113 if parseErr != nil { 114 return parseErr 115 } 116 log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id()) 117 err := client.Tests().Delete(testId) 118 if err != nil { 119 return err 120 } 121 122 return nil 123 } 124 125 func ReadTest(d *schema.ResourceData, meta interface{}) error { 126 client := meta.(*statuscake.Client) 127 128 testId, parseErr := strconv.Atoi(d.Id()) 129 if parseErr != nil { 130 return parseErr 131 } 132 testResp, err := client.Tests().Detail(testId) 133 if err != nil { 134 return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err) 135 } 136 d.Set("website_name", testResp.WebsiteName) 137 d.Set("website_url", testResp.WebsiteURL) 138 d.Set("check_rate", testResp.CheckRate) 139 d.Set("test_type", testResp.TestType) 140 d.Set("paused", testResp.Paused) 141 d.Set("timeout", testResp.Timeout) 142 d.Set("contact_id", testResp.ContactID) 143 d.Set("confirmations", testResp.Confirmation) 144 145 return nil 146 } 147 148 func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { 149 testId, parseErr := strconv.Atoi(d.Id()) 150 if parseErr != nil { 151 log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id()) 152 } 153 test := &statuscake.Test{ 154 TestID: testId, 155 } 156 if v, ok := d.GetOk("website_name"); ok { 157 test.WebsiteName = v.(string) 158 } 159 if v, ok := d.GetOk("website_url"); ok { 160 test.WebsiteURL = v.(string) 161 } 162 if v, ok := d.GetOk("check_rate"); ok { 163 test.CheckRate = v.(int) 164 } 165 if v, ok := d.GetOk("contact_id"); ok { 166 test.ContactID = v.(int) 167 } 168 if v, ok := d.GetOk("test_type"); ok { 169 test.TestType = v.(string) 170 } 171 if v, ok := d.GetOk("paused"); ok { 172 test.Paused = v.(bool) 173 } 174 if v, ok := d.GetOk("timeout"); ok { 175 test.Timeout = v.(int) 176 } 177 if v, ok := d.GetOk("contact_id"); ok { 178 test.ContactID = v.(int) 179 } 180 if v, ok := d.GetOk("confirmations"); ok { 181 test.Confirmation = v.(int) 182 } 183 return test 184 }