forked from davejohncole/kea_python
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkeahook.py
189 lines (157 loc) · 6.69 KB
/
keahook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import kea
class UNSPECIFIED:
pass
class CommandError(Exception):
def __init__(self, reason):
self.reason = reason
def get_arg(args, name, default=UNSPECIFIED, error_msg=None):
if args is None or name not in args:
if default is not UNSPECIFIED:
return default
if error_msg:
raise CommandError(error_msg)
raise CommandError("'%s' parameter not specified" % name)
return args[name]
def get_string_arg(args, name, default=UNSPECIFIED, error_msg=None):
value = get_arg(args, name, default, error_msg)
if value != default and not isinstance(value, str):
if error_msg:
raise CommandError(error_msg)
raise CommandError("'%s' is not a string" % name)
return value
def get_int_arg(args, name, default=UNSPECIFIED, error_msg=None):
value = get_arg(args, name, default)
if value != default and not isinstance(value, int):
if error_msg:
raise CommandError(error_msg)
raise CommandError("'%s' is not an integer" % name)
return value
def get_map_arg(args, name, default=UNSPECIFIED, error_msg=None):
value = get_arg(args, name, default)
if value != default and not isinstance(value, dict):
if error_msg:
raise CommandError(error_msg)
raise CommandError("'%s' is not a map" % name)
return value
def wrap_handler(handle, get_response):
try:
cmd = handle.getArgument('command')
args = cmd.get('arguments')
if args is not None and not isinstance(args, dict):
raise CommandError('parameters missing or is not a map')
handle.setArgument('response', get_response(args))
except CommandError as e:
handle.setArgument('response', {'result': 1,
'text': e.reason})
return 1
except Exception as e:
kea.logger.exception('')
handle.setArgument('response', {'result': 1,
'text': str(e)})
return 1
return 0
# {"command": "reservation-add",
# "arguments": {"reservation": {"subnet-id": 1,
# reservation attribs}}}
def reservation_add(handle):
def get_response(args):
resv = get_map_arg(args, 'reservation')
subnet_id = get_int_arg(resv, 'subnet-id')
del resv['subnet-id']
host = kea.HostReservationParser4().parse(subnet_id, resv)
kea.HostMgr.instance().add(host)
return {'result': 0,
'text': 'Host added.'}
return wrap_handler(handle, get_response)
# {"command": "reservation-get",
# "arguments": {"subnet-id": 1,
# "ip-address": "192.0.2.202"}}
# {"command": "reservation-get",
# "arguments": {"subnet-id": 4,
# "identifier-type": "hw-address",
# "identifier": "01:02:03:04:05:06"}}
def reservation_get(handle):
def get_response(args):
host_mgr = kea.HostMgr.instance()
subnet_id = get_int_arg(args, 'subnet-id')
if 'ip-address' in args:
ip_address = get_string_arg(args, 'ip-address')
host = host_mgr.get(subnet_id, ip_address)
else:
identifier_type = get_string_arg(args, 'identifier-type')
identifier = get_string_arg(args, 'identifier')
host = host_mgr.get(subnet_id, identifier_type, identifier)
if host is None:
return {'result': 0, 'text': 'Host not found.'}
else:
return {'result': 0,
'text': 'Host found.',
'arguments': host.toElement()}
return wrap_handler(handle, get_response)
# {"command": "reservation-get-all",
# "arguments": {"subnet-id": 1}}
def reservation_get_all(handle):
def get_response(args):
subnet_id = get_int_arg(args, 'subnet-id')
hosts = kea.HostMgr.instance().getAll4(subnet_id)
return {'result': 0,
'text': '%s IPv4 host(s) found.' % len(hosts),
'arguments': {'hosts': [h.toElement() for h in hosts]}}
return wrap_handler(handle, get_response)
# {"command": "reservation-get-page",
# "arguments": {"subnet-id": 1,
# "limit": 10}}
# { "command": "reservation-get-page",
# "arguments": {"subnet-id": 1,
# "source-index": 1,
# "from": 1234567,
# "limit": 10}}
def reservation_get_page(handle):
def get_response(args):
host_mgr = kea.HostMgr.instance()
subnet_id = get_int_arg(args, 'subnet-id')
source_index = get_int_arg(args, 'source-index', 0)
lower_host_id = get_int_arg(args, 'from', 0)
page_size = get_int_arg(args, 'limit')
hosts, source_index = host_mgr.getPage4(subnet_id, source_index, lower_host_id, page_size)
if hosts:
return {'result': 0,
'text': '%s IPv4 host(s) found.' % len(hosts),
'arguments': {'count': len(hosts),
'hosts': [h.toElement() for h in hosts],
'next': {'from': hosts[-1].getHostId(),
'source-index': source_index}}}
return {'result': 3,
'text': '0 IPv4 host(s) found.',
'arguments': {'count': 0,
'hosts': []}}
return wrap_handler(handle, get_response)
# {"command": "reservation-del",
# "arguments": {"subnet-id": 1,
# "ip-address": "192.0.2.202"}}
# {"command": "reservation-del",
# "arguments": {"subnet-id": 4,
# "identifier-type": "hw-address",
# "identifier": "01:02:03:04:05:06"}}
def reservation_del(handle):
def get_response(args):
host_mgr = kea.HostMgr.instance()
subnet_id = get_int_arg(args, 'subnet-id')
if 'ip-address' in args:
ip_address = get_string_arg(args, 'ip-address')
was_deleted = host_mgr.del_(subnet_id, ip_address)
else:
identifier_type = get_string_arg(args, 'identifier-type')
identifier = get_string_arg(args, 'identifier')
was_deleted = host_mgr.del4(subnet_id, identifier_type, identifier)
if was_deleted:
return {'result': 0, 'text': 'Host deleted.'}
return {'result': 1, 'text': 'Host not deleted (not found).'}
return wrap_handler(handle, get_response)
def load(handle):
handle.registerCommandCallout('reservation-add', reservation_add)
handle.registerCommandCallout('reservation-get', reservation_get)
handle.registerCommandCallout('reservation-get-all', reservation_get_all)
handle.registerCommandCallout('reservation-get-page', reservation_get_page)
handle.registerCommandCallout('reservation-del', reservation_del)
return 0