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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
import urllib3
import requests
import json
import pprint
from dataclasses import dataclass
@dataclass
class ApiResponse:
"""
Represents an API response.
Attributes:
status (int): The HTTP status code of the response.
request (dict): The request payload sent to the API.
result (dict): The data result of the API response.
error (str): Any error message in case of a failed response.
"""
status: int
request: dict
result: dict
error: str
class VyDevice:
"""
Represents a device for interacting with the VyOS API.
Args:
hostname (str): The hostname or IP address of the VyOS device.
apikey (str): The API key for authentication.
protocol (str, optional): The protocol to use (default is 'https').
port (int, optional): The port to use (default is 443).
verify (bool, optional): Whether to verify SSL certificates (default is True).
timeout (int, optional): The request timeout in seconds (default is 10).
Attributes:
hostname (str): The hostname or IP address of the VyOS device.
apikey (str): The API key for authentication.
protocol (str): The protocol used for communication.
port (int): The port used for communication.
verify (bool): Whether SSL certificate verification is enabled.
timeout (int): The request timeout in seconds.
Methods:
_get_url(command): Get the full URL for a given API command.
_get_payload(op, path=[], file=None, url=None, name=None): Generate the API request payload.
_api_request(command, op, path=[], method='POST', file=None, url=None, name=None): Make an API request.
retrieve_show_config(path=[]): Retrieve and show the device configuration.
retrieve_return_values(path=[]): Retrieve and return specific configuration values.
reset(path=[]): Reset a specific configuration element.
image_add(url=None, file=None, path=[]): Add an image from a URL or file.
image_delete(name, url=None, file=None, path=[]): Delete a specific image.
show(path=[]): Show configuration information.
generate(path=[]): Generate configuration based on specified path.
configure_set(path=[]): Set configuration based on specified path.
configure_delete(path=[]): Delete configuration based on specified path.
config_file_save(file=None): Save the configuration to a file.
config_file_load(file=None): Load the configuration from a file.
reboot(path=["now"]): Reboot the device.
poweroff(path=["now"]): Power off the device.
"""
def __init__(self, hostname, apikey, protocol='https', port=443, verify=True, timeout=10):
"""
Initializes a VyDevice instance.
Args:
hostname (str): The hostname or IP address of the VyOS device.
apikey (str): The API key for authentication.
protocol (str, optional): The protocol to use (default is 'https').
port (int, optional): The port to use (default is 443).
verify (bool, optional): Whether to verify SSL certificates (default is True).
timeout (int, optional): The request timeout in seconds (default is 10).
"""
self.hostname = hostname
self.apikey = apikey
self.protocol = protocol
self.port = port
self.verify = verify
self.timeout = timeout
def _get_url(self, command):
"""
Get the full URL for a specific API command.
Args:
command (str): The API command to construct the URL for.
Returns:
str: The full URL for the API command.
"""
return f"{self.protocol}://{self.hostname}:{self.port}/{command}"
def _get_payload(self, op, path=[], file=None, url=None, name=None):
"""
Generate the payload for an API request.
Args:
op (str): The operation to perform in the API request.
path (list, optional): The path elements for the API request (default is an empty list).
file (str, optional): The file to include in the request (default is None).
url (str, optional): The URL to include in the request (default is None).
name (str, optional): The name to include in the request (default is None).
Returns:
dict: The payload for the API request.
"""
if isinstance(path, str):
path = [path]
if isinstance(path, list) and len(path) == 1:
# If path is a list and contains only one element, use it directly
data = {'op': op, 'path': path[0]}
else:
data = []
current_command = {'op': op, 'path': []}
for p in path:
if isinstance(p, list):
# If the current item is a list, merge it into the current command
if current_command['path']:
data.append(current_command)
current_command = {'op': op, 'path': p}
else:
# Otherwise, add the item to the current command's path
current_command['path'].append(p)
# Add the last command to data
if current_command['path']:
data.append(current_command)
payload = {
'data': json.dumps(data),
'key': self.apikey
}
if file is not None:
data['file'] = file
if url is not None:
payload['url'] = url
if name is not None:
data['name'] = name
return payload
def _api_request(self, command, op, path=[], method='POST', file=None, url=None, name=None):
"""
Make an API request.
Args:
command (str): The API command to execute.
op (str): The operation to perform in the API request.
path (list, optional): The path elements for the API request (default is an empty list).
method (str, optional): The HTTP method to use for the request (default is 'POST').
file (str, optional): The file to include in the request (default is None).
url (str, optional): The URL to include in the request (default is None).
name (str, optional): The name to include in the request (default is None).
Returns:
ApiResponse: An ApiResponse object representing the API response.
"""
url = self._get_url(command)
payload = self._get_payload(op, path=path, file=file, url=url, name=name)
headers = {}
error = False
result = {}
try:
resp = requests.post(url, verify=self.verify, data=payload, timeout=self.timeout, headers=headers)
if resp.status_code == 200:
try:
resp_decoded = resp.json()
if resp_decoded['success'] == True:
result = resp_decoded['data']
error = False
else:
error = resp_decoded['error']
except json.JSONDecodeError:
error = 'json decode error'
else:
error = 'http error'
status = resp.status_code
except requests.exceptions.ConnectionError as e:
error = 'connection error: ' + str(e)
status = 0
# Removing apikey from payload for security reasons
del(payload['key'])
return ApiResponse(status=status, request=payload, result=result, error=error)
def retrieve_show_config(self, path=[]):
"""
Retrieve and show the device configuration.
Args:
path (list, optional): The path elements for the configuration retrieval (default is an empty list).
Returns:
ApiResponse: An ApiResponse object representing the API response.
"""
return self._api_request(command="retrieve", op='showConfig', path=path, method="POST")
def retrieve_return_values(self, path=[]):
"""
Retrieve and return specific configuration values.
Args:
path (list, optional): The path elements for the configuration retrieval (default is an empty list).
Returns:
ApiResponse: An ApiResponse object representing the API response.
"""
return self._api_request(command="retrieve", op='returnValues', path=path, method="POST")
def reset(self, path=[]):
"""
Reset a specific configuration element.
Args:
path (list, optional): The path elements for the configuration reset (default is an empty list).
Returns:
ApiResponse: An ApiResponse object representing the API response.
"""
return self._api_request(command="reset", op='reset', path=path, method="POST")
def image_add(self, url=None, file=None, path=[]):
"""
Add an image from a URL or file.
Args:
url (str, optional): The URL of the image to add (default is None).
file (str, optional): The path to the local image file to add (default is None).
path (list, optional): The path elements for the image addition (default is an empty list).
Returns:
ApiResponse: An ApiResponse object representing the API response.
"""
return self._api_request(command="image", op='add', url=url, method="POST")
def image_delete(self, name, url=None, file=None, path=[]):
"""
Delete a specific image.
Args:
name (str): The name of the image to delete.
url (str, optional): The URL of the image to delete (default is None).
file (str, optional): The path to the local image file to delete (default is None).
path (list, optional): The path elements for the image deletion (default is an empty list).
Returns:
ApiResponse: An ApiResponse object representing the API response.
"""
return self._api_request(command="image", op='delete', name=name, method="POST")
def show(self, path=[]):
"""
Show configuration information.
Args:
path (list, optional): The path elements for the configuration display (default is an empty list).
Returns:
ApiResponse: An ApiResponse object representing the API response.
"""
return self._api_request(command="show", op='show', path=path, method="POST")
def generate(self, path=[]):
"""
Generate configuration based on the given path.
Args:
path (list, optional): The path elements for configuration generation (default is an empty list).
Returns:
ApiResponse: An ApiResponse object representing the API response.
"""
return self._api_request(command="generate", op='generate', path=path, method="POST")
def configure_set(self, path=[]):
"""
Set configuration based on the given path.
Args:
path (list, optional): The path elements for configuration setting (default is an empty list).
Returns:
ApiResponse: An ApiResponse object representing the API response.
"""
return self._api_request(command="configure", op='set', path=path, method="POST")
def configure_delete(self, path=[]):
"""
Delete configuration based on the given path.
Args:
path (list, optional): The path elements for configuration deletion (default is an empty list).
Returns:
ApiResponse: An ApiResponse object representing the API response.
"""
return self._api_request(command="configure", op='delete', path=path, method="POST")
def config_file_save(self, file=None):
"""
Save the configuration to a file.
Args:
file (str, optional): The path to the file where the configuration will be saved (default is None).
Returns:
ApiResponse: An ApiResponse object representing the API response.
"""
return self._api_request(command="config-file", op='save', file=file, method="POST")
def config_file_load(self, file=None):
"""
Load the configuration from a file.
Args:
file (str, optional): The path to the file from which the configuration will be loaded (default is None).
Returns:
ApiResponse: An ApiResponse object representing the API response.
"""
return self._api_request(command="config-file", op='load', file=file, method="POST")
def reboot(self, path=["now"]):
"""
Reboot the device.
Args:
path (list, optional): The path elements for the reboot operation (default is ["now"]).
Returns:
ApiResponse: An ApiResponse object representing the API response.
"""
return self._api_request(command="reboot", op='reboot', path=path, method="POST")
def poweroff(self, path=["now"]):
"""
Power off the device.
Args:
path (list, optional): The path elements for the power off operation (default is ["now"]).
Returns:
ApiResponse: An ApiResponse object representing the API response.
"""
return self._api_request(command="poweroff", op='poweroff', path=path, method="POST")
|