| 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 list of configlets in parallel |
| 6 |
|
| 7 |
from cvprac.cvp_client import CvpClient |
| 8 |
import ssl |
| 9 |
from concurrent.futures import ThreadPoolExecutor |
| 10 |
ssl._create_default_https_context = ssl._create_unverified_context |
| 11 |
import requests.packages.urllib3 |
| 12 |
requests.packages.urllib3.disable_warnings() |
| 13 |
|
| 14 |
# Create connection to CloudVision |
| 15 |
clnt = CvpClient() |
| 16 |
|
| 17 |
clnt.connect(nodes=['cvp1'], username="username",password="password") |
| 18 |
|
| 19 |
import time |
| 20 |
from functools import wraps |
| 21 |
|
| 22 |
def get_list_of_configlets(configlets): |
| 23 |
""" |
| 24 |
Create a thread pool and download specified urls |
| 25 |
""" |
| 26 |
|
| 27 |
futures_list = [] |
| 28 |
results = [] |
| 29 |
|
| 30 |
with ThreadPoolExecutor(max_workers=40) as executor: |
| 31 |
for configlet in configlets: |
| 32 |
futures = executor.submit(clnt.api.get_configlet_by_name, configlet) |
| 33 |
futures_list.append(futures) |
| 34 |
|
| 35 |
for future in futures_list: |
| 36 |
try: |
| 37 |
result = future.result(timeout=60) |
| 38 |
results.append(result) |
| 39 |
except Exception: |
| 40 |
results.append(None) |
| 41 |
return results |
| 42 |
|
| 43 |
if __name__ == "__main__": |
| 44 |
# Example with pre-defined list |
| 45 |
configlets = ["tp-avd_tp-avd-leaf1","tp-avd_tp-avd-leaf2","tp-avd_tp-avd-leaf3","tp-avd_tp-avd-leaf4"] |
| 46 |
|
| 47 |
# Example with loading list of configlets from a file |
| 48 |
# with open("configlet_list.txt") as f: |
| 49 |
# configlets = f.read().splitlines() |
| 50 |
|
| 51 |
results = get_list_of_configlets(configlets) |
| 52 |
for result in results: |
| 53 |
print(result) |