github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/circonus/resource_circonus_check_mysql.go (about) 1 package circonus 2 3 import ( 4 "bytes" 5 "fmt" 6 "strings" 7 8 "github.com/circonus-labs/circonus-gometrics/api/config" 9 "github.com/hashicorp/errwrap" 10 "github.com/hashicorp/terraform/helper/hashcode" 11 "github.com/hashicorp/terraform/helper/schema" 12 ) 13 14 const ( 15 // circonus_check.mysql.* resource attribute names 16 checkMySQLDSNAttr = "dsn" 17 checkMySQLQueryAttr = "query" 18 ) 19 20 var checkMySQLDescriptions = attrDescrs{ 21 checkMySQLDSNAttr: "The connect DSN for the MySQL instance", 22 checkMySQLQueryAttr: "The SQL to use as the query", 23 } 24 25 var schemaCheckMySQL = &schema.Schema{ 26 Type: schema.TypeSet, 27 Optional: true, 28 MaxItems: 1, 29 MinItems: 1, 30 Set: hashCheckMySQL, 31 Elem: &schema.Resource{ 32 Schema: convertToHelperSchema(checkMySQLDescriptions, map[schemaAttr]*schema.Schema{ 33 checkMySQLDSNAttr: &schema.Schema{ 34 Type: schema.TypeString, 35 Required: true, 36 ValidateFunc: validateRegexp(checkMySQLDSNAttr, `^.+$`), 37 }, 38 checkMySQLQueryAttr: &schema.Schema{ 39 Type: schema.TypeString, 40 Required: true, 41 StateFunc: func(v interface{}) string { return strings.TrimSpace(v.(string)) }, 42 ValidateFunc: validateRegexp(checkMySQLQueryAttr, `.+`), 43 }, 44 }), 45 }, 46 } 47 48 // checkAPIToStateMySQL reads the Config data out of circonusCheck.CheckBundle into the 49 // statefile. 50 func checkAPIToStateMySQL(c *circonusCheck, d *schema.ResourceData) error { 51 MySQLConfig := make(map[string]interface{}, len(c.Config)) 52 53 MySQLConfig[string(checkMySQLDSNAttr)] = c.Config[config.DSN] 54 MySQLConfig[string(checkMySQLQueryAttr)] = c.Config[config.SQL] 55 56 if err := d.Set(checkMySQLAttr, schema.NewSet(hashCheckMySQL, []interface{}{MySQLConfig})); err != nil { 57 return errwrap.Wrapf(fmt.Sprintf("Unable to store check %q attribute: {{err}}", checkMySQLAttr), err) 58 } 59 60 return nil 61 } 62 63 // hashCheckMySQL creates a stable hash of the normalized values 64 func hashCheckMySQL(v interface{}) int { 65 m := v.(map[string]interface{}) 66 b := &bytes.Buffer{} 67 b.Grow(defaultHashBufSize) 68 69 writeString := func(attrName schemaAttr) { 70 if v, ok := m[string(attrName)]; ok && v.(string) != "" { 71 fmt.Fprint(b, strings.TrimSpace(v.(string))) 72 } 73 } 74 75 // Order writes to the buffer using lexically sorted list for easy visual 76 // reconciliation with other lists. 77 writeString(checkMySQLDSNAttr) 78 writeString(checkMySQLQueryAttr) 79 80 s := b.String() 81 return hashcode.String(s) 82 } 83 84 func checkConfigToAPIMySQL(c *circonusCheck, l interfaceList) error { 85 c.Type = string(apiCheckTypeMySQL) 86 87 // Iterate over all `mysql` attributes, even though we have a max of 1 in the 88 // schema. 89 for _, mapRaw := range l { 90 mysqlConfig := newInterfaceMap(mapRaw) 91 92 if v, found := mysqlConfig[checkMySQLDSNAttr]; found { 93 c.Config[config.DSN] = v.(string) 94 } 95 96 if v, found := mysqlConfig[checkMySQLQueryAttr]; found { 97 c.Config[config.SQL] = v.(string) 98 } 99 } 100 101 return nil 102 }