def get_central_policy_dict(self, key_name='policyName', remove_key=False): central_policy_list = self.get_central_policy_list() return list_to_dict(central_policy_list, key_name, remove_key=remove_key)
def get_policy_list_dict(self, policy_list_type='all', key_name='name', remove_key=False, cache=True): policy_list = self.get_policy_list_list(policy_list_type, cache=cache) return list_to_dict(policy_list, key_name, remove_key=remove_key)
def get_device_status_dict(self, key_name='host-name', remove_key=False): """Obtain a dict of specified device type Args: category (str): vedges or controllers Returns: result (dict): Device status """ result = self.get_device_status_list() return list_to_dict(result.json, key_name=key_name, remove_key=remove_key)
def get_device_config_dict(self, device_type, key_name='host-name', remove_key=False): """Get the config status of a list of devices as a dictionary Args: device_type (str): vedge or controller key_name (string): The name of the attribute to use as the dictionary key remove_key (boolean): remove the search key from the element Returns: result (dict): All data associated with a response. """ device_list = self.get_device_config_list(device_type) return list_to_dict(device_list, key_name=key_name, remove_key=remove_key)
def get_local_policy_dict(self, key_name='policyName', remove_key=False): """Get all Local Policies from vManage. Args: key_name (str): The name of the attribute to use as the key remove_key (bool): Remove the key from the dict (default: False) Returns: response (dict): A dict of all Local Polices currently in vManage. """ local_policy_list = self.get_local_policy_list() return list_to_dict(local_policy_list, key_name, remove_key=remove_key)
def get_policy_list_dict(self, policy_list_type='all', key_name='name', remove_key=False, cache=True): """Get a dictionary of policy lists Args: policy_list_type (str): Policy list type key_name (string): The name of the attribute to use as the dictionary key remove_key (boolean): Remove the search key from the element cache (boolean): Use cached data Returns: result (dict): All data associated with a response. """ policy_list = self.get_policy_list_list(policy_list_type, cache=cache) return list_to_dict(policy_list, key_name, remove_key=remove_key)
def get_device_template_dict(self, factory_default=False, key_name='templateName', remove_key=True, name_list=None): """Obtain a dictionary of all configured device templates. Args: factory_default (bool): Wheter to return factory default templates key_name (string): The name of the attribute to use as the dictionary key remove_key (boolean): remove the search key from the element Returns: result (dict): All data associated with a response. """ if name_list is None: name_list = [] device_template_list = self.get_device_template_list(factory_default=factory_default, name_list=name_list) return list_to_dict(device_template_list, key_name, remove_key)
def get_policy_definition_dict(self, definition_type, key_name='name', remove_key=False): """Get all Policy Definition Lists from vManage. Args: definition_type (str): Policy definition type key_name (string): The name of the attribute to use as the dictionary key remove_key (boolean): Remove the search key from the element Returns: result (dict): All data associated with a response. """ policy_definition_list = self.get_policy_definition_list( definition_type) return list_to_dict(policy_definition_list, key_name, remove_key=remove_key)
def get_policy_definition_dict(self, t, key_name='name', remove_key=False): policy_definition_list = self.get_policy_definition_list(t) return list_to_dict(policy_definition_list, key_name, remove_key=remove_key)
def import_templates_from_file(self, import_file, update=False, check_mode=False, name_list=None, template_type=None): """Import templates from a file. All object Names will be translated to IDs. Args: import_file (str): The name of the import file name_list (list): List of device templates to export template_type (str): Template type: device or template check_mode (bool): Try the import, but don't make changes (default: False) update (bool): Update existing templates (default: False) """ feature_template_updates = [] device_template_updates = [] imported_template_data = {} # Read in the datafile if not os.path.exists(import_file): raise Exception(f"Cannot find file {import_file}") with open(import_file) as f: if import_file.endswith('.yaml') or import_file.endswith('.yml'): imported_template_data = yaml.safe_load(f) else: imported_template_data = json.load(f) if 'vmanage_feature_templates' in imported_template_data: imported_feature_template_list = imported_template_data['vmanage_feature_templates'] else: imported_feature_template_list = [] imported_device_template_list = [] #pylint: disable=too-many-nested-blocks if template_type != 'feature': # Import the device templates and associated feature templates if 'vmanage_device_templates' in imported_template_data: imported_device_template_list = imported_template_data['vmanage_device_templates'] if name_list: feature_name_list = [] pruned_device_template_list = [] for device_template in imported_device_template_list: if device_template['templateName'] in name_list: pruned_device_template_list.append(device_template) if 'generalTemplates' in device_template: for general_template in device_template['generalTemplates']: if 'templateName' in general_template: feature_name_list.append(general_template['templateName']) if 'subTemplates' in general_template: for sub_template in general_template['subTemplates']: if 'templateName' in sub_template: feature_name_list.append(sub_template['templateName']) imported_device_template_list = pruned_device_template_list name_list = list(set(feature_name_list)) # Since device templates depend on feature templates, we always add them. if name_list: pruned_feature_template_list = [] imported_feature_template_dict = list_to_dict(imported_feature_template_list, key_name='templateName', remove_key=False) for feature_template_name in name_list: if feature_template_name in imported_feature_template_dict: pruned_feature_template_list.append(imported_feature_template_dict[feature_template_name]) # Otherwise, we hope the feature list is already there (e.g. Factory Default) imported_feature_template_list = pruned_feature_template_list # Process the feature templates feature_template_updates = self.template_data.import_feature_template_list(imported_feature_template_list, check_mode=check_mode, update=update) # Process the device templates device_template_updates = self.template_data.import_device_template_list(imported_device_template_list, check_mode=check_mode, update=update) return { 'feature_template_updates': feature_template_updates, 'device_template_updates': device_template_updates, }