mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Add mlnxos_interface module (#33839)
* Add new module: mlnxos_interface for configuring mlnxos ethernet interfaces Signed-off-by: Samer Deeb <samerd@mellanox.com> * Update mlnxos.py * Fix Documentation Signed-off-by: Samer Deeb <samerd@mellanox.com> * Add missing documentation Signed-off-by: Samer Deeb <samerd@mellanox.com>
This commit is contained in:
parent
bedb864bcb
commit
1df57ac1ac
6 changed files with 732 additions and 1 deletions
|
@ -17,9 +17,9 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
import json
|
||||
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.basic import env_fallback
|
||||
from ansible.module_utils.connection import Connection, ConnectionError
|
||||
from ansible.module_utils.network.common.utils import to_list, EntityCollection
|
||||
|
||||
|
@ -77,3 +77,153 @@ def load_config(module, config):
|
|||
conn.edit_config(config)
|
||||
except ConnectionError as exc:
|
||||
module.fail_json(msg=to_text(exc))
|
||||
|
||||
|
||||
def _parse_json_output(out):
|
||||
out_list = out.split('\n')
|
||||
first_index = 0
|
||||
opening_char = None
|
||||
lines_count = len(out_list)
|
||||
while first_index < lines_count:
|
||||
first_line = out_list[first_index].strip()
|
||||
if not first_line or first_line[0] not in ("[", "{"):
|
||||
first_index += 1
|
||||
continue
|
||||
opening_char = first_line[0]
|
||||
break
|
||||
if not opening_char:
|
||||
return "null"
|
||||
closing_char = ']' if opening_char == '[' else '}'
|
||||
last_index = lines_count - 1
|
||||
found = False
|
||||
while last_index > first_index:
|
||||
last_line = out_list[last_index].strip()
|
||||
if not last_line or last_line[0] != closing_char:
|
||||
last_index -= 1
|
||||
continue
|
||||
found = True
|
||||
break
|
||||
if not found:
|
||||
return opening_char + closing_char
|
||||
return "".join(out_list[first_index:last_index + 1])
|
||||
|
||||
|
||||
def show_cmd(module, cmd, json_fmt=True, fail_on_error=True):
|
||||
if json_fmt:
|
||||
cmd += " | json-print"
|
||||
conn = get_connection(module)
|
||||
command_obj = to_commands(module, to_list(cmd))[0]
|
||||
try:
|
||||
out = conn.get(**command_obj)
|
||||
except ConnectionError:
|
||||
if fail_on_error:
|
||||
raise
|
||||
return None
|
||||
|
||||
if json_fmt:
|
||||
out = _parse_json_output(out)
|
||||
try:
|
||||
cfg = json.loads(out)
|
||||
except ValueError:
|
||||
module.fail_json(
|
||||
msg="got invalid json",
|
||||
stderr=to_text(out, errors='surrogate_then_replace'))
|
||||
else:
|
||||
cfg = to_text(out, errors='surrogate_then_replace').strip()
|
||||
return cfg
|
||||
|
||||
|
||||
def get_interfaces_config(module, interface_type, flags=None, json_fmt=True):
|
||||
cmd = "show interfaces %s" % interface_type
|
||||
if flags:
|
||||
cmd += " %s" % flags
|
||||
return show_cmd(module, cmd, json_fmt)
|
||||
|
||||
|
||||
def get_bgp_summary(module):
|
||||
cmd = "show ip bgp summary"
|
||||
return show_cmd(module, cmd, json_fmt=False, fail_on_error=False)
|
||||
|
||||
|
||||
class BaseMlnxosModule(object):
|
||||
|
||||
def __init__(self):
|
||||
self._module = None
|
||||
self._commands = list()
|
||||
self._current_config = None
|
||||
self._required_config = None
|
||||
|
||||
def init_module(self):
|
||||
pass
|
||||
|
||||
def load_current_config(self):
|
||||
pass
|
||||
|
||||
def get_required_config(self):
|
||||
pass
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def check_declarative_intent_params(self, result):
|
||||
return None
|
||||
|
||||
def validate_param_values(self, obj, param=None):
|
||||
if param is None:
|
||||
param = self._module.params
|
||||
for key in obj:
|
||||
# validate the param value (if validator func exists)
|
||||
try:
|
||||
validator = getattr(self, 'validate_%s' % key)
|
||||
if callable(validator):
|
||||
validator(param.get(key))
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def get_config_attr(cls, item, arg):
|
||||
return item.get(arg)
|
||||
|
||||
@classmethod
|
||||
def get_mtu(cls, item):
|
||||
mtu = cls.get_config_attr(item, "MTU")
|
||||
mtu_parts = mtu.split()
|
||||
try:
|
||||
return int(mtu_parts[0])
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
def validate_mtu(self, value):
|
||||
if value and not 1500 <= int(value) <= 9612:
|
||||
self._module.fail_json(msg='mtu must be between 1500 and 9612')
|
||||
|
||||
def generate_commands(self):
|
||||
pass
|
||||
|
||||
def run(self):
|
||||
self.init_module()
|
||||
|
||||
result = {'changed': False}
|
||||
|
||||
self.get_required_config()
|
||||
self.load_current_config()
|
||||
|
||||
self.generate_commands()
|
||||
result['commands'] = self._commands
|
||||
|
||||
if self._commands:
|
||||
if not self._module.check_mode:
|
||||
load_config(self._module, self._commands)
|
||||
result['changed'] = True
|
||||
|
||||
failed_conditions = self.check_declarative_intent_params(result)
|
||||
|
||||
if failed_conditions:
|
||||
msg = 'One or more conditional statements have not been satisfied'
|
||||
self._module.fail_json(msg=msg,
|
||||
failed_conditions=failed_conditions)
|
||||
|
||||
self._module.exit_json(**result)
|
||||
|
||||
@classmethod
|
||||
def main(cls):
|
||||
app = cls()
|
||||
app.run()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue