Skip to content

Commit 97c0aa5

Browse files
committed
Using the latest version of the statuscake client
1 parent d26f5f9 commit 97c0aa5

3 files changed

Lines changed: 167 additions & 6 deletions

File tree

builtin/providers/statuscake/provider.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ func Provider() terraform.ResourceProvider {
3232
}
3333

3434
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
35-
username := d.Get("username").(string)
36-
apiKey := d.Get("apikey").(string)
37-
38-
return statuscake.New(apiKey, username), nil
35+
auth := statuscake.Auth{
36+
Username: d.Get("username").(string),
37+
Apikey: d.Get("apikey").(string),
38+
}
39+
return statuscake.New(auth)
3940
}

builtin/providers/statuscake/resource_statuscaketest.go

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,109 @@
11
package statuscake
22

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+
)
412

513
func resourceStatusCakeTest() *schema.Resource {
614
return &schema.Resource{
715
Create: CreateTest,
816
Update: UpdateTest,
917
Delete: DeleteTest,
1018
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+
},
1153
}
1254
}
1355

1456
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)
1686
}
1787

1888
func UpdateTest(d *schema.ResourceData, meta interface{}) error {
1989
return nil
2090
}
2191

2292
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+
23107
return nil
24108
}
25109

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package statuscake
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/DreamItGetIT/statuscake"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
"github.com/hashicorp/terraform/terraform"
10+
)
11+
12+
func TestAccStatusCake_basic(t *testing.T) {
13+
var test statuscake.Test
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheck(t) },
17+
Providers: testAccProviders,
18+
CheckDestroy: testAccTestCheckDestroy(&test),
19+
Steps: []resource.TestStep{
20+
resource.TestStep{
21+
Config: testAccTestConfig_basic,
22+
Check: resource.ComposeTestCheckFunc(
23+
testAccTestCheckExists("statuscake_test.google", &test),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
func testAccTestCheckExists(rn string, project *statuscake.Test) resource.TestCheckFunc {
31+
return func(s *terraform.State) error {
32+
rs, ok := s.RootModule().Resources[rn]
33+
if !ok {
34+
return fmt.Errorf("resource not found: %s", rn)
35+
}
36+
37+
if rs.Primary.ID == "" {
38+
return fmt.Errorf("TestID not set")
39+
}
40+
41+
// client := testAccProvider.Meta().(*statuscake.Client)
42+
// gotProject, err := client.GetProject(rs.Primary.ID)
43+
// if err != nil {
44+
// return fmt.Errorf("error getting project: %s", err)
45+
// }
46+
//
47+
// *project = *gotProject
48+
49+
return nil
50+
}
51+
}
52+
53+
func testAccTestCheckDestroy(project *statuscake.Test) resource.TestCheckFunc {
54+
// return func(s *terraform.State) error {
55+
// client := testAccProvider.Meta().(*statuscake.Client)
56+
// // _, err := client.Tests().All()
57+
// // if err == nil {
58+
// // return fmt.Errorf("test still exists")
59+
// // }
60+
// // if _, ok := err.(*statuscake.NotFoundError); !ok {
61+
// // return fmt.Errorf("got something other than NotFoundError (%v) when getting test", err)
62+
// // }
63+
//
64+
// return nil
65+
// }
66+
return nil
67+
}
68+
69+
const testAccTestConfig_basic = `
70+
resource "statuscake_test" "google" {
71+
website_name = "google.com"
72+
website_url = "www.google.com"
73+
test_type = "HTTP"
74+
check_rate = 300
75+
}
76+
`

0 commit comments

Comments
 (0)