mirror of
https://github.com/status-im/consul.git
synced 2025-01-10 22:06:20 +00:00
agent/consul: Intention.Get endpoint
This commit is contained in:
parent
9e307e178e
commit
e8c4156f07
@ -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,
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user