|
1 | 1 | package statuscake |
2 | 2 |
|
3 | | -import "github.com/hashicorp/terraform/helper/schema" |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "log" |
| 8 | + |
| 9 | + "github.com/DreamItGetIT/statuscake" |
| 10 | + "github.com/hashicorp/terraform/helper/schema" |
| 11 | +) |
4 | 12 |
|
5 | 13 | func resourceStatusCakeTest() *schema.Resource { |
6 | 14 | return &schema.Resource{ |
7 | 15 | Create: CreateTest, |
8 | 16 | Update: UpdateTest, |
9 | 17 | Delete: DeleteTest, |
10 | 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 | + }, |
11 | 53 | } |
12 | 54 | } |
13 | 55 |
|
14 | 56 | func CreateTest(d *schema.ResourceData, meta interface{}) error { |
15 | | - return nil |
| 57 | + client := meta.(*statuscake.Client) |
| 58 | + |
| 59 | + newTest := &statuscake.Test{ |
| 60 | + WebsiteName: "posters.dreamitget.it", |
| 61 | + WebsiteURL: "https://posters.dreamitget.it", |
| 62 | + TestType: "HTTP", |
| 63 | + CheckRate: 500, |
| 64 | + } |
| 65 | + |
| 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)) |
| 76 | + |
| 77 | + response, err := client.Tests().Put(newTest) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("Error creating StatusCake Test: %s", err.Error()) |
| 80 | + } |
| 81 | + |
| 82 | + d.Set("test_id", fmt.Sprintf("%d", response.TestID)) |
| 83 | + d.SetId(fmt.Sprintf("%d", response.TestID)) |
| 84 | + |
| 85 | + return UpdateTest(d, meta) |
16 | 86 | } |
17 | 87 |
|
18 | 88 | func UpdateTest(d *schema.ResourceData, meta interface{}) error { |
19 | 89 | return nil |
20 | 90 | } |
21 | 91 |
|
22 | 92 | func DeleteTest(d *schema.ResourceData, meta interface{}) error { |
| 93 | + client := meta.(*statuscake.Client) |
| 94 | + |
| 95 | + testId, parseErr := strconv.Atoi(d.Id()) |
| 96 | + if parseErr != nil { |
| 97 | + return parseErr |
| 98 | + } |
| 99 | + testIntId := int(testId) |
| 100 | + |
| 101 | + log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id()) |
| 102 | + err := client.Tests().Delete(testIntId) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
23 | 107 | return nil |
24 | 108 | } |
25 | 109 |
|
|
0 commit comments