From 5a17482ab71ef7102d41385b42446ab53fe46be7 Mon Sep 17 00:00:00 2001 From: Simon Croome Date: Wed, 15 Aug 2018 21:27:57 +0100 Subject: [PATCH 1/3] Update for new api --- connectivity.go | 8 +++++--- types/connectivity.go | 30 ++++++++++++++++++------------ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/connectivity.go b/connectivity.go index 76d9fb1..b0c1162 100644 --- a/connectivity.go +++ b/connectivity.go @@ -3,18 +3,20 @@ package storageos import ( "encoding/json" "net/http" + "path" "github.com/storageos/go-api/types" ) var ( - // ConnectivityAPIPrefix is a partial path to the HTTP endpoint. - ConnectivityAPIPrefix = "diagnostics/connectivity" + // NetworkDiagnosticsAPIPrefix is a partial path to the HTTP endpoint for + // the node connectivity diagnostics report. + NetworkDiagnosticsAPIPrefix = "diagnostics/network" ) // Connectivity returns a node by its reference. func (c *Client) Connectivity(ref string) ([]types.ConnectivityResult, error) { - resp, err := c.do("GET", ConnectivityAPIPrefix+"/"+ref, doOptions{}) + resp, err := c.do("GET", path.Join(NetworkDiagnosticsAPIPrefix, ref), doOptions{}) if err != nil { if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound { return nil, ErrNoSuchNode diff --git a/types/connectivity.go b/types/connectivity.go index 1b2bdd3..ce7e63a 100644 --- a/types/connectivity.go +++ b/types/connectivity.go @@ -1,5 +1,7 @@ package types +import "time" + type TestName string const ( @@ -10,21 +12,25 @@ const ( // ConnectivityResult capture's a node connectivity report to a given target. type ConnectivityResult struct { - Target *Node `json:"target"` + // Label is a human-readable reference for the service being tested. + Label string `json:"label"` - Connectivity map[TestName]bool `json:"connectivity"` + // Address is the host:port of the service being tested. + Address string `json:"address"` - Timeout bool `json:"timeout"` // true iff the test timed out before completion - Errors []error `json:"errors"` // errors encountered during testing -} + // Source is a human-readable reference for the source host where the tests + // were run from.. + Source string `json:"source"` -// Passes returns true iff all tests passed -func (r ConnectivityResult) Passes() bool { - passes := len(r.Connectivity) > 0 + // LatencyNS is the duration in nanoseconds that the check took to complete. + // Will also be set on unsuccessful attempts. + LatencyNS time.Duration `json:"latency_ns"` - for _, v := range r.Connectivity { - passes = passes && v - } + // Error is set if the test returned an error. + Error string `json:"error"` +} - return passes +// Passes returns true iff no error +func (r ConnectivityResult) Passes() bool { + return len(r.Error) == 0 } From cd929b8cfd6f465fceeea209791b9268da64c761 Mon Sep 17 00:00:00 2001 From: Simon Croome Date: Thu, 16 Aug 2018 01:03:08 +0100 Subject: [PATCH 2/3] Add IsOK() helpers --- connectivity.go | 12 +++++++----- types/connectivity.go | 27 ++++++++++++++++----------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/connectivity.go b/connectivity.go index b0c1162..bd2a3d6 100644 --- a/connectivity.go +++ b/connectivity.go @@ -14,8 +14,10 @@ var ( NetworkDiagnosticsAPIPrefix = "diagnostics/network" ) -// Connectivity returns a node by its reference. -func (c *Client) Connectivity(ref string) ([]types.ConnectivityResult, error) { +// Connectivity returns a collection of connectivity reports. If a reference to +// a node is given, it will only check connectivity from that node. Otherwise, +// connectivity between all cluster nodes will be returned. +func (c *Client) Connectivity(ref string) (types.ConnectivityResults, error) { resp, err := c.do("GET", path.Join(NetworkDiagnosticsAPIPrefix, ref), doOptions{}) if err != nil { if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound { @@ -25,9 +27,9 @@ func (c *Client) Connectivity(ref string) ([]types.ConnectivityResult, error) { } defer resp.Body.Close() - var nodecon []types.ConnectivityResult - if err := json.NewDecoder(resp.Body).Decode(&nodecon); err != nil { + var results types.ConnectivityResults + if err := json.NewDecoder(resp.Body).Decode(&results); err != nil { return nil, err } - return nodecon, nil + return results, nil } diff --git a/types/connectivity.go b/types/connectivity.go index ce7e63a..8d3177d 100644 --- a/types/connectivity.go +++ b/types/connectivity.go @@ -2,14 +2,6 @@ package types import "time" -type TestName string - -const ( - APIConnectivity TestName = "api" - NatsConnectivity TestName = "nats" - EtcdConnectivity TestName = "etcd" -) - // ConnectivityResult capture's a node connectivity report to a given target. type ConnectivityResult struct { // Label is a human-readable reference for the service being tested. @@ -19,7 +11,7 @@ type ConnectivityResult struct { Address string `json:"address"` // Source is a human-readable reference for the source host where the tests - // were run from.. + // were run from. Source string `json:"source"` // LatencyNS is the duration in nanoseconds that the check took to complete. @@ -30,7 +22,20 @@ type ConnectivityResult struct { Error string `json:"error"` } -// Passes returns true iff no error -func (r ConnectivityResult) Passes() bool { +// IsOK returns true iff no error +func (r ConnectivityResult) IsOK() bool { return len(r.Error) == 0 } + +// ConnectivityResults is a collection of connectivty reports. +type ConnectivityResults []ConnectivityResult + +// IsOK returns true iff no error in any result. +func (r ConnectivityResults) IsOK() bool { + for _, result := range r { + if !result.IsOK() { + return false + } + } + return true +} From 298adc613d7d3257ac60dab8daa9e3ffacf0954a Mon Sep 17 00:00:00 2001 From: Simon Croome Date: Thu, 16 Aug 2018 01:15:02 +0100 Subject: [PATCH 3/3] Rename api function Connectivity->NetworkDiagnostics --- connectivity.go => network_diagnostics.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename connectivity.go => network_diagnostics.go (68%) diff --git a/connectivity.go b/network_diagnostics.go similarity index 68% rename from connectivity.go rename to network_diagnostics.go index bd2a3d6..5d8a0bc 100644 --- a/connectivity.go +++ b/network_diagnostics.go @@ -14,10 +14,10 @@ var ( NetworkDiagnosticsAPIPrefix = "diagnostics/network" ) -// Connectivity returns a collection of connectivity reports. If a reference to -// a node is given, it will only check connectivity from that node. Otherwise, -// connectivity between all cluster nodes will be returned. -func (c *Client) Connectivity(ref string) (types.ConnectivityResults, error) { +// NetworkDiagnostics returns a collection of network connectivity reports. If +// a reference to a node is given, it will only check connectivity from that +// node. Otherwise, connectivity between all cluster nodes will be returned. +func (c *Client) NetworkDiagnostics(ref string) (types.ConnectivityResults, error) { resp, err := c.do("GET", path.Join(NetworkDiagnosticsAPIPrefix, ref), doOptions{}) if err != nil { if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {