agent/consul: Intention.Get endpoint

This commit is contained in:
Mitchell Hashimoto 2018-02-28 10:44:49 -08:00
parent 9e307e178e
commit e8c4156f07
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 72 additions and 1 deletions

View File

@ -65,6 +65,37 @@ func (s *Intention) Apply(
return nil
}
// Get returns a single intention by ID.
func (s *Intention) Get(
args *structs.IntentionQueryRequest,
reply *structs.IndexedIntentions) error {
// Forward if necessary
if done, err := s.srv.forward("Intention.Get", args, args, reply); done {
return err
}
return s.srv.blockingQuery(
&args.QueryOptions,
&reply.QueryMeta,
func(ws memdb.WatchSet, state *state.Store) error {
index, ixn, err := state.IntentionGet(ws, args.IntentionID)
if err != nil {
return err
}
if ixn == nil {
return ErrQueryNotFound
}
reply.Index = index
reply.Intentions = structs.Intentions{ixn}
// TODO: acl filtering
return nil
},
)
}
// List returns all the intentions.
func (s *Intention) List(
args *structs.DCSpecificRequest,

View File

@ -2,6 +2,7 @@ package consul
import (
"os"
"reflect"
"testing"
"github.com/hashicorp/consul/agent/structs"
@ -37,7 +38,29 @@ func TestIntentionApply_new(t *testing.T) {
t.Fatal("reply should be non-empty")
}
// TODO test read
// Read
ixn.Intention.ID = reply
{
req := &structs.IntentionQueryRequest{
Datacenter: "dc1",
IntentionID: ixn.Intention.ID,
}
var resp structs.IndexedIntentions
if err := msgpackrpc.CallWithCodec(codec, "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 resp.Index != actual.ModifyIndex {
t.Fatalf("bad index: %d", resp.Index)
}
actual.CreateIndex, actual.ModifyIndex = 0, 0
if !reflect.DeepEqual(actual, ixn.Intention) {
t.Fatalf("bad: %v", actual)
}
}
}
func TestIntentionList(t *testing.T) {

View File

@ -99,3 +99,20 @@ type IntentionRequest struct {
func (q *IntentionRequest) RequestDatacenter() string {
return q.Datacenter
}
// IntentionQueryRequest is used to query intentions.
type IntentionQueryRequest struct {
// Datacenter is the target this request is intended for.
Datacenter string
// IntentionID is the ID of a specific intention.
IntentionID string
// Options for queries
QueryOptions
}
// RequestDatacenter returns the datacenter for a given request.
func (q *IntentionQueryRequest) RequestDatacenter() string {
return q.Datacenter
}