From e265f50e29d1ccbfe42044b1520fe473e0c15c57 Mon Sep 17 00:00:00 2001 From: Ronald Ekambi Date: Tue, 21 Nov 2023 13:32:19 -0500 Subject: [PATCH] catalog services endpoint --- dependency/catalog_services.go | 21 +++++++++++---- dependency/catalog_services_test.go | 41 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/dependency/catalog_services.go b/dependency/catalog_services.go index 5a89028e7..af62e2c1c 100644 --- a/dependency/catalog_services.go +++ b/dependency/catalog_services.go @@ -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() { @@ -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. @@ -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 } @@ -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{ diff --git a/dependency/catalog_services_test.go b/dependency/catalog_services_test.go index 76fc4cbc7..cfaf74a43 100644 --- a/dependency/catalog_services_test.go +++ b/dependency/catalog_services_test.go @@ -23,6 +23,12 @@ func TestNewCatalogServicesQuery(t *testing.T) { &CatalogServicesQuery{}, false, }, + { + "invalid query param (unsupported key)", + "?unsupported=foo", + nil, + true, + }, { "node", "node", @@ -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 {