diff options
author | Alexandre Garand <alexandre.garand@fretlink.com> | 2019-08-09 11:10:50 +0200 |
---|---|---|
committer | Alexandre Garand <alexandre.garand@fretlink.com> | 2019-08-09 11:10:50 +0200 |
commit | 00adefd9b3c8168d52522da0c1370c45171c03e3 (patch) | |
tree | c2ecc2f0aa567fcadf1ebfa96764f2057215a03e | |
parent | 4bbf9881d0a859d7eb54fcac2c8cdfa429503f9b (diff) | |
parent | 0b957cd46a2a3a209c45778da2a7e1e55a94f3c7 (diff) | |
download | terraform-provider-statuscake-00adefd9b3c8168d52522da0c1370c45171c03e3.tar.gz terraform-provider-statuscake-00adefd9b3c8168d52522da0c1370c45171c03e3.tar.zst terraform-provider-statuscake-00adefd9b3c8168d52522da0c1370c45171c03e3.zip |
Merge branch 'add_contact_groups' of github.com:alexandreFre/terraform-provider-statuscake
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | statuscake/provider.go | 7 | ||||
-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 |
7 files changed, 361 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index b414902..dff110a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md | |||
@@ -1,4 +1,5 @@ | |||
1 | ## 1.0.0 (Unreleased) | 1 | ## 1.1.0 (Unreleased) |
2 | ## 1.0.0 (July 02, 2019) | ||
2 | 3 | ||
3 | NOTES: | 4 | NOTES: |
4 | 5 | ||
diff --git a/statuscake/provider.go b/statuscake/provider.go index 7f38d7f..bba03fc 100644 --- a/statuscake/provider.go +++ b/statuscake/provider.go | |||
@@ -24,8 +24,11 @@ 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 | |
28 | "statuscake_ssl": resourceStatusCakeSsl(), | 28 | "statuscake_ssl": resourceStatusCakeSsl(), |
29 | "statuscake_test": resourceStatusCakeTest(), | ||
30 | "statuscake_contact_group": resourceStatusCakeContactGroup(), | ||
31 | >>>>>>> 0b957cd46a2a3a209c45778da2a7e1e55a94f3c7 | ||
29 | }, | 32 | }, |
30 | 33 | ||
31 | ConfigureFunc: providerConfigure, | 34 | ConfigureFunc: providerConfigure, |
diff --git a/statuscake/resource_statuscakecontactgroup.go b/statuscake/resource_statuscakecontactgroup.go new file mode 100644 index 0000000..612f96e --- /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 | |||
118 | log.Printf("[DEBUG] Deleting StatusCake ContactGroup: %s", d.Id()) | ||
119 | err := statuscake.NewContactGroups(client).Delete(d.Get("contact_id").(int)) | ||
120 | |||
121 | return err | ||
122 | } | ||
123 | |||
124 | func ReadContactGroup(d *schema.ResourceData, meta interface{}) error { | ||
125 | client := meta.(*statuscake.Client) | ||
126 | |||
127 | response, err := statuscake.NewContactGroups(client).Detail(d.Get("contact_id").(int)) | ||
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 db2e56d..f45b0e0 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 and in StatusCake. | 6 | |
7 | The StatusCake provider configures tests and contact groups in StatusCake. | ||
7 | --- | 8 | --- |
8 | 9 | ||
9 | # StatusCake Provider | 10 | # StatusCake Provider |
@@ -47,4 +48,11 @@ resource "statuscake_ssl" "google" { | |||
47 | alert_broken = false | 48 | alert_broken = false |
48 | alert_mixed = true | 49 | alert_mixed = true |
49 | } | 50 | } |
51 | |||
52 | resource "statuscake_contact_group" "exemple" { | ||
53 | emails= ["email1","email2"] | ||
54 | group_name= "group name" | ||
55 | ping_url= "url" | ||
56 | } | ||
57 | |||
50 | ``` | 58 | ``` |
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 652552f..a069603 100644 --- a/website/statuscake.erb +++ b/website/statuscake.erb | |||
@@ -18,6 +18,9 @@ | |||
18 | </li> | 18 | </li> |
19 | <li<%= sidebar_current("docs-statuscake-ssl") %>> | 19 | <li<%= sidebar_current("docs-statuscake-ssl") %>> |
20 | <a href="/docs/providers/statuscake/r/ssl.html">statuscake_ssl</a> | 20 | <a href="/docs/providers/statuscake/r/ssl.html">statuscake_ssl</a> |
21 | </li> | ||
22 | <li<%= sidebar_current("docs-statuscake-contact_group") %>> | ||
23 | <a href="/docs/providers/statuscake/r/contact_group.html">statuscake_contact_group</a> | ||
21 | </li> | 24 | </li> |
22 | </ul> | 25 | </ul> |
23 | </li> | 26 | </li> |