1#
2# (c) 2017 Red Hat Inc.
3#
4# This file is part of Ansible
5#
6# Ansible is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# Ansible is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
18#
19from __future__ import (absolute_import, division, print_function)
20__metaclass__ = type
21
22DOCUMENTATION = """
23---
24cliconf: aruba
25short_description: Use aruba cliconf to run command on Aruba platform
26description:
27  - This aruba plugin provides low level abstraction apis for
28    sending and receiving CLI commands from Aruba network devices.
29version_added: 2.4
30"""
31
32import re
33import json
34
35from itertools import chain
36
37from ansible.module_utils._text import to_bytes, to_text
38from ansible.module_utils.network.common.utils import to_list
39from ansible.plugins.cliconf import CliconfBase, enable_mode
40
41
42class Cliconf(CliconfBase):
43
44    def get_device_info(self):
45        device_info = {}
46
47        device_info['network_os'] = 'aruba'
48        reply = self.get('show version')
49        data = to_text(reply, errors='surrogate_or_strict').strip()
50
51        match = re.search(r'Version (\S+)', data)
52        if match:
53            device_info['network_os_version'] = match.group(1)
54
55        match = re.search(r'^MODEL: (\S+)\),', data, re.M)
56        if match:
57            device_info['network_os_model'] = match.group(1)
58
59        reply = self.get('show hostname')
60        data = to_text(reply, errors='surrogate_or_strict').strip()
61
62        match = re.search(r'^Hostname is (.+)', data, re.M)
63        if match:
64            device_info['network_os_hostname'] = match.group(1)
65
66        return device_info
67
68    @enable_mode
69    def get_config(self, source='running', format='text', flags=None):
70        if source not in ('running', 'startup'):
71            return self.invalid_params("fetching configuration from %s is not supported" % source)
72        if source == 'running':
73            cmd = 'show running-config all'
74        else:
75            cmd = 'show startup-config'
76        return self.send_command(cmd)
77
78    @enable_mode
79    def edit_config(self, command):
80        for cmd in chain(['configure terminal'], to_list(command), ['end']):
81            self.send_command(cmd)
82
83    def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
84        return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
85
86    def get_capabilities(self):
87        result = super(Cliconf, self).get_capabilities()
88        return json.dumps(result)
89
90    def set_cli_prompt_context(self):
91        """
92        Make sure we are in the operational cli mode
93        :return: None
94        """
95        if self._connection.connected:
96            self._update_cli_prompt_context(config_context=')#')
97