github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/circonus/utils.go (about) 1 package circonus 2 3 import ( 4 "fmt" 5 "log" 6 "regexp" 7 "strconv" 8 "strings" 9 "time" 10 11 "github.com/circonus-labs/circonus-gometrics/api" 12 "github.com/circonus-labs/circonus-gometrics/api/config" 13 "github.com/hashicorp/errwrap" 14 "github.com/hashicorp/terraform/helper/schema" 15 ) 16 17 // convertToHelperSchema converts the schema and injects the necessary 18 // parameters, notably the descriptions, in order to be valid input to 19 // Terraform's helper schema. 20 func convertToHelperSchema(descrs attrDescrs, in map[schemaAttr]*schema.Schema) map[string]*schema.Schema { 21 out := make(map[string]*schema.Schema, len(in)) 22 for k, v := range in { 23 if descr, ok := descrs[k]; ok { 24 // NOTE(sean@): At some point this check needs to be uncommented and all 25 // empty descriptions need to be populated. 26 // 27 // if len(descr) == 0 { 28 // log.Printf("[WARN] PROVIDER BUG: Description of attribute %s empty", k) 29 // } 30 31 v.Description = string(descr) 32 } else { 33 log.Printf("[WARN] PROVIDER BUG: Unable to find description for attr %q", k) 34 } 35 36 out[string(k)] = v 37 } 38 39 return out 40 } 41 42 func failoverGroupIDToCID(groupID int) string { 43 if groupID == 0 { 44 return "" 45 } 46 47 return fmt.Sprintf("%s/%d", config.ContactGroupPrefix, groupID) 48 } 49 50 func failoverGroupCIDToID(cid api.CIDType) (int, error) { 51 re := regexp.MustCompile("^" + config.ContactGroupPrefix + "/(" + config.DefaultCIDRegex + ")$") 52 matches := re.FindStringSubmatch(string(*cid)) 53 if matches == nil || len(matches) < 2 { 54 return -1, fmt.Errorf("Did not find a valid contact_group ID in the CID %q", string(*cid)) 55 } 56 57 contactGroupID, err := strconv.Atoi(matches[1]) 58 if err != nil { 59 return -1, errwrap.Wrapf(fmt.Sprintf("invalid contact_group ID: unable to find an ID in %q: {{error}}", string(*cid)), err) 60 } 61 62 return contactGroupID, nil 63 } 64 65 // flattenList returns a list of all string values to a []*string. 66 func flattenList(l []interface{}) []*string { 67 vals := make([]*string, 0, len(l)) 68 for _, v := range l { 69 val, ok := v.(string) 70 if ok && val != "" { 71 vals = append(vals, &val) 72 } 73 } 74 return vals 75 } 76 77 // flattenSet flattens the values in a schema.Set and returns a []*string 78 func flattenSet(s *schema.Set) []*string { 79 return flattenList(s.List()) 80 } 81 82 func derefStringList(lp []*string) []string { 83 l := make([]string, 0, len(lp)) 84 for _, sp := range lp { 85 if sp != nil { 86 l = append(l, *sp) 87 } 88 } 89 return l 90 } 91 92 // listToSet returns a TypeSet from the given list. 93 func stringListToSet(stringList []string, keyName schemaAttr) []interface{} { 94 m := make([]interface{}, 0, len(stringList)) 95 for _, v := range stringList { 96 s := make(map[string]interface{}, 1) 97 s[string(keyName)] = v 98 m = append(m, s) 99 } 100 101 return m 102 } 103 104 func normalizeTimeDurationStringToSeconds(v interface{}) string { 105 switch v.(type) { 106 case string: 107 d, err := time.ParseDuration(v.(string)) 108 if err != nil { 109 return fmt.Sprintf("<unable to normalize time duration %s: %v>", v.(string), err) 110 } 111 112 return fmt.Sprintf("%ds", int(d.Seconds())) 113 default: 114 return fmt.Sprintf("<unable to normalize duration on %#v>", v) 115 } 116 } 117 118 func indirect(v interface{}) interface{} { 119 switch v.(type) { 120 case string: 121 return v 122 case *string: 123 p := v.(*string) 124 if p == nil { 125 return nil 126 } 127 return *p 128 default: 129 return v 130 } 131 } 132 133 func suppressEquivalentTimeDurations(k, old, new string, d *schema.ResourceData) bool { 134 d1, err := time.ParseDuration(old) 135 if err != nil { 136 return false 137 } 138 139 d2, err := time.ParseDuration(new) 140 if err != nil { 141 return false 142 } 143 144 return d1 == d2 145 } 146 147 func suppressWhitespace(v interface{}) string { 148 return strings.TrimSpace(v.(string)) 149 }