forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for coordinates to client API.
- Loading branch information
James Phillips
committed
Oct 23, 2015
1 parent
660da92
commit 787f946
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
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
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
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,66 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/hashicorp/serf/coordinate" | ||
) | ||
|
||
// CoordinateEntry represents a node and its associated network coordinate. | ||
type CoordinateEntry struct { | ||
Node string | ||
Coord *coordinate.Coordinate | ||
} | ||
|
||
// CoordinateDatacenterMap represents a datacenter and its associated WAN | ||
// nodes and their associates coordinates. | ||
type CoordinateDatacenterMap struct { | ||
Datacenter string | ||
Coordinates []CoordinateEntry | ||
} | ||
|
||
// Coordinate can be used to query the coordinate endpoints | ||
type Coordinate struct { | ||
c *Client | ||
} | ||
|
||
// Coordinate returns a handle to the coordinate endpoints | ||
func (c *Client) Coordinate() *Coordinate { | ||
return &Coordinate{c} | ||
} | ||
|
||
// Datacenters is used to return the coordinates of all the servers in the WAN | ||
// pool. | ||
func (c *Coordinate) Datacenters() ([]*CoordinateDatacenterMap, error) { | ||
r := c.c.newRequest("GET", "/v1/coordinate/datacenters") | ||
_, resp, err := requireOK(c.c.doRequest(r)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
var out []*CoordinateDatacenterMap | ||
if err := decodeBody(resp, &out); err != nil { | ||
return nil, err | ||
} | ||
return out, nil | ||
} | ||
|
||
// Nodes is used to return the coordinates of all the nodes in the LAN pool. | ||
func (c *Coordinate) Nodes(q *QueryOptions) ([]*CoordinateEntry, *QueryMeta, error) { | ||
r := c.c.newRequest("GET", "/v1/coordinate/nodes") | ||
r.setQueryOptions(q) | ||
rtt, resp, err := requireOK(c.c.doRequest(r)) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
qm := &QueryMeta{} | ||
parseQueryMeta(resp, qm) | ||
qm.RequestTime = rtt | ||
|
||
var out []*CoordinateEntry | ||
if err := decodeBody(resp, &out); err != nil { | ||
return nil, nil, err | ||
} | ||
return out, qm, 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/consul/testutil" | ||
) | ||
|
||
func TestCoordinate_Datacenters(t *testing.T) { | ||
t.Parallel() | ||
c, s := makeClient(t) | ||
defer s.Stop() | ||
|
||
coordinate := c.Coordinate() | ||
|
||
testutil.WaitForResult(func() (bool, error) { | ||
datacenters, err := coordinate.Datacenters() | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if len(datacenters) == 0 { | ||
return false, fmt.Errorf("Bad: %v", datacenters) | ||
} | ||
|
||
return true, nil | ||
}, func(err error) { | ||
t.Fatalf("err: %s", err) | ||
}) | ||
} | ||
|
||
func TestCoordinate_Nodes(t *testing.T) { | ||
t.Parallel() | ||
c, s := makeClient(t) | ||
defer s.Stop() | ||
|
||
coordinate := c.Coordinate() | ||
|
||
testutil.WaitForResult(func() (bool, error) { | ||
_, _, err := coordinate.Nodes(nil) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
// There's not a good way to populate coordinates without | ||
// waiting for them to calculate and update, so the best | ||
// we can do is call the endpoint and make sure we don't | ||
// get an error. | ||
return true, nil | ||
}, func(err error) { | ||
t.Fatalf("err: %s", err) | ||
}) | ||
} |