agent: POST /v1/connect/intentions
This commit is contained in:
parent
4003bca543
commit
c78b82f43b
|
@ -39,7 +39,7 @@ func init() {
|
||||||
registerEndpoint("/v1/catalog/services", []string{"GET"}, (*HTTPServer).CatalogServices)
|
registerEndpoint("/v1/catalog/services", []string{"GET"}, (*HTTPServer).CatalogServices)
|
||||||
registerEndpoint("/v1/catalog/service/", []string{"GET"}, (*HTTPServer).CatalogServiceNodes)
|
registerEndpoint("/v1/catalog/service/", []string{"GET"}, (*HTTPServer).CatalogServiceNodes)
|
||||||
registerEndpoint("/v1/catalog/node/", []string{"GET"}, (*HTTPServer).CatalogNodeServices)
|
registerEndpoint("/v1/catalog/node/", []string{"GET"}, (*HTTPServer).CatalogNodeServices)
|
||||||
registerEndpoint("/v1/connect/intentions", []string{"GET"}, (*HTTPServer).IntentionList)
|
registerEndpoint("/v1/connect/intentions", []string{"GET", "POST"}, (*HTTPServer).IntentionList)
|
||||||
registerEndpoint("/v1/coordinate/datacenters", []string{"GET"}, (*HTTPServer).CoordinateDatacenters)
|
registerEndpoint("/v1/coordinate/datacenters", []string{"GET"}, (*HTTPServer).CoordinateDatacenters)
|
||||||
registerEndpoint("/v1/coordinate/nodes", []string{"GET"}, (*HTTPServer).CoordinateNodes)
|
registerEndpoint("/v1/coordinate/nodes", []string{"GET"}, (*HTTPServer).CoordinateNodes)
|
||||||
registerEndpoint("/v1/coordinate/node/", []string{"GET"}, (*HTTPServer).CoordinateNode)
|
registerEndpoint("/v1/coordinate/node/", []string{"GET"}, (*HTTPServer).CoordinateNode)
|
||||||
|
|
|
@ -1,16 +1,29 @@
|
||||||
package agent
|
package agent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/agent/structs"
|
"github.com/hashicorp/consul/agent/structs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// /v1/connect/intentions
|
// /v1/connection/intentions
|
||||||
func (s *HTTPServer) IntentionList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
func (s *HTTPServer) IntentionEndpoint(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||||
if req.Method != "GET" {
|
switch req.Method {
|
||||||
return nil, MethodNotAllowedError{req.Method, []string{"GET"}}
|
case "GET":
|
||||||
|
return s.IntentionList(resp, req)
|
||||||
|
|
||||||
|
case "POST":
|
||||||
|
return s.IntentionCreate(resp, req)
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, MethodNotAllowedError{req.Method, []string{"GET", "POST"}}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /v1/connect/intentions
|
||||||
|
func (s *HTTPServer) IntentionList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||||
|
// Method is tested in IntentionEndpoint
|
||||||
|
|
||||||
var args structs.DCSpecificRequest
|
var args structs.DCSpecificRequest
|
||||||
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
||||||
|
@ -28,3 +41,29 @@ func (s *HTTPServer) IntentionList(resp http.ResponseWriter, req *http.Request)
|
||||||
}
|
}
|
||||||
return reply.Intentions, nil
|
return reply.Intentions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// POST /v1/connect/intentions
|
||||||
|
func (s *HTTPServer) IntentionCreate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||||
|
// Method is tested in IntentionEndpoint
|
||||||
|
|
||||||
|
args := structs.IntentionRequest{
|
||||||
|
Op: structs.IntentionOpCreate,
|
||||||
|
}
|
||||||
|
s.parseDC(req, &args.Datacenter)
|
||||||
|
s.parseToken(req, &args.Token)
|
||||||
|
if err := decodeBody(req, &args.Intention, nil); err != nil {
|
||||||
|
resp.WriteHeader(http.StatusBadRequest)
|
||||||
|
fmt.Fprintf(resp, "Request decode failed: %v", err)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var reply string
|
||||||
|
if err := s.agent.RPC("Intention.Apply", &args, &reply); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return intentionCreateResponse{reply}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// intentionCreateResponse is the response structure for creating an intention.
|
||||||
|
type intentionCreateResponse struct{ ID string }
|
||||||
|
|
|
@ -69,3 +69,43 @@ func TestIntentionsList_values(t *testing.T) {
|
||||||
t.Fatalf("bad: %#v", actual)
|
t.Fatalf("bad: %#v", actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIntentionsCreate_good(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
a := NewTestAgent(t.Name(), "")
|
||||||
|
defer a.Shutdown()
|
||||||
|
|
||||||
|
// Make sure an empty list is non-nil.
|
||||||
|
args := &structs.Intention{SourceName: "foo"}
|
||||||
|
req, _ := http.NewRequest("POST", "/v1/connect/intentions", jsonReader(args))
|
||||||
|
resp := httptest.NewRecorder()
|
||||||
|
obj, err := a.srv.IntentionCreate(resp, req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
value := obj.(intentionCreateResponse)
|
||||||
|
if value.ID == "" {
|
||||||
|
t.Fatalf("bad: %v", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the value
|
||||||
|
{
|
||||||
|
req := &structs.IntentionQueryRequest{
|
||||||
|
Datacenter: "dc1",
|
||||||
|
IntentionID: value.ID,
|
||||||
|
}
|
||||||
|
var resp structs.IndexedIntentions
|
||||||
|
if err := a.RPC("Intention.Get", req, &resp); err != nil {
|
||||||
|
t.Fatalf("err: %v", err)
|
||||||
|
}
|
||||||
|
if len(resp.Intentions) != 1 {
|
||||||
|
t.Fatalf("bad: %v", resp)
|
||||||
|
}
|
||||||
|
actual := resp.Intentions[0]
|
||||||
|
if actual.SourceName != "foo" {
|
||||||
|
t.Fatalf("bad: %#v", actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ type Intention struct {
|
||||||
|
|
||||||
// CreatedAt and UpdatedAt keep track of when this record was created
|
// CreatedAt and UpdatedAt keep track of when this record was created
|
||||||
// or modified.
|
// or modified.
|
||||||
CreatedAt, UpdatedAt time.Time
|
CreatedAt, UpdatedAt time.Time `mapstructure:"-"`
|
||||||
|
|
||||||
RaftIndex
|
RaftIndex
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue