From 6b49ed46cc179ff21f4640d04320f474da7e3dbd Mon Sep 17 00:00:00 2001 From: GGabriele Date: Tue, 6 Sep 2016 15:16:07 +0200 Subject: Added n7k support --- network/nxos/nxos_feature.py | 77 +++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 44 deletions(-) diff --git a/network/nxos/nxos_feature.py b/network/nxos/nxos_feature.py index c0ba2a93..99f423ff 100644 --- a/network/nxos/nxos_feature.py +++ b/network/nxos/nxos_feature.py @@ -24,15 +24,17 @@ short_description: Manage features in NX-OS switches description: - Offers ability to enable and disable features in NX-OS extends_documentation_fragment: nxos -author: Jason Edelman (@jedelman8), Gabriele Gerbino (@GGabriele) +author: + - Jason Edelman (@jedelman8) + - Gabriele Gerbino (@GGabriele) options: feature: description: - - Name of feature + - Name of feature. required: true state: description: - - Desired state of the feature + - Desired state of the feature. required: false default: 'enabled' choices: ['enabled','disabled'] @@ -89,36 +91,20 @@ feature: def execute_config_command(commands, module): try: module.configure(commands) - except ShellError, clie: + except ShellError: + clie = get_exception() module.fail_json(msg='Error sending CLI commands', error=str(clie), commands=commands) -def get_cli_body_ssh(command, response, module): - """Get response for when transport=cli. This is kind of a hack and mainly - needed because these modules were originally written for NX-API. And - not every command supports "| json" when using cli/ssh. As such, we assume - if | json returns an XML string, it is a valid command, but that the - resource doesn't exist yet. - """ - if 'xml' in response[0]: - body = [] - else: - try: - body = [json.loads(response[0])] - except ValueError: - module.fail_json(msg='Command does not support JSON output', - command=command) - return body - - def execute_show(cmds, module, command_type=None): try: if command_type: response = module.execute(cmds, command_type=command_type) else: response = module.execute(cmds) - except ShellError, clie: + except ShellError: + clie = get_exception() module.fail_json(msg='Error sending {0}'.format(cmds), error=str(clie)) return response @@ -126,10 +112,8 @@ def execute_show(cmds, module, command_type=None): def execute_show_command(command, module, command_type='cli_show'): if module.params['transport'] == 'cli': - command += ' | json' cmds = [command] - response = execute_show(cmds, module) - body = get_cli_body_ssh(command, response, module) + body = execute_show(cmds, module) elif module.params['transport'] == 'nxapi': cmds = [command] body = execute_show(cmds, module, command_type=command_type) @@ -152,27 +136,32 @@ def apply_key_map(key_map, table): def get_available_features(feature, module): available_features = {} + feature_regex = '(?P\S+)\s+\d+\s+(?P.*)' command = 'show feature' - body = execute_show_command(command, module) + body = execute_show_command(command, module, command_type='cli_show_ascii') - try: - body = body[0]['TABLE_cfcFeatureCtrlTable']['ROW_cfcFeatureCtrlTable'] - except (TypeError, IndexError): - return available_features - - for each_feature in body: - feature = each_feature['cfcFeatureCtrlName2'] - state = each_feature['cfcFeatureCtrlOpStatus2'] - - if 'enabled' in state: - state = 'enabled' + split_body = body[0].splitlines() - if feature not in available_features.keys(): - available_features[feature] = state - else: - if (available_features[feature] == 'disabled' and - state == 'enabled'): - available_features[feature] = state + for line in split_body: + try: + match_feature = re.match(feature_regex, line, re.DOTALL) + feature_group = match_feature.groupdict() + feature = feature_group['feature'] + state = feature_group['state'] + except AttributeError: + feature = '' + state = '' + + if feature and state: + if 'enabled' in state: + state = 'enabled' + + if feature not in available_features.keys(): + available_features[feature] = state + else: + if (available_features[feature] == 'disabled' and + state == 'enabled'): + available_features[feature] = state return available_features -- cgit v1.2.1 From 9339c85b2a5d9a6c68abb8e68935dbf4d640f623 Mon Sep 17 00:00:00 2001 From: GGabriele Date: Tue, 6 Sep 2016 15:26:59 +0200 Subject: Removed tabs --- network/nxos/nxos_feature.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/network/nxos/nxos_feature.py b/network/nxos/nxos_feature.py index 99f423ff..0a993837 100644 --- a/network/nxos/nxos_feature.py +++ b/network/nxos/nxos_feature.py @@ -144,24 +144,24 @@ def get_available_features(feature, module): for line in split_body: try: - match_feature = re.match(feature_regex, line, re.DOTALL) - feature_group = match_feature.groupdict() - feature = feature_group['feature'] - state = feature_group['state'] + match_feature = re.match(feature_regex, line, re.DOTALL) + feature_group = match_feature.groupdict() + feature = feature_group['feature'] + state = feature_group['state'] except AttributeError: - feature = '' - state = '' + feature = '' + state = '' if feature and state: - if 'enabled' in state: - state = 'enabled' - - if feature not in available_features.keys(): - available_features[feature] = state - else: - if (available_features[feature] == 'disabled' and - state == 'enabled'): - available_features[feature] = state + if 'enabled' in state: + state = 'enabled' + + if feature not in available_features.keys(): + available_features[feature] = state + else: + if (available_features[feature] == 'disabled' and + state == 'enabled'): + available_features[feature] = state return available_features -- cgit v1.2.1 From c24c34cfc05806bce3b4217b58b66782b7295f5a Mon Sep 17 00:00:00 2001 From: GGabriele Date: Tue, 6 Sep 2016 15:34:06 +0200 Subject: Added newline --- network/nxos/nxos_feature.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/nxos/nxos_feature.py b/network/nxos/nxos_feature.py index 0a993837..7f40cd71 100644 --- a/network/nxos/nxos_feature.py +++ b/network/nxos/nxos_feature.py @@ -138,8 +138,8 @@ def get_available_features(feature, module): available_features = {} feature_regex = '(?P\S+)\s+\d+\s+(?P.*)' command = 'show feature' - body = execute_show_command(command, module, command_type='cli_show_ascii') + body = execute_show_command(command, module, command_type='cli_show_ascii') split_body = body[0].splitlines() for line in split_body: -- cgit v1.2.1