-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.py
549 lines (444 loc) · 17 KB
/
client.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
from typing import Optional, Dict, Any, List, Union
import os
from urllib.parse import urlparse, urljoin
import aiohttp
import logging
from utils import make_api_request, VyOSAPIError
import asyncio
import sys
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Import endpoint handlers
from endpoints.retrieve import RetrieveEndpoint, ShowConfigEndpoint
from endpoints.show import ShowEndpoint
from endpoints.configure import ConfigureEndpoint
from endpoints.generate import GenerateEndpoint
from endpoints.reset import ResetEndpoint
from endpoints.image import ImageEndpoint
from endpoints.config_file import ConfigFileEndpoint
from endpoints.reboot import RebootEndpoint
from endpoints.poweroff import PoweroffEndpoint
from endpoints.graphql import GraphQLEndpoint
class PathBuilder:
"""
Builder class for creating VyOS API paths.
Allows chaining of attributes to build the path.
"""
def __init__(self, client, endpoint, op, path=None):
"""
Initialize a new PathBuilder instance.
Args:
client: VyOSClient instance
endpoint: API endpoint (e.g., "/configure", "/show")
op: Operation to perform (e.g., "showConfig", "set")
path: Initial path list
"""
self.client = client
self.endpoint = endpoint
self.op = op
self.path = path or []
def __getattr__(self, attr):
"""
Handle attribute access to build the path.
Args:
attr: Attribute name
Returns:
New PathBuilder instance with the attribute added to the path
"""
# Replace underscores with hyphens for VyOS command compatibility
path_segment = attr.replace('_', '-')
new_path = self.path + [path_segment]
return PathBuilder(self.client, self.endpoint, self.op, new_path)
async def __call__(self, *args, **kwargs):
"""
Execute the API request when the PathBuilder is called.
Returns:
API response as a dictionary
"""
try:
print(f"Making {self.endpoint} request with op={self.op}, path={self.path}")
return await self.client.execute_request(self.endpoint, self.op, self.path, **kwargs)
except Exception as e:
print(f"Error in PathBuilder.__call__: {str(e)}")
raise
class ConfigureBuilder:
"""
Builder for VyOS configuration operations (set, delete, comment).
"""
def __init__(self, client):
"""
Initialize a new ConfigureBuilder instance.
Args:
client: VyOSClient instance
"""
self.client = client
def set(self, path=None):
"""
Start building a set operation path.
Args:
path: Initial path list (optional)
Returns:
PathBuilder instance for the set operation
"""
if isinstance(path, list) and path:
# Directly execute if a path list is provided
return self.client.execute_request("/configure", "set", path)
return PathBuilder(self.client, "/configure", "set")
def delete(self, path=None):
"""
Start building a delete operation path.
Args:
path: Initial path list (optional)
Returns:
PathBuilder instance for the delete operation
"""
if isinstance(path, list) and path:
# Directly execute if a path list is provided
return self.client.execute_request("/configure", "delete", path)
return PathBuilder(self.client, "/configure", "delete")
def comment(self, path=None):
"""
Start building a comment operation path.
Args:
path: Initial path list (optional)
Returns:
PathBuilder instance for the comment operation
"""
if isinstance(path, list) and path:
# Directly execute if a path list is provided
return self.client.execute_request("/configure", "comment", path)
return PathBuilder(self.client, "/configure", "comment")
def batch(self):
"""
Create a batch configuration operation.
Returns:
BatchOperation instance
"""
return BatchOperation(self.client)
class BatchOperation:
"""
Class for batching multiple configuration operations.
"""
def __init__(self, client):
"""
Initialize a new BatchOperation instance.
Args:
client: VyOSClient instance
"""
self.client = client
self.operations = []
def set(self, path):
"""
Add a set operation to the batch.
Args:
path: Path list for the operation
Returns:
Self for chaining
"""
self.operations.append({"op": "set", "path": path})
return self
def delete(self, path):
"""
Add a delete operation to the batch.
Args:
path: Path list for the operation
Returns:
Self for chaining
"""
self.operations.append({"op": "delete", "path": path})
return self
def comment(self, path):
"""
Add a comment operation to the batch.
Args:
path: Path list for the operation
Returns:
Self for chaining
"""
self.operations.append({"op": "comment", "path": path})
return self
async def execute(self):
"""
Execute all operations in the batch.
Returns:
API response
"""
return await self.client.execute_request("/configure", "batch", self.operations)
class VyOSClient:
"""
Client for interacting with the VyOS API.
"""
def __init__(self, host, api_key, https=True, cert_path=None, trust_self_signed=False):
"""
Initialize a new VyOSClient instance.
Args:
host: VyOS router hostname or IP address
api_key: API key for authentication
https: Whether to use HTTPS (default: True)
cert_path: Path to SSL certificate (default: None)
trust_self_signed: Whether to trust self-signed certificates (default: False)
"""
# Validate required parameters
if not host:
raise ValueError("VyOS host must be specified")
if not api_key:
raise ValueError("API key must be specified")
# Normalize the host to ensure it doesn't already have http/https prefix
if host.startswith("http://") or host.startswith("https://"):
print("Warning: Host should not include protocol. Removing protocol prefix.")
self.host = host.split("://")[1]
else:
self.host = host
self.api_key = api_key
self.https = https
self.cert_path = cert_path
self.trust_self_signed = trust_self_signed
protocol = "https" if https else "http"
self.base_url = f"{protocol}://{self.host}"
print(f"VyOSClient initialized with base URL: {self.base_url}")
# Log security configuration
if self.https:
if self.trust_self_signed:
print("WARNING: Configured to trust self-signed certificates. This should only be used in development environments.")
elif not self.cert_path:
print("WARNING: Using HTTPS without a certificate path and not trusting self-signed certificates. This may cause connection issues with self-signed certificates.")
else:
print(f"Using HTTPS with certificate from: {self.cert_path}")
else:
print("WARNING: Using plain HTTP connection. This is not secure and should only be used in isolated networks.")
# Initialize endpoint handlers
self._init_endpoints()
def _init_endpoints(self) -> None:
"""Initialize all endpoint handlers."""
# Regular endpoint handlers
# Note: These conflicts with the @property decorators
# self.show = ShowEndpoint(self)
# self.configure = ConfigureEndpoint(self)
# self.generate = GenerateEndpoint(self)
# self.reset = ResetEndpoint(self)
# self.image = ImageEndpoint(self)
# self.config_file = ConfigFileEndpoint(self)
# self.reboot = RebootEndpoint(self)
# self.poweroff = PoweroffEndpoint(self)
# Special endpoint handlers
self.retrieve = RetrieveEndpoint(self)
# self.showConfig = ShowConfigEndpoint(self)
print("VyOSClient endpoint handlers initialized")
@property
def showConfig(self):
"""
Start building a showConfig operation path.
Returns:
PathBuilder instance for the showConfig operation
"""
return PathBuilder(self, "/retrieve", "showConfig")
@property
def show(self):
"""
Start building a show operation path.
Returns:
PathBuilder instance for the show operation
"""
return PathBuilder(self, "/show", "show")
@property
def configure(self):
"""
Get the configuration builder.
Returns:
ConfigureBuilder instance
"""
return ConfigureBuilder(self)
@property
def generate(self):
"""
Start building a generate operation path.
Returns:
PathBuilder instance for the generate operation
"""
return PathBuilder(self, "/generate", "generate")
@property
def reset(self):
"""
Start building a reset operation path.
Returns:
PathBuilder instance for the reset operation
"""
return PathBuilder(self, "/reset", "reset")
@property
def reboot(self):
"""
Execute the reboot command.
Returns:
Awaitable for the API response
"""
return PathBuilder(self, "/reboot", "reboot", ["now"])
@property
def poweroff(self):
"""
Execute the poweroff command.
Returns:
Awaitable for the API response
"""
return PathBuilder(self, "/poweroff", "poweroff", ["now"])
@property
def graphql(self):
"""
Get the GraphQL endpoint.
Returns:
GraphQLEndpoint instance
"""
if not hasattr(self, '_graphql'):
self._graphql = GraphQLEndpoint(self)
return self._graphql
@property
def image(self):
"""
Class for image operations.
"""
class ImageOperations:
def __init__(self, client):
self.client = client
async def add(self, url):
"""
Add an image from URL.
Args:
url: URL to the image
Returns:
API response
"""
return await self.client.execute_request("/image", "add", url=url)
async def delete(self, name):
"""
Delete an image by name.
Args:
name: Image name
Returns:
API response
"""
return await self.client.execute_request("/image", "delete", name=name)
return ImageOperations(self)
@property
def config_file(self):
"""
Class for config file operations.
"""
class ConfigFileOperations:
def __init__(self, client):
self.client = client
async def save(self, file=None):
"""
Save configuration to file.
Args:
file: File path (optional)
Returns:
API response
"""
if file:
return await self.client.execute_request("/config-file", "save", file=file)
return await self.client.execute_request("/config-file", "save")
async def load(self, file):
"""
Load configuration from file.
Args:
file: File path
Returns:
API response
"""
return await self.client.execute_request("/config-file", "load", file=file)
return ConfigFileOperations(self)
async def test_connection(self):
"""
Test the connection to the VyOS router.
Returns:
True if the connection is successful, False otherwise.
Also returns the error message if the connection failed.
"""
try:
print(f"Testing connection to VyOS router at {self.base_url}...")
result = await self.showConfig()
if result.get("success") is True:
print("Connection test successful!")
return True, None
else:
error_msg = result.get("error", "Unknown error")
print(f"Connection test failed: {error_msg}")
return False, error_msg
except Exception as e:
print(f"Connection test failed with exception: {str(e)}")
return False, str(e)
async def execute_request(self, endpoint, op, path=None, **kwargs):
"""
Execute an API request.
Args:
endpoint: API endpoint
op: Operation to perform
path: Path list (optional)
**kwargs: Additional parameters
Returns:
API response
"""
url = urljoin(self.base_url, endpoint)
# Prepare the data payload
if op == "batch":
# For batch operations, path is a list of operations
data = path
else:
# For normal operations
data = {"op": op}
# Add path if specified
if isinstance(path, list):
data["path"] = path
# Add additional parameters
for key, value in kwargs.items():
data[key] = value
try:
print(f"Preparing request to VyOS API: {url}")
print(f"Request data: {data}")
# Convert data to a JSON string
try:
import json
data_json = json.dumps(data)
except (TypeError, ValueError) as e:
print(f"Error serializing request data to JSON: {e}")
raise VyOSAPIError(f"Invalid request data: {str(e)}")
# Execute the request
result = await make_api_request(
url=url,
data=data_json,
client=self,
)
print(f"Received response from VyOS API: {result.get('success', False)}")
if "error" in result and result["error"] is not None:
print(f"API returned error: {result['error']}")
return result
except VyOSAPIError as e:
print(f"VyOS API error: {e.message}")
if hasattr(e, 'response') and e.response:
print(f"Response status: {e.response.status}")
response_text = getattr(e.response, 'text', 'No response text available')
print(f"Response content: {response_text}")
raise
except Exception as e:
print(f"Unexpected error in execute_request: {str(e)}")
import traceback
print(traceback.format_exc())
raise VyOSAPIError(f"Unexpected error: {str(e)}")
class ConfigureEndpoint:
"""Special endpoint for the configure API."""
def __init__(self, client):
self._client = client
self.set = PathBuilder(client, endpoint="configure", op="set")
self.delete = PathBuilder(client, endpoint="configure", op="delete")
def batch(self):
"""Create a batch operation."""
return BatchOperation(self._client)
def __call__(self, *args):
"""Direct call to the configure endpoint."""
return self._client._execute("configure", None, *args)
class VyOSAPIError(Exception):
"""Exception raised for VyOS API errors."""
def __init__(self, message: str, status_code: int = None, response: Dict[str, Any] = None):
self.message = message
self.status_code = status_code
self.response = response
super().__init__(self.message)