diff options
Diffstat (limited to 'statuscake/resource_statuscakecontactgroup.go')
-rw-r--r-- | statuscake/resource_statuscakecontactgroup.go | 138 |
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 @@ | |||
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 | } | ||