@@ -480,6 +480,99 @@ session.api.templates.create(omp_vsmart)
480480More details about how to use and how to add new: [ Feature Templates README.md] ( https://github.com/cisco-open/cisco-catalyst-wan-sdk/blob/main/catalystwan/api/templates/README.md )
481481</details >
482482
483+ <details >
484+ <summary> <b>Export Templates to CSV</b> <i>(click to expand)</i></summary>
485+
486+ ``` python
487+ import os
488+ import json
489+ import logging
490+ import csv
491+ from typing import List
492+ from catalystwan.api.template_api import TemplatesAPI
493+ from catalystwan.session import create_manager_session
494+ from catalystwan.api.templates.device_template.device_template import DeviceTemplate
495+
496+ # Configure logging
497+ logging.basicConfig(level = logging.INFO , format = " %(asctime)s - %(levelname)s - %(message)s " )
498+
499+ # Define vManage connection details
500+ url = " localhost"
501+ username = " username"
502+ password = " password"
503+ port = 443
504+
505+
506+ def save_csv_template (response : dict , template_name : str ) -> None :
507+ """ Save the response data to a CSV file."""
508+ try :
509+ columns = [col[" property" ] for col in response.get(" header" , {}).get(" columns" , [])]
510+ data = response.get(" data" , [])
511+ if not columns or not data:
512+ logging.warning(f " No data found for template ' { template_name} '. Skipping CSV creation. " )
513+
514+ csv_file = f " { template_name} .csv "
515+ with open (csv_file, mode = " w" , newline = " " ) as file :
516+ writer = csv.DictWriter(file , fieldnames = columns)
517+ writer.writeheader()
518+ for row in data:
519+ writer.writerow(row)
520+ logging.info(f " CSV file ' { csv_file} ' has been created. " )
521+ except Exception as e:
522+ logging.error(f " Failed to save CSV for template ' { template_name} ': { e} " )
523+
524+
525+ def get_non_default_device_templates (session ) -> List[DeviceTemplate]:
526+ """ Retrieve all non-default device templates."""
527+ try :
528+ device_templates = session.api.templates.get(DeviceTemplate).filter(factory_default = False )
529+ logging.info(f " Retrieved { len (device_templates)} non-default device templates. " )
530+ return device_templates
531+ except Exception as e:
532+ logging.error(f " Failed to retrieve device templates: { e} " )
533+ return []
534+
535+
536+ def get_device_ids_attached (session , template : DeviceTemplate) -> bool :
537+ """ Retrieve device IDs attached to a template and save the configuration as a CSV."""
538+ try :
539+ # Fetch attached devices
540+ response = session.get(f " dataservice/template/device/config/attached/ { template.id} " ).json()
541+ device_ids = [device[" uuid" ] for device in response.get(" data" , []) if device.get(" uuid" )]
542+ # Prepare payload
543+ payload = {
544+ " deviceIds" : device_ids,
545+ " templateId" : template.id,
546+ " isEdited" : False ,
547+ " isMasterEdited" : False ,
548+ }
549+
550+ # Send POST request
551+ response = session.post(" dataservice/template/device/config/input/" , json = payload)
552+ response.raise_for_status() # Raise an exception for HTTP errors
553+
554+ # Save the response as a CSV
555+ save_csv_template(response.json(), template.name)
556+ return True
557+ except Exception as e:
558+ logging.error(f " Error occurred while processing template ' { template.name} ': { e} " )
559+ return False
560+
561+
562+ def main ():
563+ """ Main function to retrieve and process device templates."""
564+ with create_manager_session(url = url, username = username, password = password, port = port) as session:
565+ device_templates = get_non_default_device_templates(session)
566+ for template in device_templates:
567+ get_device_ids_attached(session, template)
568+
569+
570+ if __name__ == " __main__" :
571+ main()
572+ ```
573+ The script will generate CSV files for each non-default device template in the current directory.
574+ </details >
575+
483576### Note:
484577To remove ` InsecureRequestWarning ` , you can include in your scripts (warning is suppressed when ` catalystwan_devel ` environment variable is set):
485578``` Python
0 commit comments