aboutsummaryrefslogtreecommitdiffhomepage
path: root/statuscake/resource_statuscakecontactgroup.go
diff options
context:
space:
mode:
authorAlexandre Garand <alexandre.garand@fretlink.com>2019-08-09 11:10:50 +0200
committerAlexandre Garand <alexandre.garand@fretlink.com>2019-08-09 11:10:50 +0200
commit00adefd9b3c8168d52522da0c1370c45171c03e3 (patch)
treec2ecc2f0aa567fcadf1ebfa96764f2057215a03e /statuscake/resource_statuscakecontactgroup.go
parent4bbf9881d0a859d7eb54fcac2c8cdfa429503f9b (diff)
parent0b957cd46a2a3a209c45778da2a7e1e55a94f3c7 (diff)
downloadterraform-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
Diffstat (limited to 'statuscake/resource_statuscakecontactgroup.go')
-rw-r--r--statuscake/resource_statuscakecontactgroup.go138
1 files changed, 138 insertions, 0 deletions
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 @@
1package statuscake
2
3import (
4 "fmt"
5
6 "github.com/DreamItGetIT/statuscake"
7 "github.com/hashicorp/terraform/helper/schema"
8 "log"
9 "strconv"
10)
11
12func 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
60func 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
90func 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
115func 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
124func 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}