Skip to content

Commit

Permalink
Merge pull request #49 from storageos/feature/DEV-1916-api-endpoint-t…
Browse files Browse the repository at this point in the history
…o-validate-network

Feature/dev 1916 api endpoint to validate network
  • Loading branch information
croomes authored Aug 16, 2018
2 parents 55cd63c + 298adc6 commit 88f0633
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 49 deletions.
31 changes: 0 additions & 31 deletions connectivity.go

This file was deleted.

35 changes: 35 additions & 0 deletions network_diagnostics.go
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
}
47 changes: 29 additions & 18 deletions types/connectivity.go
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
}

0 comments on commit 88f0633

Please sign in to comment.