-
Notifications
You must be signed in to change notification settings - Fork 2k
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 #495 from hashicorp/f-regions
Add regions endpoint
- Loading branch information
Showing
11 changed files
with
274 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package api | ||
|
||
import "sort" | ||
|
||
// Regions is used to query the regions in the cluster. | ||
type Regions struct { | ||
client *Client | ||
} | ||
|
||
// Regions returns a handle on the allocs endpoints. | ||
func (c *Client) Regions() *Regions { | ||
return &Regions{client: c} | ||
} | ||
|
||
// List returns a list of all of the regions. | ||
func (r *Regions) List() ([]string, error) { | ||
var resp []string | ||
if _, err := r.client.query("/v1/regions", &resp, nil); err != nil { | ||
return nil, err | ||
} | ||
sort.Strings(resp) | ||
return resp, 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,42 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/nomad/testutil" | ||
) | ||
|
||
func TestRegionsList(t *testing.T) { | ||
c1, s1 := makeClient(t, nil, func(c *testutil.TestServerConfig) { | ||
c.Region = "regionA" | ||
}) | ||
defer s1.Stop() | ||
|
||
c2, s2 := makeClient(t, nil, func(c *testutil.TestServerConfig) { | ||
c.Region = "regionB" | ||
}) | ||
defer s2.Stop() | ||
|
||
// Join the servers | ||
if _, err := c2.Agent().Join(s1.SerfAddr); err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
|
||
// Regions returned and sorted | ||
testutil.WaitForResult(func() (bool, error) { | ||
regions, err := c1.Regions().List() | ||
if err != nil { | ||
return false, err | ||
} | ||
if n := len(regions); n != 2 { | ||
return false, fmt.Errorf("expected 2 regions, got: %d", n) | ||
} | ||
if regions[0] != "regionA" || regions[1] != "regionB" { | ||
return false, fmt.Errorf("bad: %#v", regions) | ||
} | ||
return true, nil | ||
}, func(err error) { | ||
t.Fatalf("err: %v", err) | ||
}) | ||
} |
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,24 @@ | ||
package agent | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/hashicorp/nomad/nomad/structs" | ||
) | ||
|
||
func (s *HTTPServer) RegionListRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
if req.Method != "GET" { | ||
return nil, CodedError(405, ErrInvalidMethod) | ||
} | ||
|
||
var args structs.GenericRequest | ||
if s.parse(resp, req, &args.Region, &args.QueryOptions) { | ||
return nil, nil | ||
} | ||
|
||
var regions []string | ||
if err := s.agent.RPC("Region.List", &args, ®ions); err != nil { | ||
return nil, err | ||
} | ||
return regions, 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,29 @@ | ||
package agent | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestHTTP_RegionList(t *testing.T) { | ||
httpTest(t, nil, func(s *TestServer) { | ||
// Make the HTTP request | ||
req, err := http.NewRequest("GET", "/v1/regions", nil) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
respW := httptest.NewRecorder() | ||
|
||
// Make the request | ||
obj, err := s.Server.RegionListRequest(respW, req) | ||
if err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
|
||
out := obj.([]string) | ||
if len(out) != 1 || out[0] != "global" { | ||
t.Fatalf("unexpected regions: %#v", out) | ||
} | ||
}) | ||
} |
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,16 @@ | ||
package nomad | ||
|
||
import "github.com/hashicorp/nomad/nomad/structs" | ||
|
||
// Region is used to query and list the known regions | ||
type Region struct { | ||
srv *Server | ||
} | ||
|
||
// List is used to list all of the known regions. No leader forwarding is | ||
// required for this endpoint because memberlist is used to populate the | ||
// peers list we read from. | ||
func (r *Region) List(args *structs.GenericRequest, reply *[]string) error { | ||
*reply = r.srv.Regions() | ||
return 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,46 @@ | ||
package nomad | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/net-rpc-msgpackrpc" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
"github.com/hashicorp/nomad/testutil" | ||
) | ||
|
||
func TestRegionList(t *testing.T) { | ||
// Make the servers | ||
s1 := testServer(t, func(c *Config) { | ||
c.Region = "region1" | ||
}) | ||
defer s1.Shutdown() | ||
codec := rpcClient(t, s1) | ||
|
||
s2 := testServer(t, func(c *Config) { | ||
c.Region = "region2" | ||
}) | ||
defer s2.Shutdown() | ||
|
||
// Join the servers | ||
s2Addr := fmt.Sprintf("127.0.0.1:%d", | ||
s2.config.SerfConfig.MemberlistConfig.BindPort) | ||
if n, err := s1.Join([]string{s2Addr}); err != nil || n != 1 { | ||
t.Fatalf("Failed joining: %v (%d joined)", err, n) | ||
} | ||
|
||
// Query the regions list | ||
testutil.WaitForResult(func() (bool, error) { | ||
var arg structs.GenericRequest | ||
var out []string | ||
if err := msgpackrpc.CallWithCodec(codec, "Region.List", &arg, &out); err != nil { | ||
t.Fatalf("err: %v", err) | ||
} | ||
if len(out) != 2 || out[0] != "region1" || out[1] != "region2" { | ||
t.Fatalf("unexpected regions: %v", out) | ||
} | ||
return true, nil | ||
}, func(err error) { | ||
t.Fatalf("err: %v", err) | ||
}) | ||
} |
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,38 @@ | ||
--- | ||
layout: "http" | ||
page_title: "HTTP API: /v1/regions" | ||
sidebar_current: "docs-http-regions" | ||
description: > | ||
The '/v1/regions' endpoint lists the known cluster regions. | ||
--- | ||
|
||
# /v1/regions | ||
|
||
## GET | ||
|
||
<dl> | ||
<dt>Description</dt> | ||
<dd> | ||
Returns the known region names. | ||
</dd> | ||
|
||
<dt>Method</dt> | ||
<dd>GET</dd> | ||
|
||
<dt>URL</dt> | ||
<dd>`/v1/regions`</dd> | ||
|
||
<dt>Parameters</dt> | ||
<dd> | ||
None | ||
</dd> | ||
|
||
<dt>Returns</dt> | ||
<dd> | ||
|
||
```javascript | ||
["region1","region2"] | ||
``` | ||
|
||
</dd> | ||
</dl> |
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