Skip to content

Commit 414bf25

Browse files
committed
Add the missing model
1 parent e4ae57c commit 414bf25

File tree

1 file changed

+239
-0
lines changed

1 file changed

+239
-0
lines changed

consulate/models/agent.py

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
# coding=utf-8
2+
"""Models for the Agent endpoints"""
3+
from consulate.models import base
4+
from consulate import utils
5+
6+
7+
def _validate_args(value, model):
8+
"""Validate that the args values are all strings and that it does not
9+
conflict with other attributes.
10+
11+
:param list([str]) value: The args value
12+
:param consulate.models.agent.Check model: The model instance.
13+
:rtype: bool
14+
15+
"""
16+
return all([isinstance(v, str) for v in value]) \
17+
and not model.args and not model.grpc and not model.http \
18+
and not model.ttl
19+
20+
21+
def _validate_grpc(value, model):
22+
"""Validate that the HTTP value is a URL and that it does not conflict
23+
with other attributes.
24+
25+
:param str value: The URL value
26+
:param consulate.models.agent.Check model: The model instance.
27+
:rtype: bool
28+
29+
"""
30+
return utils.validate_url(value) \
31+
and not model.args and not model.http \
32+
and not model.tcp and not model.ttl
33+
34+
35+
def _validate_http(value, model):
36+
"""Validate that the HTTP value is a URL and that it does not conflict
37+
with other attributes.
38+
39+
:param str value: The URL value
40+
:param consulate.models.agent.Check model: The model instance.
41+
:rtype: bool
42+
43+
"""
44+
return utils.validate_url(value) \
45+
and not model.args and not model.grpc and not model.tcp \
46+
and not model.ttl
47+
48+
49+
def _validate_interval(value, model):
50+
"""Validate that interval does not conflict with other attributes.
51+
52+
:param str value: The interval value
53+
:param consulate.models.agent.Check model: The model instance.
54+
:rtype: bool
55+
56+
"""
57+
return utils.validate_go_interval(value) and not model.ttl
58+
59+
60+
def _validate_tcp(_value, model):
61+
"""Validate that the TCP does not conflict with other attributes.
62+
63+
:param str _value: The TCP value
64+
:param consulate.models.agent.Check model: The model instance.
65+
:rtype: bool
66+
67+
"""
68+
return not model.args and not model.grpc \
69+
and not model.http and not model.ttl
70+
71+
72+
def _validate_ttl(value, model):
73+
"""Validate that the TTL does not conflict with other attributes.
74+
75+
:param str value: The TTL value
76+
:param consulate.models.agent.Check model: The model instance.
77+
:rtype: bool
78+
79+
"""
80+
return utils.validate_go_interval(value) and not model.args \
81+
and not model.grpc and not model.http \
82+
and not model.tcp and not model.interval
83+
84+
85+
class Check(base.Model):
86+
"""Model for making Check API requests to Consul."""
87+
88+
__slots__ = ['id', 'name', 'interval', 'notes',
89+
'deregister_critical_service_after', 'args',
90+
'docker_container_id', 'grpc', 'grpc_use_tls',
91+
'http', 'method', 'header', 'timeout', 'tls_skip_verify',
92+
'tcp', 'ttl', 'service_id', 'status']
93+
94+
__attributes__ = {
95+
'id': {
96+
'key': 'ID',
97+
'type': str
98+
},
99+
'name': {
100+
'key': 'Name',
101+
'type': str,
102+
'required': True
103+
},
104+
'interval': {
105+
'key': 'Interval',
106+
'type': str,
107+
'validator': _validate_interval
108+
},
109+
'notes': {
110+
'key': 'Notes',
111+
'type': str
112+
},
113+
'deregister_critical_service_after': {
114+
'key': 'DeregisterCriticalServiceAfter',
115+
'type': str,
116+
'validator': utils.validate_go_interval
117+
},
118+
'args': {
119+
'key': 'Args',
120+
'type': list,
121+
'validator': _validate_args
122+
},
123+
'docker_container_id': {
124+
'key': 'DockerContainerID',
125+
'type': str
126+
},
127+
'grpc': {
128+
'key': 'GRPC',
129+
'type': str,
130+
'validator': _validate_grpc
131+
},
132+
'grpc_use_tls': {
133+
'key': 'GRPCUseTLS',
134+
'type': bool
135+
},
136+
'http': {
137+
'key': 'HTTP',
138+
'type': str,
139+
'validator': _validate_http
140+
},
141+
'method': {
142+
'key': 'Method',
143+
'type': str,
144+
'enum': {
145+
'HEAD', 'GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'TRACE'
146+
}
147+
},
148+
'header': {
149+
'key': 'Header',
150+
'type': dict,
151+
'validator': lambda h, _m: all(
152+
[(isinstance(k, str) and isinstance(v, str))
153+
for k, v in h.items()]),
154+
'cast_to': lambda h: {k: [v] for k, v in h.items()}
155+
},
156+
'timeout': {
157+
'key': 'Timeout',
158+
'type': str,
159+
'validator': utils.validate_go_interval
160+
},
161+
'tls_skip_verify': {
162+
'key': 'TLSSkipVerify',
163+
'type': bool
164+
},
165+
'tcp': {
166+
'key': 'TCP',
167+
'type': str,
168+
'validator': _validate_tcp
169+
},
170+
'ttl': {
171+
'key': 'TTL',
172+
'type': str,
173+
'validator': _validate_ttl
174+
},
175+
'service_id': {
176+
'key': 'ServiceID',
177+
'type': str
178+
},
179+
'status': {
180+
'key': 'Status',
181+
'type': str,
182+
'enum': {'passing', 'warning', 'critical', 'maintenance'}
183+
}
184+
}
185+
186+
def __init__(self, **kwargs):
187+
super(Check, self).__init__(**kwargs)
188+
if (self.args or self.grpc or self.http or self.tcp) \
189+
and not self.interval:
190+
raise ValueError('"interval" must be specified when specifying '
191+
'args, grpc, http, or tcp.')
192+
193+
194+
class Service(base.Model):
195+
"""Model for making Check API requests to Consul."""
196+
197+
__slots__ = ['id', 'name', 'tags', 'address', 'port', 'check',
198+
'checks', 'enable_tag_override']
199+
200+
__attributes__ = {
201+
'id': {
202+
'key': 'ID',
203+
'type': str
204+
},
205+
'name': {
206+
'key': 'Name',
207+
'type': str,
208+
'required': True
209+
},
210+
'tags': {
211+
'key': 'Tags',
212+
'type': list,
213+
'validator': lambda t, _m: all([isinstance(v, str) for v in t])
214+
},
215+
'address': {
216+
'key': 'Address',
217+
'type': str
218+
},
219+
'port': {
220+
'key': 'Port',
221+
'type': int
222+
},
223+
'check': {
224+
'key': 'Check',
225+
'type': Check,
226+
'cast_to': dict
227+
},
228+
'checks': {
229+
'key': 'Checks',
230+
'type': list,
231+
'validator': lambda c, _m: all([isinstance(v, Check) for v in c]),
232+
'cast_to': lambda c: [dict(check) for check in c]
233+
},
234+
'enable_tag_override': {
235+
'Key': 'EnableTagOverride',
236+
'type': bool
237+
}
238+
}
239+

0 commit comments

Comments
 (0)