forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.go
198 lines (176 loc) · 4.41 KB
/
common.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package ultradns
import (
"fmt"
"log"
"github.com/Ensighten/udnssdk"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)
// Conversion helper functions
type rRSetResource struct {
OwnerName string
RRType string
RData []string
TTL int
Profile udnssdk.RawProfile
Zone string
}
// profileAttrSchemaMap is a map from each ultradns_tcpool attribute name onto its respective ProfileSchema URI
var profileAttrSchemaMap = map[string]udnssdk.ProfileSchema{
"dirpool_profile": udnssdk.DirPoolSchema,
"rdpool_profile": udnssdk.RDPoolSchema,
"sbpool_profile": udnssdk.SBPoolSchema,
"tcpool_profile": udnssdk.TCPoolSchema,
}
func (r rRSetResource) RRSetKey() udnssdk.RRSetKey {
return udnssdk.RRSetKey{
Zone: r.Zone,
Type: r.RRType,
Name: r.OwnerName,
}
}
func (r rRSetResource) RRSet() udnssdk.RRSet {
return udnssdk.RRSet{
OwnerName: r.OwnerName,
RRType: r.RRType,
RData: r.RData,
TTL: r.TTL,
Profile: r.Profile,
}
}
func (r rRSetResource) ID() string {
return fmt.Sprintf("%s.%s", r.OwnerName, r.Zone)
}
func unzipRdataHosts(configured []interface{}) []string {
hs := make([]string, 0, len(configured))
for _, rRaw := range configured {
data := rRaw.(map[string]interface{})
h := data["host"].(string)
hs = append(hs, h)
}
return hs
}
func schemaPingProbe() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"packets": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 3,
},
"packet_size": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 56,
},
"limit": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Set: hashLimits,
Elem: resourceProbeLimits(),
},
},
}
}
func resourceProbeLimits() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"warning": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"critical": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"fail": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
},
}
}
type probeResource struct {
Name string
Zone string
ID string
Agents []string
Interval string
PoolRecord string
Threshold int
Type udnssdk.ProbeType
Details *udnssdk.ProbeDetailsDTO
}
func (p probeResource) RRSetKey() udnssdk.RRSetKey {
return p.Key().RRSetKey()
}
func (p probeResource) ProbeInfoDTO() udnssdk.ProbeInfoDTO {
return udnssdk.ProbeInfoDTO{
ID: p.ID,
PoolRecord: p.PoolRecord,
ProbeType: p.Type,
Interval: p.Interval,
Agents: p.Agents,
Threshold: p.Threshold,
Details: p.Details,
}
}
func (p probeResource) Key() udnssdk.ProbeKey {
return udnssdk.ProbeKey{
Zone: p.Zone,
Name: p.Name,
ID: p.ID,
}
}
func mapFromLimit(name string, l udnssdk.ProbeDetailsLimitDTO) map[string]interface{} {
return map[string]interface{}{
"name": name,
"warning": l.Warning,
"critical": l.Critical,
"fail": l.Fail,
}
}
// hashLimits generates a hashcode for a limits block
func hashLimits(v interface{}) int {
m := v.(map[string]interface{})
h := hashcode.String(m["name"].(string))
log.Printf("[INFO] hashLimits(): %v -> %v", m["name"].(string), h)
return h
}
// makeSetFromLimits encodes an array of Limits into a
// *schema.Set in the appropriate structure for the schema
func makeSetFromLimits(ls map[string]udnssdk.ProbeDetailsLimitDTO) *schema.Set {
s := &schema.Set{F: hashLimits}
for name, l := range ls {
s.Add(mapFromLimit(name, l))
}
return s
}
func makeProbeDetailsLimit(configured interface{}) *udnssdk.ProbeDetailsLimitDTO {
l := configured.(map[string]interface{})
return &udnssdk.ProbeDetailsLimitDTO{
Warning: l["warning"].(int),
Critical: l["critical"].(int),
Fail: l["fail"].(int),
}
}
// makeSetFromStrings encodes an []string into a
// *schema.Set in the appropriate structure for the schema
func makeSetFromStrings(ss []string) *schema.Set {
st := &schema.Set{F: schema.HashString}
for _, s := range ss {
st.Add(s)
}
return st
}
// hashRdata generates a hashcode for an Rdata block
func hashRdatas(v interface{}) int {
m := v.(map[string]interface{})
h := hashcode.String(m["host"].(string))
log.Printf("[DEBUG] hashRdatas(): %v -> %v", m["host"].(string), h)
return h
}