diff options
Diffstat (limited to 'docs/labs/lab03-configlet-management')
12 files changed, 343 insertions, 0 deletions
diff --git a/docs/labs/lab03-configlet-management/assign_configlet_to_device.py b/docs/labs/lab03-configlet-management/assign_configlet_to_device.py new file mode 100644 index 0000000..2973408 --- /dev/null +++ b/docs/labs/lab03-configlet-management/assign_configlet_to_device.py @@ -0,0 +1,23 @@ +# Copyright (c) 2021 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. + +from cvprac.cvp_client import CvpClient +import ssl +ssl._create_default_https_context = ssl._create_unverified_context +import requests.packages.urllib3 + +requests.packages.urllib3.disable_warnings() + +# Create connection to CloudVision +clnt = CvpClient() +clnt.connect(nodes=['cvp1'], username="username",password="password") + +configletName = 'cvprac_example2' + +device_name = "tp-avd-leaf1" +device = clnt.api.get_device_by_name(device_name) + +configlet = clnt.api.get_configlet_by_name(configletName) + +clnt.api.apply_configlets_to_device("", device, [configlet]) diff --git a/docs/labs/lab03-configlet-management/backup_configlets.py b/docs/labs/lab03-configlet-management/backup_configlets.py new file mode 100644 index 0000000..6543a6b --- /dev/null +++ b/docs/labs/lab03-configlet-management/backup_configlets.py @@ -0,0 +1,20 @@ +# Copyright (c) 2021 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. +# +# Get configlets and save them to individual files +from cvprac.cvp_client import CvpClient +import ssl +ssl._create_default_https_context = ssl._create_unverified_context +import requests.packages.urllib3 +requests.packages.urllib3.disable_warnings() + +# Create connection to CloudVision +clnt = CvpClient() +clnt.connect(['cvp1'],'username', 'password') + +configlets = clnt.api.get_configlets(start=0,end=0) + +for configlet in configlets['data']: + with open(configlet['name'],'w') as f: + f.write(configlet['config']) diff --git a/docs/labs/lab03-configlet-management/backup_configletsV2.py b/docs/labs/lab03-configlet-management/backup_configletsV2.py new file mode 100644 index 0000000..0247f19 --- /dev/null +++ b/docs/labs/lab03-configlet-management/backup_configletsV2.py @@ -0,0 +1,48 @@ +# Copyright (c) 2021 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. +# +# Get configlets and save them to individual files using multi threading +from cvprac.cvp_client import CvpClient +import ssl +ssl._create_default_https_context = ssl._create_unverified_context +import requests.packages.urllib3 +requests.packages.urllib3.disable_warnings() +from concurrent.futures import ThreadPoolExecutor +from functools import wraps + +# Create connection to CloudVision +clnt = CvpClient() +clnt.connect(['cvp1'],'username', 'password') + +total = clnt.api.get_configlets(start=0,end=1)['total'] + +def get_list_of_configlets(): + """ + Create a thread pool and download specified urls + """ + + futures_list = [] + results = [] + + with ThreadPoolExecutor(max_workers=40) as executor: + for i in range(0,total+1,10): + futures = executor.submit(clnt.api.get_configlets, start=i,end=i+10) + futures_list.append(futures) + + for future in futures_list: + try: + result = future.result(timeout=60) + results.append(result) + except Exception: + results.append(None) + print(future.result()) + return results + +if __name__ == "__main__": + + results = get_list_of_configlets() + for future in results: + for configlet in future['data']: + with open(configlet['name'],'w') as f: + f.write(configlet['config']) diff --git a/docs/labs/lab03-configlet-management/common.cfg b/docs/labs/lab03-configlet-management/common.cfg new file mode 100644 index 0000000..7620ae1 --- /dev/null +++ b/docs/labs/lab03-configlet-management/common.cfg @@ -0,0 +1,6 @@ +! +ip name-server vrf management 1.1.1.1 +ip name-server vrf management 8.8.8.8 +! +ntp server vrf management time.google.com +! diff --git a/docs/labs/lab03-configlet-management/config_search.py b/docs/labs/lab03-configlet-management/config_search.py new file mode 100644 index 0000000..4c3ad27 --- /dev/null +++ b/docs/labs/lab03-configlet-management/config_search.py @@ -0,0 +1,54 @@ +# Copyright (c) 2022 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. + +from cvprac.cvp_client import CvpClient +import ssl +ssl._create_default_https_context = ssl._create_unverified_context +import requests.packages.urllib3 +requests.packages.urllib3.disable_warnings() + +# Create connection to CloudVision +clnt = CvpClient() +clnt.connect(nodes=['cvp1'], username="username",password="password") + +def main(): + + print('Retrieving configlets ...') + + inventory = clnt.api.get_inventory() + data = clnt.api.get_configlets_and_mappers()['data'] + print(data) + + print('Number of configlets:', len(data['configlets'])) + + searchAgain = True + while searchAgain: + try: + search = input( "\nEnter Config Line: " ) + print(f"\n\n\'{search}\' has been found in following configlets:\n\n") + print(f"{'Hostname':<30}{'Serial number':<50}{'MAC address':<30}{'Configlets':<40}") + print("=" * 150) + for i in inventory: + device = i['hostname'] + device_sn = i['serialNumber'] + device_mac = i['systemMacAddress'] + configlet_list = [] + for c in data['configlets']: + for g in data['generatedConfigletMappers']: + if device_mac == g['netElementId'] and c['key'] == g['configletBuilderId'] and search in c['config']: + configlet_list.append(c['name']) + for k in data['configletMappers']: + if device_mac == k['objectId'] and c['key'] == k['configletId'] and search in c['config']: + configlet_list.append(c['name']) + configlet_list_final = ",".join(configlet_list) + if len(configlet_list) > 0: + print(f"{device:<30}{device_sn:<50}{device_mac:<30}{configlet_list_final:<30}") + + except KeyboardInterrupt: + print('\nExiting... \n') + return + +if __name__ == '__main__': + main() + diff --git a/docs/labs/lab03-configlet-management/configlet_list.txt b/docs/labs/lab03-configlet-management/configlet_list.txt new file mode 100644 index 0000000..645252c --- /dev/null +++ b/docs/labs/lab03-configlet-management/configlet_list.txt @@ -0,0 +1,6 @@ +tp-avd_tp-avd-leaf1 +tp-avd_tp-avd-leaf2 +tp-avd_tp-avd-leaf3 +tp-avd_tp-avd-leaf4 +tp-avd_tp-avd-spine1 +tp-avd_tp-avd-spine2
\ No newline at end of file diff --git a/docs/labs/lab03-configlet-management/create_configlet.py b/docs/labs/lab03-configlet-management/create_configlet.py new file mode 100644 index 0000000..8579fbf --- /dev/null +++ b/docs/labs/lab03-configlet-management/create_configlet.py @@ -0,0 +1,24 @@ +# Copyright (c) 2021 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. + +from cvprac.cvp_client import CvpClient +import ssl +ssl._create_default_https_context = ssl._create_unverified_context +import requests.packages.urllib3 +requests.packages.urllib3.disable_warnings() + +# Create connection to CloudVision +clnt = CvpClient() +clnt.connect(nodes=['cvp1'], username="username",password="password") + +configletName = "cvprac_example" + +configlet = """! +interface Ethernet10 + description test + ip address 10.144.144.1/24 +! +""" + +clnt.api.add_configlet(configletName,configlet) diff --git a/docs/labs/lab03-configlet-management/create_configlet_from_file.py b/docs/labs/lab03-configlet-management/create_configlet_from_file.py new file mode 100644 index 0000000..d6d07c6 --- /dev/null +++ b/docs/labs/lab03-configlet-management/create_configlet_from_file.py @@ -0,0 +1,19 @@ +# Copyright (c) 2021 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. + +from cvprac.cvp_client import CvpClient +import ssl +ssl._create_default_https_context = ssl._create_unverified_context +import requests.packages.urllib3 +requests.packages.urllib3.disable_warnings() + +# Create connection to CloudVision +clnt = CvpClient() +clnt.connect(nodes=['cvp1'], username="username",password="password") + +configletName = "cvprac_example2" + +with open("common.cfg") as file: + configlet = file.read() +clnt.api.add_configlet(configletName, configlet) diff --git a/docs/labs/lab03-configlet-management/get_applied_netelements.py b/docs/labs/lab03-configlet-management/get_applied_netelements.py new file mode 100644 index 0000000..71e020e --- /dev/null +++ b/docs/labs/lab03-configlet-management/get_applied_netelements.py @@ -0,0 +1,36 @@ +# Copyright (c) 2023 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. + +from cvprac.cvp_client import CvpClient +import ssl +import argparse +ssl._create_default_https_context = ssl._create_unverified_context +import requests.packages.urllib3 +requests.packages.urllib3.disable_warnings() + +# Create connection to CloudVision +clnt = CvpClient() +clnt.connect(nodes=['cvp1'], username="username", password="password") + +parser = argparse.ArgumentParser( + description='Get the list of devices and containers a configlet is attached to') +parser.add_argument('-c', '--configlet', required=True, help='The name of the configlet') +args = parser.parse_args() + +configlet_name = args.configlet +devices = clnt.api.get_applied_devices(configlet_name) + +containers = clnt.api.get_applied_containers(configlet_name) +print(f"Total number of devices {configlet_name} is attached to: {devices['total']}\n") +print(f"Total number of containers {configlet_name} is attached to: {containers['total']}\n") +col1 = "Device FQDN/hostname" +col2 = "IP Address" +print(f"{col1:<40}{col2:<40}") +print("="*80) +for device in devices['data']: + print(f"{device['hostName']:<40}{device['ipAddress']}") + +print("\nList of containers:\n") +for container in containers['data']: + print(container['containerName']) diff --git a/docs/labs/lab03-configlet-management/get_configlets.py b/docs/labs/lab03-configlet-management/get_configlets.py new file mode 100644 index 0000000..fc3dc2d --- /dev/null +++ b/docs/labs/lab03-configlet-management/get_configlets.py @@ -0,0 +1,53 @@ +# Copyright (c) 2021 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. +# +# Get list of configlets in parallel + +from cvprac.cvp_client import CvpClient +import ssl +from concurrent.futures import ThreadPoolExecutor +ssl._create_default_https_context = ssl._create_unverified_context +import requests.packages.urllib3 +requests.packages.urllib3.disable_warnings() + +# Create connection to CloudVision +clnt = CvpClient() + +clnt.connect(nodes=['cvp1'], username="username",password="password") + +import time +from functools import wraps + +def get_list_of_configlets(configlets): + """ + Create a thread pool and download specified urls + """ + + futures_list = [] + results = [] + + with ThreadPoolExecutor(max_workers=40) as executor: + for configlet in configlets: + futures = executor.submit(clnt.api.get_configlet_by_name, configlet) + futures_list.append(futures) + + for future in futures_list: + try: + result = future.result(timeout=60) + results.append(result) + except Exception: + results.append(None) + return results + +if __name__ == "__main__": + # Example with pre-defined list + configlets = ["tp-avd_tp-avd-leaf1","tp-avd_tp-avd-leaf2","tp-avd_tp-avd-leaf3","tp-avd_tp-avd-leaf4"] + + # Example with loading list of configlets from a file + # with open("configlet_list.txt") as f: + # configlets = f.read().splitlines() + + results = get_list_of_configlets(configlets) + for result in results: + print(result) diff --git a/docs/labs/lab03-configlet-management/reorder_configlet_on_device.py b/docs/labs/lab03-configlet-management/reorder_configlet_on_device.py new file mode 100644 index 0000000..3a5bb6f --- /dev/null +++ b/docs/labs/lab03-configlet-management/reorder_configlet_on_device.py @@ -0,0 +1,26 @@ +# Copyright (c) 2021 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. + +from cvprac.cvp_client import CvpClient +import ssl +ssl._create_default_https_context = ssl._create_unverified_context +import requests.packages.urllib3 +requests.packages.urllib3.disable_warnings() + +# Create connection to CloudVision +clnt = CvpClient() +clnt.connect(nodes=['cvp1'], username="username",password="password") + +configletNames = ['tp-avd_tp-avd-leaf1','vlan144','api_models'] + +device_name = "tp-avd-leaf1" +device = clnt.api.get_device_by_name(device_name) + +configlets = [] + +for name in configletNames: + configlets.append(clnt.api.get_configlet_by_name(name)) + +# Apply configlets in the order specified in the list +clnt.api.apply_configlets_to_device("", device, configlets, reorder_configlets=True) diff --git a/docs/labs/lab03-configlet-management/update_configlet.py b/docs/labs/lab03-configlet-management/update_configlet.py new file mode 100644 index 0000000..b3eb9c0 --- /dev/null +++ b/docs/labs/lab03-configlet-management/update_configlet.py @@ -0,0 +1,28 @@ +# Copyright (c) 2021 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the COPYING file. + +from cvprac.cvp_client import CvpClient +import ssl +ssl._create_default_https_context = ssl._create_unverified_context +import requests.packages.urllib3 +requests.packages.urllib3.disable_warnings() + +# Create connection to CloudVision +clnt = CvpClient() +clnt.connect(nodes=['cvp1'], username="username",password="password") + +# Modify existing configlet + +configletName = "cvprac_example" + +configlet = """! +interface Ethernet10 + description DUB_R04 + ip address 10.144.144.2/24 +! +""" + +configletID = clnt.api.get_configlet_by_name(configletName)['key'] + +clnt.api.update_configlet( configlet, configletID, configletName) |