From 478e133852c1c49f98c0388920142dc507479487 Mon Sep 17 00:00:00 2001 From: stack72 Date: Mon, 28 Sep 2015 11:39:32 +0100 Subject: Created the initial scaffolding for the statuscake provider --- provider.go | 39 +++++++++++++++++++++++++++++++++++++++ provider_test.go | 38 ++++++++++++++++++++++++++++++++++++++ resource_statuscaketest.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 provider.go create mode 100644 provider_test.go create mode 100644 resource_statuscaketest.go diff --git a/provider.go b/provider.go new file mode 100644 index 0000000..7402e80 --- /dev/null +++ b/provider.go @@ -0,0 +1,39 @@ +package statuscake + +import ( + "github.com/DreamItGetIT/statuscake" + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/terraform" +) + +func Provider() terraform.ResourceProvider { + return &schema.Provider{ + Schema: map[string]*schema.Schema{ + "username": &schema.Schema{ + Type: schema.TypeString, + Required: true, + DefaultFunc: schema.EnvDefaultFunc("STATUSCAKE_USERNAME", nil), + Description: "Username for StatusCake Account.", + }, + "apikey": &schema.Schema{ + Type: schema.TypeString, + Required: true, + DefaultFunc: schema.EnvDefaultFunc("STATUSCAKE_APIKEY", nil), + Description: "API Key for StatusCake", + }, + }, + + ResourcesMap: map[string]*schema.Resource{ + "statuscake_test": resourceStatusCakeTest(), + }, + + ConfigureFunc: providerConfigure, + } +} + +func providerConfigure(d *schema.ResourceData) (interface{}, error) { + username := d.Get("username").(string) + apiKey := d.Get("apikey").(string) + + return statuscake.New(apiKey, username), nil +} diff --git a/provider_test.go b/provider_test.go new file mode 100644 index 0000000..83045d0 --- /dev/null +++ b/provider_test.go @@ -0,0 +1,38 @@ +package statuscake + +import ( + "os" + "testing" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/terraform" +) + +var testAccProviders map[string]terraform.ResourceProvider +var testAccProvider *schema.Provider + +func init() { + testAccProvider = Provider().(*schema.Provider) + testAccProviders = map[string]terraform.ResourceProvider{ + "statuscake": testAccProvider, + } +} + +func TestProvider(t *testing.T) { + if err := Provider().(*schema.Provider).InternalValidate(); err != nil { + t.Fatalf("err: %s", err) + } +} + +func TestProvider_impl(t *testing.T) { + var _ terraform.ResourceProvider = Provider() +} + +func testAccPreCheck(t *testing.T) { + if v := os.Getenv("STATUSCAKE_USERNAME"); v == "" { + t.Fatal("STATUSCAKE_USERNAME must be set for acceptance tests") + } + if v := os.Getenv("STATUSCAKE_APIKEY"); v == "" { + t.Fatal("STATUSCAKE_APIKEY must be set for acceptance tests") + } +} diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go new file mode 100644 index 0000000..f34a97a --- /dev/null +++ b/resource_statuscaketest.go @@ -0,0 +1,28 @@ +package statuscake + +import "github.com/hashicorp/terraform/helper/schema" + +func resourceStatusCakeTest() *schema.Resource { + return &schema.Resource{ + Create: CreateTest, + Update: UpdateTest, + Delete: DeleteTest, + Read: ReadTest, + } +} + +func CreateTest(d *schema.ResourceData, meta interface{}) error { + return nil +} + +func UpdateTest(d *schema.ResourceData, meta interface{}) error { + return nil +} + +func DeleteTest(d *schema.ResourceData, meta interface{}) error { + return nil +} + +func ReadTest(d *schema.ResourceData, meta interface{}) error { + return nil +} -- cgit v1.2.3 From b8f05dfc572fcdd53e58e0a1e3851ccc4db8f17e Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 1 Oct 2015 23:09:13 +0100 Subject: Using the latest version of the statuscake client --- provider.go | 9 +++-- resource_statuscaketest.go | 88 ++++++++++++++++++++++++++++++++++++++++- resource_statuscaketest_test.go | 76 +++++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 resource_statuscaketest_test.go diff --git a/provider.go b/provider.go index 7402e80..abca376 100644 --- a/provider.go +++ b/provider.go @@ -32,8 +32,9 @@ func Provider() terraform.ResourceProvider { } func providerConfigure(d *schema.ResourceData) (interface{}, error) { - username := d.Get("username").(string) - apiKey := d.Get("apikey").(string) - - return statuscake.New(apiKey, username), nil + auth := statuscake.Auth{ + Username: d.Get("username").(string), + Apikey: d.Get("apikey").(string), + } + return statuscake.New(auth) } diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go index f34a97a..8eb5401 100644 --- a/resource_statuscaketest.go +++ b/resource_statuscaketest.go @@ -1,6 +1,14 @@ package statuscake -import "github.com/hashicorp/terraform/helper/schema" +import ( + "fmt" + "strconv" + + "log" + + "github.com/DreamItGetIT/statuscake" + "github.com/hashicorp/terraform/helper/schema" +) func resourceStatusCakeTest() *schema.Resource { return &schema.Resource{ @@ -8,11 +16,73 @@ func resourceStatusCakeTest() *schema.Resource { Update: UpdateTest, Delete: DeleteTest, Read: ReadTest, + + Schema: map[string]*schema.Schema{ + "test_id": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + + "website_name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + + "website_url": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + + "check_rate": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Default: 300, + }, + + "test_type": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + + "paused": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + }, } } func CreateTest(d *schema.ResourceData, meta interface{}) error { - return nil + client := meta.(*statuscake.Client) + + newTest := &statuscake.Test{ + WebsiteName: "posters.dreamitget.it", + WebsiteURL: "https://posters.dreamitget.it", + TestType: "HTTP", + CheckRate: 500, + } + + // newTest := &statuscake.Test{ + // WebsiteName: d.Get("website_name").(string), + // WebsiteURL: d.Get("website_url").(string), + // TestType: d.Get("test_type").(string), + // CheckRate: 500, + // } + + log.Printf("[DEBUG] Check Rate: %d", d.Get("check_rate").(int)) + log.Printf("[DEBUG] TestType: %s", d.Get("test_type").(string)) + log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) + + response, err := client.Tests().Put(newTest) + if err != nil { + return fmt.Errorf("Error creating StatusCake Test: %s", err.Error()) + } + + d.Set("test_id", fmt.Sprintf("%d", response.TestID)) + d.SetId(fmt.Sprintf("%d", response.TestID)) + + return UpdateTest(d, meta) } func UpdateTest(d *schema.ResourceData, meta interface{}) error { @@ -20,6 +90,20 @@ func UpdateTest(d *schema.ResourceData, meta interface{}) error { } func DeleteTest(d *schema.ResourceData, meta interface{}) error { + client := meta.(*statuscake.Client) + + testId, parseErr := strconv.Atoi(d.Id()) + if parseErr != nil { + return parseErr + } + testIntId := int(testId) + + log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id()) + err := client.Tests().Delete(testIntId) + if err != nil { + return err + } + return nil } diff --git a/resource_statuscaketest_test.go b/resource_statuscaketest_test.go new file mode 100644 index 0000000..8bbd4b5 --- /dev/null +++ b/resource_statuscaketest_test.go @@ -0,0 +1,76 @@ +package statuscake + +import ( + "fmt" + "testing" + + "github.com/DreamItGetIT/statuscake" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccStatusCake_basic(t *testing.T) { + var test statuscake.Test + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccTestCheckDestroy(&test), + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccTestConfig_basic, + Check: resource.ComposeTestCheckFunc( + testAccTestCheckExists("statuscake_test.google", &test), + ), + }, + }, + }) +} + +func testAccTestCheckExists(rn string, project *statuscake.Test) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[rn] + if !ok { + return fmt.Errorf("resource not found: %s", rn) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("TestID not set") + } + + // client := testAccProvider.Meta().(*statuscake.Client) + // gotProject, err := client.GetProject(rs.Primary.ID) + // if err != nil { + // return fmt.Errorf("error getting project: %s", err) + // } + // + // *project = *gotProject + + return nil + } +} + +func testAccTestCheckDestroy(project *statuscake.Test) resource.TestCheckFunc { + // return func(s *terraform.State) error { + // client := testAccProvider.Meta().(*statuscake.Client) + // // _, err := client.Tests().All() + // // if err == nil { + // // return fmt.Errorf("test still exists") + // // } + // // if _, ok := err.(*statuscake.NotFoundError); !ok { + // // return fmt.Errorf("got something other than NotFoundError (%v) when getting test", err) + // // } + // + // return nil + // } + return nil +} + +const testAccTestConfig_basic = ` +resource "statuscake_test" "google" { + website_name = "google.com" + website_url = "www.google.com" + test_type = "HTTP" + check_rate = 300 +} +` -- cgit v1.2.3 From 7dec75fcd796fa25f655c1f1cda2d202f117eb4b Mon Sep 17 00:00:00 2001 From: stack72 Date: Sat, 3 Oct 2015 23:11:26 +0100 Subject: Adding the update and read functionality for the statuscake client --- provider.go | 6 ++-- resource_statuscaketest.go | 85 ++++++++++++++++++++++++++++++++++------------ 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 @@ package statuscake import ( - "github.com/DreamItGetIT/statuscake" + wtf "github.com/DreamItGetIT/statuscake" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/terraform" ) @@ -32,9 +32,9 @@ func Provider() terraform.ResourceProvider { } func providerConfigure(d *schema.ResourceData) (interface{}, error) { - auth := statuscake.Auth{ + auth := wtf.Auth{ Username: d.Get("username").(string), Apikey: d.Get("apikey").(string), } - return statuscake.New(auth) + return wtf.New(auth) } 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 ( "log" - "github.com/DreamItGetIT/statuscake" + wtf "github.com/DreamItGetIT/statuscake" "github.com/hashicorp/terraform/helper/schema" ) @@ -49,29 +49,24 @@ func resourceStatusCakeTest() *schema.Resource { Optional: true, Default: false, }, + "timeout": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + }, }, } } func CreateTest(d *schema.ResourceData, meta interface{}) error { - client := meta.(*statuscake.Client) + client := meta.(*wtf.Client) - newTest := &statuscake.Test{ - WebsiteName: "posters.dreamitget.it", - WebsiteURL: "https://posters.dreamitget.it", - TestType: "HTTP", - CheckRate: 500, + newTest := &wtf.Test{ + WebsiteName: d.Get("website_name").(string), + WebsiteURL: d.Get("website_url").(string), + TestType: d.Get("test_type").(string), + CheckRate: d.Get("check_rate").(int), } - // newTest := &statuscake.Test{ - // WebsiteName: d.Get("website_name").(string), - // WebsiteURL: d.Get("website_url").(string), - // TestType: d.Get("test_type").(string), - // CheckRate: 500, - // } - - log.Printf("[DEBUG] Check Rate: %d", d.Get("check_rate").(int)) - log.Printf("[DEBUG] TestType: %s", d.Get("test_type").(string)) log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) response, err := client.Tests().Put(newTest) @@ -82,24 +77,31 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error { d.Set("test_id", fmt.Sprintf("%d", response.TestID)) d.SetId(fmt.Sprintf("%d", response.TestID)) - return UpdateTest(d, meta) + return ReadTest(d, meta) } func UpdateTest(d *schema.ResourceData, meta interface{}) error { + client := meta.(*wtf.Client) + + params := getStatusCakeTestInput(d) + + log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id()) + _, err := client.Tests().Put(params) + if err != nil { + return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error()) + } return nil } func DeleteTest(d *schema.ResourceData, meta interface{}) error { - client := meta.(*statuscake.Client) + client := meta.(*wtf.Client) testId, parseErr := strconv.Atoi(d.Id()) if parseErr != nil { return parseErr } - testIntId := int(testId) - log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id()) - err := client.Tests().Delete(testIntId) + err := client.Tests().Delete(testId) if err != nil { return err } @@ -108,5 +110,46 @@ func DeleteTest(d *schema.ResourceData, meta interface{}) error { } func ReadTest(d *schema.ResourceData, meta interface{}) error { + client := meta.(*wtf.Client) + + testId, parseErr := strconv.Atoi(d.Id()) + if parseErr != nil { + return parseErr + } + testResp, err := client.Tests().Details(testId) + if err != nil { + return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err) + } + d.Set("check_rate", testResp.CheckRate) + return nil } + +func getStatusCakeTestInput(d *schema.ResourceData) *wtf.Test { + testId, parseErr := strconv.Atoi(d.Id()) + if parseErr != nil { + log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id()) + } + test := &wtf.Test{ + TestID: testId, + } + if v, ok := d.GetOk("website_name"); ok { + test.WebsiteName = v.(string) + } + if v, ok := d.GetOk("website_url"); ok { + test.WebsiteURL = v.(string) + } + if v, ok := d.GetOk("check_rate"); ok { + test.CheckRate = v.(int) + } + if v, ok := d.GetOk("test_type"); ok { + test.TestType = v.(string) + } + if v, ok := d.GetOk("paused"); ok { + test.Paused = v.(bool) + } + if v, ok := d.GetOk("timeout"); ok { + test.Timeout = v.(int) + } + return test +} -- cgit v1.2.3 From 32574941630dadbe86b887c174e1b7bcfd9580c1 Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 8 Oct 2015 08:52:15 +0100 Subject: Migrating the statuscake_test resource to use the new version of the statuscake client --- resource_statuscaketest.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go index 6834135..17488a5 100644 --- a/resource_statuscaketest.go +++ b/resource_statuscaketest.go @@ -69,7 +69,7 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error { log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) - response, err := client.Tests().Put(newTest) + response, err := client.Tests().Update(newTest) if err != nil { return fmt.Errorf("Error creating StatusCake Test: %s", err.Error()) } @@ -86,7 +86,7 @@ func UpdateTest(d *schema.ResourceData, meta interface{}) error { params := getStatusCakeTestInput(d) log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id()) - _, err := client.Tests().Put(params) + _, err := client.Tests().Update(params) if err != nil { return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error()) } @@ -116,7 +116,7 @@ func ReadTest(d *schema.ResourceData, meta interface{}) error { if parseErr != nil { return parseErr } - testResp, err := client.Tests().Details(testId) + testResp, err := client.Tests().Detail(testId) if err != nil { return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err) } -- cgit v1.2.3 From 4eeeab6482a659f2a3c4077a38bb3c83967666ac Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 8 Oct 2015 15:36:11 +0100 Subject: Added some acceptance tests for the statuscake provider --- provider.go | 6 +-- resource_statuscaketest.go | 16 ++++---- resource_statuscaketest_test.go | 82 ++++++++++++++++++++++++++++++----------- 3 files changed, 72 insertions(+), 32 deletions(-) diff --git a/provider.go b/provider.go index 7d96e0e..abca376 100644 --- a/provider.go +++ b/provider.go @@ -1,7 +1,7 @@ package statuscake import ( - wtf "github.com/DreamItGetIT/statuscake" + "github.com/DreamItGetIT/statuscake" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/terraform" ) @@ -32,9 +32,9 @@ func Provider() terraform.ResourceProvider { } func providerConfigure(d *schema.ResourceData) (interface{}, error) { - auth := wtf.Auth{ + auth := statuscake.Auth{ Username: d.Get("username").(string), Apikey: d.Get("apikey").(string), } - return wtf.New(auth) + return statuscake.New(auth) } diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go index 17488a5..7f5ff67 100644 --- a/resource_statuscaketest.go +++ b/resource_statuscaketest.go @@ -6,7 +6,7 @@ import ( "log" - wtf "github.com/DreamItGetIT/statuscake" + "github.com/DreamItGetIT/statuscake" "github.com/hashicorp/terraform/helper/schema" ) @@ -58,9 +58,9 @@ func resourceStatusCakeTest() *schema.Resource { } func CreateTest(d *schema.ResourceData, meta interface{}) error { - client := meta.(*wtf.Client) + client := meta.(*statuscake.Client) - newTest := &wtf.Test{ + newTest := &statuscake.Test{ WebsiteName: d.Get("website_name").(string), WebsiteURL: d.Get("website_url").(string), TestType: d.Get("test_type").(string), @@ -81,7 +81,7 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error { } func UpdateTest(d *schema.ResourceData, meta interface{}) error { - client := meta.(*wtf.Client) + client := meta.(*statuscake.Client) params := getStatusCakeTestInput(d) @@ -94,7 +94,7 @@ func UpdateTest(d *schema.ResourceData, meta interface{}) error { } func DeleteTest(d *schema.ResourceData, meta interface{}) error { - client := meta.(*wtf.Client) + client := meta.(*statuscake.Client) testId, parseErr := strconv.Atoi(d.Id()) if parseErr != nil { @@ -110,7 +110,7 @@ func DeleteTest(d *schema.ResourceData, meta interface{}) error { } func ReadTest(d *schema.ResourceData, meta interface{}) error { - client := meta.(*wtf.Client) + client := meta.(*statuscake.Client) testId, parseErr := strconv.Atoi(d.Id()) if parseErr != nil { @@ -125,12 +125,12 @@ func ReadTest(d *schema.ResourceData, meta interface{}) error { return nil } -func getStatusCakeTestInput(d *schema.ResourceData) *wtf.Test { +func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { testId, parseErr := strconv.Atoi(d.Id()) if parseErr != nil { log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id()) } - test := &wtf.Test{ + test := &statuscake.Test{ TestID: testId, } if v, ok := d.GetOk("website_name"); ok { diff --git a/resource_statuscaketest_test.go b/resource_statuscaketest_test.go index 8bbd4b5..f5b47e4 100644 --- a/resource_statuscaketest_test.go +++ b/resource_statuscaketest_test.go @@ -2,6 +2,7 @@ package statuscake import ( "fmt" + "strconv" "testing" "github.com/DreamItGetIT/statuscake" @@ -27,7 +28,34 @@ func TestAccStatusCake_basic(t *testing.T) { }) } -func testAccTestCheckExists(rn string, project *statuscake.Test) resource.TestCheckFunc { +func TestAccStatusCake_withUpdate(t *testing.T) { + var test statuscake.Test + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccTestCheckDestroy(&test), + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccTestConfig_basic, + Check: resource.ComposeTestCheckFunc( + testAccTestCheckExists("statuscake_test.google", &test), + ), + }, + + resource.TestStep{ + Config: testAccTestConfig_update, + Check: resource.ComposeTestCheckFunc( + testAccTestCheckExists("statuscake_test.google", &test), + resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"), + resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"), + ), + }, + }, + }) +} + +func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[rn] if !ok { @@ -38,31 +66,33 @@ func testAccTestCheckExists(rn string, project *statuscake.Test) resource.TestCh return fmt.Errorf("TestID not set") } - // client := testAccProvider.Meta().(*statuscake.Client) - // gotProject, err := client.GetProject(rs.Primary.ID) - // if err != nil { - // return fmt.Errorf("error getting project: %s", err) - // } - // - // *project = *gotProject + client := testAccProvider.Meta().(*statuscake.Client) + testId, parseErr := strconv.Atoi(rs.Primary.ID) + if parseErr != nil { + return fmt.Errorf("error in statuscake test CheckExists: %s", parseErr) + } + + gotTest, err := client.Tests().Detail(testId) + if err != nil { + return fmt.Errorf("error getting project: %s", err) + } + + *test = *gotTest return nil } } -func testAccTestCheckDestroy(project *statuscake.Test) resource.TestCheckFunc { - // return func(s *terraform.State) error { - // client := testAccProvider.Meta().(*statuscake.Client) - // // _, err := client.Tests().All() - // // if err == nil { - // // return fmt.Errorf("test still exists") - // // } - // // if _, ok := err.(*statuscake.NotFoundError); !ok { - // // return fmt.Errorf("got something other than NotFoundError (%v) when getting test", err) - // // } - // - // return nil - // } +func testAccTestCheckDestroy(test *statuscake.Test) resource.TestCheckFunc { + return func(s *terraform.State) error { + client := testAccProvider.Meta().(*statuscake.Client) + err := client.Tests().Delete(test.TestID) + if err == nil { + return fmt.Errorf("test still exists") + } + + return nil + } return nil } @@ -74,3 +104,13 @@ resource "statuscake_test" "google" { check_rate = 300 } ` + +const testAccTestConfig_update = ` +resource "statuscake_test" "google" { + website_name = "google.com" + website_url = "www.google.com" + test_type = "HTTP" + check_rate = 500 + paused = true +} +` -- cgit v1.2.3 From 4917b1d3ebabeb85d8b8087a375fc90d41c7d6c1 Mon Sep 17 00:00:00 2001 From: Trevor Pounds Date: Fri, 12 Feb 2016 21:38:15 -0800 Subject: Fix `go vet -unreachable` warnings. --- resource_statuscaketest_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/resource_statuscaketest_test.go b/resource_statuscaketest_test.go index f5b47e4..bbc6932 100644 --- a/resource_statuscaketest_test.go +++ b/resource_statuscaketest_test.go @@ -93,7 +93,6 @@ func testAccTestCheckDestroy(test *statuscake.Test) resource.TestCheckFunc { return nil } - return nil } const testAccTestConfig_basic = ` -- cgit v1.2.3 From e7b04dd5bb4a5947d1f4e9049ec90032b62faa5e Mon Sep 17 00:00:00 2001 From: Lee Johnson Date: Fri, 10 Jun 2016 12:38:56 -0400 Subject: Adding ability to add and update contact groups using the terraform statuscake provider --- resource_statuscaketest.go | 9 +++++ resource_statuscaketest_test.go | 83 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go index 7f5ff67..d403848 100644 --- a/resource_statuscaketest.go +++ b/resource_statuscaketest.go @@ -33,6 +33,11 @@ func resourceStatusCakeTest() *schema.Resource { Required: true, }, + "contact_id": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + }, + "check_rate": &schema.Schema{ Type: schema.TypeInt, Optional: true, @@ -65,6 +70,7 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error { WebsiteURL: d.Get("website_url").(string), TestType: d.Get("test_type").(string), CheckRate: d.Get("check_rate").(int), + ContactID: d.Get("contact_id").(int), } log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) @@ -142,6 +148,9 @@ func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { if v, ok := d.GetOk("check_rate"); ok { test.CheckRate = v.(int) } + if v, ok := d.GetOk("contact_id"); ok { + test.ContactID = v.(int) + } if v, ok := d.GetOk("test_type"); ok { test.TestType = v.(string) } diff --git a/resource_statuscaketest_test.go b/resource_statuscaketest_test.go index bbc6932..4123ac6 100644 --- a/resource_statuscaketest_test.go +++ b/resource_statuscaketest_test.go @@ -2,6 +2,7 @@ package statuscake import ( "fmt" + "os" "strconv" "testing" @@ -10,6 +11,19 @@ import ( "github.com/hashicorp/terraform/terraform" ) +// check to ensure that contact group id is provided before running +// tests on it. +func testAccContactGroupPreCheck(t *testing.T, testAlt bool) { + if v := os.Getenv("CONTACT_GROUP"); v == "" { + t.Fatal("CONTACT_GROUP must be set for contact group acceptance tests") + } + if testAlt { + if v := os.Getenv("ALT_CONTACT_GROUP"); v == "" { + t.Fatal("ALT_CONTACT_GROUP must be set for contact group acceptance tests") + } + } +} + func TestAccStatusCake_basic(t *testing.T) { var test statuscake.Test @@ -55,6 +69,57 @@ func TestAccStatusCake_withUpdate(t *testing.T) { }) } +func TestAccStatusCake_contactGroup_basic(t *testing.T) { + var test statuscake.Test + + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccContactGroupPreCheck(t, false) + }, + Providers: testAccProviders, + CheckDestroy: testAccTestCheckDestroy(&test), + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccTestConfig_contactGroup, + Check: resource.ComposeTestCheckFunc( + testAccTestCheckExists("statuscake_test.google", &test), + ), + }, + }, + }) +} + +func TestAccStatusCake_contactGroup_withUpdate(t *testing.T) { + var test statuscake.Test + var altContactGroup = os.Getenv("ALT_CONTACT_GROUP") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + testAccContactGroupPreCheck(t, true) + }, + Providers: testAccProviders, + CheckDestroy: testAccTestCheckDestroy(&test), + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccTestConfig_contactGroup, + Check: resource.ComposeTestCheckFunc( + testAccTestCheckExists("statuscake_test.google", &test), + ), + }, + // make sure to creat + resource.TestStep{ + Config: testAccTestConfig_contactGroup_update, + Check: resource.ComposeTestCheckFunc( + testAccTestCheckExists("statuscake_test.google", &test), + resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", altContactGroup), + ), + }, + }, + }) +} + func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[rn] @@ -113,3 +178,21 @@ resource "statuscake_test" "google" { paused = true } ` + +var testAccTestConfig_contactGroup string = `` + + `resource "statuscake_test" "google" { + website_name = "google.com" + website_url = "www.google.com" + test_type = "HTTP" + check_rate = 300 + contact_id = ` + os.Getenv("CONTACT_GROUP") + ` + }` + +var testAccTestConfig_contactGroup_update string = `` + + `resource "statuscake_test" "google" { + website_name = "google.com" + website_url = "www.google.com" + test_type = "HTTP" + check_rate = 300 + contact_id = ` + os.Getenv("ALT_CONTACT_GROUP") + ` + }` -- cgit v1.2.3 From 516886070835127727defe348567a0edfa138528 Mon Sep 17 00:00:00 2001 From: vagrant Date: Tue, 23 Aug 2016 14:43:31 +0000 Subject: enable contact-group id in statuscake test --- resource_statuscaketest.go | 7 +++++++ resource_statuscaketest_test.go | 21 ++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go index 7f5ff67..53b3059 100644 --- a/resource_statuscaketest.go +++ b/resource_statuscaketest.go @@ -53,6 +53,10 @@ func resourceStatusCakeTest() *schema.Resource { Type: schema.TypeInt, Computed: true, }, + "contact_id": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + }, }, } } @@ -151,5 +155,8 @@ func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { if v, ok := d.GetOk("timeout"); ok { test.Timeout = v.(int) } + if v, ok := d.GetOk("contact_id"); ok { + test.ContactID = v.(int) + } return test } diff --git a/resource_statuscaketest_test.go b/resource_statuscaketest_test.go index bbc6932..bec1a45 100644 --- a/resource_statuscaketest_test.go +++ b/resource_statuscaketest_test.go @@ -49,6 +49,7 @@ func TestAccStatusCake_withUpdate(t *testing.T) { testAccTestCheckExists("statuscake_test.google", &test), resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"), resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"), + resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "23456"), ), }, }, @@ -97,19 +98,21 @@ func testAccTestCheckDestroy(test *statuscake.Test) resource.TestCheckFunc { const testAccTestConfig_basic = ` resource "statuscake_test" "google" { - website_name = "google.com" - website_url = "www.google.com" - test_type = "HTTP" - check_rate = 300 + website_name = "google.com" + website_url = "www.google.com" + test_type = "HTTP" + check_rate = 300 + contact_id = 12345 } ` const testAccTestConfig_update = ` resource "statuscake_test" "google" { - website_name = "google.com" - website_url = "www.google.com" - test_type = "HTTP" - check_rate = 500 - paused = true + website_name = "google.com" + website_url = "www.google.com" + test_type = "HTTP" + check_rate = 500 + paused = true + contact_id = 23456 } ` -- cgit v1.2.3 From 312d16361a769314812c256f7433bcb0f2c72193 Mon Sep 17 00:00:00 2001 From: Lee Johnson Date: Tue, 29 Nov 2016 16:03:58 -0500 Subject: simplifying tests by removing tests and test data that is no longer needed --- resource_statuscaketest.go | 5 --- resource_statuscaketest_test.go | 83 ----------------------------------------- 2 files changed, 88 deletions(-) diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go index 389a7d6..a1d7354 100644 --- a/resource_statuscaketest.go +++ b/resource_statuscaketest.go @@ -58,10 +58,6 @@ func resourceStatusCakeTest() *schema.Resource { Type: schema.TypeInt, Computed: true, }, - "contact_id": &schema.Schema{ - Type: schema.TypeInt, - Optional: true, - }, }, } } @@ -74,7 +70,6 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error { WebsiteURL: d.Get("website_url").(string), TestType: d.Get("test_type").(string), CheckRate: d.Get("check_rate").(int), - ContactID: d.Get("contact_id").(int), } log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) diff --git a/resource_statuscaketest_test.go b/resource_statuscaketest_test.go index 236b790..bec1a45 100644 --- a/resource_statuscaketest_test.go +++ b/resource_statuscaketest_test.go @@ -2,7 +2,6 @@ package statuscake import ( "fmt" - "os" "strconv" "testing" @@ -11,19 +10,6 @@ import ( "github.com/hashicorp/terraform/terraform" ) -// check to ensure that contact group id is provided before running -// tests on it. -func testAccContactGroupPreCheck(t *testing.T, testAlt bool) { - if v := os.Getenv("CONTACT_GROUP"); v == "" { - t.Fatal("CONTACT_GROUP must be set for contact group acceptance tests") - } - if testAlt { - if v := os.Getenv("ALT_CONTACT_GROUP"); v == "" { - t.Fatal("ALT_CONTACT_GROUP must be set for contact group acceptance tests") - } - } -} - func TestAccStatusCake_basic(t *testing.T) { var test statuscake.Test @@ -70,57 +56,6 @@ func TestAccStatusCake_withUpdate(t *testing.T) { }) } -func TestAccStatusCake_contactGroup_basic(t *testing.T) { - var test statuscake.Test - - resource.Test(t, resource.TestCase{ - PreCheck: func() { - testAccPreCheck(t) - testAccContactGroupPreCheck(t, false) - }, - Providers: testAccProviders, - CheckDestroy: testAccTestCheckDestroy(&test), - Steps: []resource.TestStep{ - resource.TestStep{ - Config: testAccTestConfig_contactGroup, - Check: resource.ComposeTestCheckFunc( - testAccTestCheckExists("statuscake_test.google", &test), - ), - }, - }, - }) -} - -func TestAccStatusCake_contactGroup_withUpdate(t *testing.T) { - var test statuscake.Test - var altContactGroup = os.Getenv("ALT_CONTACT_GROUP") - - resource.Test(t, resource.TestCase{ - PreCheck: func() { - testAccPreCheck(t) - testAccContactGroupPreCheck(t, true) - }, - Providers: testAccProviders, - CheckDestroy: testAccTestCheckDestroy(&test), - Steps: []resource.TestStep{ - resource.TestStep{ - Config: testAccTestConfig_contactGroup, - Check: resource.ComposeTestCheckFunc( - testAccTestCheckExists("statuscake_test.google", &test), - ), - }, - // make sure to creat - resource.TestStep{ - Config: testAccTestConfig_contactGroup_update, - Check: resource.ComposeTestCheckFunc( - testAccTestCheckExists("statuscake_test.google", &test), - resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", altContactGroup), - ), - }, - }, - }) -} - func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[rn] @@ -181,21 +116,3 @@ resource "statuscake_test" "google" { contact_id = 23456 } ` - -var testAccTestConfig_contactGroup string = `` + - `resource "statuscake_test" "google" { - website_name = "google.com" - website_url = "www.google.com" - test_type = "HTTP" - check_rate = 300 - contact_id = ` + os.Getenv("CONTACT_GROUP") + ` - }` - -var testAccTestConfig_contactGroup_update string = `` + - `resource "statuscake_test" "google" { - website_name = "google.com" - website_url = "www.google.com" - test_type = "HTTP" - check_rate = 300 - contact_id = ` + os.Getenv("ALT_CONTACT_GROUP") + ` - }` -- cgit v1.2.3 From c07010769685fd6127e977d9ea7e891773fe75a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rui=20Gon=C3=A7alves?= Date: Sun, 11 Dec 2016 19:41:49 +0000 Subject: provider/statuscake: fix StatusCake tests (#10660) * Update vendored statuscake SDK * Set all attributes when upserting statuscake tests --- resource_statuscaketest.go | 11 +++++++++- resource_statuscaketest_test.go | 47 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go index 53b3059..08ace97 100644 --- a/resource_statuscaketest.go +++ b/resource_statuscaketest.go @@ -67,8 +67,11 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error { newTest := &statuscake.Test{ WebsiteName: d.Get("website_name").(string), WebsiteURL: d.Get("website_url").(string), - TestType: d.Get("test_type").(string), CheckRate: d.Get("check_rate").(int), + TestType: d.Get("test_type").(string), + Paused: d.Get("paused").(bool), + Timeout: d.Get("timeout").(int), + ContactID: d.Get("contact_id").(int), } log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) @@ -124,7 +127,13 @@ func ReadTest(d *schema.ResourceData, meta interface{}) error { if err != nil { return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err) } + d.Set("website_name", testResp.WebsiteName) + d.Set("website_url", testResp.WebsiteURL) d.Set("check_rate", testResp.CheckRate) + d.Set("test_type", testResp.TestType) + d.Set("paused", testResp.Paused) + d.Set("timeout", testResp.Timeout) + d.Set("contact_id", testResp.ContactID) return nil } diff --git a/resource_statuscaketest_test.go b/resource_statuscaketest_test.go index bec1a45..de30254 100644 --- a/resource_statuscaketest_test.go +++ b/resource_statuscaketest_test.go @@ -22,6 +22,7 @@ func TestAccStatusCake_basic(t *testing.T) { Config: testAccTestConfig_basic, Check: resource.ComposeTestCheckFunc( testAccTestCheckExists("statuscake_test.google", &test), + testAccTestCheckAttributes("statuscake_test.google", &test), ), }, }, @@ -47,9 +48,10 @@ func TestAccStatusCake_withUpdate(t *testing.T) { Config: testAccTestConfig_update, Check: resource.ComposeTestCheckFunc( testAccTestCheckExists("statuscake_test.google", &test), + testAccTestCheckAttributes("statuscake_test.google", &test), resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"), resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"), - resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "23456"), + resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "0"), ), }, }, @@ -75,7 +77,7 @@ func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheck gotTest, err := client.Tests().Detail(testId) if err != nil { - return fmt.Errorf("error getting project: %s", err) + return fmt.Errorf("error getting test: %s", err) } *test = *gotTest @@ -84,6 +86,46 @@ func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheck } } +func testAccTestCheckAttributes(rn string, test *statuscake.Test) resource.TestCheckFunc { + return func(s *terraform.State) error { + attrs := s.RootModule().Resources[rn].Primary.Attributes + + check := func(key, stateValue, testValue string) error { + if testValue != stateValue { + return fmt.Errorf("different values for %s in state (%s) and in statuscake (%s)", + key, stateValue, testValue) + } + return nil + } + + for key, value := range attrs { + var err error + + switch key { + case "website_name": + err = check(key, value, test.WebsiteName) + case "website_url": + err = check(key, value, test.WebsiteURL) + case "check_rate": + err = check(key, value, strconv.Itoa(test.CheckRate)) + case "test_type": + err = check(key, value, test.TestType) + case "paused": + err = check(key, value, strconv.FormatBool(test.Paused)) + case "timeout": + err = check(key, value, strconv.Itoa(test.Timeout)) + case "contact_id": + err = check(key, value, strconv.Itoa(test.ContactID)) + } + + if err != nil { + return err + } + } + return nil + } +} + func testAccTestCheckDestroy(test *statuscake.Test) resource.TestCheckFunc { return func(s *terraform.State) error { client := testAccProvider.Meta().(*statuscake.Client) @@ -113,6 +155,5 @@ resource "statuscake_test" "google" { test_type = "HTTP" check_rate = 500 paused = true - contact_id = 23456 } ` -- cgit v1.2.3 From b581540543290cbcd9b0e07ef93fec8b1d24b5c2 Mon Sep 17 00:00:00 2001 From: Sam Crang Date: Fri, 13 Jan 2017 11:35:15 +0000 Subject: Add support for StatusCake confirmation servers (#11179) * Add support for StatusCake confirmation servers * Add documentation for new StatusCake confirmations argument --- resource_statuscaketest.go | 23 ++++++++++++++++------- resource_statuscaketest_test.go | 4 ++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go index 08ace97..6c340d8 100644 --- a/resource_statuscaketest.go +++ b/resource_statuscaketest.go @@ -57,6 +57,10 @@ func resourceStatusCakeTest() *schema.Resource { Type: schema.TypeInt, Optional: true, }, + "confirmations": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + }, }, } } @@ -65,13 +69,14 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error { client := meta.(*statuscake.Client) newTest := &statuscake.Test{ - WebsiteName: d.Get("website_name").(string), - WebsiteURL: d.Get("website_url").(string), - CheckRate: d.Get("check_rate").(int), - TestType: d.Get("test_type").(string), - Paused: d.Get("paused").(bool), - Timeout: d.Get("timeout").(int), - ContactID: d.Get("contact_id").(int), + WebsiteName: d.Get("website_name").(string), + WebsiteURL: d.Get("website_url").(string), + CheckRate: d.Get("check_rate").(int), + TestType: d.Get("test_type").(string), + Paused: d.Get("paused").(bool), + Timeout: d.Get("timeout").(int), + ContactID: d.Get("contact_id").(int), + Confirmation: d.Get("confirmations").(int), } log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) @@ -134,6 +139,7 @@ func ReadTest(d *schema.ResourceData, meta interface{}) error { d.Set("paused", testResp.Paused) d.Set("timeout", testResp.Timeout) d.Set("contact_id", testResp.ContactID) + d.Set("confirmations", testResp.Confirmation) return nil } @@ -167,5 +173,8 @@ func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { if v, ok := d.GetOk("contact_id"); ok { test.ContactID = v.(int) } + if v, ok := d.GetOk("confirmations"); ok { + test.Confirmation = v.(int) + } return test } diff --git a/resource_statuscaketest_test.go b/resource_statuscaketest_test.go index de30254..ac3cc92 100644 --- a/resource_statuscaketest_test.go +++ b/resource_statuscaketest_test.go @@ -52,6 +52,7 @@ func TestAccStatusCake_withUpdate(t *testing.T) { resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"), resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"), resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "0"), + resource.TestCheckResourceAttr("statuscake_test.google", "confirmations", "0"), ), }, }, @@ -116,6 +117,8 @@ func testAccTestCheckAttributes(rn string, test *statuscake.Test) resource.TestC err = check(key, value, strconv.Itoa(test.Timeout)) case "contact_id": err = check(key, value, strconv.Itoa(test.ContactID)) + case "confirmations": + err = check(key, value, strconv.Itoa(test.Confirmation)) } if err != nil { @@ -145,6 +148,7 @@ resource "statuscake_test" "google" { test_type = "HTTP" check_rate = 300 contact_id = 12345 + confirmations = 1 } ` -- cgit v1.2.3 From 6ad7af97bbcfa90a6798dd78ae19adfcef179bdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rui=20Gon=C3=A7alves?= Date: Tue, 31 Jan 2017 12:44:06 +0000 Subject: Fix statuscake test timeout schema (#11541) --- resource_statuscaketest.go | 5 ++++- resource_statuscaketest_test.go | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go index 0cdc76d..bc05364 100644 --- a/resource_statuscaketest.go +++ b/resource_statuscaketest.go @@ -54,10 +54,13 @@ func resourceStatusCakeTest() *schema.Resource { Optional: true, Default: false, }, + "timeout": &schema.Schema{ Type: schema.TypeInt, - Computed: true, + Optional: true, + Default: 40, }, + "confirmations": &schema.Schema{ Type: schema.TypeInt, Optional: true, diff --git a/resource_statuscaketest_test.go b/resource_statuscaketest_test.go index ac3cc92..1def4e3 100644 --- a/resource_statuscaketest_test.go +++ b/resource_statuscaketest_test.go @@ -51,6 +51,7 @@ func TestAccStatusCake_withUpdate(t *testing.T) { testAccTestCheckAttributes("statuscake_test.google", &test), resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"), resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"), + resource.TestCheckResourceAttr("statuscake_test.google", "timeout", "40"), resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "0"), resource.TestCheckResourceAttr("statuscake_test.google", "confirmations", "0"), ), @@ -147,6 +148,7 @@ resource "statuscake_test" "google" { website_url = "www.google.com" test_type = "HTTP" check_rate = 300 + timeout = 10 contact_id = 12345 confirmations = 1 } -- cgit v1.2.3 From 90db3fb0ba1aff53f83ece8f5d62f56943ed090e Mon Sep 17 00:00:00 2001 From: Paul Stack Date: Wed, 15 Feb 2017 23:29:05 +0000 Subject: provider/statuscake: Add support for Port in statuscake_test (#11966) Fixes: #11923 This required the upstream library to have a PR accepted --- resource_statuscaketest.go | 28 +++++++++++++++++++--------- resource_statuscaketest_test.go | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go index bc05364..dbc15c9 100644 --- a/resource_statuscaketest.go +++ b/resource_statuscaketest.go @@ -18,50 +18,55 @@ func resourceStatusCakeTest() *schema.Resource { Read: ReadTest, Schema: map[string]*schema.Schema{ - "test_id": &schema.Schema{ + "test_id": { Type: schema.TypeString, Computed: true, }, - "website_name": &schema.Schema{ + "website_name": { Type: schema.TypeString, Required: true, }, - "website_url": &schema.Schema{ + "website_url": { Type: schema.TypeString, Required: true, }, - "contact_id": &schema.Schema{ + "contact_id": { Type: schema.TypeInt, Optional: true, }, - "check_rate": &schema.Schema{ + "check_rate": { Type: schema.TypeInt, Optional: true, Default: 300, }, - "test_type": &schema.Schema{ + "test_type": { Type: schema.TypeString, Required: true, }, - "paused": &schema.Schema{ + "paused": { Type: schema.TypeBool, Optional: true, Default: false, }, - "timeout": &schema.Schema{ + "timeout": { Type: schema.TypeInt, Optional: true, Default: 40, }, - "confirmations": &schema.Schema{ + "confirmations": { + Type: schema.TypeInt, + Optional: true, + }, + + "port": { Type: schema.TypeInt, Optional: true, }, @@ -81,6 +86,7 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error { Timeout: d.Get("timeout").(int), ContactID: d.Get("contact_id").(int), Confirmation: d.Get("confirmations").(int), + Port: d.Get("port").(int), } log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) @@ -144,6 +150,7 @@ func ReadTest(d *schema.ResourceData, meta interface{}) error { d.Set("timeout", testResp.Timeout) d.Set("contact_id", testResp.ContactID) d.Set("confirmations", testResp.Confirmation) + d.Set("port", testResp.Port) return nil } @@ -183,5 +190,8 @@ func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { if v, ok := d.GetOk("confirmations"); ok { test.Confirmation = v.(int) } + if v, ok := d.GetOk("port"); ok { + test.Port = v.(int) + } return test } diff --git a/resource_statuscaketest_test.go b/resource_statuscaketest_test.go index 1def4e3..17940ed 100644 --- a/resource_statuscaketest_test.go +++ b/resource_statuscaketest_test.go @@ -18,7 +18,7 @@ func TestAccStatusCake_basic(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccTestCheckDestroy(&test), Steps: []resource.TestStep{ - resource.TestStep{ + { Config: testAccTestConfig_basic, Check: resource.ComposeTestCheckFunc( testAccTestCheckExists("statuscake_test.google", &test), @@ -29,6 +29,25 @@ func TestAccStatusCake_basic(t *testing.T) { }) } +func TestAccStatusCake_tcp(t *testing.T) { + var test statuscake.Test + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccTestCheckDestroy(&test), + Steps: []resource.TestStep{ + { + Config: testAccTestConfig_tcp, + Check: resource.ComposeTestCheckFunc( + testAccTestCheckExists("statuscake_test.google", &test), + testAccTestCheckAttributes("statuscake_test.google", &test), + ), + }, + }, + }) +} + func TestAccStatusCake_withUpdate(t *testing.T) { var test statuscake.Test @@ -37,14 +56,14 @@ func TestAccStatusCake_withUpdate(t *testing.T) { Providers: testAccProviders, CheckDestroy: testAccTestCheckDestroy(&test), Steps: []resource.TestStep{ - resource.TestStep{ + { Config: testAccTestConfig_basic, Check: resource.ComposeTestCheckFunc( testAccTestCheckExists("statuscake_test.google", &test), ), }, - resource.TestStep{ + { Config: testAccTestConfig_update, Check: resource.ComposeTestCheckFunc( testAccTestCheckExists("statuscake_test.google", &test), @@ -163,3 +182,16 @@ resource "statuscake_test" "google" { paused = true } ` + +const testAccTestConfig_tcp = ` +resource "statuscake_test" "google" { + website_name = "google.com" + website_url = "www.google.com" + test_type = "TCP" + check_rate = 300 + timeout = 10 + contact_id = 12345 + confirmations = 1 + port = 80 +} +` -- cgit v1.2.3 From e7c3fa5620554870596fc3291745089b49eccf8a Mon Sep 17 00:00:00 2001 From: Paul Stack Date: Thu, 16 Feb 2017 16:20:23 +0200 Subject: provider/statuscake: Fixing up the StatusCake acceptance tests (#12006) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` % make testacc TEST=./builtin/providers/statuscake 2 ↵ ✹ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/02/16 15:30:40 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/statuscake -v -timeout 120m === RUN TestProvider --- PASS: TestProvider (0.00s) === RUN TestProvider_impl --- PASS: TestProvider_impl (0.00s) === RUN TestAccStatusCake_basic --- PASS: TestAccStatusCake_basic (2.14s) === RUN TestAccStatusCake_tcp --- PASS: TestAccStatusCake_tcp (1.66s) === RUN TestAccStatusCake_withUpdate --- PASS: TestAccStatusCake_withUpdate (3.62s) PASS ok github.com/hashicorp/terraform/builtin/providers/statuscake 7.435s ``` --- resource_statuscaketest_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/resource_statuscaketest_test.go b/resource_statuscaketest_test.go index 17940ed..d38dc1c 100644 --- a/resource_statuscaketest_test.go +++ b/resource_statuscaketest_test.go @@ -168,7 +168,7 @@ resource "statuscake_test" "google" { test_type = "HTTP" check_rate = 300 timeout = 10 - contact_id = 12345 + contact_id = 43402 confirmations = 1 } ` @@ -190,7 +190,7 @@ resource "statuscake_test" "google" { test_type = "TCP" check_rate = 300 timeout = 10 - contact_id = 12345 + contact_id = 43402 confirmations = 1 port = 80 } -- cgit v1.2.3 From 2a6605e977ead1c970973bdb3517b20ac99bdc15 Mon Sep 17 00:00:00 2001 From: Andre Silva Date: Sun, 5 Mar 2017 13:44:22 +0000 Subject: provider/statuscake: use default status code list when updating test (#12375) --- resource_statuscaketest.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go index dbc15c9..4912758 100644 --- a/resource_statuscaketest.go +++ b/resource_statuscaketest.go @@ -193,5 +193,13 @@ func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { if v, ok := d.GetOk("port"); ok { test.Port = v.(int) } + + defaultStatusCodes := "204, 205, 206, 303, 400, 401, 403, 404, 405, 406, " + + "408, 410, 413, 444, 429, 494, 495, 496, 499, 500, 501, 502, 503, " + + "504, 505, 506, 507, 508, 509, 510, 511, 521, 522, 523, 524, 520, " + + "598, 599" + + test.StatusCodes = defaultStatusCodes + return test } -- cgit v1.2.3 From 0e962b8ebd36cbb0e2f722a1859594b748713d65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rui=20Gonc=CC=A7alves?= Date: Tue, 4 Apr 2017 16:58:11 +0100 Subject: Add support for StatusCake trigger rate --- resource_statuscaketest.go | 11 +++++++++++ resource_statuscaketest_test.go | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go index 4912758..101ee83 100644 --- a/resource_statuscaketest.go +++ b/resource_statuscaketest.go @@ -70,6 +70,12 @@ func resourceStatusCakeTest() *schema.Resource { Type: schema.TypeInt, Optional: true, }, + + "trigger_rate": { + Type: schema.TypeInt, + Optional: true, + Default: 5, + }, }, } } @@ -87,6 +93,7 @@ func CreateTest(d *schema.ResourceData, meta interface{}) error { ContactID: d.Get("contact_id").(int), Confirmation: d.Get("confirmations").(int), Port: d.Get("port").(int), + TriggerRate: d.Get("trigger_rate").(int), } log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) @@ -151,6 +158,7 @@ func ReadTest(d *schema.ResourceData, meta interface{}) error { d.Set("contact_id", testResp.ContactID) d.Set("confirmations", testResp.Confirmation) d.Set("port", testResp.Port) + d.Set("trigger_rate", testResp.TriggerRate) return nil } @@ -193,6 +201,9 @@ func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { if v, ok := d.GetOk("port"); ok { test.Port = v.(int) } + if v, ok := d.GetOk("trigger_rate"); ok { + test.TriggerRate = v.(int) + } defaultStatusCodes := "204, 205, 206, 303, 400, 401, 403, 404, 405, 406, " + "408, 410, 413, 444, 429, 494, 495, 496, 499, 500, 501, 502, 503, " + diff --git a/resource_statuscaketest_test.go b/resource_statuscaketest_test.go index d38dc1c..f07fcc5 100644 --- a/resource_statuscaketest_test.go +++ b/resource_statuscaketest_test.go @@ -73,6 +73,7 @@ func TestAccStatusCake_withUpdate(t *testing.T) { resource.TestCheckResourceAttr("statuscake_test.google", "timeout", "40"), resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "0"), resource.TestCheckResourceAttr("statuscake_test.google", "confirmations", "0"), + resource.TestCheckResourceAttr("statuscake_test.google", "trigger_rate", "20"), ), }, }, @@ -139,6 +140,8 @@ func testAccTestCheckAttributes(rn string, test *statuscake.Test) resource.TestC err = check(key, value, strconv.Itoa(test.ContactID)) case "confirmations": err = check(key, value, strconv.Itoa(test.Confirmation)) + case "trigger_rate": + err = check(key, value, strconv.Itoa(test.TriggerRate)) } if err != nil { @@ -170,6 +173,7 @@ resource "statuscake_test" "google" { timeout = 10 contact_id = 43402 confirmations = 1 + trigger_rate = 10 } ` @@ -180,6 +184,7 @@ resource "statuscake_test" "google" { test_type = "HTTP" check_rate = 500 paused = true + trigger_rate = 20 } ` -- cgit v1.2.3 From dbde71c990295605ce351a494888c15f3d6be9a8 Mon Sep 17 00:00:00 2001 From: Jake Champlin Date: Tue, 6 Jun 2017 12:40:02 -0400 Subject: Transfer statuscake provider --- provider.go | 40 ------ provider_test.go | 38 ----- resource_statuscaketest.go | 216 ----------------------------- resource_statuscaketest_test.go | 202 --------------------------- statuscake/provider.go | 40 ++++++ statuscake/provider_test.go | 38 +++++ statuscake/resource_statuscaketest.go | 216 +++++++++++++++++++++++++++++ statuscake/resource_statuscaketest_test.go | 202 +++++++++++++++++++++++++++ 8 files changed, 496 insertions(+), 496 deletions(-) delete mode 100644 provider.go delete mode 100644 provider_test.go delete mode 100644 resource_statuscaketest.go delete mode 100644 resource_statuscaketest_test.go create mode 100644 statuscake/provider.go create mode 100644 statuscake/provider_test.go create mode 100644 statuscake/resource_statuscaketest.go create mode 100644 statuscake/resource_statuscaketest_test.go diff --git a/provider.go b/provider.go deleted file mode 100644 index abca376..0000000 --- a/provider.go +++ /dev/null @@ -1,40 +0,0 @@ -package statuscake - -import ( - "github.com/DreamItGetIT/statuscake" - "github.com/hashicorp/terraform/helper/schema" - "github.com/hashicorp/terraform/terraform" -) - -func Provider() terraform.ResourceProvider { - return &schema.Provider{ - Schema: map[string]*schema.Schema{ - "username": &schema.Schema{ - Type: schema.TypeString, - Required: true, - DefaultFunc: schema.EnvDefaultFunc("STATUSCAKE_USERNAME", nil), - Description: "Username for StatusCake Account.", - }, - "apikey": &schema.Schema{ - Type: schema.TypeString, - Required: true, - DefaultFunc: schema.EnvDefaultFunc("STATUSCAKE_APIKEY", nil), - Description: "API Key for StatusCake", - }, - }, - - ResourcesMap: map[string]*schema.Resource{ - "statuscake_test": resourceStatusCakeTest(), - }, - - ConfigureFunc: providerConfigure, - } -} - -func providerConfigure(d *schema.ResourceData) (interface{}, error) { - auth := statuscake.Auth{ - Username: d.Get("username").(string), - Apikey: d.Get("apikey").(string), - } - return statuscake.New(auth) -} diff --git a/provider_test.go b/provider_test.go deleted file mode 100644 index 83045d0..0000000 --- a/provider_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package statuscake - -import ( - "os" - "testing" - - "github.com/hashicorp/terraform/helper/schema" - "github.com/hashicorp/terraform/terraform" -) - -var testAccProviders map[string]terraform.ResourceProvider -var testAccProvider *schema.Provider - -func init() { - testAccProvider = Provider().(*schema.Provider) - testAccProviders = map[string]terraform.ResourceProvider{ - "statuscake": testAccProvider, - } -} - -func TestProvider(t *testing.T) { - if err := Provider().(*schema.Provider).InternalValidate(); err != nil { - t.Fatalf("err: %s", err) - } -} - -func TestProvider_impl(t *testing.T) { - var _ terraform.ResourceProvider = Provider() -} - -func testAccPreCheck(t *testing.T) { - if v := os.Getenv("STATUSCAKE_USERNAME"); v == "" { - t.Fatal("STATUSCAKE_USERNAME must be set for acceptance tests") - } - if v := os.Getenv("STATUSCAKE_APIKEY"); v == "" { - t.Fatal("STATUSCAKE_APIKEY must be set for acceptance tests") - } -} diff --git a/resource_statuscaketest.go b/resource_statuscaketest.go deleted file mode 100644 index 101ee83..0000000 --- a/resource_statuscaketest.go +++ /dev/null @@ -1,216 +0,0 @@ -package statuscake - -import ( - "fmt" - "strconv" - - "log" - - "github.com/DreamItGetIT/statuscake" - "github.com/hashicorp/terraform/helper/schema" -) - -func resourceStatusCakeTest() *schema.Resource { - return &schema.Resource{ - Create: CreateTest, - Update: UpdateTest, - Delete: DeleteTest, - Read: ReadTest, - - Schema: map[string]*schema.Schema{ - "test_id": { - Type: schema.TypeString, - Computed: true, - }, - - "website_name": { - Type: schema.TypeString, - Required: true, - }, - - "website_url": { - Type: schema.TypeString, - Required: true, - }, - - "contact_id": { - Type: schema.TypeInt, - Optional: true, - }, - - "check_rate": { - Type: schema.TypeInt, - Optional: true, - Default: 300, - }, - - "test_type": { - Type: schema.TypeString, - Required: true, - }, - - "paused": { - Type: schema.TypeBool, - Optional: true, - Default: false, - }, - - "timeout": { - Type: schema.TypeInt, - Optional: true, - Default: 40, - }, - - "confirmations": { - Type: schema.TypeInt, - Optional: true, - }, - - "port": { - Type: schema.TypeInt, - Optional: true, - }, - - "trigger_rate": { - Type: schema.TypeInt, - Optional: true, - Default: 5, - }, - }, - } -} - -func CreateTest(d *schema.ResourceData, meta interface{}) error { - client := meta.(*statuscake.Client) - - newTest := &statuscake.Test{ - WebsiteName: d.Get("website_name").(string), - WebsiteURL: d.Get("website_url").(string), - CheckRate: d.Get("check_rate").(int), - TestType: d.Get("test_type").(string), - Paused: d.Get("paused").(bool), - Timeout: d.Get("timeout").(int), - ContactID: d.Get("contact_id").(int), - Confirmation: d.Get("confirmations").(int), - Port: d.Get("port").(int), - TriggerRate: d.Get("trigger_rate").(int), - } - - log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) - - response, err := client.Tests().Update(newTest) - if err != nil { - return fmt.Errorf("Error creating StatusCake Test: %s", err.Error()) - } - - d.Set("test_id", fmt.Sprintf("%d", response.TestID)) - d.SetId(fmt.Sprintf("%d", response.TestID)) - - return ReadTest(d, meta) -} - -func UpdateTest(d *schema.ResourceData, meta interface{}) error { - client := meta.(*statuscake.Client) - - params := getStatusCakeTestInput(d) - - log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id()) - _, err := client.Tests().Update(params) - if err != nil { - return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error()) - } - return nil -} - -func DeleteTest(d *schema.ResourceData, meta interface{}) error { - client := meta.(*statuscake.Client) - - testId, parseErr := strconv.Atoi(d.Id()) - if parseErr != nil { - return parseErr - } - log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id()) - err := client.Tests().Delete(testId) - if err != nil { - return err - } - - return nil -} - -func ReadTest(d *schema.ResourceData, meta interface{}) error { - client := meta.(*statuscake.Client) - - testId, parseErr := strconv.Atoi(d.Id()) - if parseErr != nil { - return parseErr - } - testResp, err := client.Tests().Detail(testId) - if err != nil { - return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err) - } - d.Set("website_name", testResp.WebsiteName) - d.Set("website_url", testResp.WebsiteURL) - d.Set("check_rate", testResp.CheckRate) - d.Set("test_type", testResp.TestType) - d.Set("paused", testResp.Paused) - d.Set("timeout", testResp.Timeout) - d.Set("contact_id", testResp.ContactID) - d.Set("confirmations", testResp.Confirmation) - d.Set("port", testResp.Port) - d.Set("trigger_rate", testResp.TriggerRate) - - return nil -} - -func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { - testId, parseErr := strconv.Atoi(d.Id()) - if parseErr != nil { - log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id()) - } - test := &statuscake.Test{ - TestID: testId, - } - if v, ok := d.GetOk("website_name"); ok { - test.WebsiteName = v.(string) - } - if v, ok := d.GetOk("website_url"); ok { - test.WebsiteURL = v.(string) - } - if v, ok := d.GetOk("check_rate"); ok { - test.CheckRate = v.(int) - } - if v, ok := d.GetOk("contact_id"); ok { - test.ContactID = v.(int) - } - if v, ok := d.GetOk("test_type"); ok { - test.TestType = v.(string) - } - if v, ok := d.GetOk("paused"); ok { - test.Paused = v.(bool) - } - if v, ok := d.GetOk("timeout"); ok { - test.Timeout = v.(int) - } - if v, ok := d.GetOk("contact_id"); ok { - test.ContactID = v.(int) - } - if v, ok := d.GetOk("confirmations"); ok { - test.Confirmation = v.(int) - } - if v, ok := d.GetOk("port"); ok { - test.Port = v.(int) - } - if v, ok := d.GetOk("trigger_rate"); ok { - test.TriggerRate = v.(int) - } - - defaultStatusCodes := "204, 205, 206, 303, 400, 401, 403, 404, 405, 406, " + - "408, 410, 413, 444, 429, 494, 495, 496, 499, 500, 501, 502, 503, " + - "504, 505, 506, 507, 508, 509, 510, 511, 521, 522, 523, 524, 520, " + - "598, 599" - - test.StatusCodes = defaultStatusCodes - - return test -} diff --git a/resource_statuscaketest_test.go b/resource_statuscaketest_test.go deleted file mode 100644 index f07fcc5..0000000 --- a/resource_statuscaketest_test.go +++ /dev/null @@ -1,202 +0,0 @@ -package statuscake - -import ( - "fmt" - "strconv" - "testing" - - "github.com/DreamItGetIT/statuscake" - "github.com/hashicorp/terraform/helper/resource" - "github.com/hashicorp/terraform/terraform" -) - -func TestAccStatusCake_basic(t *testing.T) { - var test statuscake.Test - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccTestCheckDestroy(&test), - Steps: []resource.TestStep{ - { - Config: testAccTestConfig_basic, - Check: resource.ComposeTestCheckFunc( - testAccTestCheckExists("statuscake_test.google", &test), - testAccTestCheckAttributes("statuscake_test.google", &test), - ), - }, - }, - }) -} - -func TestAccStatusCake_tcp(t *testing.T) { - var test statuscake.Test - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccTestCheckDestroy(&test), - Steps: []resource.TestStep{ - { - Config: testAccTestConfig_tcp, - Check: resource.ComposeTestCheckFunc( - testAccTestCheckExists("statuscake_test.google", &test), - testAccTestCheckAttributes("statuscake_test.google", &test), - ), - }, - }, - }) -} - -func TestAccStatusCake_withUpdate(t *testing.T) { - var test statuscake.Test - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccTestCheckDestroy(&test), - Steps: []resource.TestStep{ - { - Config: testAccTestConfig_basic, - Check: resource.ComposeTestCheckFunc( - testAccTestCheckExists("statuscake_test.google", &test), - ), - }, - - { - Config: testAccTestConfig_update, - Check: resource.ComposeTestCheckFunc( - testAccTestCheckExists("statuscake_test.google", &test), - testAccTestCheckAttributes("statuscake_test.google", &test), - resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"), - resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"), - resource.TestCheckResourceAttr("statuscake_test.google", "timeout", "40"), - resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "0"), - resource.TestCheckResourceAttr("statuscake_test.google", "confirmations", "0"), - resource.TestCheckResourceAttr("statuscake_test.google", "trigger_rate", "20"), - ), - }, - }, - }) -} - -func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[rn] - if !ok { - return fmt.Errorf("resource not found: %s", rn) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("TestID not set") - } - - client := testAccProvider.Meta().(*statuscake.Client) - testId, parseErr := strconv.Atoi(rs.Primary.ID) - if parseErr != nil { - return fmt.Errorf("error in statuscake test CheckExists: %s", parseErr) - } - - gotTest, err := client.Tests().Detail(testId) - if err != nil { - return fmt.Errorf("error getting test: %s", err) - } - - *test = *gotTest - - return nil - } -} - -func testAccTestCheckAttributes(rn string, test *statuscake.Test) resource.TestCheckFunc { - return func(s *terraform.State) error { - attrs := s.RootModule().Resources[rn].Primary.Attributes - - check := func(key, stateValue, testValue string) error { - if testValue != stateValue { - return fmt.Errorf("different values for %s in state (%s) and in statuscake (%s)", - key, stateValue, testValue) - } - return nil - } - - for key, value := range attrs { - var err error - - switch key { - case "website_name": - err = check(key, value, test.WebsiteName) - case "website_url": - err = check(key, value, test.WebsiteURL) - case "check_rate": - err = check(key, value, strconv.Itoa(test.CheckRate)) - case "test_type": - err = check(key, value, test.TestType) - case "paused": - err = check(key, value, strconv.FormatBool(test.Paused)) - case "timeout": - err = check(key, value, strconv.Itoa(test.Timeout)) - case "contact_id": - err = check(key, value, strconv.Itoa(test.ContactID)) - case "confirmations": - err = check(key, value, strconv.Itoa(test.Confirmation)) - case "trigger_rate": - err = check(key, value, strconv.Itoa(test.TriggerRate)) - } - - if err != nil { - return err - } - } - return nil - } -} - -func testAccTestCheckDestroy(test *statuscake.Test) resource.TestCheckFunc { - return func(s *terraform.State) error { - client := testAccProvider.Meta().(*statuscake.Client) - err := client.Tests().Delete(test.TestID) - if err == nil { - return fmt.Errorf("test still exists") - } - - return nil - } -} - -const testAccTestConfig_basic = ` -resource "statuscake_test" "google" { - website_name = "google.com" - website_url = "www.google.com" - test_type = "HTTP" - check_rate = 300 - timeout = 10 - contact_id = 43402 - confirmations = 1 - trigger_rate = 10 -} -` - -const testAccTestConfig_update = ` -resource "statuscake_test" "google" { - website_name = "google.com" - website_url = "www.google.com" - test_type = "HTTP" - check_rate = 500 - paused = true - trigger_rate = 20 -} -` - -const testAccTestConfig_tcp = ` -resource "statuscake_test" "google" { - website_name = "google.com" - website_url = "www.google.com" - test_type = "TCP" - check_rate = 300 - timeout = 10 - contact_id = 43402 - confirmations = 1 - port = 80 -} -` diff --git a/statuscake/provider.go b/statuscake/provider.go new file mode 100644 index 0000000..abca376 --- /dev/null +++ b/statuscake/provider.go @@ -0,0 +1,40 @@ +package statuscake + +import ( + "github.com/DreamItGetIT/statuscake" + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/terraform" +) + +func Provider() terraform.ResourceProvider { + return &schema.Provider{ + Schema: map[string]*schema.Schema{ + "username": &schema.Schema{ + Type: schema.TypeString, + Required: true, + DefaultFunc: schema.EnvDefaultFunc("STATUSCAKE_USERNAME", nil), + Description: "Username for StatusCake Account.", + }, + "apikey": &schema.Schema{ + Type: schema.TypeString, + Required: true, + DefaultFunc: schema.EnvDefaultFunc("STATUSCAKE_APIKEY", nil), + Description: "API Key for StatusCake", + }, + }, + + ResourcesMap: map[string]*schema.Resource{ + "statuscake_test": resourceStatusCakeTest(), + }, + + ConfigureFunc: providerConfigure, + } +} + +func providerConfigure(d *schema.ResourceData) (interface{}, error) { + auth := statuscake.Auth{ + Username: d.Get("username").(string), + Apikey: d.Get("apikey").(string), + } + return statuscake.New(auth) +} diff --git a/statuscake/provider_test.go b/statuscake/provider_test.go new file mode 100644 index 0000000..83045d0 --- /dev/null +++ b/statuscake/provider_test.go @@ -0,0 +1,38 @@ +package statuscake + +import ( + "os" + "testing" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/terraform" +) + +var testAccProviders map[string]terraform.ResourceProvider +var testAccProvider *schema.Provider + +func init() { + testAccProvider = Provider().(*schema.Provider) + testAccProviders = map[string]terraform.ResourceProvider{ + "statuscake": testAccProvider, + } +} + +func TestProvider(t *testing.T) { + if err := Provider().(*schema.Provider).InternalValidate(); err != nil { + t.Fatalf("err: %s", err) + } +} + +func TestProvider_impl(t *testing.T) { + var _ terraform.ResourceProvider = Provider() +} + +func testAccPreCheck(t *testing.T) { + if v := os.Getenv("STATUSCAKE_USERNAME"); v == "" { + t.Fatal("STATUSCAKE_USERNAME must be set for acceptance tests") + } + if v := os.Getenv("STATUSCAKE_APIKEY"); v == "" { + t.Fatal("STATUSCAKE_APIKEY must be set for acceptance tests") + } +} diff --git a/statuscake/resource_statuscaketest.go b/statuscake/resource_statuscaketest.go new file mode 100644 index 0000000..101ee83 --- /dev/null +++ b/statuscake/resource_statuscaketest.go @@ -0,0 +1,216 @@ +package statuscake + +import ( + "fmt" + "strconv" + + "log" + + "github.com/DreamItGetIT/statuscake" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceStatusCakeTest() *schema.Resource { + return &schema.Resource{ + Create: CreateTest, + Update: UpdateTest, + Delete: DeleteTest, + Read: ReadTest, + + Schema: map[string]*schema.Schema{ + "test_id": { + Type: schema.TypeString, + Computed: true, + }, + + "website_name": { + Type: schema.TypeString, + Required: true, + }, + + "website_url": { + Type: schema.TypeString, + Required: true, + }, + + "contact_id": { + Type: schema.TypeInt, + Optional: true, + }, + + "check_rate": { + Type: schema.TypeInt, + Optional: true, + Default: 300, + }, + + "test_type": { + Type: schema.TypeString, + Required: true, + }, + + "paused": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + + "timeout": { + Type: schema.TypeInt, + Optional: true, + Default: 40, + }, + + "confirmations": { + Type: schema.TypeInt, + Optional: true, + }, + + "port": { + Type: schema.TypeInt, + Optional: true, + }, + + "trigger_rate": { + Type: schema.TypeInt, + Optional: true, + Default: 5, + }, + }, + } +} + +func CreateTest(d *schema.ResourceData, meta interface{}) error { + client := meta.(*statuscake.Client) + + newTest := &statuscake.Test{ + WebsiteName: d.Get("website_name").(string), + WebsiteURL: d.Get("website_url").(string), + CheckRate: d.Get("check_rate").(int), + TestType: d.Get("test_type").(string), + Paused: d.Get("paused").(bool), + Timeout: d.Get("timeout").(int), + ContactID: d.Get("contact_id").(int), + Confirmation: d.Get("confirmations").(int), + Port: d.Get("port").(int), + TriggerRate: d.Get("trigger_rate").(int), + } + + log.Printf("[DEBUG] Creating new StatusCake Test: %s", d.Get("website_name").(string)) + + response, err := client.Tests().Update(newTest) + if err != nil { + return fmt.Errorf("Error creating StatusCake Test: %s", err.Error()) + } + + d.Set("test_id", fmt.Sprintf("%d", response.TestID)) + d.SetId(fmt.Sprintf("%d", response.TestID)) + + return ReadTest(d, meta) +} + +func UpdateTest(d *schema.ResourceData, meta interface{}) error { + client := meta.(*statuscake.Client) + + params := getStatusCakeTestInput(d) + + log.Printf("[DEBUG] StatusCake Test Update for %s", d.Id()) + _, err := client.Tests().Update(params) + if err != nil { + return fmt.Errorf("Error Updating StatusCake Test: %s", err.Error()) + } + return nil +} + +func DeleteTest(d *schema.ResourceData, meta interface{}) error { + client := meta.(*statuscake.Client) + + testId, parseErr := strconv.Atoi(d.Id()) + if parseErr != nil { + return parseErr + } + log.Printf("[DEBUG] Deleting StatusCake Test: %s", d.Id()) + err := client.Tests().Delete(testId) + if err != nil { + return err + } + + return nil +} + +func ReadTest(d *schema.ResourceData, meta interface{}) error { + client := meta.(*statuscake.Client) + + testId, parseErr := strconv.Atoi(d.Id()) + if parseErr != nil { + return parseErr + } + testResp, err := client.Tests().Detail(testId) + if err != nil { + return fmt.Errorf("Error Getting StatusCake Test Details for %s: Error: %s", d.Id(), err) + } + d.Set("website_name", testResp.WebsiteName) + d.Set("website_url", testResp.WebsiteURL) + d.Set("check_rate", testResp.CheckRate) + d.Set("test_type", testResp.TestType) + d.Set("paused", testResp.Paused) + d.Set("timeout", testResp.Timeout) + d.Set("contact_id", testResp.ContactID) + d.Set("confirmations", testResp.Confirmation) + d.Set("port", testResp.Port) + d.Set("trigger_rate", testResp.TriggerRate) + + return nil +} + +func getStatusCakeTestInput(d *schema.ResourceData) *statuscake.Test { + testId, parseErr := strconv.Atoi(d.Id()) + if parseErr != nil { + log.Printf("[DEBUG] Error Parsing StatusCake TestID: %s", d.Id()) + } + test := &statuscake.Test{ + TestID: testId, + } + if v, ok := d.GetOk("website_name"); ok { + test.WebsiteName = v.(string) + } + if v, ok := d.GetOk("website_url"); ok { + test.WebsiteURL = v.(string) + } + if v, ok := d.GetOk("check_rate"); ok { + test.CheckRate = v.(int) + } + if v, ok := d.GetOk("contact_id"); ok { + test.ContactID = v.(int) + } + if v, ok := d.GetOk("test_type"); ok { + test.TestType = v.(string) + } + if v, ok := d.GetOk("paused"); ok { + test.Paused = v.(bool) + } + if v, ok := d.GetOk("timeout"); ok { + test.Timeout = v.(int) + } + if v, ok := d.GetOk("contact_id"); ok { + test.ContactID = v.(int) + } + if v, ok := d.GetOk("confirmations"); ok { + test.Confirmation = v.(int) + } + if v, ok := d.GetOk("port"); ok { + test.Port = v.(int) + } + if v, ok := d.GetOk("trigger_rate"); ok { + test.TriggerRate = v.(int) + } + + defaultStatusCodes := "204, 205, 206, 303, 400, 401, 403, 404, 405, 406, " + + "408, 410, 413, 444, 429, 494, 495, 496, 499, 500, 501, 502, 503, " + + "504, 505, 506, 507, 508, 509, 510, 511, 521, 522, 523, 524, 520, " + + "598, 599" + + test.StatusCodes = defaultStatusCodes + + return test +} diff --git a/statuscake/resource_statuscaketest_test.go b/statuscake/resource_statuscaketest_test.go new file mode 100644 index 0000000..f07fcc5 --- /dev/null +++ b/statuscake/resource_statuscaketest_test.go @@ -0,0 +1,202 @@ +package statuscake + +import ( + "fmt" + "strconv" + "testing" + + "github.com/DreamItGetIT/statuscake" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccStatusCake_basic(t *testing.T) { + var test statuscake.Test + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccTestCheckDestroy(&test), + Steps: []resource.TestStep{ + { + Config: testAccTestConfig_basic, + Check: resource.ComposeTestCheckFunc( + testAccTestCheckExists("statuscake_test.google", &test), + testAccTestCheckAttributes("statuscake_test.google", &test), + ), + }, + }, + }) +} + +func TestAccStatusCake_tcp(t *testing.T) { + var test statuscake.Test + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccTestCheckDestroy(&test), + Steps: []resource.TestStep{ + { + Config: testAccTestConfig_tcp, + Check: resource.ComposeTestCheckFunc( + testAccTestCheckExists("statuscake_test.google", &test), + testAccTestCheckAttributes("statuscake_test.google", &test), + ), + }, + }, + }) +} + +func TestAccStatusCake_withUpdate(t *testing.T) { + var test statuscake.Test + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccTestCheckDestroy(&test), + Steps: []resource.TestStep{ + { + Config: testAccTestConfig_basic, + Check: resource.ComposeTestCheckFunc( + testAccTestCheckExists("statuscake_test.google", &test), + ), + }, + + { + Config: testAccTestConfig_update, + Check: resource.ComposeTestCheckFunc( + testAccTestCheckExists("statuscake_test.google", &test), + testAccTestCheckAttributes("statuscake_test.google", &test), + resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"), + resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"), + resource.TestCheckResourceAttr("statuscake_test.google", "timeout", "40"), + resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "0"), + resource.TestCheckResourceAttr("statuscake_test.google", "confirmations", "0"), + resource.TestCheckResourceAttr("statuscake_test.google", "trigger_rate", "20"), + ), + }, + }, + }) +} + +func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[rn] + if !ok { + return fmt.Errorf("resource not found: %s", rn) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("TestID not set") + } + + client := testAccProvider.Meta().(*statuscake.Client) + testId, parseErr := strconv.Atoi(rs.Primary.ID) + if parseErr != nil { + return fmt.Errorf("error in statuscake test CheckExists: %s", parseErr) + } + + gotTest, err := client.Tests().Detail(testId) + if err != nil { + return fmt.Errorf("error getting test: %s", err) + } + + *test = *gotTest + + return nil + } +} + +func testAccTestCheckAttributes(rn string, test *statuscake.Test) resource.TestCheckFunc { + return func(s *terraform.State) error { + attrs := s.RootModule().Resources[rn].Primary.Attributes + + check := func(key, stateValue, testValue string) error { + if testValue != stateValue { + return fmt.Errorf("different values for %s in state (%s) and in statuscake (%s)", + key, stateValue, testValue) + } + return nil + } + + for key, value := range attrs { + var err error + + switch key { + case "website_name": + err = check(key, value, test.WebsiteName) + case "website_url": + err = check(key, value, test.WebsiteURL) + case "check_rate": + err = check(key, value, strconv.Itoa(test.CheckRate)) + case "test_type": + err = check(key, value, test.TestType) + case "paused": + err = check(key, value, strconv.FormatBool(test.Paused)) + case "timeout": + err = check(key, value, strconv.Itoa(test.Timeout)) + case "contact_id": + err = check(key, value, strconv.Itoa(test.ContactID)) + case "confirmations": + err = check(key, value, strconv.Itoa(test.Confirmation)) + case "trigger_rate": + err = check(key, value, strconv.Itoa(test.TriggerRate)) + } + + if err != nil { + return err + } + } + return nil + } +} + +func testAccTestCheckDestroy(test *statuscake.Test) resource.TestCheckFunc { + return func(s *terraform.State) error { + client := testAccProvider.Meta().(*statuscake.Client) + err := client.Tests().Delete(test.TestID) + if err == nil { + return fmt.Errorf("test still exists") + } + + return nil + } +} + +const testAccTestConfig_basic = ` +resource "statuscake_test" "google" { + website_name = "google.com" + website_url = "www.google.com" + test_type = "HTTP" + check_rate = 300 + timeout = 10 + contact_id = 43402 + confirmations = 1 + trigger_rate = 10 +} +` + +const testAccTestConfig_update = ` +resource "statuscake_test" "google" { + website_name = "google.com" + website_url = "www.google.com" + test_type = "HTTP" + check_rate = 500 + paused = true + trigger_rate = 20 +} +` + +const testAccTestConfig_tcp = ` +resource "statuscake_test" "google" { + website_name = "google.com" + website_url = "www.google.com" + test_type = "TCP" + check_rate = 300 + timeout = 10 + contact_id = 43402 + confirmations = 1 + port = 80 +} +` -- cgit v1.2.3