-
Notifications
You must be signed in to change notification settings - Fork 6
/
resource_phonenumber_funcs.go
156 lines (129 loc) · 3.86 KB
/
resource_phonenumber_funcs.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
twilioc "github.com/tulip/twiliogo"
)
func phonenumberCreate(d *schema.ResourceData, meta interface{}) error {
m := meta.(*twilioMeta)
var filters []twilioc.Optional
if v, ok := d.GetOk("location"); ok {
locationFilter := (v.(*schema.Set)).List()[0].(map[string]interface{})
for k, v := range locationFilter {
switch k {
case "near_number":
filters = append(filters, twilioc.NearNumber(v.(string)))
case "near_lat_long":
if (v.(*schema.Set)).Len() != 0 {
latLong := (v.(*schema.Set)).List()[0].(map[string]interface{})
filters = append(filters, twilioc.NearLatLong{
Latitude: latLong["latitude"].(float64),
Longitude: latLong["longitude"].(float64),
})
}
case "distance":
filters = append(filters, twilioc.Distance(v.(int)))
case "postal_code":
filters = append(filters, twilioc.InPostalCode(v.(string)))
case "rate_center":
filters = append(filters, twilioc.InRateCenter(v.(string)))
case "lata":
filters = append(filters, twilioc.InLata(v.(string)))
}
}
}
numbers, err := twilioc.GetLocalAvailablePhoneNumbers(
m.Client,
d.Get("iso_country_code").(string),
filters...,
)
if err != nil {
return err
}
if len(*numbers) == 0 {
return fmt.Errorf("Twilio API returned no numbers matching criteria")
}
numberStruct, err := twilioc.BuyPhoneNumber(
m.Client,
twilioc.PhoneNumber((*numbers)[0].PhoneNumber),
)
if err != nil {
return err
}
d.SetId(numberStruct.Sid)
return phonenumberUpdate(d, meta)
}
func phonenumberRead(d *schema.ResourceData, meta interface{}) error {
m := meta.(*twilioMeta)
numberStruct, err := twilioc.GetIncomingPhoneNumber(m.Client, d.Id())
if err != nil {
return err
}
if numberStruct == nil {
d.SetId("")
return err
}
d.Set("name", numberStruct.FriendlyName)
d.Set("phone_number", numberStruct.PhoneNumber)
return nil
}
func phonenumberUpdate(d *schema.ResourceData, meta interface{}) error {
m := meta.(*twilioMeta)
var voiceCallerIDLookup *bool
incomingNumber := new(twilioc.IncomingPhoneNumber)
incomingNumber.Sid = d.Id()
if d.HasChange("name") {
incomingNumber.FriendlyName = d.Get("name").(string)
}
if d.HasChange("api_version") {
incomingNumber.ApiVersion = d.Get("api_version").(string)
}
if d.HasChange("voice_caller_id_lookup") {
voiceCallerIDLookup = new(bool)
*voiceCallerIDLookup = d.Get("voice_caller_id_lookup").(bool)
}
if d.HasChange("voice_url") {
incomingNumber.VoiceUrl = d.Get("voice_url").(string)
}
if d.HasChange("voice_method") {
incomingNumber.VoiceMethod = d.Get("voice_method").(string)
}
if d.HasChange("voice_fallback_url") {
incomingNumber.VoiceFallbackUrl = d.Get("voice_fallback_url").(string)
}
if d.HasChange("voice_fallback_method") {
incomingNumber.VoiceFallbackMethod = d.Get("voice_fallback_method").(string)
}
if d.HasChange("sms_url") {
incomingNumber.SmsUrl = d.Get("sms_url").(string)
}
if d.HasChange("sms_method") {
incomingNumber.SmsMethod = d.Get("sms_method").(string)
}
if d.HasChange("sms_fallback_url") {
incomingNumber.SmsFallbackUrl = d.Get("sms_fallback_url").(string)
}
if d.HasChange("sms_fallback_method") {
incomingNumber.SmsFallbackMethod = d.Get("sms_fallback_method").(string)
}
if d.HasChange("status_callback") {
incomingNumber.StatusCallback = d.Get("status_callback").(string)
}
if d.HasChange("status_callback_method") {
incomingNumber.StatusCallbackMethod = d.Get("status_callback_method").(string)
}
_, err := twilioc.UpdatePhoneNumberFields(m.Client, incomingNumber, voiceCallerIDLookup)
if err != nil {
return err
}
return phonenumberRead(d, meta)
}
func phonenumberDelete(d *schema.ResourceData, meta interface{}) error {
m := meta.(*twilioMeta)
err := twilioc.ReleasePhoneNumber(m.Client, d.Id())
if err != nil {
return err
}
d.SetId("")
return nil
}