From c78b82f43b5573c51c9cb6c902e39beb770087a1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 28 Feb 2018 14:02:00 -0800 Subject: [PATCH] agent: POST /v1/connect/intentions --- agent/http_oss.go | 2 +- agent/intentions_endpoint.go | 47 ++++++++++++++++++++++++++++--- agent/intentions_endpoint_test.go | 40 ++++++++++++++++++++++++++ agent/structs/intention.go | 2 +- 4 files changed, 85 insertions(+), 6 deletions(-) diff --git a/agent/http_oss.go b/agent/http_oss.go index 28ece14ae..61bef8d2a 100644 --- a/agent/http_oss.go +++ b/agent/http_oss.go @@ -39,7 +39,7 @@ func init() { registerEndpoint("/v1/catalog/services", []string{"GET"}, (*HTTPServer).CatalogServices) registerEndpoint("/v1/catalog/service/", []string{"GET"}, (*HTTPServer).CatalogServiceNodes) 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/nodes", []string{"GET"}, (*HTTPServer).CoordinateNodes) registerEndpoint("/v1/coordinate/node/", []string{"GET"}, (*HTTPServer).CoordinateNode) diff --git a/agent/intentions_endpoint.go b/agent/intentions_endpoint.go index 0cdd0dc43..62340e7e7 100644 --- a/agent/intentions_endpoint.go +++ b/agent/intentions_endpoint.go @@ -1,16 +1,29 @@ package agent import ( + "fmt" "net/http" "github.com/hashicorp/consul/agent/structs" ) -// /v1/connect/intentions -func (s *HTTPServer) IntentionList(resp http.ResponseWriter, req *http.Request) (interface{}, error) { - if req.Method != "GET" { - return nil, MethodNotAllowedError{req.Method, []string{"GET"}} +// /v1/connection/intentions +func (s *HTTPServer) IntentionEndpoint(resp http.ResponseWriter, req *http.Request) (interface{}, error) { + switch req.Method { + 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 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 } + +// 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 } diff --git a/agent/intentions_endpoint_test.go b/agent/intentions_endpoint_test.go index 2e56eabf7..db6a16580 100644 --- a/agent/intentions_endpoint_test.go +++ b/agent/intentions_endpoint_test.go @@ -69,3 +69,43 @@ func TestIntentionsList_values(t *testing.T) { 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) + } + } +} diff --git a/agent/structs/intention.go b/agent/structs/intention.go index cce6e3e0f..7255bc8f1 100644 --- a/agent/structs/intention.go +++ b/agent/structs/intention.go @@ -39,7 +39,7 @@ type Intention struct { // CreatedAt and UpdatedAt keep track of when this record was created // or modified. - CreatedAt, UpdatedAt time.Time + CreatedAt, UpdatedAt time.Time `mapstructure:"-"` RaftIndex }