feat: Implement push-based external endpoints (#724)
* refactor: Move SSH outside of endpoint.go * refactor: Use pointers for Alert receivers * feat: Implement push-based external endpoints * Fix failing tests * Validate external endpoints on start * Add tests for external endpoints * refactor some error equality checks * Improve docs and refactor some code * Fix UI-related issues with external endpoints
This commit is contained in:
@ -103,26 +103,24 @@ func TestIntegrationQuery(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEndpoint_ValidateAndSetDefaultsWithNoDNSQueryName(t *testing.T) {
|
||||
defer func() { recover() }()
|
||||
func TestDNS_validateAndSetDefault(t *testing.T) {
|
||||
dns := &DNS{
|
||||
QueryType: "A",
|
||||
QueryName: "",
|
||||
}
|
||||
err := dns.validateAndSetDefault()
|
||||
if err == nil {
|
||||
t.Fatal("Should've returned an error because endpoint's dns didn't have a query name, which is a mandatory field for dns")
|
||||
t.Error("Should've returned an error because endpoint's dns didn't have a query name, which is a mandatory field for dns")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEndpoint_ValidateAndSetDefaultsWithInvalidDNSQueryType(t *testing.T) {
|
||||
defer func() { recover() }()
|
||||
dns := &DNS{
|
||||
QueryType: "B",
|
||||
QueryName: "example.com",
|
||||
}
|
||||
err := dns.validateAndSetDefault()
|
||||
if err == nil {
|
||||
t.Fatal("Should've returned an error because endpoint's dns query type is invalid, it needs to be a valid query name like A, AAAA, CNAME...")
|
||||
t.Error("Should've returned an error because endpoint's dns query type is invalid, it needs to be a valid query name like A, AAAA, CNAME...")
|
||||
}
|
||||
}
|
||||
|
@ -55,12 +55,6 @@ var (
|
||||
// ErrEndpointWithNoURL is the error with which Gatus will panic if an endpoint is configured with no url
|
||||
ErrEndpointWithNoURL = errors.New("you must specify an url for each endpoint")
|
||||
|
||||
// ErrEndpointWithNoName is the error with which Gatus will panic if an endpoint is configured with no name
|
||||
ErrEndpointWithNoName = errors.New("you must specify a name for each endpoint")
|
||||
|
||||
// ErrEndpointWithInvalidNameOrGroup is the error with which Gatus will panic if an endpoint has an invalid character where it shouldn't
|
||||
ErrEndpointWithInvalidNameOrGroup = errors.New("endpoint name and group must not have \" or \\")
|
||||
|
||||
// ErrUnknownEndpointType is the error with which Gatus will panic if an endpoint has an unknown type
|
||||
ErrUnknownEndpointType = errors.New("unknown endpoint type")
|
||||
|
||||
@ -72,13 +66,9 @@ var (
|
||||
// This is because the free whois service we are using should not be abused, especially considering the fact that
|
||||
// the data takes a while to be updated.
|
||||
ErrInvalidEndpointIntervalForDomainExpirationPlaceholder = errors.New("the minimum interval for an endpoint with a condition using the " + DomainExpirationPlaceholder + " placeholder is 300s (5m)")
|
||||
// ErrEndpointWithoutSSHUsername is the error with which Gatus will panic if an endpoint with SSH monitoring is configured without a user.
|
||||
ErrEndpointWithoutSSHUsername = errors.New("you must specify a username for each endpoint with SSH")
|
||||
// ErrEndpointWithoutSSHPassword is the error with which Gatus will panic if an endpoint with SSH monitoring is configured without a password.
|
||||
ErrEndpointWithoutSSHPassword = errors.New("you must specify a password for each endpoint with SSH")
|
||||
)
|
||||
|
||||
// Endpoint is the configuration of a monitored
|
||||
// Endpoint is the configuration of a service to be monitored
|
||||
type Endpoint struct {
|
||||
// Enabled defines whether to enable the monitoring of the endpoint
|
||||
Enabled *bool `yaml:"enabled,omitempty"`
|
||||
@ -95,6 +85,9 @@ type Endpoint struct {
|
||||
// DNS is the configuration of DNS monitoring
|
||||
DNS *DNS `yaml:"dns,omitempty"`
|
||||
|
||||
// SSH is the configuration of SSH monitoring.
|
||||
SSH *SSH `yaml:"ssh,omitempty"`
|
||||
|
||||
// Method of the request made to the url of the endpoint
|
||||
Method string `yaml:"method,omitempty"`
|
||||
|
||||
@ -127,27 +120,6 @@ type Endpoint struct {
|
||||
|
||||
// NumberOfSuccessesInARow is the number of successful evaluations in a row
|
||||
NumberOfSuccessesInARow int `yaml:"-"`
|
||||
|
||||
// SSH is the configuration of SSH monitoring.
|
||||
SSH *SSH `yaml:"ssh,omitempty"`
|
||||
}
|
||||
|
||||
type SSH struct {
|
||||
// Username is the username to use when connecting to the SSH server.
|
||||
Username string `yaml:"username,omitempty"`
|
||||
// Password is the password to use when connecting to the SSH server.
|
||||
Password string `yaml:"password,omitempty"`
|
||||
}
|
||||
|
||||
// ValidateAndSetDefaults validates the endpoint
|
||||
func (s *SSH) ValidateAndSetDefaults() error {
|
||||
if s.Username == "" {
|
||||
return ErrEndpointWithoutSSHUsername
|
||||
}
|
||||
if s.Password == "" {
|
||||
return ErrEndpointWithoutSSHPassword
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsEnabled returns whether the endpoint is enabled or not
|
||||
@ -188,7 +160,12 @@ func (endpoint *Endpoint) Type() EndpointType {
|
||||
|
||||
// ValidateAndSetDefaults validates the endpoint's configuration and sets the default value of args that have one
|
||||
func (endpoint *Endpoint) ValidateAndSetDefaults() error {
|
||||
// Set default values
|
||||
if err := validateEndpointNameGroupAndAlerts(endpoint.Name, endpoint.Group, endpoint.Alerts); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(endpoint.URL) == 0 {
|
||||
return ErrEndpointWithNoURL
|
||||
}
|
||||
if endpoint.ClientConfig == nil {
|
||||
endpoint.ClientConfig = client.GetDefaultConfig()
|
||||
} else {
|
||||
@ -221,20 +198,6 @@ func (endpoint *Endpoint) ValidateAndSetDefaults() error {
|
||||
if _, contentTypeHeaderExists := endpoint.Headers[ContentTypeHeader]; !contentTypeHeaderExists && endpoint.GraphQL {
|
||||
endpoint.Headers[ContentTypeHeader] = "application/json"
|
||||
}
|
||||
for _, endpointAlert := range endpoint.Alerts {
|
||||
if err := endpointAlert.ValidateAndSetDefaults(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(endpoint.Name) == 0 {
|
||||
return ErrEndpointWithNoName
|
||||
}
|
||||
if strings.ContainsAny(endpoint.Name, "\"\\") || strings.ContainsAny(endpoint.Group, "\"\\") {
|
||||
return ErrEndpointWithInvalidNameOrGroup
|
||||
}
|
||||
if len(endpoint.URL) == 0 {
|
||||
return ErrEndpointWithNoURL
|
||||
}
|
||||
if len(endpoint.Conditions) == 0 {
|
||||
return ErrEndpointWithNoCondition
|
||||
}
|
||||
@ -249,6 +212,9 @@ func (endpoint *Endpoint) ValidateAndSetDefaults() error {
|
||||
if endpoint.DNS != nil {
|
||||
return endpoint.DNS.validateAndSetDefault()
|
||||
}
|
||||
if endpoint.SSH != nil {
|
||||
return endpoint.SSH.validate()
|
||||
}
|
||||
if endpoint.Type() == EndpointTypeUNKNOWN {
|
||||
return ErrUnknownEndpointType
|
||||
}
|
||||
@ -257,9 +223,6 @@ func (endpoint *Endpoint) ValidateAndSetDefaults() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if endpoint.SSH != nil {
|
||||
return endpoint.SSH.ValidateAndSetDefaults()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -276,6 +239,15 @@ func (endpoint *Endpoint) Key() string {
|
||||
return util.ConvertGroupAndEndpointNameToKey(endpoint.Group, endpoint.Name)
|
||||
}
|
||||
|
||||
// Close HTTP connections between watchdog and endpoints to avoid dangling socket file descriptors
|
||||
// on configuration reload.
|
||||
// More context on https://github.com/TwiN/gatus/issues/536
|
||||
func (endpoint *Endpoint) Close() {
|
||||
if endpoint.Type() == EndpointTypeHTTP {
|
||||
client.GetHTTPClient(endpoint.ClientConfig).CloseIdleConnections()
|
||||
}
|
||||
}
|
||||
|
||||
// EvaluateHealth sends a request to the endpoint's URL and evaluates the conditions of the endpoint.
|
||||
func (endpoint *Endpoint) EvaluateHealth() *Result {
|
||||
result := &Result{Success: true, Errors: []string{}}
|
||||
@ -419,15 +391,6 @@ func (endpoint *Endpoint) call(result *Result) {
|
||||
}
|
||||
}
|
||||
|
||||
// Close HTTP connections between watchdog and endpoints to avoid dangling socket file descriptors
|
||||
// on configuration reload.
|
||||
// More context on https://github.com/TwiN/gatus/issues/536
|
||||
func (endpoint *Endpoint) Close() {
|
||||
if endpoint.Type() == EndpointTypeHTTP {
|
||||
client.GetHTTPClient(endpoint.ClientConfig).CloseIdleConnections()
|
||||
}
|
||||
}
|
||||
|
||||
func (endpoint *Endpoint) buildHTTPRequest() *http.Request {
|
||||
var bodyBuffer *bytes.Buffer
|
||||
if endpoint.GraphQL {
|
||||
|
32
core/endpoint_common.go
Normal file
32
core/endpoint_common.go
Normal file
@ -0,0 +1,32 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/TwiN/gatus/v5/alerting/alert"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrEndpointWithNoName is the error with which Gatus will panic if an endpoint is configured with no name
|
||||
ErrEndpointWithNoName = errors.New("you must specify a name for each endpoint")
|
||||
|
||||
// ErrEndpointWithInvalidNameOrGroup is the error with which Gatus will panic if an endpoint has an invalid character where it shouldn't
|
||||
ErrEndpointWithInvalidNameOrGroup = errors.New("endpoint name and group must not have \" or \\")
|
||||
)
|
||||
|
||||
// validateEndpointNameGroupAndAlerts validates the name, group and alerts of an endpoint
|
||||
func validateEndpointNameGroupAndAlerts(name, group string, alerts []*alert.Alert) error {
|
||||
if len(name) == 0 {
|
||||
return ErrEndpointWithNoName
|
||||
}
|
||||
if strings.ContainsAny(name, "\"\\") || strings.ContainsAny(group, "\"\\") {
|
||||
return ErrEndpointWithInvalidNameOrGroup
|
||||
}
|
||||
for _, endpointAlert := range alerts {
|
||||
if err := endpointAlert.ValidateAndSetDefaults(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
51
core/endpoint_common_test.go
Normal file
51
core/endpoint_common_test.go
Normal file
@ -0,0 +1,51 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/TwiN/gatus/v5/alerting/alert"
|
||||
)
|
||||
|
||||
func TestValidateEndpointNameGroupAndAlerts(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
name string
|
||||
group string
|
||||
alerts []*alert.Alert
|
||||
expectedErr error
|
||||
}{
|
||||
{
|
||||
name: "n",
|
||||
group: "g",
|
||||
alerts: []*alert.Alert{{Type: "slack"}},
|
||||
},
|
||||
{
|
||||
name: "n",
|
||||
alerts: []*alert.Alert{{Type: "slack"}},
|
||||
},
|
||||
{
|
||||
group: "g",
|
||||
alerts: []*alert.Alert{{Type: "slack"}},
|
||||
expectedErr: ErrEndpointWithNoName,
|
||||
},
|
||||
{
|
||||
name: "\"",
|
||||
alerts: []*alert.Alert{{Type: "slack"}},
|
||||
expectedErr: ErrEndpointWithInvalidNameOrGroup,
|
||||
},
|
||||
{
|
||||
name: "n",
|
||||
group: "\\",
|
||||
alerts: []*alert.Alert{{Type: "slack"}},
|
||||
expectedErr: ErrEndpointWithInvalidNameOrGroup,
|
||||
},
|
||||
}
|
||||
for _, scenario := range scenarios {
|
||||
t.Run(scenario.name, func(t *testing.T) {
|
||||
err := validateEndpointNameGroupAndAlerts(scenario.name, scenario.group, scenario.alerts)
|
||||
if !errors.Is(err, scenario.expectedErr) {
|
||||
t.Errorf("expected error to be %v but got %v", scenario.expectedErr, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
@ -123,6 +124,7 @@ func TestEndpoint(t *testing.T) {
|
||||
Name: "website-health",
|
||||
URL: "https://twin.sh/health",
|
||||
Conditions: []Condition{"[DOMAIN_EXPIRATION] > 100h"},
|
||||
Interval: 5 * time.Minute,
|
||||
},
|
||||
ExpectedResult: &Result{
|
||||
Success: true,
|
||||
@ -195,7 +197,10 @@ func TestEndpoint(t *testing.T) {
|
||||
} else {
|
||||
client.InjectHTTPClient(nil)
|
||||
}
|
||||
scenario.Endpoint.ValidateAndSetDefaults()
|
||||
err := scenario.Endpoint.ValidateAndSetDefaults()
|
||||
if err != nil {
|
||||
t.Error("did not expect an error, got", err)
|
||||
}
|
||||
result := scenario.Endpoint.EvaluateHealth()
|
||||
if result.Success != scenario.ExpectedResult.Success {
|
||||
t.Errorf("Expected success to be %v, got %v", scenario.ExpectedResult.Success, result.Success)
|
||||
@ -430,7 +435,10 @@ func TestEndpoint_ValidateAndSetDefaultsWithClientConfig(t *testing.T) {
|
||||
Timeout: 0,
|
||||
},
|
||||
}
|
||||
endpoint.ValidateAndSetDefaults()
|
||||
err := endpoint.ValidateAndSetDefaults()
|
||||
if err != nil {
|
||||
t.Fatal("did not expect an error, got", err)
|
||||
}
|
||||
if endpoint.ClientConfig == nil {
|
||||
t.Error("client configuration should've been set to the default configuration")
|
||||
} else {
|
||||
@ -466,7 +474,7 @@ func TestEndpoint_ValidateAndSetDefaultsWithDNS(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEndpoint_ValidateAndSetDefaultsWithSSH(t *testing.T) {
|
||||
tests := []struct {
|
||||
scenarios := []struct {
|
||||
name string
|
||||
username string
|
||||
password string
|
||||
@ -492,20 +500,20 @@ func TestEndpoint_ValidateAndSetDefaultsWithSSH(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
for _, scenario := range scenarios {
|
||||
t.Run(scenario.name, func(t *testing.T) {
|
||||
endpoint := &Endpoint{
|
||||
Name: "ssh-test",
|
||||
URL: "https://example.com",
|
||||
SSH: &SSH{
|
||||
Username: test.username,
|
||||
Password: test.password,
|
||||
Username: scenario.username,
|
||||
Password: scenario.password,
|
||||
},
|
||||
Conditions: []Condition{Condition("[STATUS] == 0")},
|
||||
}
|
||||
err := endpoint.ValidateAndSetDefaults()
|
||||
if err != test.expectedErr {
|
||||
t.Errorf("expected error %v, got %v", test.expectedErr, err)
|
||||
if !errors.Is(err, scenario.expectedErr) {
|
||||
t.Errorf("expected error %v, got %v", scenario.expectedErr, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -575,7 +583,10 @@ func TestEndpoint_buildHTTPRequest(t *testing.T) {
|
||||
URL: "https://twin.sh/health",
|
||||
Conditions: []Condition{condition},
|
||||
}
|
||||
endpoint.ValidateAndSetDefaults()
|
||||
err := endpoint.ValidateAndSetDefaults()
|
||||
if err != nil {
|
||||
t.Fatal("did not expect an error, got", err)
|
||||
}
|
||||
request := endpoint.buildHTTPRequest()
|
||||
if request.Method != "GET" {
|
||||
t.Error("request.Method should've been GET, but was", request.Method)
|
||||
@ -598,7 +609,10 @@ func TestEndpoint_buildHTTPRequestWithCustomUserAgent(t *testing.T) {
|
||||
"User-Agent": "Test/2.0",
|
||||
},
|
||||
}
|
||||
endpoint.ValidateAndSetDefaults()
|
||||
err := endpoint.ValidateAndSetDefaults()
|
||||
if err != nil {
|
||||
t.Fatal("did not expect an error, got", err)
|
||||
}
|
||||
request := endpoint.buildHTTPRequest()
|
||||
if request.Method != "GET" {
|
||||
t.Error("request.Method should've been GET, but was", request.Method)
|
||||
@ -622,7 +636,10 @@ func TestEndpoint_buildHTTPRequestWithHostHeader(t *testing.T) {
|
||||
"Host": "example.com",
|
||||
},
|
||||
}
|
||||
endpoint.ValidateAndSetDefaults()
|
||||
err := endpoint.ValidateAndSetDefaults()
|
||||
if err != nil {
|
||||
t.Fatal("did not expect an error, got", err)
|
||||
}
|
||||
request := endpoint.buildHTTPRequest()
|
||||
if request.Method != "POST" {
|
||||
t.Error("request.Method should've been POST, but was", request.Method)
|
||||
@ -649,7 +666,10 @@ func TestEndpoint_buildHTTPRequestWithGraphQLEnabled(t *testing.T) {
|
||||
}
|
||||
}`,
|
||||
}
|
||||
endpoint.ValidateAndSetDefaults()
|
||||
err := endpoint.ValidateAndSetDefaults()
|
||||
if err != nil {
|
||||
t.Fatal("did not expect an error, got", err)
|
||||
}
|
||||
request := endpoint.buildHTTPRequest()
|
||||
if request.Method != "POST" {
|
||||
t.Error("request.Method should've been POST, but was", request.Method)
|
||||
@ -671,7 +691,10 @@ func TestIntegrationEvaluateHealth(t *testing.T) {
|
||||
URL: "https://twin.sh/health",
|
||||
Conditions: []Condition{condition, bodyCondition},
|
||||
}
|
||||
endpoint.ValidateAndSetDefaults()
|
||||
err := endpoint.ValidateAndSetDefaults()
|
||||
if err != nil {
|
||||
t.Fatal("did not expect an error, got", err)
|
||||
}
|
||||
result := endpoint.EvaluateHealth()
|
||||
if !result.ConditionResults[0].Success {
|
||||
t.Errorf("Condition '%s' should have been a success", condition)
|
||||
@ -699,7 +722,10 @@ func TestIntegrationEvaluateHealthWithErrorAndHideURL(t *testing.T) {
|
||||
HideURL: true,
|
||||
},
|
||||
}
|
||||
endpoint.ValidateAndSetDefaults()
|
||||
err := endpoint.ValidateAndSetDefaults()
|
||||
if err != nil {
|
||||
t.Fatal("did not expect an error, got", err)
|
||||
}
|
||||
result := endpoint.EvaluateHealth()
|
||||
if result.Success {
|
||||
t.Error("Because one of the conditions was invalid, result.Success should have been false")
|
||||
@ -724,7 +750,10 @@ func TestIntegrationEvaluateHealthForDNS(t *testing.T) {
|
||||
},
|
||||
Conditions: []Condition{conditionSuccess, conditionBody},
|
||||
}
|
||||
endpoint.ValidateAndSetDefaults()
|
||||
err := endpoint.ValidateAndSetDefaults()
|
||||
if err != nil {
|
||||
t.Fatal("did not expect an error, got", err)
|
||||
}
|
||||
result := endpoint.EvaluateHealth()
|
||||
if !result.ConditionResults[0].Success {
|
||||
t.Errorf("Conditions '%s' and '%s' should have been a success", conditionSuccess, conditionBody)
|
||||
@ -792,7 +821,10 @@ func TestIntegrationEvaluateHealthForICMP(t *testing.T) {
|
||||
URL: "icmp://127.0.0.1",
|
||||
Conditions: []Condition{"[CONNECTED] == true"},
|
||||
}
|
||||
endpoint.ValidateAndSetDefaults()
|
||||
err := endpoint.ValidateAndSetDefaults()
|
||||
if err != nil {
|
||||
t.Fatal("did not expect an error, got", err)
|
||||
}
|
||||
result := endpoint.EvaluateHealth()
|
||||
if !result.ConditionResults[0].Success {
|
||||
t.Errorf("Conditions '%s' should have been a success", endpoint.Conditions[0])
|
||||
|
89
core/external_endpoint.go
Normal file
89
core/external_endpoint.go
Normal file
@ -0,0 +1,89 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/TwiN/gatus/v5/alerting/alert"
|
||||
"github.com/TwiN/gatus/v5/util"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrExternalEndpointWithNoToken is the error with which Gatus will panic if an external endpoint is configured without a token.
|
||||
ErrExternalEndpointWithNoToken = errors.New("you must specify a token for each external endpoint")
|
||||
)
|
||||
|
||||
// ExternalEndpoint is an endpoint whose result is pushed from outside Gatus, which means that
|
||||
// said endpoints are not monitored by Gatus itself; Gatus only displays their results and takes
|
||||
// care of alerting
|
||||
type ExternalEndpoint struct {
|
||||
// Enabled defines whether to enable the monitoring of the endpoint
|
||||
Enabled *bool `yaml:"enabled,omitempty"`
|
||||
|
||||
// Name of the endpoint. Can be anything.
|
||||
Name string `yaml:"name"`
|
||||
|
||||
// Group the endpoint is a part of. Used for grouping multiple endpoints together on the front end.
|
||||
Group string `yaml:"group,omitempty"`
|
||||
|
||||
// Token is the bearer token that must be provided through the Authorization header to push results to the endpoint
|
||||
Token string `yaml:"token,omitempty"`
|
||||
|
||||
// Alerts is the alerting configuration for the endpoint in case of failure
|
||||
Alerts []*alert.Alert `yaml:"alerts,omitempty"`
|
||||
|
||||
// NumberOfFailuresInARow is the number of unsuccessful evaluations in a row
|
||||
NumberOfFailuresInARow int `yaml:"-"`
|
||||
|
||||
// NumberOfSuccessesInARow is the number of successful evaluations in a row
|
||||
NumberOfSuccessesInARow int `yaml:"-"`
|
||||
}
|
||||
|
||||
// ValidateAndSetDefaults validates the ExternalEndpoint and sets the default values
|
||||
func (externalEndpoint *ExternalEndpoint) ValidateAndSetDefaults() error {
|
||||
if err := validateEndpointNameGroupAndAlerts(externalEndpoint.Name, externalEndpoint.Group, externalEndpoint.Alerts); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(externalEndpoint.Token) == 0 {
|
||||
return ErrExternalEndpointWithNoToken
|
||||
}
|
||||
for _, externalEndpointAlert := range externalEndpoint.Alerts {
|
||||
if err := externalEndpointAlert.ValidateAndSetDefaults(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsEnabled returns whether the endpoint is enabled or not
|
||||
func (externalEndpoint *ExternalEndpoint) IsEnabled() bool {
|
||||
if externalEndpoint.Enabled == nil {
|
||||
return true
|
||||
}
|
||||
return *externalEndpoint.Enabled
|
||||
}
|
||||
|
||||
// DisplayName returns an identifier made up of the Name and, if not empty, the Group.
|
||||
func (externalEndpoint *ExternalEndpoint) DisplayName() string {
|
||||
if len(externalEndpoint.Group) > 0 {
|
||||
return externalEndpoint.Group + "/" + externalEndpoint.Name
|
||||
}
|
||||
return externalEndpoint.Name
|
||||
}
|
||||
|
||||
// Key returns the unique key for the Endpoint
|
||||
func (externalEndpoint *ExternalEndpoint) Key() string {
|
||||
return util.ConvertGroupAndEndpointNameToKey(externalEndpoint.Group, externalEndpoint.Name)
|
||||
}
|
||||
|
||||
// ToEndpoint converts the ExternalEndpoint to an Endpoint
|
||||
func (externalEndpoint *ExternalEndpoint) ToEndpoint() *Endpoint {
|
||||
endpoint := &Endpoint{
|
||||
Enabled: externalEndpoint.Enabled,
|
||||
Name: externalEndpoint.Name,
|
||||
Group: externalEndpoint.Group,
|
||||
Alerts: externalEndpoint.Alerts,
|
||||
NumberOfFailuresInARow: externalEndpoint.NumberOfFailuresInARow,
|
||||
NumberOfSuccessesInARow: externalEndpoint.NumberOfSuccessesInARow,
|
||||
}
|
||||
return endpoint
|
||||
}
|
25
core/external_endpoint_test.go
Normal file
25
core/external_endpoint_test.go
Normal file
@ -0,0 +1,25 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExternalEndpoint_ToEndpoint(t *testing.T) {
|
||||
externalEndpoint := &ExternalEndpoint{
|
||||
Name: "name",
|
||||
Group: "group",
|
||||
}
|
||||
convertedEndpoint := externalEndpoint.ToEndpoint()
|
||||
if externalEndpoint.Name != convertedEndpoint.Name {
|
||||
t.Errorf("expected %s, got %s", externalEndpoint.Name, convertedEndpoint.Name)
|
||||
}
|
||||
if externalEndpoint.Group != convertedEndpoint.Group {
|
||||
t.Errorf("expected %s, got %s", externalEndpoint.Group, convertedEndpoint.Group)
|
||||
}
|
||||
if externalEndpoint.Key() != convertedEndpoint.Key() {
|
||||
t.Errorf("expected %s, got %s", externalEndpoint.Key(), convertedEndpoint.Key())
|
||||
}
|
||||
if externalEndpoint.DisplayName() != convertedEndpoint.DisplayName() {
|
||||
t.Errorf("expected %s, got %s", externalEndpoint.DisplayName(), convertedEndpoint.DisplayName())
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@ import (
|
||||
// Result of the evaluation of a Endpoint
|
||||
type Result struct {
|
||||
// HTTPStatus is the HTTP response status code
|
||||
HTTPStatus int `json:"status"`
|
||||
HTTPStatus int `json:"status,omitempty"`
|
||||
|
||||
// DNSRCode is the response code of a DNS query in a human-readable format
|
||||
//
|
||||
@ -30,7 +30,7 @@ type Result struct {
|
||||
Errors []string `json:"errors,omitempty"`
|
||||
|
||||
// ConditionResults results of the Endpoint's conditions
|
||||
ConditionResults []*ConditionResult `json:"conditionResults"`
|
||||
ConditionResults []*ConditionResult `json:"conditionResults,omitempty"`
|
||||
|
||||
// Success whether the result signifies a success or not
|
||||
Success bool `json:"success"`
|
||||
|
29
core/ssh.go
Normal file
29
core/ssh.go
Normal file
@ -0,0 +1,29 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrEndpointWithoutSSHUsername is the error with which Gatus will panic if an endpoint with SSH monitoring is configured without a user.
|
||||
ErrEndpointWithoutSSHUsername = errors.New("you must specify a username for each SSH endpoint")
|
||||
|
||||
// ErrEndpointWithoutSSHPassword is the error with which Gatus will panic if an endpoint with SSH monitoring is configured without a password.
|
||||
ErrEndpointWithoutSSHPassword = errors.New("you must specify a password for each SSH endpoint")
|
||||
)
|
||||
|
||||
type SSH struct {
|
||||
Username string `yaml:"username,omitempty"`
|
||||
Password string `yaml:"password,omitempty"`
|
||||
}
|
||||
|
||||
// validate validates the endpoint
|
||||
func (s *SSH) validate() error {
|
||||
if len(s.Username) == 0 {
|
||||
return ErrEndpointWithoutSSHUsername
|
||||
}
|
||||
if len(s.Password) == 0 {
|
||||
return ErrEndpointWithoutSSHPassword
|
||||
}
|
||||
return nil
|
||||
}
|
25
core/ssh_test.go
Normal file
25
core/ssh_test.go
Normal file
@ -0,0 +1,25 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSSH_validate(t *testing.T) {
|
||||
ssh := &SSH{}
|
||||
if err := ssh.validate(); err == nil {
|
||||
t.Error("expected an error")
|
||||
} else if !errors.Is(err, ErrEndpointWithoutSSHUsername) {
|
||||
t.Errorf("expected error to be '%v', got '%v'", ErrEndpointWithoutSSHUsername, err)
|
||||
}
|
||||
ssh.Username = "username"
|
||||
if err := ssh.validate(); err == nil {
|
||||
t.Error("expected an error")
|
||||
} else if !errors.Is(err, ErrEndpointWithoutSSHPassword) {
|
||||
t.Errorf("expected error to be '%v', got '%v'", ErrEndpointWithoutSSHPassword, err)
|
||||
}
|
||||
ssh.Password = "password"
|
||||
if err := ssh.validate(); err != nil {
|
||||
t.Errorf("expected no error, got '%v'", err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user