Aruba indenting (#33884)

* Fixing aruba's inconsitent indenting.

* Adding config with different children indentation and unit test to confirm the different spacing does not matter.

* Fixing pylint check. Missed an r prefix.
This commit is contained in:
James Mighion 2017-12-14 01:07:34 -08:00 committed by Ganesh Nalawade
parent 53abf45cec
commit f8e3cfe9e2
4 changed files with 34 additions and 10 deletions

View file

@ -25,6 +25,9 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# #
import re
from ansible.module_utils._text import to_text from ansible.module_utils._text import to_text
from ansible.module_utils.basic import env_fallback, return_values from ansible.module_utils.basic import env_fallback, return_values
from ansible.module_utils.network.common.utils import to_list, ComplexList from ansible.module_utils.network.common.utils import to_list, ComplexList
@ -77,11 +80,19 @@ def get_config(module, flags=None):
rc, out, err = exec_command(module, cmd) rc, out, err = exec_command(module, cmd)
if rc != 0: if rc != 0:
module.fail_json(msg='unable to retrieve current config', stderr=to_text(err, errors='surrogate_then_replace')) module.fail_json(msg='unable to retrieve current config', stderr=to_text(err, errors='surrogate_then_replace'))
cfg = to_text(out, errors='surrogate_then_replace').strip() cfg = sanitize(to_text(out, errors='surrogate_then_replace').strip())
_DEVICE_CONFIGS[cmd] = cfg _DEVICE_CONFIGS[cmd] = cfg
return cfg return cfg
def sanitize(resp):
# Takes response from device and adjusts leading whitespace to just 1 space
cleaned = []
for line in resp.splitlines():
cleaned.append(re.sub(r"^\s+", " ", line))
return '\n'.join(cleaned).strip()
def to_commands(module, commands): def to_commands(module, commands):
spec = { spec = {
'command': dict(key=True), 'command': dict(key=True),

View file

@ -233,11 +233,11 @@ def get_running_config(module, config=None):
contents = config contents = config
else: else:
contents = get_config(module) contents = get_config(module)
return NetworkConfig(indent=1, contents=contents) return NetworkConfig(contents=contents)
def get_candidate(module): def get_candidate(module):
candidate = NetworkConfig(indent=1) candidate = NetworkConfig()
if module.params['src']: if module.params['src']:
candidate.load(module.params['src']) candidate.load(module.params['src'])
@ -307,7 +307,7 @@ def main():
if module.params['backup'] or (module._diff and module.params['diff_against'] == 'running'): if module.params['backup'] or (module._diff and module.params['diff_against'] == 'running'):
contents = get_config(module) contents = get_config(module)
config = NetworkConfig(indent=1, contents=contents) config = NetworkConfig(contents=contents)
if module.params['backup']: if module.params['backup']:
result['__backup__'] = contents result['__backup__'] = contents
@ -354,8 +354,8 @@ def main():
elif module.params['save_when'] == 'modified': elif module.params['save_when'] == 'modified':
output = run_commands(module, ['show running-config', 'show startup-config']) output = run_commands(module, ['show running-config', 'show startup-config'])
running_config = NetworkConfig(indent=1, contents=output[0], ignore_lines=diff_ignore_lines) running_config = NetworkConfig(contents=output[0], ignore_lines=diff_ignore_lines)
startup_config = NetworkConfig(indent=1, contents=output[1], ignore_lines=diff_ignore_lines) startup_config = NetworkConfig(contents=output[1], ignore_lines=diff_ignore_lines)
if running_config.sha1 != startup_config.sha1: if running_config.sha1 != startup_config.sha1:
save_config(module, result) save_config(module, result)
@ -371,7 +371,7 @@ def main():
contents = running_config.config_text contents = running_config.config_text
# recreate the object in order to process diff_ignore_lines # recreate the object in order to process diff_ignore_lines
running_config = NetworkConfig(indent=1, contents=contents, ignore_lines=diff_ignore_lines) running_config = NetworkConfig(contents=contents, ignore_lines=diff_ignore_lines)
if module.params['diff_against'] == 'running': if module.params['diff_against'] == 'running':
if module.check_mode: if module.check_mode:
@ -391,7 +391,7 @@ def main():
contents = module.params['intended_config'] contents = module.params['intended_config']
if contents is not None: if contents is not None:
base_config = NetworkConfig(indent=1, contents=contents, ignore_lines=diff_ignore_lines) base_config = NetworkConfig(contents=contents, ignore_lines=diff_ignore_lines)
if running_config.sha1 != base_config.sha1: if running_config.sha1 != base_config.sha1:
result.update({ result.update({

View file

@ -10,3 +10,8 @@ interface GigabitEthernet0/1
description test string description test string
shutdown shutdown
! !
wlan ssid-profile "blah"
essid "blah"
!
ip access-list session blah
any any any permit

View file

@ -59,6 +59,14 @@ class TestArubaConfigModule(TestArubaModule):
set_module_args(dict(src=src)) set_module_args(dict(src=src))
self.execute_module() self.execute_module()
def test_aruba_config_unchanged_different_spacing(self):
# Tab indented
set_module_args(dict(lines=['description test string'], parents=['interface GigabitEthernet0/0']))
self.execute_module(changed=False)
# 3 spaces indented
set_module_args(dict(lines=['essid "blah"'], parents=['wlan ssid-profile "blah"']))
self.execute_module(changed=False)
def test_aruba_config_src(self): def test_aruba_config_src(self):
src = load_fixture('aruba_config_src.cfg') src = load_fixture('aruba_config_src.cfg')
set_module_args(dict(src=src)) set_module_args(dict(src=src))