-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from storageos/feature/DEV-1916-api-endpoint-t…
…o-validate-network Feature/dev 1916 api endpoint to validate network
- Loading branch information
Showing
3 changed files
with
64 additions
and
49 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package storageos | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"path" | ||
|
||
"github.com/storageos/go-api/types" | ||
) | ||
|
||
var ( | ||
// NetworkDiagnosticsAPIPrefix is a partial path to the HTTP endpoint for | ||
// the node connectivity diagnostics report. | ||
NetworkDiagnosticsAPIPrefix = "diagnostics/network" | ||
) | ||
|
||
// 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 { | ||
return nil, ErrNoSuchNode | ||
} | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
var results types.ConnectivityResults | ||
if err := json.NewDecoder(resp.Body).Decode(&results); err != nil { | ||
return nil, err | ||
} | ||
return results, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,41 @@ | ||
package types | ||
|
||
type TestName string | ||
|
||
const ( | ||
APIConnectivity TestName = "api" | ||
NatsConnectivity TestName = "nats" | ||
EtcdConnectivity TestName = "etcd" | ||
) | ||
import "time" | ||
|
||
// 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"` | ||
|
||
// Address is the host:port of the service being tested. | ||
Address string `json:"address"` | ||
|
||
// Source is a human-readable reference for the source host where the tests | ||
// were run from. | ||
Source string `json:"source"` | ||
|
||
Connectivity map[TestName]bool `json:"connectivity"` | ||
// 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"` | ||
|
||
Timeout bool `json:"timeout"` // true iff the test timed out before completion | ||
Errors []error `json:"errors"` // errors encountered during testing | ||
// Error is set if the test returned an error. | ||
Error string `json:"error"` | ||
} | ||
|
||
// Passes returns true iff all tests passed | ||
func (r ConnectivityResult) Passes() bool { | ||
passes := len(r.Connectivity) > 0 | ||
// IsOK returns true iff no error | ||
func (r ConnectivityResult) IsOK() bool { | ||
return len(r.Error) == 0 | ||
} | ||
|
||
for _, v := range r.Connectivity { | ||
passes = passes && v | ||
} | ||
// ConnectivityResults is a collection of connectivty reports. | ||
type ConnectivityResults []ConnectivityResult | ||
|
||
return passes | ||
// 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 | ||
} |