Skip to content
This repository was archived by the owner on Apr 15, 2024. It is now read-only.

Commit 4a54b18

Browse files
author
Konstantin Nazarov
committed
Catalog API should support tokens
In a case where the acl_token is set to "anonymous" in consul server config, by default services in catalog are invisible, and trying to list them shows only "consul" service. In context of #116
1 parent 7a6fbb4 commit 4a54b18

1 file changed

Lines changed: 60 additions & 6 deletions

File tree

consul/base.py

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,13 @@ class Catalog(object):
932932
def __init__(self, agent):
933933
self.agent = agent
934934

935-
def register(self, node, address, service=None, check=None, dc=None):
935+
def register(self,
936+
node,
937+
address,
938+
service=None,
939+
check=None,
940+
dc=None,
941+
token=None):
936942
"""
937943
A low level mechanism for directly registering or updating entries
938944
in the catalog. It is usually recommended to use
@@ -980,6 +986,8 @@ def register(self, node, address, service=None, check=None, dc=None):
980986
*dc* is the datacenter of the node and defaults to this agents
981987
datacenter.
982988
989+
*token* is an optional `ACL token`_ to apply to this request.
990+
983991
This manipulates the health check entry, but does not setup a
984992
script or TTL to actually update the status. The full documentation
985993
is `here <https://consul.io/docs/agent/http.html#catalog>`_.
@@ -994,10 +1002,18 @@ def register(self, node, address, service=None, check=None, dc=None):
9941002
data['service'] = service
9951003
if check:
9961004
data['check'] = check
1005+
token = token or self.agent.token
1006+
if token:
1007+
data['token'] = token
9971008
return self.agent.http.put(
9981009
CB.bool(), '/v1/catalog/register', data=json.dumps(data))
9991010

1000-
def deregister(self, node, service_id=None, check_id=None, dc=None):
1011+
def deregister(self,
1012+
node,
1013+
service_id=None,
1014+
check_id=None,
1015+
dc=None,
1016+
token=None):
10011017
"""
10021018
A low level mechanism for directly removing entries in the catalog.
10031019
It is usually recommended to use the agent APIs, as they are
@@ -1009,6 +1025,8 @@ def deregister(self, node, service_id=None, check_id=None, dc=None):
10091025
and *check_id* should be provided and only that service or check
10101026
will be removed.
10111027
1028+
*token* is an optional `ACL token`_ to apply to this request.
1029+
10121030
Returns *True* on success.
10131031
"""
10141032
assert not (service_id and check_id)
@@ -1020,6 +1038,9 @@ def deregister(self, node, service_id=None, check_id=None, dc=None):
10201038
data['serviceid'] = service_id
10211039
if check_id:
10221040
data['checkid'] = check_id
1041+
token = token or self.agent.token
1042+
if token:
1043+
data['token'] = token
10231044
return self.agent.http.put(
10241045
CB.bool(), '/v1/catalog/deregister', data=json.dumps(data))
10251046

@@ -1036,7 +1057,8 @@ def nodes(
10361057
wait=None,
10371058
consistency=None,
10381059
dc=None,
1039-
near=None):
1060+
near=None,
1061+
token=None):
10401062
"""
10411063
Returns a tuple of (*index*, *nodes*) of all nodes known
10421064
about in the *dc* datacenter. *dc* defaults to the current
@@ -1056,6 +1078,8 @@ def nodes(
10561078
not specified *consistency* will the consistency level this client
10571079
was configured with.
10581080
1081+
*token* is an optional `ACL token`_ to apply to this request.
1082+
10591083
The response looks like this::
10601084
10611085
(index, [
@@ -1079,13 +1103,21 @@ def nodes(
10791103
params['wait'] = wait
10801104
if near:
10811105
params['near'] = near
1106+
token = token or self.agent.token
1107+
if token:
1108+
params['token'] = token
10821109
consistency = consistency or self.agent.consistency
10831110
if consistency in ('consistent', 'stale'):
10841111
params[consistency] = '1'
10851112
return self.agent.http.get(
10861113
CB.json(index=True), '/v1/catalog/nodes', params=params)
10871114

1088-
def services(self, index=None, wait=None, consistency=None, dc=None):
1115+
def services(self,
1116+
index=None,
1117+
wait=None,
1118+
consistency=None,
1119+
dc=None,
1120+
token=None):
10891121
"""
10901122
Returns a tuple of (*index*, *services*) of all services known
10911123
about in the *dc* datacenter. *dc* defaults to the current
@@ -1102,6 +1134,8 @@ def services(self, index=None, wait=None, consistency=None, dc=None):
11021134
not specified *consistency* will the consistency level this client
11031135
was configured with.
11041136
1137+
*token* is an optional `ACL token`_ to apply to this request.
1138+
11051139
The response looks like this::
11061140
11071141
(index, {
@@ -1124,13 +1158,22 @@ def services(self, index=None, wait=None, consistency=None, dc=None):
11241158
params['index'] = index
11251159
if wait:
11261160
params['wait'] = wait
1161+
token = token or self.agent.token
1162+
if token:
1163+
params['token'] = token
11271164
consistency = consistency or self.agent.consistency
11281165
if consistency in ('consistent', 'stale'):
11291166
params[consistency] = '1'
11301167
return self.agent.http.get(
11311168
CB.json(index=True), '/v1/catalog/services', params=params)
11321169

1133-
def node(self, node, index=None, wait=None, consistency=None, dc=None):
1170+
def node(self,
1171+
node,
1172+
index=None,
1173+
wait=None,
1174+
consistency=None,
1175+
dc=None,
1176+
token=None):
11341177
"""
11351178
Returns a tuple of (*index*, *services*) of all services provided
11361179
by *node*.
@@ -1149,6 +1192,8 @@ def node(self, node, index=None, wait=None, consistency=None, dc=None):
11491192
*dc* is the datacenter of the node and defaults to this agents
11501193
datacenter.
11511194
1195+
*token* is an optional `ACL token`_ to apply to this request.
1196+
11521197
The response looks like this::
11531198
11541199
(index, {
@@ -1182,6 +1227,9 @@ def node(self, node, index=None, wait=None, consistency=None, dc=None):
11821227
params['index'] = index
11831228
if wait:
11841229
params['wait'] = wait
1230+
token = token or self.agent.token
1231+
if token:
1232+
params['token'] = token
11851233
consistency = consistency or self.agent.consistency
11861234
if consistency in ('consistent', 'stale'):
11871235
params[consistency] = '1'
@@ -1198,7 +1246,8 @@ def service(
11981246
tag=None,
11991247
consistency=None,
12001248
dc=None,
1201-
near=None):
1249+
near=None,
1250+
token=None):
12021251
"""
12031252
Returns a tuple of (*index*, *nodes*) of the nodes providing
12041253
*service* in the *dc* datacenter. *dc* defaults to the current
@@ -1221,6 +1270,8 @@ def service(
12211270
not specified *consistency* will the consistency level this client
12221271
was configured with.
12231272
1273+
*token* is an optional `ACL token`_ to apply to this request.
1274+
12241275
The response looks like this::
12251276
12261277
(index, [
@@ -1246,6 +1297,9 @@ def service(
12461297
params['wait'] = wait
12471298
if near:
12481299
params['near'] = near
1300+
token = token or self.agent.token
1301+
if token:
1302+
params['token'] = token
12491303
consistency = consistency or self.agent.consistency
12501304
if consistency in ('consistent', 'stale'):
12511305
params[consistency] = '1'

0 commit comments

Comments
 (0)