Skip to content

Commit

Permalink
Changed the name of the filespec option and added the ability to chan…
Browse files Browse the repository at this point in the history
…ge default weight
  • Loading branch information
stevenca committed Jul 20, 2023
1 parent 538b3c3 commit a5912d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
30 changes: 18 additions & 12 deletions plugins/modules/mdd_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
description: The host for which the data is to be generated
required: true
type: str
mdd_filespec_list:
description: mdd_filespec_list to identify configuration file, e.g. "oc-"
filespec_list:
description: List of filespecs to identify configuration file, e.g. "[oc-*.yml]"
required: true
type: list
tags:
Expand All @@ -33,6 +33,10 @@
description: The key list dict for the context-aware sorting
required: false
type: dict
default_weight:
description: The default wieght for data that does not specify
default: 1000
type: int
"""

RETURN = r'''
Expand All @@ -59,7 +63,7 @@
mdd_data_root: "{{ mdd_data_root }}"
host: "{{ inventory_hostname }}"
tags: "{{ tags }}"
mdd_filespec_list: "{{ mdd_filespec_list }}"
filespec_list: "{{ filespec_list }}"
register: mdd_output
- debug:
Expand Down Expand Up @@ -92,7 +96,7 @@ def get_merge_key(path, list_key_map=None):
'mdd:openconfig:openconfig-network-instance:network-instances:openconfig-network-instance:network-instance:[a-zA-Z0-9_-]+:openconfig-network-instance:protocols:openconfig-network-instance:protocol:[a-zA-Z0-9_-]+:openconfig-network-instance:ospfv2:openconfig-network-instance:areas:openconfig-network-instance:area$': 'openconfig-network-instance:identifier',
'mdd:openconfig:openconfig-network-instance:network-instances:openconfig-network-instance:network-instance:[a-zA-Z0-9_-]+:openconfig-network-instance:vlans:openconfig-network-instance:vlan$': 'openconfig-network-instance:vlan-id',
'mdd:openconfig:openconfig-routing-policy:routing-policy:openconfig-routing-policy:defined-sets:openconfig-bgp-policy:bgp-defined-sets:openconfig-bgp-policy:ext-community-sets:openconfig-bgp-policy:ext-community-set$': 'openconfig-bgp-policy:ext-community-set-name',
'mdd:openconfig:openconfig-routing-policy:routing-policy:openconfig-routing-policy:defined-sets:openconfig-routing-policy:mdd_filespec_list-sets:openconfig-routing-policy:mdd_filespec_list-set$': 'openconfig-routing-policy:name',
'mdd:openconfig:openconfig-routing-policy:routing-policy:openconfig-routing-policy:defined-sets:openconfig-routing-policy:filespec_list-sets:openconfig-routing-policy:filespec_list-set$': 'openconfig-routing-policy:name',
'mdd:openconfig:openconfig-routing-policy:routing-policy:openconfig-routing-policy:defined-sets:openconfig-routing-policy:tag-sets:openconfig-routing-policy:tag-set$': 'openconfig-routing-policy:name',
'mdd:openconfig:openconfig-routing-policy:routing-policy:openconfig-routing-policy:policy-definitions:openconfig-routing-policy:policy-definition$': 'openconfig-routing-policy:name',
'mdd:openconfig:openconfig-system:system:openconfig-system-ext:services:openconfig-system-ext:key-chains:openconfig-system-ext:key-chain$': 'openconfig-system-ext:name',
Expand Down Expand Up @@ -258,13 +262,13 @@ def combine(config_list, list_key_map=None):
def intersection(lst1, lst2):
return list(set(lst1) & set(lst2))

def matches_filespec(filename, mdd_filespec_list):
for filespec in mdd_filespec_list:
def matches_filespec(filename, filespec_list):
for filespec in filespec_list:
if fnmatch.fnmatch(filename, filespec):
return True
return False

def find_and_read_configs(top_dir, device_name, mdd_filespec_list, tags=[]):
def find_and_read_configs(top_dir, device_name, filespec_list, default_weight, tags=[]):
tags.append('all') # every device gets an "all" tag
configs = []
hierarchy_level = 0
Expand All @@ -273,7 +277,7 @@ def find_and_read_configs(top_dir, device_name, mdd_filespec_list, tags=[]):
current_dir = os.path.join(root, device_name)
while current_dir != top_dir:
for filename in os.listdir(current_dir):
if matches_filespec(filename, mdd_filespec_list):
if matches_filespec(filename, filespec_list):
with open(os.path.join(current_dir, filename), 'r') as f:
# Consider catching when a user gives us a file that is not really yaml
yaml_configs = yaml.safe_load_all(f)
Expand All @@ -287,7 +291,7 @@ def find_and_read_configs(top_dir, device_name, mdd_filespec_list, tags=[]):
'config': yaml_config.get('mdd_data', {}),
'filepath': f.name,
'tags': matched_tags,
'weight': yaml_config.get('weight', 1000),
'weight': yaml_config.get('weight', default_weight),
'level': hierarchy_level
}
)
Expand All @@ -300,21 +304,23 @@ def main():
arguments = dict(
mdd_root=dict(required=True, type='str'),
host=dict(required=True, type='str'),
mdd_filespec_list=dict(required=True, type='list'),
filespec_list=dict(required=True, type='list'),
default_weight=dict(type='int', default=1000),
tags=dict(required=False, type='list'),
list_key_map=dict(required=False, type='dict')
)
module = AnsibleModule(argument_spec=arguments, supports_check_mode=False)
mdd_root = module.params['mdd_root']
host = module.params['host']
mdd_filespec_list = module.params['mdd_filespec_list']
filespec_list = module.params['filespec_list']
tags = module.params['tags']
list_key_map = module.params['list_key_map']
default_weight = module.params['default_weight']

mdd_data = {}
mdd_metadata = {}
module.debug('POOP')
configs_list = find_and_read_configs(mdd_root, host, mdd_filespec_list, tags)
configs_list = find_and_read_configs(mdd_root, host, filespec_list, default_weight, tags)
mdd_data, metadata = combine(configs_list, list_key_map)
module.exit_json(changed=False, mdd_data=mdd_data, mdd_metadata=metadata, failed=False)

Expand Down
2 changes: 1 addition & 1 deletion roles/data/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
mdd_combine:
mdd_root: "{{ mdd_data_root }}"
host: "{{ inventory_hostname }}"
mdd_filespec_list: "{{ mdd_data_patterns }}"
filespec_list: "{{ mdd_data_patterns }}"
tags: "{{ tags }}"
register: mdd_combine_output

Expand Down

0 comments on commit a5912d8

Please sign in to comment.