From dca483b4a2adab888ecbe9bb447770a8db41792e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 1 Mar 2018 16:53:31 -0800 Subject: [PATCH] agent: PUT /v1/connect/intentions/:id --- agent/intentions_endpoint.go | 29 +++++++++++++++- agent/intentions_endpoint_test.go | 55 +++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/agent/intentions_endpoint.go b/agent/intentions_endpoint.go index 9f974309e3..40a9f22822 100644 --- a/agent/intentions_endpoint.go +++ b/agent/intentions_endpoint.go @@ -76,7 +76,7 @@ func (s *HTTPServer) IntentionSpecific(resp http.ResponseWriter, req *http.Reque return s.IntentionSpecificGet(id, resp, req) case "PUT": - panic("TODO") + return s.IntentionSpecificUpdate(id, resp, req) case "DELETE": return s.IntentionSpecificDelete(id, resp, req) @@ -113,6 +113,33 @@ func (s *HTTPServer) IntentionSpecificGet(id string, resp http.ResponseWriter, r return reply.Intentions[0], nil } +// PUT /v1/connect/intentions/:id +func (s *HTTPServer) IntentionSpecificUpdate(id string, resp http.ResponseWriter, req *http.Request) (interface{}, error) { + // Method is tested in IntentionEndpoint + + args := structs.IntentionRequest{ + Op: structs.IntentionOpUpdate, + } + 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 + } + + // Use the ID from the URL + args.Intention.ID = id + + var reply string + if err := s.agent.RPC("Intention.Apply", &args, &reply); err != nil { + return nil, err + } + + return nil, nil + +} + // DELETE /v1/connect/intentions/:id func (s *HTTPServer) IntentionSpecificDelete(id string, resp http.ResponseWriter, req *http.Request) (interface{}, error) { // Method is tested in IntentionEndpoint diff --git a/agent/intentions_endpoint_test.go b/agent/intentions_endpoint_test.go index d38fc6c434..c3753ea97a 100644 --- a/agent/intentions_endpoint_test.go +++ b/agent/intentions_endpoint_test.go @@ -154,6 +154,61 @@ func TestIntentionsSpecificGet_good(t *testing.T) { } } +func TestIntentionsSpecificUpdate_good(t *testing.T) { + t.Parallel() + + a := NewTestAgent(t.Name(), "") + defer a.Shutdown() + + // The intention + ixn := &structs.Intention{SourceName: "foo"} + + // Create an intention directly + var reply string + { + req := structs.IntentionRequest{ + Datacenter: "dc1", + Op: structs.IntentionOpCreate, + Intention: ixn, + } + if err := a.RPC("Intention.Apply", &req, &reply); err != nil { + t.Fatalf("err: %s", err) + } + } + + // Update the intention + ixn.ID = "bogus" + ixn.SourceName = "bar" + req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/connect/intentions/%s", reply), jsonReader(ixn)) + resp := httptest.NewRecorder() + obj, err := a.srv.IntentionSpecific(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } + if obj != nil { + t.Fatalf("obj should be nil: %v", err) + } + + // Read the value + { + req := &structs.IntentionQueryRequest{ + Datacenter: "dc1", + IntentionID: reply, + } + 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 != "bar" { + t.Fatalf("bad: %#v", actual) + } + } +} + func TestIntentionsSpecificDelete_good(t *testing.T) { t.Parallel()