agent: Support ACL upserting

This commit is contained in:
Armon Dadgar 2015-05-05 19:25:10 -07:00
parent cc9bd0a00b
commit 532c06ac43
2 changed files with 34 additions and 10 deletions

View File

@ -2,9 +2,10 @@ package agent
import ( import (
"fmt" "fmt"
"github.com/hashicorp/consul/consul/structs"
"net/http" "net/http"
"strings" "strings"
"github.com/hashicorp/consul/consul/structs"
) )
// aclCreateResponse is used to wrap the ACL ID // aclCreateResponse is used to wrap the ACL ID
@ -80,14 +81,8 @@ func (s *HTTPServer) aclSet(resp http.ResponseWriter, req *http.Request, update
} }
} }
// Ensure there is no ID set for create // Ensure there is an ID set for update. ID is optional for
if !update && args.ACL.ID != "" { // create, as one will be generated if not provided.
resp.WriteHeader(400)
resp.Write([]byte(fmt.Sprintf("ACL ID cannot be set")))
return nil, nil
}
// Ensure there is an ID set for update
if update && args.ACL.ID == "" { if update && args.ACL.ID == "" {
resp.WriteHeader(400) resp.WriteHeader(400)
resp.Write([]byte(fmt.Sprintf("ACL ID must be set"))) resp.Write([]byte(fmt.Sprintf("ACL ID must be set")))

View File

@ -3,10 +3,11 @@ package agent
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"github.com/hashicorp/consul/consul/structs"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"github.com/hashicorp/consul/consul/structs"
) )
func makeTestACL(t *testing.T, srv *HTTPServer) string { func makeTestACL(t *testing.T, srv *HTTPServer) string {
@ -62,6 +63,34 @@ func TestACLUpdate(t *testing.T) {
}) })
} }
func TestACLUpdate_Upsert(t *testing.T) {
httpTest(t, func(srv *HTTPServer) {
body := bytes.NewBuffer(nil)
enc := json.NewEncoder(body)
raw := map[string]interface{}{
"ID": "my-old-id",
"Name": "User Token 2",
"Type": "client",
"Rules": "",
}
enc.Encode(raw)
req, err := http.NewRequest("PUT", "/v1/acl/update?token=root", body)
if err != nil {
t.Fatalf("err: %v", err)
}
resp := httptest.NewRecorder()
obj, err := srv.ACLUpdate(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
aclResp := obj.(aclCreateResponse)
if aclResp.ID != "my-old-id" {
t.Fatalf("bad: %v", aclResp)
}
})
}
func TestACLDestroy(t *testing.T) { func TestACLDestroy(t *testing.T) {
httpTest(t, func(srv *HTTPServer) { httpTest(t, func(srv *HTTPServer) {
id := makeTestACL(t, srv) id := makeTestACL(t, srv)