Skip to content

Commit

Permalink
Add consul check type to circonus_check resource (#13030)
Browse files Browse the repository at this point in the history
* Add the `consul` check type to the `circonus_check` resource.

* Fix a tab-complete fail.

`Parse` != `Path`, but lexically close.

* Dept of 2nd thoughts: `s/service_name/service/g`
  • Loading branch information
sean- authored Mar 24, 2017
1 parent c5935ab commit 32db4d1
Show file tree
Hide file tree
Showing 9 changed files with 886 additions and 4 deletions.
10 changes: 10 additions & 0 deletions builtin/providers/circonus/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (

const (
apiCheckTypeCAQL circonusCheckType = "caql"
apiCheckTypeConsul circonusCheckType = "consul"
apiCheckTypeICMPPing circonusCheckType = "ping_icmp"
apiCheckTypeHTTP circonusCheckType = "http"
apiCheckTypeJSON circonusCheckType = "json"
Expand Down Expand Up @@ -108,15 +109,24 @@ func (c *circonusCheck) Fixup() error {
}

func (c *circonusCheck) Validate() error {
if len(c.Metrics) == 0 {
return fmt.Errorf("At least one %s must be specified", checkMetricAttr)
}

if c.Timeout > float32(c.Period) {
return fmt.Errorf("Timeout (%f) can not exceed period (%d)", c.Timeout, c.Period)
}

// Check-type specific validation
switch apiCheckType(c.Type) {
case apiCheckTypeCloudWatchAttr:
if !(c.Period == 60 || c.Period == 300) {
return fmt.Errorf("Period must be either 1m or 5m for a %s check", apiCheckTypeCloudWatchAttr)
}
case apiCheckTypeConsulAttr:
if v, found := c.Config[config.URL]; !found || v == "" {
return fmt.Errorf("%s must have at least one check mode set: %s, %s, or %s must be set", checkConsulAttr, checkConsulServiceAttr, checkConsulNodeAttr, checkConsulStateAttr)
}
}

return nil
Expand Down
13 changes: 13 additions & 0 deletions builtin/providers/circonus/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ const (
providerAutoTagAttr = "auto_tag"
providerKeyAttr = "key"

apiConsulCheckBlacklist = "check_name_blacklist"
apiConsulDatacenterAttr = "dc"
apiConsulNodeBlacklist = "node_blacklist"
apiConsulServiceBlacklist = "service_blacklist"
apiConsulStaleAttr = "stale"
checkConsulTokenHeader = `X-Consul-Token`
checkConsulV1NodePrefix = "node"
checkConsulV1Prefix = "/v1/health"
checkConsulV1ServicePrefix = "service"
checkConsulV1StatePrefix = "state"
defaultCheckConsulHTTPAddr = "http://consul.service.consul"
defaultCheckConsulPort = "8500"

defaultCheckJSONMethod = "GET"
defaultCheckJSONPort = "443"
defaultCheckJSONVersion = "1.1"
Expand Down
23 changes: 19 additions & 4 deletions builtin/providers/circonus/resource_circonus_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,22 @@ const (
checkCAQLAttr = "caql"
checkCloudWatchAttr = "cloudwatch"
checkCollectorAttr = "collector"
checkConsulAttr = "consul"
checkHTTPAttr = "http"
checkHTTPTrapAttr = "httptrap"
checkICMPPingAttr = "icmp_ping"
checkJSONAttr = "json"
checkMetricAttr = "metric"
checkMetricLimitAttr = "metric_limit"
checkMySQLAttr = "mysql"
checkNameAttr = "name"
checkNotesAttr = "notes"
checkPeriodAttr = "period"
checkPostgreSQLAttr = "postgresql"
checkMetricAttr = "metric"
checkStatsdAttr = "statsd"
checkTCPAttr = "tcp"
checkTagsAttr = "tags"
checkTargetAttr = "target"
checkTCPAttr = "tcp"
checkTimeoutAttr = "timeout"
checkTypeAttr = "type"

Expand Down Expand Up @@ -75,6 +76,7 @@ const (
// Circonus API constants from their API endpoints
apiCheckTypeCAQLAttr apiCheckType = "caql"
apiCheckTypeCloudWatchAttr apiCheckType = "cloudwatch"
apiCheckTypeConsulAttr apiCheckType = "consul"
apiCheckTypeHTTPAttr apiCheckType = "http"
apiCheckTypeHTTPTrapAttr apiCheckType = "httptrap"
apiCheckTypeICMPPingAttr apiCheckType = "ping_icmp"
Expand All @@ -90,6 +92,7 @@ var checkDescriptions = attrDescrs{
checkCAQLAttr: "CAQL check configuration",
checkCloudWatchAttr: "CloudWatch check configuration",
checkCollectorAttr: "The collector(s) that are responsible for gathering the metrics",
checkConsulAttr: "Consul check configuration",
checkHTTPAttr: "HTTP check configuration",
checkHTTPTrapAttr: "HTTP Trap check configuration",
checkICMPPingAttr: "ICMP ping check configuration",
Expand Down Expand Up @@ -157,6 +160,7 @@ func resourceCheck() *schema.Resource {
}),
},
},
checkConsulAttr: schemaCheckConsul,
checkHTTPAttr: schemaCheckHTTP,
checkHTTPTrapAttr: schemaCheckHTTPTrap,
checkJSONAttr: schemaCheckJSON,
Expand Down Expand Up @@ -577,6 +581,7 @@ func checkConfigToAPI(c *circonusCheck, d *schema.ResourceData) error {
checkTypeParseMap := map[string]func(*circonusCheck, interfaceList) error{
checkCAQLAttr: checkConfigToAPICAQL,
checkCloudWatchAttr: checkConfigToAPICloudWatch,
checkConsulAttr: checkConfigToAPIConsul,
checkHTTPAttr: checkConfigToAPIHTTP,
checkHTTPTrapAttr: checkConfigToAPIHTTPTrap,
checkICMPPingAttr: checkConfigToAPIICMPPing,
Expand All @@ -589,8 +594,17 @@ func checkConfigToAPI(c *circonusCheck, d *schema.ResourceData) error {

for checkType, fn := range checkTypeParseMap {
if listRaw, found := d.GetOk(checkType); found {
if err := fn(c, listRaw.(*schema.Set).List()); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Unable to parse type %q: {{err}}", string(checkType)), err)
switch u := listRaw.(type) {
case []interface{}:
if err := fn(c, u); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Unable to parse type %q: {{err}}", string(checkType)), err)
}
case *schema.Set:
if err := fn(c, u.List()); err != nil {
return errwrap.Wrapf(fmt.Sprintf("Unable to parse type %q: {{err}}", string(checkType)), err)
}
default:
return fmt.Errorf("PROVIDER BUG: unsupported check type interface: %q", checkType)
}
}
}
Expand All @@ -604,6 +618,7 @@ func parseCheckTypeConfig(c *circonusCheck, d *schema.ResourceData) error {
checkTypeConfigHandlers := map[apiCheckType]func(*circonusCheck, *schema.ResourceData) error{
apiCheckTypeCAQLAttr: checkAPIToStateCAQL,
apiCheckTypeCloudWatchAttr: checkAPIToStateCloudWatch,
apiCheckTypeConsulAttr: checkAPIToStateConsul,
apiCheckTypeHTTPAttr: checkAPIToStateHTTP,
apiCheckTypeHTTPTrapAttr: checkAPIToStateHTTPTrap,
apiCheckTypeICMPPingAttr: checkAPIToStateICMPPing,
Expand Down
Loading

0 comments on commit 32db4d1

Please sign in to comment.