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

     1  package circonus
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/circonus-labs/circonus-gometrics/api"
     8  	"github.com/circonus-labs/circonus-gometrics/api/config"
     9  	"github.com/hashicorp/errwrap"
    10  )
    11  
    12  // The circonusCheck type is the backing store of the `circonus_check` resource.
    13  
    14  type circonusCheck struct {
    15  	api.CheckBundle
    16  }
    17  
    18  type circonusCheckType string
    19  
    20  const (
    21  	// CheckBundle.Status can be one of these values
    22  	checkStatusActive   = "active"
    23  	checkStatusDisabled = "disabled"
    24  )
    25  
    26  const (
    27  	apiCheckTypeCAQL       circonusCheckType = "caql"
    28  	apiCheckTypeConsul     circonusCheckType = "consul"
    29  	apiCheckTypeICMPPing   circonusCheckType = "ping_icmp"
    30  	apiCheckTypeHTTP       circonusCheckType = "http"
    31  	apiCheckTypeJSON       circonusCheckType = "json"
    32  	apiCheckTypeMySQL      circonusCheckType = "mysql"
    33  	apiCheckTypeStatsd     circonusCheckType = "statsd"
    34  	apiCheckTypePostgreSQL circonusCheckType = "postgres"
    35  	apiCheckTypeTCP        circonusCheckType = "tcp"
    36  )
    37  
    38  func newCheck() circonusCheck {
    39  	return circonusCheck{
    40  		CheckBundle: *api.NewCheckBundle(),
    41  	}
    42  }
    43  
    44  func loadCheck(ctxt *providerContext, cid api.CIDType) (circonusCheck, error) {
    45  	var c circonusCheck
    46  	cb, err := ctxt.client.FetchCheckBundle(cid)
    47  	if err != nil {
    48  		return circonusCheck{}, err
    49  	}
    50  	c.CheckBundle = *cb
    51  
    52  	return c, nil
    53  }
    54  
    55  func checkAPIStatusToBool(s string) bool {
    56  	var active bool
    57  	switch s {
    58  	case checkStatusActive:
    59  		active = true
    60  	case checkStatusDisabled:
    61  		active = false
    62  	default:
    63  		log.Printf("[ERROR] PROVIDER BUG: check status %q unsupported", s)
    64  	}
    65  
    66  	return active
    67  }
    68  
    69  func checkActiveToAPIStatus(active bool) string {
    70  	if active {
    71  		return checkStatusActive
    72  	}
    73  
    74  	return checkStatusDisabled
    75  }
    76  
    77  func (c *circonusCheck) Create(ctxt *providerContext) error {
    78  	cb, err := ctxt.client.CreateCheckBundle(&c.CheckBundle)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	c.CID = cb.CID
    84  
    85  	return nil
    86  }
    87  
    88  func (c *circonusCheck) Update(ctxt *providerContext) error {
    89  	_, err := ctxt.client.UpdateCheckBundle(&c.CheckBundle)
    90  	if err != nil {
    91  		return errwrap.Wrapf(fmt.Sprintf("Unable to update check bundle %s: {{err}}", c.CID), err)
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  func (c *circonusCheck) Fixup() error {
    98  	switch apiCheckType(c.Type) {
    99  	case apiCheckTypeCloudWatchAttr:
   100  		switch c.Period {
   101  		case 60:
   102  			c.Config[config.Granularity] = "1"
   103  		case 300:
   104  			c.Config[config.Granularity] = "5"
   105  		}
   106  	}
   107  
   108  	return nil
   109  }
   110  
   111  func (c *circonusCheck) Validate() error {
   112  	if len(c.Metrics) == 0 {
   113  		return fmt.Errorf("At least one %s must be specified", checkMetricAttr)
   114  	}
   115  
   116  	if c.Timeout > float32(c.Period) {
   117  		return fmt.Errorf("Timeout (%f) can not exceed period (%d)", c.Timeout, c.Period)
   118  	}
   119  
   120  	// Check-type specific validation
   121  	switch apiCheckType(c.Type) {
   122  	case apiCheckTypeCloudWatchAttr:
   123  		if !(c.Period == 60 || c.Period == 300) {
   124  			return fmt.Errorf("Period must be either 1m or 5m for a %s check", apiCheckTypeCloudWatchAttr)
   125  		}
   126  	case apiCheckTypeConsulAttr:
   127  		if v, found := c.Config[config.URL]; !found || v == "" {
   128  			return fmt.Errorf("%s must have at least one check mode set: %s, %s, or %s must be set", checkConsulAttr, checkConsulServiceAttr, checkConsulNodeAttr, checkConsulStateAttr)
   129  		}
   130  	}
   131  
   132  	return nil
   133  }