Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/22189.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
http: Add peer query param on catalog service API
```
2 changes: 2 additions & 0 deletions agent/catalog_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ func (s *HTTPHandlers) catalogServiceNodes(resp http.ResponseWriter, req *http.R
return nil, nil
}

s.parsePeerName(req, &args)

// Check for a tag
params := req.URL.Query()
if _, ok := params["tag"]; ok {
Expand Down
70 changes: 70 additions & 0 deletions agent/catalog_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,76 @@ func TestCatalogServiceNodes_Filter(t *testing.T) {
require.Len(t, nodes, 1)
}

func TestCatalogServiceNodes_PeerFilter(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}

t.Parallel()
a := StartTestAgent(t, TestAgent{HCL: "", Overrides: `peering = { test_allow_peer_registrations = true }`})
defer a.Shutdown()

peerName := "test"
queryPath := "/v1/catalog/service/api?filter=" + url.QueryEscape("ServiceMeta.somekey == somevalue") + peerQuerySuffix(peerName)

// Make sure an empty list is returned, not a nil
{
req, _ := http.NewRequest("GET", queryPath, nil)
resp := httptest.NewRecorder()
obj, err := a.srv.CatalogServiceNodes(resp, req)
require.NoError(t, err)

assertIndex(t, resp)

nodes := obj.(structs.ServiceNodes)
require.Empty(t, nodes)
}

// Register node
args := &structs.RegisterRequest{
Datacenter: "dc1",
Node: "foo",
Address: "127.0.0.1",
PeerName: peerName,
Service: &structs.NodeService{
Service: "api",
Meta: map[string]string{
"somekey": "somevalue",
},
},
}

var out struct{}
require.NoError(t, a.RPC(context.Background(), "Catalog.Register", args, &out))

// Register a second service for the node
args = &structs.RegisterRequest{
Datacenter: "dc1",
Node: "foo",
Address: "127.0.0.1",
PeerName: peerName,
Service: &structs.NodeService{
ID: "api2",
Service: "api",
Meta: map[string]string{
"somekey": "notvalue",
},
},
SkipNodeUpdate: true,
}

require.NoError(t, a.RPC(context.Background(), "Catalog.Register", args, &out))

req, _ := http.NewRequest("GET", queryPath, nil)
resp := httptest.NewRecorder()
obj, err := a.srv.CatalogServiceNodes(resp, req)
require.NoError(t, err)
assertIndex(t, resp)

nodes := obj.(structs.ServiceNodes)
require.Len(t, nodes, 1)
}

func TestCatalogServiceNodes_WanTranslation(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
Expand Down
2 changes: 2 additions & 0 deletions website/content/api-docs/catalog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,8 @@ The table below shows this endpoint's support for
- `filter` `(string: "")` - Specifies the expression used to filter the
queries results prior to returning the data.

- `peer` `(string: "")` - Specifies the imported service's peer. Applies only to imported services.

- `merge-central-config` - Include this flag in a request for `connect-proxy` kind or `*-gateway` kind
services to return a fully resolved service definition that includes merged values from the
[proxy-defaults/global](/consul/docs/connect/config-entries/proxy-defaults) and
Expand Down
Loading