github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/ultradns/common.go (about) 1 package ultradns 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/Ensighten/udnssdk" 8 "github.com/hashicorp/terraform/helper/hashcode" 9 "github.com/hashicorp/terraform/helper/schema" 10 ) 11 12 // Conversion helper functions 13 type rRSetResource struct { 14 OwnerName string 15 RRType string 16 RData []string 17 TTL int 18 Profile udnssdk.RawProfile 19 Zone string 20 } 21 22 // profileAttrSchemaMap is a map from each ultradns_tcpool attribute name onto its respective ProfileSchema URI 23 var profileAttrSchemaMap = map[string]udnssdk.ProfileSchema{ 24 "dirpool_profile": udnssdk.DirPoolSchema, 25 "rdpool_profile": udnssdk.RDPoolSchema, 26 "sbpool_profile": udnssdk.SBPoolSchema, 27 "tcpool_profile": udnssdk.TCPoolSchema, 28 } 29 30 func (r rRSetResource) RRSetKey() udnssdk.RRSetKey { 31 return udnssdk.RRSetKey{ 32 Zone: r.Zone, 33 Type: r.RRType, 34 Name: r.OwnerName, 35 } 36 } 37 38 func (r rRSetResource) RRSet() udnssdk.RRSet { 39 return udnssdk.RRSet{ 40 OwnerName: r.OwnerName, 41 RRType: r.RRType, 42 RData: r.RData, 43 TTL: r.TTL, 44 Profile: r.Profile, 45 } 46 } 47 48 func (r rRSetResource) ID() string { 49 return fmt.Sprintf("%s.%s", r.OwnerName, r.Zone) 50 } 51 52 func unzipRdataHosts(configured []interface{}) []string { 53 hs := make([]string, 0, len(configured)) 54 for _, rRaw := range configured { 55 data := rRaw.(map[string]interface{}) 56 h := data["host"].(string) 57 hs = append(hs, h) 58 } 59 return hs 60 } 61 62 func schemaPingProbe() *schema.Resource { 63 return &schema.Resource{ 64 Schema: map[string]*schema.Schema{ 65 "packets": &schema.Schema{ 66 Type: schema.TypeInt, 67 Optional: true, 68 Default: 3, 69 }, 70 "packet_size": &schema.Schema{ 71 Type: schema.TypeInt, 72 Optional: true, 73 Default: 56, 74 }, 75 "limit": &schema.Schema{ 76 Type: schema.TypeSet, 77 Optional: true, 78 Set: hashLimits, 79 Elem: resourceProbeLimits(), 80 }, 81 }, 82 } 83 } 84 85 func resourceProbeLimits() *schema.Resource { 86 return &schema.Resource{ 87 Schema: map[string]*schema.Schema{ 88 "name": &schema.Schema{ 89 Type: schema.TypeString, 90 Required: true, 91 }, 92 "warning": &schema.Schema{ 93 Type: schema.TypeInt, 94 Required: true, 95 }, 96 "critical": &schema.Schema{ 97 Type: schema.TypeInt, 98 Required: true, 99 }, 100 "fail": &schema.Schema{ 101 Type: schema.TypeInt, 102 Required: true, 103 }, 104 }, 105 } 106 } 107 108 type probeResource struct { 109 Name string 110 Zone string 111 ID string 112 113 Agents []string 114 Interval string 115 PoolRecord string 116 Threshold int 117 Type udnssdk.ProbeType 118 119 Details *udnssdk.ProbeDetailsDTO 120 } 121 122 func (p probeResource) RRSetKey() udnssdk.RRSetKey { 123 return p.Key().RRSetKey() 124 } 125 126 func (p probeResource) ProbeInfoDTO() udnssdk.ProbeInfoDTO { 127 return udnssdk.ProbeInfoDTO{ 128 ID: p.ID, 129 PoolRecord: p.PoolRecord, 130 ProbeType: p.Type, 131 Interval: p.Interval, 132 Agents: p.Agents, 133 Threshold: p.Threshold, 134 Details: p.Details, 135 } 136 } 137 138 func (p probeResource) Key() udnssdk.ProbeKey { 139 return udnssdk.ProbeKey{ 140 Zone: p.Zone, 141 Name: p.Name, 142 ID: p.ID, 143 } 144 } 145 146 func mapFromLimit(name string, l udnssdk.ProbeDetailsLimitDTO) map[string]interface{} { 147 return map[string]interface{}{ 148 "name": name, 149 "warning": l.Warning, 150 "critical": l.Critical, 151 "fail": l.Fail, 152 } 153 } 154 155 // hashLimits generates a hashcode for a limits block 156 func hashLimits(v interface{}) int { 157 m := v.(map[string]interface{}) 158 h := hashcode.String(m["name"].(string)) 159 log.Printf("[INFO] hashLimits(): %v -> %v", m["name"].(string), h) 160 return h 161 } 162 163 // makeSetFromLimits encodes an array of Limits into a 164 // *schema.Set in the appropriate structure for the schema 165 func makeSetFromLimits(ls map[string]udnssdk.ProbeDetailsLimitDTO) *schema.Set { 166 s := &schema.Set{F: hashLimits} 167 for name, l := range ls { 168 s.Add(mapFromLimit(name, l)) 169 } 170 return s 171 } 172 173 func makeProbeDetailsLimit(configured interface{}) *udnssdk.ProbeDetailsLimitDTO { 174 l := configured.(map[string]interface{}) 175 return &udnssdk.ProbeDetailsLimitDTO{ 176 Warning: l["warning"].(int), 177 Critical: l["critical"].(int), 178 Fail: l["fail"].(int), 179 } 180 } 181 182 // makeSetFromStrings encodes an []string into a 183 // *schema.Set in the appropriate structure for the schema 184 func makeSetFromStrings(ss []string) *schema.Set { 185 st := &schema.Set{F: schema.HashString} 186 for _, s := range ss { 187 st.Add(s) 188 } 189 return st 190 } 191 192 // hashRdata generates a hashcode for an Rdata block 193 func hashRdatas(v interface{}) int { 194 m := v.(map[string]interface{}) 195 h := hashcode.String(m["host"].(string)) 196 log.Printf("[DEBUG] hashRdatas(): %v -> %v", m["host"].(string), h) 197 return h 198 }