github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/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 apiCheckTypeICMPPing circonusCheckType = "ping_icmp" 29 apiCheckTypeHTTP circonusCheckType = "http" 30 apiCheckTypeJSON circonusCheckType = "json" 31 apiCheckTypeMySQL circonusCheckType = "mysql" 32 apiCheckTypePostgreSQL circonusCheckType = "postgres" 33 apiCheckTypeTCP circonusCheckType = "tcp" 34 ) 35 36 func newCheck() circonusCheck { 37 return circonusCheck{ 38 CheckBundle: *api.NewCheckBundle(), 39 } 40 } 41 42 func loadCheck(ctxt *providerContext, cid api.CIDType) (circonusCheck, error) { 43 var c circonusCheck 44 cb, err := ctxt.client.FetchCheckBundle(cid) 45 if err != nil { 46 return circonusCheck{}, err 47 } 48 c.CheckBundle = *cb 49 50 return c, nil 51 } 52 53 func checkAPIStatusToBool(s string) bool { 54 var active bool 55 switch s { 56 case checkStatusActive: 57 active = true 58 case checkStatusDisabled: 59 active = false 60 default: 61 log.Printf("[ERROR] PROVIDER BUG: check status %q unsupported", s) 62 } 63 64 return active 65 } 66 67 func checkActiveToAPIStatus(active bool) string { 68 if active { 69 return checkStatusActive 70 } 71 72 return checkStatusDisabled 73 } 74 75 func (c *circonusCheck) Create(ctxt *providerContext) error { 76 cb, err := ctxt.client.CreateCheckBundle(&c.CheckBundle) 77 if err != nil { 78 return err 79 } 80 81 c.CID = cb.CID 82 83 return nil 84 } 85 86 func (c *circonusCheck) Update(ctxt *providerContext) error { 87 _, err := ctxt.client.UpdateCheckBundle(&c.CheckBundle) 88 if err != nil { 89 return errwrap.Wrapf(fmt.Sprintf("Unable to update check bundle %s: {{err}}", c.CID), err) 90 } 91 92 return nil 93 } 94 95 func (c *circonusCheck) Fixup() error { 96 switch apiCheckType(c.Type) { 97 case apiCheckTypeCloudWatchAttr: 98 switch c.Period { 99 case 60: 100 c.Config[config.Granularity] = "1" 101 case 300: 102 c.Config[config.Granularity] = "5" 103 } 104 } 105 106 return nil 107 } 108 109 func (c *circonusCheck) Validate() error { 110 if c.Timeout > float32(c.Period) { 111 return fmt.Errorf("Timeout (%f) can not exceed period (%d)", c.Timeout, c.Period) 112 } 113 114 switch apiCheckType(c.Type) { 115 case apiCheckTypeCloudWatchAttr: 116 if !(c.Period == 60 || c.Period == 300) { 117 return fmt.Errorf("Period must be either 1m or 5m for a %s check", apiCheckTypeCloudWatchAttr) 118 } 119 } 120 121 return nil 122 }