diff options
author | Alexandre Garand <alexandre.garand@fretlink.com> | 2019-08-09 11:02:14 +0200 |
---|---|---|
committer | Alexandre Garand <alexandre.garand@fretlink.com> | 2019-08-09 12:30:58 +0200 |
commit | 49c1c7b4dc69ffb9ab52330e6dc52ccdd6351087 (patch) | |
tree | 34f1df96cf365a584acf76482d5b5b5fb4d267ee | |
parent | ebf8436f01bcacb71e3c378f08b165dfd6dd90e6 (diff) | |
download | terraform-provider-statuscake-49c1c7b4dc69ffb9ab52330e6dc52ccdd6351087.tar.gz terraform-provider-statuscake-49c1c7b4dc69ffb9ab52330e6dc52ccdd6351087.tar.zst terraform-provider-statuscake-49c1c7b4dc69ffb9ab52330e6dc52ccdd6351087.zip |
add contact_group resource to the provider
-rw-r--r-- | statuscake/provider.go | 3 | ||||
-rw-r--r-- | statuscake/resource_statuscakecontactgroup.go | 138 | ||||
-rw-r--r-- | statuscake/resource_statuscakecontactgroup_test.go | 157 | ||||
-rw-r--r-- | website/docs/index.html.markdown | 10 | ||||
-rw-r--r-- | website/docs/r/contact_group.html.markdown | 47 | ||||
-rw-r--r-- | website/statuscake.erb | 3 |
6 files changed, 356 insertions, 2 deletions
diff --git a/statuscake/provider.go b/statuscake/provider.go index 93c691f..774444f 100644 --- a/statuscake/provider.go +++ b/statuscake/provider.go | |||
@@ -24,7 +24,8 @@ func Provider() terraform.ResourceProvider { | |||
24 | }, | 24 | }, |
25 | 25 | ||
26 | ResourcesMap: map[string]*schema.Resource{ | 26 | ResourcesMap: map[string]*schema.Resource{ |
27 | "statuscake_test": resourceStatusCakeTest(), | 27 | "statuscake_test": resourceStatusCakeTest(), |
28 | "statuscake_contact_group": resourceStatusCakeContactGroup(), | ||
28 | }, | 29 | }, |
29 | 30 | ||
30 | ConfigureFunc: providerConfigure, | 31 | ConfigureFunc: providerConfigure, |
diff --git a/statuscake/resource_statuscakecontactgroup.go b/statuscake/resource_statuscakecontactgroup.go new file mode 100644 index 0000000..aa11c3a --- /dev/null +++ b/statuscake/resource_statuscakecontactgroup.go | |||
@@ -0,0 +1,138 @@ | |||
1 | package statuscake | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | |||
6 | "github.com/DreamItGetIT/statuscake" | ||
7 | "github.com/hashicorp/terraform/helper/schema" | ||
8 | "log" | ||
9 | "strconv" | ||
10 | ) | ||
11 | |||
12 | func resourceStatusCakeContactGroup() *schema.Resource { | ||
13 | return &schema.Resource{ | ||
14 | Create: CreateContactGroup, | ||
15 | Update: UpdateContactGroup, | ||
16 | Delete: DeleteContactGroup, | ||
17 | Read: ReadContactGroup, | ||
18 | Importer: &schema.ResourceImporter{ | ||
19 | State: schema.ImportStatePassthrough, | ||
20 | }, | ||
21 | |||
22 | Schema: map[string]*schema.Schema{ | ||
23 | "contact_id": { | ||
24 | Type: schema.TypeInt, | ||
25 | Computed: true, | ||
26 | }, | ||
27 | "desktop_alert": { | ||
28 | Type: schema.TypeString, | ||
29 | Optional: true, | ||
30 | }, | ||
31 | "ping_url": { | ||
32 | Type: schema.TypeString, | ||
33 | Optional: true, | ||
34 | }, | ||
35 | "group_name": { | ||
36 | Type: schema.TypeString, | ||
37 | Required: true, | ||
38 | }, | ||
39 | "pushover": { | ||
40 | Type: schema.TypeString, | ||
41 | Optional: true, | ||
42 | }, | ||
43 | "boxcar": { | ||
44 | Type: schema.TypeString, | ||
45 | Optional: true, | ||
46 | }, | ||
47 | "mobiles": { | ||
48 | Type: schema.TypeString, | ||
49 | Optional: true, | ||
50 | }, | ||
51 | "emails": { | ||
52 | Type: schema.TypeSet, | ||
53 | Elem: &schema.Schema{Type: schema.TypeString}, | ||
54 | Optional: true, | ||
55 | }, | ||
56 | }, | ||
57 | } | ||
58 | } | ||
59 | |||
60 | func CreateContactGroup(d *schema.ResourceData, meta interface{}) error { | ||
61 | client := meta.(*statuscake.Client) | ||
62 | |||
63 | newContactGroup := &statuscake.ContactGroup{ | ||
64 | GroupName: d.Get("group_name").(string), | ||
65 | Emails: castSetToSliceStrings(d.Get("emails").(*schema.Set).List()), | ||
66 | Mobiles: d.Get("mobiles").(string), | ||
67 | Boxcar: d.Get("boxcar").(string), | ||
68 | Pushover: d.Get("pushover").(string), | ||
69 | DesktopAlert: d.Get("desktop_alert").(string), | ||
70 | PingURL: d.Get("ping_url").(string), | ||
71 | } | ||
72 | |||
73 | log.Printf("[DEBUG] Creating new StatusCake Contact group: %s", d.Get("group_name").(string)) | ||
74 | |||
75 | response, err := statuscake.NewContactGroups(client).Create(newContactGroup) | ||
76 | if err != nil { | ||
77 | return fmt.Errorf("Error creating StatusCake ContactGroup: %s", err.Error()) | ||
78 | } | ||
79 | |||
80 | d.Set("mobiles", newContactGroup.Mobiles) | ||
81 | d.Set("boxcar", newContactGroup.Boxcar) | ||
82 | d.Set("pushover", newContactGroup.Pushover) | ||
83 | d.Set("desktop_alert", newContactGroup.DesktopAlert) | ||
84 | d.Set("contact_id", newContactGroup.ContactID) | ||
85 | d.SetId(strconv.Itoa(response.ContactID)) | ||
86 | |||
87 | return ReadContactGroup(d, meta) | ||
88 | } | ||
89 | |||
90 | func UpdateContactGroup(d *schema.ResourceData, meta interface{}) error { | ||
91 | client := meta.(*statuscake.Client) | ||
92 | |||
93 | params := &statuscake.ContactGroup{ | ||
94 | GroupName: d.Get("group_name").(string), | ||
95 | Emails: castSetToSliceStrings(d.Get("emails").(*schema.Set).List()), | ||
96 | Mobiles: d.Get("mobiles").(string), | ||
97 | ContactID: d.Get("contact_id").(int), | ||
98 | Boxcar: d.Get("boxcar").(string), | ||
99 | Pushover: d.Get("pushover").(string), | ||
100 | DesktopAlert: d.Get("desktop_alert").(string), | ||
101 | PingURL: d.Get("ping_url").(string), | ||
102 | } | ||
103 | log.Printf("[DEBUG] StatusCake ContactGroup Update for %s", d.Id()) | ||
104 | _, err := statuscake.NewContactGroups(client).Update(params) | ||
105 | d.Set("mobiles", params.Mobiles) | ||
106 | d.Set("boxcar", params.Boxcar) | ||
107 | d.Set("pushover", params.Pushover) | ||
108 | d.Set("desktop_alert", params.DesktopAlert) | ||
109 | if err != nil { | ||
110 | return fmt.Errorf("Error Updating StatusCake ContactGroup: %s", err.Error()) | ||
111 | } | ||
112 | return ReadContactGroup(d, meta) | ||
113 | } | ||
114 | |||
115 | func DeleteContactGroup(d *schema.ResourceData, meta interface{}) error { | ||
116 | client := meta.(*statuscake.Client) | ||
117 | id, _ := strconv.Atoi(d.Id()) | ||
118 | log.Printf("[DEBUG] Deleting StatusCake ContactGroup: %s", d.Id()) | ||
119 | err := statuscake.NewContactGroups(client).Delete(id) | ||
120 | |||
121 | return err | ||
122 | } | ||
123 | |||
124 | func ReadContactGroup(d *schema.ResourceData, meta interface{}) error { | ||
125 | client := meta.(*statuscake.Client) | ||
126 | id, _ := strconv.Atoi(d.Id()) | ||
127 | response, err := statuscake.NewContactGroups(client).Detail(id) | ||
128 | if err != nil { | ||
129 | return fmt.Errorf("Error Getting StatusCake ContactGroup Details for %s: Error: %s", d.Id(), err) | ||
130 | } | ||
131 | d.Set("group_name", response.GroupName) | ||
132 | d.Set("emails", response.Emails) | ||
133 | d.Set("contact_id", response.ContactID) | ||
134 | d.Set("ping_url", response.PingURL) | ||
135 | d.SetId(strconv.Itoa(response.ContactID)) | ||
136 | |||
137 | return nil | ||
138 | } | ||
diff --git a/statuscake/resource_statuscakecontactgroup_test.go b/statuscake/resource_statuscakecontactgroup_test.go new file mode 100644 index 0000000..a409808 --- /dev/null +++ b/statuscake/resource_statuscakecontactgroup_test.go | |||
@@ -0,0 +1,157 @@ | |||
1 | package statuscake | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "github.com/DreamItGetIT/statuscake" | ||
6 | "github.com/hashicorp/terraform/helper/resource" | ||
7 | "github.com/hashicorp/terraform/terraform" | ||
8 | "strconv" | ||
9 | "testing" | ||
10 | ) | ||
11 | |||
12 | func TestAccStatusCakeContactGroup_basic(t *testing.T) { | ||
13 | var contactGroup statuscake.ContactGroup | ||
14 | |||
15 | resource.Test(t, resource.TestCase{ | ||
16 | PreCheck: func() { testAccPreCheck(t) }, | ||
17 | Providers: testAccProviders, | ||
18 | CheckDestroy: testAccContactGroupCheckDestroy(&contactGroup), | ||
19 | Steps: []resource.TestStep{ | ||
20 | { | ||
21 | Config: testAccContactGroupConfig_basic, | ||
22 | Check: resource.ComposeTestCheckFunc( | ||
23 | testAccContactGroupCheckExists("statuscake_contact_group.exemple", &contactGroup), | ||
24 | testAccContactGroupCheckAttributes("statuscake_contact_group.exemple", &contactGroup), | ||
25 | ), | ||
26 | }, | ||
27 | }, | ||
28 | }) | ||
29 | } | ||
30 | |||
31 | func TestAccStatusCakeContactGroup_withUpdate(t *testing.T) { | ||
32 | var contactGroup statuscake.ContactGroup | ||
33 | |||
34 | resource.Test(t, resource.TestCase{ | ||
35 | PreCheck: func() { testAccPreCheck(t) }, | ||
36 | Providers: testAccProviders, | ||
37 | CheckDestroy: testAccContactGroupCheckDestroy(&contactGroup), | ||
38 | Steps: []resource.TestStep{ | ||
39 | { | ||
40 | Config: testAccContactGroupConfig_basic, | ||
41 | Check: resource.ComposeTestCheckFunc( | ||
42 | testAccContactGroupCheckExists("statuscake_contact_group.exemple", &contactGroup), | ||
43 | testAccContactGroupCheckAttributes("statuscake_contact_group.exemple", &contactGroup), | ||
44 | ), | ||
45 | }, | ||
46 | |||
47 | { | ||
48 | Config: testAccContactGroupConfig_update, | ||
49 | Check: resource.ComposeTestCheckFunc( | ||
50 | testAccContactGroupCheckExists("statuscake_contact_group.exemple", &contactGroup), | ||
51 | testAccContactGroupCheckAttributes("statuscake_contact_group.exemple", &contactGroup), | ||
52 | resource.TestCheckResourceAttr("statuscake_contact_group.exemple", "group_name", "group"), | ||
53 | resource.TestCheckResourceAttr("statuscake_contact_group.exemple", "ping_url", "https"), | ||
54 | ), | ||
55 | }, | ||
56 | }, | ||
57 | }) | ||
58 | } | ||
59 | |||
60 | func testAccContactGroupCheckExists(rn string, contactGroup *statuscake.ContactGroup) resource.TestCheckFunc { | ||
61 | return func(s *terraform.State) error { | ||
62 | rs, ok := s.RootModule().Resources[rn] | ||
63 | if !ok { | ||
64 | return fmt.Errorf("resource not found: %s", rn) | ||
65 | } | ||
66 | |||
67 | if rs.Primary.ID == "" { | ||
68 | return fmt.Errorf("ContactGroupID not set") | ||
69 | } | ||
70 | |||
71 | client := testAccProvider.Meta().(*statuscake.Client) | ||
72 | contactGroupId, _ := strconv.Atoi(rs.Primary.ID) | ||
73 | |||
74 | gotContactGroup, err := statuscake.NewContactGroups(client).Detail(contactGroupId) | ||
75 | if err != nil { | ||
76 | return fmt.Errorf("error getting ContactGroup: %s", err) | ||
77 | } | ||
78 | |||
79 | *contactGroup = *gotContactGroup | ||
80 | |||
81 | return nil | ||
82 | } | ||
83 | } | ||
84 | |||
85 | func testAccContactGroupCheckAttributes(rn string, contactGroup *statuscake.ContactGroup) resource.TestCheckFunc { | ||
86 | return func(s *terraform.State) error { | ||
87 | attrs := s.RootModule().Resources[rn].Primary.Attributes | ||
88 | |||
89 | check := func(key, stateValue, contactGroupValue string) error { | ||
90 | if contactGroupValue != stateValue { | ||
91 | return fmt.Errorf("different values for %s in state (%s) and in statuscake (%s)", | ||
92 | key, stateValue, contactGroupValue) | ||
93 | } | ||
94 | return nil | ||
95 | } | ||
96 | |||
97 | for key, value := range attrs { | ||
98 | var err error | ||
99 | |||
100 | switch key { | ||
101 | case "contact_id": | ||
102 | err = check(key, value, strconv.Itoa(contactGroup.ContactID)) | ||
103 | case "desktop_alert": | ||
104 | err = check(key, value, contactGroup.DesktopAlert) | ||
105 | case "ping_url": | ||
106 | err = check(key, value, contactGroup.PingURL) | ||
107 | case "group_name": | ||
108 | err = check(key, value, contactGroup.GroupName) | ||
109 | case "pushover": | ||
110 | err = check(key, value, contactGroup.Pushover) | ||
111 | case "boxcar": | ||
112 | err = check(key, value, contactGroup.Boxcar) | ||
113 | case "mobiles": | ||
114 | err = check(key, value, contactGroup.Mobiles) | ||
115 | case "emails": | ||
116 | for _, tv := range contactGroup.Emails { | ||
117 | err = check(key, value, tv) | ||
118 | if err != nil { | ||
119 | return err | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | if err != nil { | ||
124 | return err | ||
125 | } | ||
126 | } | ||
127 | return nil | ||
128 | } | ||
129 | } | ||
130 | |||
131 | func testAccContactGroupCheckDestroy(contactGroup *statuscake.ContactGroup) resource.TestCheckFunc { | ||
132 | return func(s *terraform.State) error { | ||
133 | client := testAccProvider.Meta().(*statuscake.Client) | ||
134 | _, err := statuscake.NewContactGroups(client).Detail(contactGroup.ContactID) | ||
135 | if err == nil { | ||
136 | return fmt.Errorf("contact_group still exists") | ||
137 | } | ||
138 | |||
139 | return nil | ||
140 | } | ||
141 | } | ||
142 | |||
143 | const testAccContactGroupConfig_basic = ` | ||
144 | resource "statuscake_contact_group" "exemple" { | ||
145 | emails= ["aaa","bbb"] | ||
146 | group_name= "groupname" | ||
147 | ping_url= "http" | ||
148 | } | ||
149 | ` | ||
150 | |||
151 | const testAccContactGroupConfig_update = ` | ||
152 | resource "statuscake_contact_group" "exemple" { | ||
153 | emails= ["aaa","bbb","ccc"] | ||
154 | group_name= "group" | ||
155 | ping_url= "https" | ||
156 | } | ||
157 | ` | ||
diff --git a/website/docs/index.html.markdown b/website/docs/index.html.markdown index d25a866..653b9ec 100644 --- a/website/docs/index.html.markdown +++ b/website/docs/index.html.markdown | |||
@@ -3,7 +3,8 @@ layout: "statuscake" | |||
3 | page_title: "Provider: StatusCake" | 3 | page_title: "Provider: StatusCake" |
4 | sidebar_current: "docs-statuscake-index" | 4 | sidebar_current: "docs-statuscake-index" |
5 | description: |- | 5 | description: |- |
6 | The StatusCake provider configures tests in StatusCake. | 6 | |
7 | The StatusCake provider configures tests and contact groups in StatusCake. | ||
7 | --- | 8 | --- |
8 | 9 | ||
9 | # StatusCake Provider | 10 | # StatusCake Provider |
@@ -36,4 +37,11 @@ resource "statuscake_test" "google" { | |||
36 | check_rate = 300 | 37 | check_rate = 300 |
37 | contact_id = 12345 | 38 | contact_id = 12345 |
38 | } | 39 | } |
40 | |||
41 | resource "statuscake_contact_group" "exemple" { | ||
42 | emails= ["email1","email2"] | ||
43 | group_name= "group name" | ||
44 | ping_url= "url" | ||
45 | } | ||
46 | |||
39 | ``` | 47 | ``` |
diff --git a/website/docs/r/contact_group.html.markdown b/website/docs/r/contact_group.html.markdown new file mode 100644 index 0000000..ed3354f --- /dev/null +++ b/website/docs/r/contact_group.html.markdown | |||
@@ -0,0 +1,47 @@ | |||
1 | --- | ||
2 | layout: "statuscake" | ||
3 | page_title: "StatusCake: statuscake_contact_group" | ||
4 | sidebar_current: "docs-statuscake-contact_group" | ||
5 | description: |- | ||
6 | The statuscake_contact_group resource allows StatusCake contact groups to be managed by Terraform. | ||
7 | --- | ||
8 | |||
9 | # statuscake\_contact_group | ||
10 | |||
11 | The contact_group resource allows StatusCake contact groups to be managed by Terraform. | ||
12 | |||
13 | ## Example Usage | ||
14 | |||
15 | ```hcl | ||
16 | resource "statuscake_contact_group" "exemple" { | ||
17 | emails= ["email1","email2"] | ||
18 | group_name= "group name" | ||
19 | ping_url= "url" | ||
20 | } | ||
21 | ``` | ||
22 | |||
23 | ## Argument Reference | ||
24 | |||
25 | The following arguments are supported: | ||
26 | |||
27 | * `desktop_alert` - (Required) Set to 1 To Enable Desktop Alerts | ||
28 | * `ping_url` - (Optional) | ||
29 | * `group_name` - (Optional) The internal Group Name | ||
30 | * `pushover` - (Optional) A Pushover Account Key | ||
31 | * `boxcar` - (Optional) A Boxcar API Key | ||
32 | * `mobiles` - (Optional) Comma Seperated List of International Format Cell Numbers | ||
33 | * `emails` - (Optional) List of Emails To Alert. | ||
34 | |||
35 | ## Attributes Reference | ||
36 | |||
37 | The following attribute is exported: | ||
38 | |||
39 | * `contact_id` - A unique identifier for the contact group. | ||
40 | |||
41 | ## Import | ||
42 | |||
43 | StatusCake contact groups can be imported using the contact group id, e.g. | ||
44 | |||
45 | ``` | ||
46 | tf import statuscake_contact_group.example 123 | ||
47 | ``` | ||
diff --git a/website/statuscake.erb b/website/statuscake.erb index d5edf59..93828d0 100644 --- a/website/statuscake.erb +++ b/website/statuscake.erb | |||
@@ -16,6 +16,9 @@ | |||
16 | <li<%= sidebar_current("docs-statuscake-test") %>> | 16 | <li<%= sidebar_current("docs-statuscake-test") %>> |
17 | <a href="/docs/providers/statuscake/r/test.html">statuscake_test</a> | 17 | <a href="/docs/providers/statuscake/r/test.html">statuscake_test</a> |
18 | </li> | 18 | </li> |
19 | <li<%= sidebar_current("docs-statuscake-contact_group") %>> | ||
20 | <a href="/docs/providers/statuscake/r/contact_group.html">statuscake_contact_group</a> | ||
21 | </li> | ||
19 | </ul> | 22 | </ul> |
20 | </li> | 23 | </li> |
21 | </ul> | 24 | </ul> |