aboutsummaryrefslogtreecommitdiffhomepage
path: root/statuscake/resource_statuscakecontactgroup.go
blob: 612f96ebdc0e599085cb73b2be65feaaaf5c5cbb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package statuscake

import (
	"fmt"

	"github.com/DreamItGetIT/statuscake"
	"github.com/hashicorp/terraform/helper/schema"
	"log"
	"strconv"
)

func resourceStatusCakeContactGroup() *schema.Resource {
	return &schema.Resource{
		Create: CreateContactGroup,
		Update: UpdateContactGroup,
		Delete: DeleteContactGroup,
		Read:   ReadContactGroup,
		Importer: &schema.ResourceImporter{
			State: schema.ImportStatePassthrough,
		},

		Schema: map[string]*schema.Schema{
			"contact_id": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"desktop_alert": {
				Type:     schema.TypeString,
				Optional: true,
			},
			"ping_url": {
				Type:     schema.TypeString,
				Optional: true,
			},
			"group_name": {
				Type:     schema.TypeString,
				Required: true,
			},
			"pushover": {
				Type:     schema.TypeString,
				Optional: true,
			},
			"boxcar": {
				Type:     schema.TypeString,
				Optional: true,
			},
			"mobiles": {
				Type:     schema.TypeString,
				Optional: true,
			},
			"emails": {
				Type:     schema.TypeSet,
				Elem:     &schema.Schema{Type: schema.TypeString},
				Optional: true,
			},
		},
	}
}

func CreateContactGroup(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*statuscake.Client)

	newContactGroup := &statuscake.ContactGroup{
		GroupName:    d.Get("group_name").(string),
		Emails:       castSetToSliceStrings(d.Get("emails").(*schema.Set).List()),
		Mobiles:      d.Get("mobiles").(string),
		Boxcar:       d.Get("boxcar").(string),
		Pushover:     d.Get("pushover").(string),
		DesktopAlert: d.Get("desktop_alert").(string),
		PingURL:      d.Get("ping_url").(string),
	}

	log.Printf("[DEBUG] Creating new StatusCake Contact group: %s", d.Get("group_name").(string))

	response, err := statuscake.NewContactGroups(client).Create(newContactGroup)
	if err != nil {
		return fmt.Errorf("Error creating StatusCake ContactGroup: %s", err.Error())
	}

	d.Set("mobiles", newContactGroup.Mobiles)
	d.Set("boxcar", newContactGroup.Boxcar)
	d.Set("pushover", newContactGroup.Pushover)
	d.Set("desktop_alert", newContactGroup.DesktopAlert)
	d.Set("contact_id", newContactGroup.ContactID)
	d.SetId(strconv.Itoa(response.ContactID))

	return ReadContactGroup(d, meta)
}

func UpdateContactGroup(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*statuscake.Client)

	params := &statuscake.ContactGroup{
		GroupName:    d.Get("group_name").(string),
		Emails:       castSetToSliceStrings(d.Get("emails").(*schema.Set).List()),
		Mobiles:      d.Get("mobiles").(string),
		ContactID:    d.Get("contact_id").(int),
		Boxcar:       d.Get("boxcar").(string),
		Pushover:     d.Get("pushover").(string),
		DesktopAlert: d.Get("desktop_alert").(string),
		PingURL:      d.Get("ping_url").(string),
	}
	log.Printf("[DEBUG] StatusCake ContactGroup Update for %s", d.Id())
	_, err := statuscake.NewContactGroups(client).Update(params)
	d.Set("mobiles", params.Mobiles)
	d.Set("boxcar", params.Boxcar)
	d.Set("pushover", params.Pushover)
	d.Set("desktop_alert", params.DesktopAlert)
	if err != nil {
		return fmt.Errorf("Error Updating StatusCake ContactGroup: %s", err.Error())
	}
	return ReadContactGroup(d, meta)
}

func DeleteContactGroup(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*statuscake.Client)

	log.Printf("[DEBUG] Deleting StatusCake ContactGroup: %s", d.Id())
	err := statuscake.NewContactGroups(client).Delete(d.Get("contact_id").(int))

	return err
}

func ReadContactGroup(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*statuscake.Client)

	response, err := statuscake.NewContactGroups(client).Detail(d.Get("contact_id").(int))
	if err != nil {
		return fmt.Errorf("Error Getting StatusCake ContactGroup Details for %s: Error: %s", d.Id(), err)
	}
	d.Set("group_name", response.GroupName)
	d.Set("emails", response.Emails)
	d.Set("contact_id", response.ContactID)
	d.Set("ping_url", response.PingURL)
	d.SetId(strconv.Itoa(response.ContactID))

	return nil
}