github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/circonus/resource_circonus_check_statsd.go (about)

     1  package circonus
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/errwrap"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  const (
    11  	// circonus_check.statsd.* resource attribute names
    12  	checkStatsdSourceIPAttr = "source_ip"
    13  )
    14  
    15  var checkStatsdDescriptions = attrDescrs{
    16  	checkStatsdSourceIPAttr: "The source IP of the statsd metrics stream",
    17  }
    18  
    19  var schemaCheckStatsd = &schema.Schema{
    20  	Type:     schema.TypeSet,
    21  	Optional: true,
    22  	MaxItems: 1,
    23  	MinItems: 1,
    24  	Elem: &schema.Resource{
    25  		Schema: convertToHelperSchema(checkStatsdDescriptions, map[schemaAttr]*schema.Schema{
    26  			checkStatsdSourceIPAttr: &schema.Schema{
    27  				Type:         schema.TypeString,
    28  				Required:     true,
    29  				ValidateFunc: validateRegexp(checkStatsdSourceIPAttr, `.+`),
    30  			},
    31  		}),
    32  	},
    33  }
    34  
    35  // checkAPIToStateStatsd reads the Config data out of circonusCheck.CheckBundle
    36  // into the statefile.
    37  func checkAPIToStateStatsd(c *circonusCheck, d *schema.ResourceData) error {
    38  	statsdConfig := make(map[string]interface{}, len(c.Config))
    39  
    40  	// Unconditionally map the target to the source_ip config attribute
    41  	statsdConfig[string(checkStatsdSourceIPAttr)] = c.Target
    42  
    43  	if err := d.Set(checkStatsdAttr, []interface{}{statsdConfig}); err != nil {
    44  		return errwrap.Wrapf(fmt.Sprintf("Unable to store check %q attribute: {{err}}", checkStatsdAttr), err)
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  func checkConfigToAPIStatsd(c *circonusCheck, l interfaceList) error {
    51  	c.Type = string(apiCheckTypeStatsd)
    52  
    53  	// Iterate over all `statsd` attributes, even though we have a max of 1 in the
    54  	// schema.
    55  	for _, mapRaw := range l {
    56  		statsdConfig := newInterfaceMap(mapRaw)
    57  
    58  		if v, found := statsdConfig[checkStatsdSourceIPAttr]; found {
    59  			switch {
    60  			case c.Target == "":
    61  				c.Target = v.(string)
    62  			case c.Target != v.(string):
    63  				return fmt.Errorf("Target (%q) must match %s (%q)", c.Target, checkStatsdSourceIPAttr, v.(string))
    64  			}
    65  		}
    66  	}
    67  
    68  	return nil
    69  }