1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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
# 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.basic import env_fallback, return_values
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)
if rc != 0:
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
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):
spec = {
'command': dict(key=True),

View file

@ -233,11 +233,11 @@ def get_running_config(module, config=None):
contents = config
else:
contents = get_config(module)
return NetworkConfig(indent=1, contents=contents)
return NetworkConfig(contents=contents)
def get_candidate(module):
candidate = NetworkConfig(indent=1)
candidate = NetworkConfig()
if 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'):
contents = get_config(module)
config = NetworkConfig(indent=1, contents=contents)
config = NetworkConfig(contents=contents)
if module.params['backup']:
result['__backup__'] = contents
@ -354,8 +354,8 @@ def main():
elif module.params['save_when'] == 'modified':
output = run_commands(module, ['show running-config', 'show startup-config'])
running_config = NetworkConfig(indent=1, contents=output[0], ignore_lines=diff_ignore_lines)
startup_config = NetworkConfig(indent=1, contents=output[1], ignore_lines=diff_ignore_lines)
running_config = NetworkConfig(contents=output[0], ignore_lines=diff_ignore_lines)
startup_config = NetworkConfig(contents=output[1], ignore_lines=diff_ignore_lines)
if running_config.sha1 != startup_config.sha1:
save_config(module, result)
@ -371,7 +371,7 @@ def main():
contents = running_config.config_text
# 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.check_mode:
@ -391,7 +391,7 @@ def main():
contents = module.params['intended_config']
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:
result.update({

View file

@ -2,11 +2,16 @@
hostname router
!
interface GigabitEthernet0/0
ip address 1.2.3.4 255.255.255.0
description test string
ip address 1.2.3.4 255.255.255.0
description test string
!
interface GigabitEthernet0/1
ip address 6.7.8.9 255.255.255.0
description test string
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))
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):
src = load_fixture('aruba_config_src.cfg')
set_module_args(dict(src=src))