github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/circonus/resource_circonus_check_caql.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.caql.* resource attribute names
    16  	checkCAQLQueryAttr = "query"
    17  )
    18  
    19  var checkCAQLDescriptions = attrDescrs{
    20  	checkCAQLQueryAttr: "The query definition",
    21  }
    22  
    23  var schemaCheckCAQL = &schema.Schema{
    24  	Type:     schema.TypeSet,
    25  	Optional: true,
    26  	MaxItems: 1,
    27  	MinItems: 1,
    28  	Set:      hashCheckCAQL,
    29  	Elem: &schema.Resource{
    30  		Schema: convertToHelperSchema(checkCAQLDescriptions, map[schemaAttr]*schema.Schema{
    31  			checkCAQLQueryAttr: &schema.Schema{
    32  				Type:         schema.TypeString,
    33  				Required:     true,
    34  				ValidateFunc: validateRegexp(checkCAQLQueryAttr, `.+`),
    35  			},
    36  		}),
    37  	},
    38  }
    39  
    40  // checkAPIToStateCAQL reads the Config data out of circonusCheck.CheckBundle
    41  // into the statefile.
    42  func checkAPIToStateCAQL(c *circonusCheck, d *schema.ResourceData) error {
    43  	caqlConfig := make(map[string]interface{}, len(c.Config))
    44  
    45  	caqlConfig[string(checkCAQLQueryAttr)] = c.Config[config.Query]
    46  
    47  	if err := d.Set(checkCAQLAttr, schema.NewSet(hashCheckCAQL, []interface{}{caqlConfig})); err != nil {
    48  		return errwrap.Wrapf(fmt.Sprintf("Unable to store check %q attribute: {{err}}", checkCAQLAttr), err)
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  // hashCheckCAQL creates a stable hash of the normalized values
    55  func hashCheckCAQL(v interface{}) int {
    56  	m := v.(map[string]interface{})
    57  	b := &bytes.Buffer{}
    58  	b.Grow(defaultHashBufSize)
    59  
    60  	writeString := func(attrName schemaAttr) {
    61  		if v, ok := m[string(attrName)]; ok && v.(string) != "" {
    62  			fmt.Fprint(b, strings.TrimSpace(v.(string)))
    63  		}
    64  	}
    65  
    66  	// Order writes to the buffer using lexically sorted list for easy visual
    67  	// reconciliation with other lists.
    68  	writeString(checkCAQLQueryAttr)
    69  
    70  	s := b.String()
    71  	return hashcode.String(s)
    72  }
    73  
    74  func checkConfigToAPICAQL(c *circonusCheck, l interfaceList) error {
    75  	c.Type = string(apiCheckTypeCAQL)
    76  	c.Target = defaultCheckCAQLTarget
    77  
    78  	// Iterate over all `caql` attributes, even though we have a max of 1 in the
    79  	// schema.
    80  	for _, mapRaw := range l {
    81  		caqlConfig := newInterfaceMap(mapRaw)
    82  
    83  		if v, found := caqlConfig[checkCAQLQueryAttr]; found {
    84  			c.Config[config.Query] = v.(string)
    85  		}
    86  	}
    87  
    88  	return nil
    89  }