Skip to content

Commit

Permalink
catalog services endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodingenthusiast committed Nov 21, 2023
1 parent 295243e commit e265f50
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
21 changes: 16 additions & 5 deletions dependency/catalog_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
_ Dependency = (*CatalogServicesQuery)(nil)

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

func init() {
Expand All @@ -37,7 +37,9 @@ type CatalogSnippet struct {
type CatalogServicesQuery struct {
stopCh chan struct{}

dc string
dc string
namespace string
partition string
}

// NewCatalogServicesQuery parses a string of the format @dc.
Expand All @@ -47,9 +49,16 @@ func NewCatalogServicesQuery(s string) (*CatalogServicesQuery, error) {
}

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

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

Expand All @@ -63,7 +72,9 @@ func (d *CatalogServicesQuery) Fetch(clients *ClientSet, opts *QueryOptions) (in
}

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

log.Printf("[TRACE] %s: GET %s", d, &url.URL{
Expand Down
41 changes: 41 additions & 0 deletions dependency/catalog_services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ func TestNewCatalogServicesQuery(t *testing.T) {
&CatalogServicesQuery{},
false,
},
{
"invalid query param (unsupported key)",
"?unsupported=foo",
nil,
true,
},
{
"node",
"node",
Expand All @@ -37,6 +43,41 @@ func TestNewCatalogServicesQuery(t *testing.T) {
},
false,
},
{
"namespace",
"?ns=foo",
&CatalogServicesQuery{
namespace: "foo",
},
false,
},
{
"partition",
"?partition=foo",
&CatalogServicesQuery{
partition: "foo",
},
false,
},
{
"partition_and_namespace",
"?namespace=foo&partition=bar",
&CatalogServicesQuery{
namespace: "foo",
partition: "bar",
},
false,
},
{
"partition_and_namespace_and_dc",
"?namespace=foo&partition=bar@dc1",
&CatalogServicesQuery{
namespace: "foo",
partition: "bar",
dc: "dc1",
},
false,
},
}

for i, tc := range cases {
Expand Down

0 comments on commit e265f50

Please sign in to comment.