agent: GET /v1/connect/intentions/:id
This commit is contained in:
parent
c78b82f43b
commit
37572829ab
|
@ -1,6 +1,7 @@
|
||||||
package consul
|
package consul
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/armon/go-metrics"
|
"github.com/armon/go-metrics"
|
||||||
|
@ -10,6 +11,11 @@ import (
|
||||||
"github.com/hashicorp/go-uuid"
|
"github.com/hashicorp/go-uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrIntentionNotFound is returned if the intention lookup failed.
|
||||||
|
ErrIntentionNotFound = errors.New("Intention not found")
|
||||||
|
)
|
||||||
|
|
||||||
// Intention manages the Connect intentions.
|
// Intention manages the Connect intentions.
|
||||||
type Intention struct {
|
type Intention struct {
|
||||||
// srv is a pointer back to the server.
|
// srv is a pointer back to the server.
|
||||||
|
@ -83,7 +89,7 @@ func (s *Intention) Get(
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if ixn == nil {
|
if ixn == nil {
|
||||||
return ErrQueryNotFound
|
return ErrIntentionNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
reply.Index = index
|
reply.Index = index
|
||||||
|
|
|
@ -39,7 +39,8 @@ func init() {
|
||||||
registerEndpoint("/v1/catalog/services", []string{"GET"}, (*HTTPServer).CatalogServices)
|
registerEndpoint("/v1/catalog/services", []string{"GET"}, (*HTTPServer).CatalogServices)
|
||||||
registerEndpoint("/v1/catalog/service/", []string{"GET"}, (*HTTPServer).CatalogServiceNodes)
|
registerEndpoint("/v1/catalog/service/", []string{"GET"}, (*HTTPServer).CatalogServiceNodes)
|
||||||
registerEndpoint("/v1/catalog/node/", []string{"GET"}, (*HTTPServer).CatalogNodeServices)
|
registerEndpoint("/v1/catalog/node/", []string{"GET"}, (*HTTPServer).CatalogNodeServices)
|
||||||
registerEndpoint("/v1/connect/intentions", []string{"GET", "POST"}, (*HTTPServer).IntentionList)
|
registerEndpoint("/v1/connect/intentions", []string{"GET", "POST"}, (*HTTPServer).IntentionEndpoint)
|
||||||
|
registerEndpoint("/v1/connect/intentions/", []string{"GET"}, (*HTTPServer).IntentionSpecific)
|
||||||
registerEndpoint("/v1/coordinate/datacenters", []string{"GET"}, (*HTTPServer).CoordinateDatacenters)
|
registerEndpoint("/v1/coordinate/datacenters", []string{"GET"}, (*HTTPServer).CoordinateDatacenters)
|
||||||
registerEndpoint("/v1/coordinate/nodes", []string{"GET"}, (*HTTPServer).CoordinateNodes)
|
registerEndpoint("/v1/coordinate/nodes", []string{"GET"}, (*HTTPServer).CoordinateNodes)
|
||||||
registerEndpoint("/v1/coordinate/node/", []string{"GET"}, (*HTTPServer).CoordinateNode)
|
registerEndpoint("/v1/coordinate/node/", []string{"GET"}, (*HTTPServer).CoordinateNode)
|
||||||
|
|
|
@ -3,7 +3,9 @@ package agent
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/agent/consul"
|
||||||
"github.com/hashicorp/consul/agent/structs"
|
"github.com/hashicorp/consul/agent/structs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -65,5 +67,51 @@ func (s *HTTPServer) IntentionCreate(resp http.ResponseWriter, req *http.Request
|
||||||
return intentionCreateResponse{reply}, nil
|
return intentionCreateResponse{reply}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IntentionSpecific handles the endpoint for /v1/connection/intentions/:id
|
||||||
|
func (s *HTTPServer) IntentionSpecific(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||||
|
id := strings.TrimPrefix(req.URL.Path, "/v1/connect/intentions/")
|
||||||
|
|
||||||
|
switch req.Method {
|
||||||
|
case "GET":
|
||||||
|
return s.IntentionSpecificGet(id, resp, req)
|
||||||
|
|
||||||
|
case "PUT":
|
||||||
|
panic("TODO")
|
||||||
|
|
||||||
|
case "DELETE":
|
||||||
|
panic("TODO")
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, MethodNotAllowedError{req.Method, []string{"GET", "PUT", "DELETE"}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /v1/connect/intentions/:id
|
||||||
|
func (s *HTTPServer) IntentionSpecificGet(id string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
||||||
|
// Method is tested in IntentionEndpoint
|
||||||
|
|
||||||
|
args := structs.IntentionQueryRequest{
|
||||||
|
IntentionID: id,
|
||||||
|
}
|
||||||
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var reply structs.IndexedIntentions
|
||||||
|
if err := s.agent.RPC("Intention.Get", &args, &reply); err != nil {
|
||||||
|
// We have to check the string since the RPC sheds the error type
|
||||||
|
if err.Error() == consul.ErrIntentionNotFound.Error() {
|
||||||
|
resp.WriteHeader(http.StatusNotFound)
|
||||||
|
fmt.Fprint(resp, err.Error())
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: validate length
|
||||||
|
return reply.Intentions[0], 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 }
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package agent
|
package agent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
@ -109,3 +110,45 @@ func TestIntentionsCreate_good(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIntentionsSpecificGet_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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the value
|
||||||
|
req, _ := http.NewRequest("GET", 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
value := obj.(*structs.Intention)
|
||||||
|
if value.ID != reply {
|
||||||
|
t.Fatalf("bad: %v", value)
|
||||||
|
}
|
||||||
|
|
||||||
|
ixn.ID = value.ID
|
||||||
|
ixn.RaftIndex = value.RaftIndex
|
||||||
|
if !reflect.DeepEqual(value, ixn) {
|
||||||
|
t.Fatalf("bad (got, want):\n\n%#v\n\n%#v", value, ixn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue