diff --git a/agent/intentions_endpoint.go b/agent/intentions_endpoint.go index d5d6b6495b..9f974309e3 100644 --- a/agent/intentions_endpoint.go +++ b/agent/intentions_endpoint.go @@ -79,7 +79,7 @@ func (s *HTTPServer) IntentionSpecific(resp http.ResponseWriter, req *http.Reque panic("TODO") case "DELETE": - panic("TODO") + return s.IntentionSpecificDelete(id, resp, req) default: return nil, MethodNotAllowedError{req.Method, []string{"GET", "PUT", "DELETE"}} @@ -113,5 +113,24 @@ func (s *HTTPServer) IntentionSpecificGet(id string, resp http.ResponseWriter, r return reply.Intentions[0], 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 + + args := structs.IntentionRequest{ + Op: structs.IntentionOpDelete, + Intention: &structs.Intention{ID: id}, + } + s.parseDC(req, &args.Datacenter) + s.parseToken(req, &args.Token) + + var reply string + if err := s.agent.RPC("Intention.Apply", &args, &reply); err != nil { + return nil, err + } + + return nil, 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 0bd956842b..d38fc6c434 100644 --- a/agent/intentions_endpoint_test.go +++ b/agent/intentions_endpoint_test.go @@ -6,6 +6,7 @@ import ( "net/http/httptest" "reflect" "sort" + "strings" "testing" "github.com/hashicorp/consul/agent/structs" @@ -152,3 +153,70 @@ func TestIntentionsSpecificGet_good(t *testing.T) { t.Fatalf("bad (got, want):\n\n%#v\n\n%#v", value, ixn) } } + +func TestIntentionsSpecificDelete_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) + } + } + + // Sanity check that the intention exists + { + 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 != "foo" { + t.Fatalf("bad: %#v", actual) + } + } + + // Delete the intention + req, _ := http.NewRequest("DELETE", fmt.Sprintf("/v1/connect/intentions/%s", reply), nil) + 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) + } + + // Verify the intention is gone + { + req := &structs.IntentionQueryRequest{ + Datacenter: "dc1", + IntentionID: reply, + } + var resp structs.IndexedIntentions + err := a.RPC("Intention.Get", req, &resp) + if err == nil || !strings.Contains(err.Error(), "not found") { + t.Fatalf("err: %v", err) + } + } + +}