agent: DELETE /v1/connect/intentions/:id

This commit is contained in:
Mitchell Hashimoto 2018-03-01 15:54:03 -08:00
parent fb02e53536
commit faeb583162
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 88 additions and 1 deletions

View File

@ -79,7 +79,7 @@ func (s *HTTPServer) IntentionSpecific(resp http.ResponseWriter, req *http.Reque
panic("TODO") panic("TODO")
case "DELETE": case "DELETE":
panic("TODO") return s.IntentionSpecificDelete(id, resp, req)
default: default:
return nil, MethodNotAllowedError{req.Method, []string{"GET", "PUT", "DELETE"}} 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 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. // intentionCreateResponse is the response structure for creating an intention.
type intentionCreateResponse struct{ ID string } type intentionCreateResponse struct{ ID string }

View File

@ -6,6 +6,7 @@ import (
"net/http/httptest" "net/http/httptest"
"reflect" "reflect"
"sort" "sort"
"strings"
"testing" "testing"
"github.com/hashicorp/consul/agent/structs" "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) 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)
}
}
}