2017-06-15 16:46:06 +00:00
|
|
|
package structs
|
2014-01-30 23:35:38 +00:00
|
|
|
|
2014-02-03 23:15:35 +00:00
|
|
|
import (
|
2017-05-15 19:49:13 +00:00
|
|
|
"time"
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2019-12-06 16:14:56 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2016-06-06 20:19:31 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2014-02-03 23:15:35 +00:00
|
|
|
)
|
|
|
|
|
2016-06-07 20:24:51 +00:00
|
|
|
// CheckDefinition is used to JSON decode the Check definitions
|
2014-01-30 23:35:38 +00:00
|
|
|
type CheckDefinition struct {
|
2016-06-06 20:19:31 +00:00
|
|
|
ID types.CheckID
|
2014-02-03 23:15:35 +00:00
|
|
|
Name string
|
|
|
|
Notes string
|
2015-01-14 01:52:17 +00:00
|
|
|
ServiceID string
|
2015-04-28 19:44:46 +00:00
|
|
|
Token string
|
2015-04-12 00:53:48 +00:00
|
|
|
Status string
|
2017-05-15 19:49:13 +00:00
|
|
|
|
|
|
|
// Copied fields from CheckType without the fields
|
|
|
|
// already present in CheckDefinition:
|
|
|
|
//
|
|
|
|
// ID (CheckID), Name, Status, Notes
|
|
|
|
//
|
2017-10-04 23:48:00 +00:00
|
|
|
ScriptArgs []string
|
2017-05-15 19:49:13 +00:00
|
|
|
HTTP string
|
2017-06-06 23:11:56 +00:00
|
|
|
Header map[string][]string
|
|
|
|
Method string
|
2017-05-15 19:49:13 +00:00
|
|
|
TCP string
|
|
|
|
Interval time.Duration
|
|
|
|
DockerContainerID string
|
|
|
|
Shell string
|
2017-12-27 04:35:22 +00:00
|
|
|
GRPC string
|
2018-02-03 01:29:34 +00:00
|
|
|
GRPCUseTLS bool
|
2017-05-15 19:49:13 +00:00
|
|
|
TLSSkipVerify bool
|
2018-06-30 06:09:58 +00:00
|
|
|
AliasNode string
|
|
|
|
AliasService string
|
2017-05-15 19:49:13 +00:00
|
|
|
Timeout time.Duration
|
|
|
|
TTL time.Duration
|
2019-10-14 20:49:49 +00:00
|
|
|
SuccessBeforePassing int
|
|
|
|
FailuresBeforeCritical int
|
2017-05-15 19:49:13 +00:00
|
|
|
DeregisterCriticalServiceAfter time.Duration
|
2019-06-26 15:43:25 +00:00
|
|
|
OutputMaxSize int
|
2014-01-30 23:35:38 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:13:36 +00:00
|
|
|
func (t *CheckDefinition) UnmarshalJSON(data []byte) (err error) {
|
|
|
|
type Alias CheckDefinition
|
|
|
|
aux := &struct {
|
|
|
|
// Parse special values
|
|
|
|
Interval interface{}
|
|
|
|
Timeout interface{}
|
|
|
|
TTL interface{}
|
|
|
|
DeregisterCriticalServiceAfter interface{}
|
|
|
|
|
|
|
|
// Translate fields
|
|
|
|
|
|
|
|
// "args" -> ScriptArgs
|
|
|
|
Args []string `json:"args"`
|
|
|
|
ScriptArgsSnake []string `json:"script_args"`
|
|
|
|
DeregisterCriticalServiceAfterSnake interface{} `json:"deregister_critical_service_after"`
|
|
|
|
DockerContainerIDSnake string `json:"docker_container_id"`
|
|
|
|
TLSSkipVerifySnake bool `json:"tls_skip_verify"`
|
|
|
|
ServiceIDSnake string `json:"service_id"`
|
|
|
|
|
|
|
|
*Alias
|
|
|
|
}{
|
|
|
|
Alias: (*Alias)(t),
|
|
|
|
}
|
2019-12-06 16:14:56 +00:00
|
|
|
if err = lib.UnmarshalJSON(data, &aux); err != nil {
|
2019-10-29 18:13:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translate Fields
|
|
|
|
if aux.DeregisterCriticalServiceAfter == nil {
|
|
|
|
aux.DeregisterCriticalServiceAfter = aux.DeregisterCriticalServiceAfterSnake
|
|
|
|
}
|
|
|
|
if len(t.ScriptArgs) == 0 {
|
|
|
|
t.ScriptArgs = aux.Args
|
|
|
|
}
|
|
|
|
if len(t.ScriptArgs) == 0 {
|
|
|
|
t.ScriptArgs = aux.ScriptArgsSnake
|
|
|
|
}
|
|
|
|
if t.DockerContainerID == "" {
|
|
|
|
t.DockerContainerID = aux.DockerContainerIDSnake
|
|
|
|
}
|
|
|
|
if aux.TLSSkipVerifySnake {
|
|
|
|
t.TLSSkipVerify = aux.TLSSkipVerifySnake
|
|
|
|
}
|
|
|
|
if t.ServiceID == "" {
|
|
|
|
t.ServiceID = aux.ServiceIDSnake
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse special values
|
|
|
|
if aux.Interval != nil {
|
|
|
|
switch v := aux.Interval.(type) {
|
|
|
|
case string:
|
|
|
|
if t.Interval, err = time.ParseDuration(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
t.Interval = time.Duration(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if aux.Timeout != nil {
|
|
|
|
switch v := aux.Timeout.(type) {
|
|
|
|
case string:
|
|
|
|
if t.Timeout, err = time.ParseDuration(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
t.Timeout = time.Duration(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if aux.TTL != nil {
|
|
|
|
switch v := aux.TTL.(type) {
|
|
|
|
case string:
|
|
|
|
if t.TTL, err = time.ParseDuration(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
t.TTL = time.Duration(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if aux.DeregisterCriticalServiceAfter != nil {
|
|
|
|
switch v := aux.DeregisterCriticalServiceAfter.(type) {
|
|
|
|
case string:
|
|
|
|
if t.DeregisterCriticalServiceAfter, err = time.ParseDuration(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
t.DeregisterCriticalServiceAfter = time.Duration(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-15 16:46:06 +00:00
|
|
|
func (c *CheckDefinition) HealthCheck(node string) *HealthCheck {
|
|
|
|
health := &HealthCheck{
|
2015-01-14 01:52:17 +00:00
|
|
|
Node: node,
|
|
|
|
CheckID: c.ID,
|
|
|
|
Name: c.Name,
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthCritical,
|
2015-01-14 01:52:17 +00:00
|
|
|
Notes: c.Notes,
|
|
|
|
ServiceID: c.ServiceID,
|
2014-02-03 23:15:35 +00:00
|
|
|
}
|
2015-04-12 00:53:48 +00:00
|
|
|
if c.Status != "" {
|
|
|
|
health.Status = c.Status
|
|
|
|
}
|
2014-02-03 23:15:35 +00:00
|
|
|
if health.CheckID == "" && health.Name != "" {
|
2016-06-06 20:19:31 +00:00
|
|
|
health.CheckID = types.CheckID(health.Name)
|
2014-02-03 23:15:35 +00:00
|
|
|
}
|
|
|
|
return health
|
2014-01-30 23:35:38 +00:00
|
|
|
}
|
2015-04-28 02:01:02 +00:00
|
|
|
|
2017-05-15 19:49:13 +00:00
|
|
|
func (c *CheckDefinition) CheckType() *CheckType {
|
|
|
|
return &CheckType{
|
2017-06-23 08:15:48 +00:00
|
|
|
CheckID: c.ID,
|
|
|
|
Name: c.Name,
|
|
|
|
Status: c.Status,
|
|
|
|
Notes: c.Notes,
|
|
|
|
|
2018-11-07 10:16:03 +00:00
|
|
|
ScriptArgs: c.ScriptArgs,
|
|
|
|
AliasNode: c.AliasNode,
|
|
|
|
AliasService: c.AliasService,
|
|
|
|
HTTP: c.HTTP,
|
|
|
|
GRPC: c.GRPC,
|
|
|
|
GRPCUseTLS: c.GRPCUseTLS,
|
|
|
|
Header: c.Header,
|
|
|
|
Method: c.Method,
|
2019-06-26 15:43:25 +00:00
|
|
|
OutputMaxSize: c.OutputMaxSize,
|
2018-11-07 10:16:03 +00:00
|
|
|
TCP: c.TCP,
|
|
|
|
Interval: c.Interval,
|
|
|
|
DockerContainerID: c.DockerContainerID,
|
|
|
|
Shell: c.Shell,
|
|
|
|
TLSSkipVerify: c.TLSSkipVerify,
|
|
|
|
Timeout: c.Timeout,
|
|
|
|
TTL: c.TTL,
|
2019-10-14 20:49:49 +00:00
|
|
|
SuccessBeforePassing: c.SuccessBeforePassing,
|
|
|
|
FailuresBeforeCritical: c.FailuresBeforeCritical,
|
2017-05-15 19:49:13 +00:00
|
|
|
DeregisterCriticalServiceAfter: c.DeregisterCriticalServiceAfter,
|
|
|
|
}
|
|
|
|
}
|