Skip to content

Commit

Permalink
catalog endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodingenthusiast committed Nov 21, 2023
1 parent 3e9f63f commit c639cbb
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 32 deletions.
25 changes: 18 additions & 7 deletions dependency/catalog_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
_ Dependency = (*CatalogNodeQuery)(nil)

// CatalogNodeQueryRe is the regular expression to use.
CatalogNodeQueryRe = regexp.MustCompile(`\A` + nodeNameRe + dcRe + `\z`)
CatalogNodeQueryRe = regexp.MustCompile(`\A` + nodeNameRe + queryRe + dcRe + `\z`)
)

func init() {
Expand All @@ -31,8 +31,10 @@ func init() {
type CatalogNodeQuery struct {
stopCh chan struct{}

dc string
name string
dc string
name string
namespace string
partition string
}

// CatalogNode is a wrapper around the node and its services.
Expand Down Expand Up @@ -60,10 +62,17 @@ func NewCatalogNodeQuery(s string) (*CatalogNodeQuery, error) {
}

m := regexpMatch(CatalogNodeQueryRe, s)
queryParams, err := GetConsulQueryOpts(m, "catalog.node")
if err != nil {
return nil, err
}

return &CatalogNodeQuery{
dc: m["dc"],
name: m["name"],
stopCh: make(chan struct{}, 1),
dc: m["dc"],
name: m["name"],
stopCh: make(chan struct{}, 1),
namespace: queryParams.Get(QueryNamespace),
partition: queryParams.Get(QueryPartition),
}, nil
}

Expand All @@ -77,7 +86,9 @@ func (d *CatalogNodeQuery) Fetch(clients *ClientSet, opts *QueryOptions) (interf
}

opts = opts.Merge(&QueryOptions{
Datacenter: d.dc,
Datacenter: d.dc,
ConsulPartition: d.partition,
ConsulNamespace: d.namespace,
})

// Grab the name
Expand Down
23 changes: 23 additions & 0 deletions dependency/catalog_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ func TestNewCatalogNodeQuery(t *testing.T) {
&CatalogNodeQuery{},
false,
},
{
"invalid query param (unsupported key)",
"key?unsupported=foo",
nil,
true,
},
{
"bad",
"!4d",
Expand All @@ -35,6 +41,12 @@ func TestNewCatalogNodeQuery(t *testing.T) {
nil,
true,
},
{
"query_only",
"?ns=foo",
nil,
true,
},
{
"node",
"node",
Expand All @@ -52,6 +64,17 @@ func TestNewCatalogNodeQuery(t *testing.T) {
},
false,
},
{
"every_option",
"node?ns=foo&partition=bar@dc1",
&CatalogNodeQuery{
name: "node",
dc: "dc1",
namespace: "foo",
partition: "bar",
},
false,
},
{
"periods",
"node.bar.com@dc1",
Expand Down
27 changes: 19 additions & 8 deletions dependency/catalog_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
_ Dependency = (*CatalogNodesQuery)(nil)

// CatalogNodesQueryRe is the regular expression to use.
CatalogNodesQueryRe = regexp.MustCompile(`\A` + dcRe + nearRe + `\z`)
CatalogNodesQueryRe = regexp.MustCompile(`\A` + queryRe + dcRe + nearRe + `\z`)
)

func init() {
Expand All @@ -40,8 +40,10 @@ type Node struct {
type CatalogNodesQuery struct {
stopCh chan struct{}

dc string
near string
dc string
near string
namespace string
partition string
}

// NewCatalogNodesQuery parses the given string into a dependency. If the name is
Expand All @@ -52,10 +54,17 @@ func NewCatalogNodesQuery(s string) (*CatalogNodesQuery, error) {
}

m := regexpMatch(CatalogNodesQueryRe, s)
queryParams, err := GetConsulQueryOpts(m, "catalog.nodes")
if err != nil {
return nil, err
}

return &CatalogNodesQuery{
dc: m["dc"],
near: m["near"],
stopCh: make(chan struct{}, 1),
dc: m["dc"],
near: m["near"],
stopCh: make(chan struct{}, 1),
namespace: queryParams.Get(QueryNamespace),
partition: queryParams.Get(QueryPartition),
}, nil
}

Expand All @@ -69,8 +78,10 @@ func (d *CatalogNodesQuery) Fetch(clients *ClientSet, opts *QueryOptions) (inter
}

opts = opts.Merge(&QueryOptions{
Datacenter: d.dc,
Near: d.near,
Datacenter: d.dc,
Near: d.near,
ConsulPartition: d.partition,
ConsulNamespace: d.namespace,
})

log.Printf("[TRACE] %s: GET %s", d, &url.URL{
Expand Down
61 changes: 61 additions & 0 deletions dependency/catalog_nodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ func TestNewCatalogNodesQuery(t *testing.T) {
&CatalogNodesQuery{},
false,
},
{
"invalid query param (unsupported key)",
"key?unsupported=foo",
nil,
true,
},
{
"node",
"node",
Expand All @@ -37,6 +43,41 @@ func TestNewCatalogNodesQuery(t *testing.T) {
},
false,
},
{
"namespace",
"?ns=foo",
&CatalogNodesQuery{
namespace: "foo",
},
false,
},
{
"partition",
"?partition=foo",
&CatalogNodesQuery{
partition: "foo",
},
false,
},
{
"namespace_and_partition",
"?ns=foo&partition=bar",
&CatalogNodesQuery{
namespace: "foo",
partition: "bar",
},
false,
},
{
"namespace_and_partition_and_near",
"?ns=foo&partition=bar~node1",
&CatalogNodesQuery{
namespace: "foo",
partition: "bar",
near: "node1",
},
false,
},
{
"near",
"~node1",
Expand All @@ -54,6 +95,26 @@ func TestNewCatalogNodesQuery(t *testing.T) {
},
false,
},
{
"query_near",
"?ns=foo~node1",
&CatalogNodesQuery{
namespace: "foo",
near: "node1",
},
false,
},
{
"every_option",
"?ns=foo&partition=bar@dc1~node1",
&CatalogNodesQuery{
dc: "dc1",
near: "node1",
partition: "bar",
namespace: "foo",
},
false,
},
}

for i, tc := range cases {
Expand Down
33 changes: 22 additions & 11 deletions dependency/catalog_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ type CatalogService struct {
type CatalogServiceQuery struct {
stopCh chan struct{}

dc string
name string
near string
tag string
dc string
name string
near string
tag string
namespace string
partition string
}

// NewCatalogServiceQuery parses a string into a CatalogServiceQuery.
Expand All @@ -59,12 +61,19 @@ func NewCatalogServiceQuery(s string) (*CatalogServiceQuery, error) {
}

m := regexpMatch(CatalogServiceQueryRe, s)
queryParams, err := GetConsulQueryOpts(m, "catalog.service")
if err != nil {
return nil, err
}

return &CatalogServiceQuery{
stopCh: make(chan struct{}, 1),
dc: m["dc"],
name: m["name"],
near: m["near"],
tag: m["tag"],
stopCh: make(chan struct{}, 1),
dc: m["dc"],
name: m["name"],
near: m["near"],
tag: m["tag"],
namespace: queryParams.Get(QueryNamespace),
partition: queryParams.Get(QueryPartition),
}, nil
}

Expand All @@ -78,8 +87,10 @@ func (d *CatalogServiceQuery) Fetch(clients *ClientSet, opts *QueryOptions) (int
}

opts = opts.Merge(&QueryOptions{
Datacenter: d.dc,
Near: d.near,
Datacenter: d.dc,
Near: d.near,
ConsulPartition: d.partition,
ConsulNamespace: d.namespace,
})

u := &url.URL{
Expand Down
45 changes: 39 additions & 6 deletions dependency/catalog_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ func TestNewCatalogServiceQuery(t *testing.T) {
nil,
true,
},
{
"query_only",
"?ns=foo",
nil,
true,
},
{
"invalid query param (unsupported key)",
"name?unsupported=foo",
nil,
true,
},
{
"near_only",
"~near",
Expand Down Expand Up @@ -58,6 +70,15 @@ func TestNewCatalogServiceQuery(t *testing.T) {
},
false,
},
{
"name_query",
"name?ns=foo",
&CatalogServiceQuery{
name: "name",
namespace: "foo",
},
false,
},
{
"name_dc_near",
"name@dc1~near",
Expand All @@ -68,6 +89,16 @@ func TestNewCatalogServiceQuery(t *testing.T) {
},
false,
},
{
"name_query_near",
"name?ns=foo~near",
&CatalogServiceQuery{
name: "name",
near: "near",
namespace: "foo",
},
false,
},
{
"name_near",
"name~near",
Expand Down Expand Up @@ -107,13 +138,15 @@ func TestNewCatalogServiceQuery(t *testing.T) {
false,
},
{
"tag_name_dc_near",
"tag.name@dc~near",
"every_option",
"tag.name?ns=foo&partition=bar@dc~near",
&CatalogServiceQuery{
dc: "dc",
name: "name",
near: "near",
tag: "tag",
dc: "dc",
name: "name",
near: "near",
tag: "tag",
namespace: "foo",
partition: "bar",
},
false,
},
Expand Down

0 comments on commit c639cbb

Please sign in to comment.