| 1 |
# Copyright (c) 2021 Arista Networks, Inc. |
| 2 |
# Use of this source code is governed by the Apache License 2.0 |
| 3 |
# that can be found in the COPYING file. |
| 4 |
# |
| 5 |
# Get configlets and save them to individual files using multi threading |
| 6 |
from cvprac.cvp_client import CvpClient |
| 7 |
import ssl |
| 8 |
ssl._create_default_https_context = ssl._create_unverified_context |
| 9 |
import requests.packages.urllib3 |
| 10 |
requests.packages.urllib3.disable_warnings() |
| 11 |
from concurrent.futures import ThreadPoolExecutor |
| 12 |
from functools import wraps |
| 13 |
|
| 14 |
# Create connection to CloudVision |
| 15 |
clnt = CvpClient() |
| 16 |
clnt.connect(['cvp1'],'username', 'password') |
| 17 |
|
| 18 |
total = clnt.api.get_configlets(start=0,end=1)['total'] |
| 19 |
|
| 20 |
def get_list_of_configlets(): |
| 21 |
""" |
| 22 |
Create a thread pool and download specified urls |
| 23 |
""" |
| 24 |
|
| 25 |
futures_list = [] |
| 26 |
results = [] |
| 27 |
|
| 28 |
with ThreadPoolExecutor(max_workers=40) as executor: |
| 29 |
for i in range(0,total+1,10): |
| 30 |
futures = executor.submit(clnt.api.get_configlets, start=i,end=i+10) |
| 31 |
futures_list.append(futures) |
| 32 |
|
| 33 |
for future in futures_list: |
| 34 |
try: |
| 35 |
result = future.result(timeout=60) |
| 36 |
results.append(result) |
| 37 |
except Exception: |
| 38 |
results.append(None) |
| 39 |
print(future.result()) |
| 40 |
return results |
| 41 |
|
| 42 |
if __name__ == "__main__": |
| 43 |
|
| 44 |
results = get_list_of_configlets() |
| 45 |
for future in results: |
| 46 |
for configlet in future['data']: |
| 47 |
with open(configlet['name'],'w') as f: |
| 48 |
f.write(configlet['config']) |