Skip to content

Commit e4ae57c

Browse files
committed
Add agent models and update API
1 parent 98e3eb9 commit e4ae57c

File tree

5 files changed

+175
-161
lines changed

5 files changed

+175
-161
lines changed

consulate/adapters.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# coding=utf-8
12
"""
23
HTTP Client Library Adapters
34

consulate/api/agent.py

Lines changed: 72 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
"""
55
from consulate.api import base
6+
from consulate.models import agent as models
67

78
_TOKENS = [
89
'acl_token',
@@ -12,35 +13,6 @@
1213
]
1314

1415

15-
def _validate_check(script, http, interval, ttl):
16-
"""Validate the check arguments passed into check or service creation.
17-
18-
:param script: The optional script to run in the check
19-
:type script: str or None
20-
:param http: The optional HTTP endpoint to use in the check
21-
:type http: str or None
22-
:param interval: The optional check interval to specify
23-
:type interval: int or None
24-
:param ttl: The optional TTL interval for the check
25-
:type ttl: int or None
26-
:raises: ValueError
27-
28-
"""
29-
if script is not None and http is not None:
30-
raise ValueError('Can not specify script and http in the same check')
31-
if (script is not None or http is not None) and ttl is not None:
32-
raise ValueError('Can not specify a script or http check and ttl')
33-
elif (script or http) and interval is None:
34-
raise ValueError(
35-
'An interval is required for check scripts and '
36-
'http checks.')
37-
elif interval is not None and \
38-
(not isinstance(interval, int) or interval < 1):
39-
raise ValueError('interval must be a positive integer')
40-
elif ttl is not None and (not isinstance(ttl, int) or ttl < 1):
41-
raise ValueError('ttl must be a positive integer')
42-
43-
4416
class Agent(base.Endpoint):
4517
"""The Consul agent is the core process of Consul. The agent maintains
4618
membership information, registers services, runs checks, responds to
@@ -92,55 +64,74 @@ class Check(base.Endpoint):
9264
9365
"""
9466

95-
def register(self, name,
96-
script=None,
67+
def register(self,
68+
name,
9769
check_id=None,
9870
interval=None,
99-
ttl=None,
10071
notes=None,
101-
http=None):
72+
deregister_critical_service_after=None,
73+
args=None,
74+
docker_container_id=None,
75+
grpc=None,
76+
grpc_use_tls=None,
77+
http=None,
78+
http_method=None,
79+
header=None,
80+
timeout=None,
81+
tls_skip_verify=None,
82+
tcp=None,
83+
ttl=None,
84+
service_id=None,
85+
status=None):
10286
"""Add a new check to the local agent. Checks are either a script
10387
or TTL type. The agent is responsible for managing the status of
10488
the check and keeping the Catalog in sync.
10589
106-
The ``name`` field is mandatory, as is either ``script`` and
107-
``interval``, ``http`` and ``interval`` or ``ttl``.
108-
Only one of ``script`` and ``interval``, ``http`` and ``interval``
109-
or ``ttl`` should be provided. If an ``check_id`` is not
110-
provided, it is set to ``name``. You cannot have duplicate
111-
``check_id`` entries per agent, so it may be necessary to provide
112-
a ``check_id``. The ``notes`` field is not used by Consul, and is
113-
meant to be human readable.
114-
115-
If a ``script`` is provided, the check type is a script, and Consul
116-
will evaluate the script every ``interval`` to update the status.
117-
If a ``http`` URL is provided, Consul will poll the URL every
118-
``interval`` to update the status - only 2xx results are considered
119-
healthy.
120-
If a ``ttl`` type is used, then the ``ttl`` update APIs must be
121-
used to periodically update the state of the check.
122-
123-
:param str name: The check name
124-
:param str http: The URL to poll for health checks
125-
:param str script: The path to the script to run
126-
:param str check_id: The optional check id
127-
:param int interval: The interval to run the check
128-
:param int ttl: The ttl to specify for the check
129-
:param str notes: Administrative notes.
90+
:param str name:
91+
:param str check_id:
92+
:param str interval:
93+
:param str notes:
94+
:param str deregister_critical_service_after:
95+
:param str args:
96+
:param str docker_container_id:
97+
:param str grpc:
98+
:param str grpc_use_tls:
99+
:param str http:
100+
:param str http_method:
101+
:param str header:
102+
:param str timeout:
103+
:param str tls_skip_verify:
104+
:param str tcp:
105+
:param str ttl:
106+
:param str service_id:
107+
:param str status:
108+
130109
:rtype: bool
131110
:raises: ValueError
132111
133112
"""
134-
_validate_check(script, http, interval, ttl)
135-
return self._put_no_response_body(['register'], None, {
136-
'ID': check_id,
137-
'Name': name,
138-
'Notes': notes,
139-
'Script': script,
140-
'HTTP': http,
141-
'Interval': interval,
142-
'TTL': ttl
143-
})
113+
return self._put_no_response_body(
114+
['register'], None, dict(
115+
models.Check(
116+
name=name,
117+
id=check_id,
118+
interval=interval,
119+
notes=notes,
120+
deregister_critical_service_after=
121+
deregister_critical_service_after,
122+
args=args,
123+
docker_container_id=docker_container_id,
124+
grpc=grpc,
125+
grpc_use_tls=grpc_use_tls,
126+
http=http,
127+
method=http_method,
128+
header=header,
129+
timeout=timeout,
130+
tls_skip_verify=tls_skip_verify,
131+
tcp=tcp,
132+
ttl=ttl,
133+
service_id=service_id,
134+
status=status)))
144135

