1#
2# (c) 2020 Red Hat Inc.
3#
4# (c) 2020 Dell Inc.
5#
6# This file is part of Ansible
7#
8# Ansible is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# Ansible is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
20#
21from __future__ import (absolute_import, division, print_function)
22__metaclass__ = type
23
24DOCUMENTATION = """
25---
26cliconf: os9
27short_description: Use os9 cliconf to run command on Dell OS9 platform
28description:
29  - This os9 plugin provides low level abstraction apis for
30    sending and receiving CLI commands from Dell OS9 network devices.
31"""
32
33import re
34import json
35
36from itertools import chain
37
38from ansible.module_utils._text import to_bytes, to_text
39from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import to_list
40from ansible.plugins.cliconf import CliconfBase, enable_mode
41
42
43class Cliconf(CliconfBase):
44
45    def get_device_info(self):
46        device_info = {}
47
48        device_info['network_os'] = 'dellemc.os9.os9'
49        reply = self.get('show version')
50        data = to_text(reply, errors='surrogate_or_strict').strip()
51
52        match = re.search(r'Software Version (\S+)', data)
53        if match:
54            device_info['network_os_version'] = match.group(1)
55
56        match = re.search(r'System Type (\S+)', data, re.M)
57        if match:
58            device_info['network_os_model'] = match.group(1)
59
60        reply = self.get('show running-config | grep hostname')
61        data = to_text(reply, errors='surrogate_or_strict').strip()
62        match = re.search(r'^hostname (.+)', 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