Debian Codegraph @

ansible_collections/dellemc/enterprise_sonic/plugins/cliconf/sonic.py

raw symbols SCIP
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 #
21 from __future__ import (absolute_import, division, print_function)
22 __metaclass__ = type
23
24 DOCUMENTATION = """
25 ---
26 name: sonic
27 short_description: Use sonic cliconf to run command on Dell SONiC platforms
28 description:
29 - This sonic plugin provides low level abstraction apis for
30 sending and receiving CLI commands on Dell SONiC network devices.
31 """
32
33 import json
34
35 from ansible.errors import AnsibleConnectionFailure
36 from ansible.module_utils._text import to_bytes, to_text
37 from ansible.module_utils.common._collections_compat import Mapping
38 from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import to_list
39 from ansible.plugins.cliconf import CliconfBase, enable_mode
40
41
42 class Cliconf(CliconfBase):
43
44 def get_device_info(self):
45 device_info = {}
46 device_info['network_os'] = 'sonic'
47 return device_info
48
49 @enable_mode
50 def edit_config(self, command):
51 response = []
52 self.send_command("configure terminal")
53 for cmd in to_list(command):
54 if isinstance(cmd, dict):
55 resp = self.get(command=cmd["command"], prompt=cmd["prompt"], answer=cmd["answer"])
56 response.append(resp)
57 else:
58 response.append(self.send_command(to_bytes(cmd)))
59 self.send_command("end")
60 return response
61
62 @enable_mode
63 def get_config(self, source="running", flags=None, format=None):
64 if source not in ("running", "startup"):
65 raise ValueError(
66 "fetching configuration from %s is not supported" % source
67 )
68 if not flags:
69 flags = []
70 if source == "running":
71 cmd = "show running-config "
72 else:
73 cmd = "show startup-config "
74
75 cmd += " ".join(to_list(flags))
76 cmd = cmd.strip()
77 return self.send_command(cmd)
78
79 def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
80 return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
81
82 def get_capabilities(self):
83 result = super(Cliconf, self).get_capabilities()
84 return json.dumps(result)
85
86 def run_commands(self, commands=None, check_rc=True):
87 if commands is None:
88 raise ValueError("'commands' value is required")
89
90 responses = list()
91 for cmd in to_list(commands):
92 if not isinstance(cmd, Mapping):
93 cmd = {'command': cmd}
94
95 output = cmd.pop('output', None)
96 if output:
97 raise ValueError("'output' value %s is not supported for run_commands" % output)
98
99 try:
100 out = self.send_command(**cmd)
101 except AnsibleConnectionFailure as e:
102 if check_rc:
103 raise
104 out = getattr(e, 'err', to_text(e))
105
106 responses.append(out)
107
108 return responses
109
110 def set_cli_prompt_context(self):
111 """
112 Make sure we are in the operational cli mode
113 :return: None
114 """
115 if self._connection.connected:
116 self._update_cli_prompt_context(config_context=')#')