terraform/builtin/providers/ultradns/common.go

199 lines
4.4 KiB
Go
Raw Normal View History

ultradns providers and improvements (#9788) * vendor: update github.com/Ensighten/udnssdk to v1.2.1 * ultradns_tcpool: add * ultradns.baseurl: set default * ultradns.record: cleanup test * ultradns_record: extract common, cleanup * ultradns: extract common * ultradns_dirpool: add * ultradns_dirpool: fix rdata.ip_info.ips to be idempotent * ultradns_tcpool: add doc * ultradns_dirpool: fix rdata.geo_codes.codes to be idempotent * ultradns_dirpool: add doc * ultradns: cleanup testing * ultradns_record: rename resource * ultradns: log username from config, not client udnssdk.Client is being refactored to use x/oauth2, so don't assume we can access Username from it * ultradns_probe_ping: add * ultradns_probe_http: add * doc: add ultradns_probe_ping * doc: add ultradns_probe_http * ultradns_record: remove duplication from error messages * doc: cleanup typos in ultradns * ultradns_probe_ping: add test for pool-level probe * Clean documentation * ultradns: pull makeSetFromStrings() up to common.go * ultradns_dirpool: log hashIPInfoIPs Log the key and generated hashcode used to index ip_info.ips into a set. * ultradns: simplify hashLimits() Limits blocks only have the "name" attribute as their primary key, so hashLimits() needn't use a buffer to concatenate. Also changes log level to a more approriate DEBUG. * ultradns_tcpool: convert rdata to schema.Set RData blocks have the "host" attribute as their primary key, so it is used by hashRdatas() to create the hashcode. Tests are updated to use the new hashcode indexes instead of natural numbers. * ultradns_probe_http: convert agents to schema.Set Also pull the makeSetFromStrings() helper up to common.go * ultradns: pull hashRdatas() up to common * ultradns_dirpool: convert rdata to schema.Set Fixes TF-66 * ultradns_dirpool.conflict_resolve: fix default from response UltraDNS REST API User Guide claims that "Directional Pool Profile Fields" have a "conflictResolve" field which "If not specified, defaults to GEO." https://portal.ultradns.com/static/docs/REST-API_User_Guide.pdf But UltraDNS does not actually return a conflictResolve attribute when it has been updated to "GEO". We could fix it in udnssdk, but that would require either: * hide the response by coercing "" to "GEO" for everyone * use a pointer to allow checking for nil (requires all users to change if they fix this) An ideal solution would be to have the UltraDNS API respond with this attribute for every dirpool's rdata. So at the risk of foolish consistency in the sdk, we're going to solve it where it's visible to the user: by checking and overriding the parsing. I'm sorry. * ultradns_record: convert rdata to set UltraDNS does not store the ordering of rdata elements, so we need a way to identify if changes have been made even it the order changes. A perfect job for schema.Set. * ultradns_record: parse double-encoded answers for TXT records * ultradns: simplify hashLimits() Limits blocks only have the "name" attribute as their primary key, so hashLimits() needn't use a buffer to concatenate. * ultradns_dirpool.description: validate * ultradns_dirpool.rdata: doc need for set * ultradns_dirpool.conflict_resolve: validate
2016-12-15 17:28:34 +01:00
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
}