-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_pingdom_contact.go
126 lines (117 loc) · 2.64 KB
/
data_source_pingdom_contact.go
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
package pingdom
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/russellcardullo/go-pingdom/pingdom"
)
func dataSourcePingdomContact() *schema.Resource {
return &schema.Resource{
Read: dataSourcePingdomContactRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"paused": {
Type: schema.TypeBool,
Computed: true,
},
"teams": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"sms_notification": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"number": {
Type: schema.TypeString,
Required: true,
},
"country_code": {
Type: schema.TypeString,
Optional: true,
Default: "1",
},
"severity": {
Type: schema.TypeString,
Required: true,
},
"provider": {
Type: schema.TypeString,
Optional: true,
Default: "nexmo",
},
},
},
},
"email_notification": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address": {
Type: schema.TypeString,
Required: true,
},
"severity": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
func dataSourcePingdomContactRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*pingdom.Client)
name := d.Get("name").(string)
contacts, err := client.Contacts.List()
log.Printf("==== contacts : %v", contacts)
if err != nil {
return fmt.Errorf("Error retrieving contact: %s", err)
}
var found *pingdom.Contact
for _, contact := range contacts {
if contact.Name == name {
log.Printf("Contact: %v", contact)
found = &contact
}
}
if found == nil {
return fmt.Errorf("User '%s' not found", name)
}
if err = d.Set("name", found.Name); err != nil {
return fmt.Errorf("Error setting name: %s", err)
}
teams := []map[string]interface{}{}
for _, team := range found.Teams {
teams = append(teams, map[string]interface{}{
"id": team.ID,
"name": team.Name,
})
}
if err = d.Set("teams", teams); err != nil {
return err
}
if err = updateResourceFromContactResponse(d, found); err != nil {
return err
}
d.SetId(fmt.Sprintf("%d", found.ID))
return nil
}