agent: PUT /v1/connect/intentions/:id

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

View File

@ -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

View File

@ -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()