145136
def deregister(self, check_id):
146137
"""Remove a check from the local agent. The agent will take care
@@ -201,15 +192,14 @@ class Service(base.Endpoint):
201192
the HTTP interface.
202193
203194
"""
204-
def register(self, name,
195+
def register(self,
196+
name,
205197
service_id=None,
206198
address=None,
207199
port=None,
208200
tags=None,
209-
script=None,
210-
interval=None,
211-
ttl=None,
212-
http=None,
201+
check=None,
202+
checks=None,
213203
enable_tag_override=None):
214204
"""Add a new service to the local agent.
215205
@@ -218,50 +208,21 @@ def register(self, name,
218208
:param str address: The service IP address
219209
:param int port: The service port
220210
:param list tags: A list of tags for the service
221-
:param str script: Optional script to execute to check service
222-
:param int interval: The check execution interval
223-
:param int ttl: The TTL for external script check pings
224-
:param str http: An URL to check every interval
211+
:param check: An optional check definition for the service
212+
:type check: :class:`consulate.models.agent.Check`
213+
:param checks: A list of check definitions for the service
214+
:type checks: list([:class:`consulate.models.agent.Check`])
225215
:param bool enable_tag_override: Toggle the tag override feature
226216
:rtype: bool
227217
:raises: ValueError
228218
229219
"""
230-
# Validate the parameters
231-
if port is not None and not isinstance(port, int):
232-
raise ValueError('port must be an integer')
233-
elif tags is not None and not isinstance(tags, list):
234-
raise ValueError('tags must be a list of strings')
235-
236-
_validate_check(script, http, interval, ttl)
237-
238-
check_spec = None
239-
if script is not None:
240-
check_spec = {'script': script, 'interval': interval}
241-
elif http is not None:
242-
check_spec = {'HTTP': http, 'interval': interval}
243-
elif ttl is not None:
244-
check_spec = {'TTL': ttl}
245-
246-
# Build the payload to send to consul
247-
payload = {
248-
'id': service_id,
249-
'name': name,
250-
'port': port,
251-
'address': address,
252-
'tags': tags,
253-
'EnableTagOverride': enable_tag_override
254-
}
255-
256-
if check_spec:
257-
payload['check'] = check_spec
258-
259-
for key in list(payload.keys()):
260-
if payload[key] is None:
261-
del payload[key]
262-
263-
# Register the service
264-
return self._put_no_response_body(['register'], None, payload)
220+
return self._put_no_response_body(
221+
['register'], None,
222+
dict(models.Service(
223+
name=name, id=service_id, address=address, port=port,
224+
tags=tags, check=check, checks=checks,
225+
enable_tag_override=enable_tag_override)))
265226

266227
def deregister(self, service_id):
267228
"""Deregister the service from the local agent. The agent will

docs/agent.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ Agent
44
.. autoclass:: consulate.api.agent.Agent
55
:members:
66
:special-members:
7+
8+
.. autoclass:: consulate.models.agent.Check
9+
:members:
10+
:special-members:
11+
12+
.. autoclass:: consulate.models.agent.Service
13+
:members:
14+
:special-members:

docs/history.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ Version History
44
- Breaking Changes
55
- Removed support for Python 2.6 which has been EOLed since 2013
66
- Removed the deprecated (since 0.3) `consulate.Session` handle
7+
- Changed :meth:`~consulate.Consul.agent.check.register` to match the new API in Consul
78
- Changed :meth:`~consulate.Consul.agent.checks` to return a :data:`dict` instead of a :data:`list`.
89
- Changed :meth:`~consulate.Consul.agent.services` to return a :data:`dict` instead of a :data:`list`.
9-
- Renamed the ``check`` argument to :meth:`~consulate.Consul.agent.service.register` to ``script``.
10-
- Renamed the ``httpcheck`` argument to :meth:`~consulate.Consul.agent.service.register` to ``http``.
10+
- Changed :meth:`~consulate.Consul.agent.service.register` to match the new API in Consul and checks are now passed
11+
in as :class:`consulate.models.agent.Check` instances.
1112
- Other Changes:
1213
- Added :meth:`~consulate.Consul.agent.maintenance`, :meth:`~consulate.Consul.agent.metrics`,
1314
:meth:`~consulate.Consul.agent.monitor`, :meth:`~consulate.Consul.agent.reload`,
1415
:meth:`~consulate.Consul.agent.self`, and :meth:`~consulate.Consul.agent.token`
1516
- Added :meth:`~consulate.Consul.acl.bootstrap` and :meth:`~consulate.Consul.acl.replication`
17+
- Added :meth:`~consulate.Consul.agent.service.maintenance` (#107) - `Dj <https://github.com/Beahmer89>_`
1618
- Fixed run_once wrong args + subprocess parsing (#65) - Anthony Scalisi
1719
- Fixed :meth:`~consulate.Consul.catalog.register` and :meth:`~consulate.Consul.catalog.deregister` (#59)
1820
- Add support for ``flags``, ``cas``, and ``value`` in :meth:`Consulate.kv.acquire_lock` (#63)

0 commit comments

Comments
 (0)