-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
os_user_global - Replace old module ios_user
Replace the module ios_user with a more standard file coverage. And also take correctly some parts of the commands (by example, the current module doesn't take correctly the `privilege`part of the username command
- Loading branch information
1 parent
dcc812d
commit baa3daf
Showing
18 changed files
with
1,241 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,5 +114,6 @@ venv.bak/ | |
*.code-workspace | ||
.vscode/ | ||
.DS_Store | ||
*.swp | ||
|
||
changelogs/.plugin-cache.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
minor_changes: | ||
- ios_user_global - Put a module to replace the module ios_user with a files covered by standards (construct with `resource_module_models`) | ||
|
||
bugfixes: | ||
- gitignore - Add the line to not commit temporary files from VIM (`*.swp`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ios.py |
Empty file.
104 changes: 104 additions & 0 deletions
104
plugins/module_utils/network/ios/argspec/user_global/user_global.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2023 Red Hat | ||
# GNU General Public License v3.0+ | ||
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
|
||
__metaclass__ = type | ||
|
||
############################################# | ||
# WARNING # | ||
############################################# | ||
# | ||
# This file is auto generated by the | ||
# ansible.content_builder. | ||
# | ||
# Manually editing this file is not advised. | ||
# | ||
# To update the argspec make the desired changes | ||
# in the documentation in the module file and re-run | ||
# ansible.content_builder commenting out | ||
# the path to external 'docstring' in build.yaml. | ||
# | ||
############################################## | ||
|
||
""" | ||
The arg spec for the ios_user_global module | ||
""" | ||
|
||
|
||
class User_globalArgs(object): # pylint: disable=R0903 | ||
"""The arg spec for the ios_user_global module""" | ||
|
||
argument_spec = { | ||
"config": { | ||
"type": "dict", | ||
"options": { | ||
"enable": { | ||
"elements": "dict", | ||
"options": { | ||
"password": { | ||
"type": "dict", | ||
"options": { | ||
"type": { | ||
"choices": ["password", "secret"], | ||
"default": "secret", | ||
"type": "str", | ||
}, | ||
"hash": { | ||
"choices": [0, 5, 6, 7, 8, 9], | ||
"default": 0, | ||
"type": "int", | ||
}, | ||
"value": {"type": "str", "required": True}, | ||
}, | ||
"no_log": True, | ||
}, | ||
"level": {"type": "int"}, | ||
}, | ||
"type": "list", | ||
}, | ||
"users": { | ||
"elements": "dict", | ||
"options": { | ||
"name": {"type": "str", "required": True}, | ||
"description": {"type": "str"}, | ||
"password": { | ||
"type": "dict", | ||
"options": { | ||
"type": { | ||
"choices": ["password", "secret"], | ||
"default": "secret", | ||
"type": "str", | ||
}, | ||
"hash": { | ||
"choices": [0, 5, 6, 7, 8, 9], | ||
"default": 0, | ||
"type": "int", | ||
}, | ||
"value": {"type": "str", "required": True}, | ||
}, | ||
"no_log": True, | ||
}, | ||
"privilege": {"type": "int"}, | ||
}, | ||
"type": "list", | ||
}, | ||
}, | ||
}, | ||
"running_config": {"type": "str"}, | ||
"state": { | ||
"choices": [ | ||
"merged", | ||
"overridden", | ||
"deleted", | ||
"rendered", | ||
"gathered", | ||
"parsed", | ||
], | ||
"default": "merged", | ||
"type": "str", | ||
}, | ||
} # pylint: disable=C0301 |
Empty file.
120 changes: 120 additions & 0 deletions
120
plugins/module_utils/network/ios/config/user_global/user_global.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2023 Red Hat | ||
# GNU General Public License v3.0+ | ||
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
# | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
|
||
__metaclass__ = type | ||
|
||
""" | ||
The ios_user_global config file. | ||
It is in this file where the current configuration (as dict) | ||
is compared to the provided configuration (as dict) and the command set | ||
necessary to bring the current configuration to its desired end-state is | ||
created. | ||
""" | ||
|
||
from copy import deepcopy | ||
|
||
from ansible.module_utils.six import iteritems | ||
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module import ( | ||
ResourceModule, | ||
) | ||
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( | ||
dict_merge, | ||
) | ||
|
||
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.facts.facts import Facts | ||
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.rm_templates.user_global import ( | ||
User_globalTemplate, | ||
) | ||
|
||
|
||
class User_global(ResourceModule): | ||
""" | ||
The ios_user_global config class | ||
""" | ||
|
||
def __init__(self, module): | ||
super(User_global, self).__init__( | ||
empty_fact_val={}, | ||
facts_module=Facts(module), | ||
module=module, | ||
resource="user_global", | ||
tmplt=User_globalTemplate(), | ||
) | ||
self.parsers = [] | ||
self.list_parsers = [ | ||
"enable", | ||
"users", | ||
] | ||
|
||
def execute_module(self): | ||
"""Execute the module | ||
:rtype: A dictionary | ||
:returns: The result from module execution | ||
""" | ||
if self.state not in ["parsed", "gathered"]: | ||
self.generate_commands() | ||
self.run_commands() | ||
return self.result | ||
|
||
def generate_commands(self): | ||
"""Generate configuration commands to send based on | ||
want, have and desired state. | ||
""" | ||
wantd = self._users_list_to_dict(self.want) | ||
haved = self._users_list_to_dict(self.have) | ||
|
||
# if state is merged, merge want onto have and then compare | ||
if self.state == "merged": | ||
wantd = dict_merge(haved, wantd) | ||
|
||
# if state is deleted, empty out wantd and set haved to wantd | ||
if self.state == "deleted": | ||
wantd = {} | ||
|
||
self._compare(want=wantd, have=haved) | ||
|
||
def _compare(self, want, have): | ||
"""Leverages the base class `compare()` method and | ||
populates the list of commands to be run by comparing | ||
the `want` and `have` data with the `parsers` defined | ||
for the User_global network resource. | ||
""" | ||
self.compare(parsers=self.parsers, want=want, have=have) | ||
self._compare_lists_attrs(want, have) | ||
|
||
def _compare_lists_attrs(self, want, have): | ||
"""Compare list of dict""" | ||
for _parser in self.list_parsers: | ||
i_want = want.get(_parser, {}) | ||
i_have = have.get(_parser, {}) | ||
for key, wanting in iteritems(i_want): | ||
haveing = i_have.pop(key, {}) | ||
if wanting != haveing: | ||
if haveing and self.state in ["overridden", "replaced"]: | ||
self.addcmd(haveing, _parser, negate=True) | ||
self.addcmd(wanting, _parser) | ||
for key, haveing in iteritems(i_have): | ||
self.addcmd(haveing, _parser, negate=True) | ||
|
||
def _users_list_to_dict(self, data): | ||
"""Convert all list of dicts to dicts of dicts""" | ||
p_key = { | ||
"enable": "level", | ||
"users": "name", | ||
} | ||
tmp_data = deepcopy(data) | ||
for k, _v in p_key.items(): | ||
if k in tmp_data: | ||
if k == "enable": | ||
tmp_data[k] = {str(i.get(_v, 15)): i for i in tmp_data[k]} | ||
else: | ||
tmp_data[k] = {str(i[_v]): i for i in tmp_data[k]} | ||
return tmp_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
84 changes: 84 additions & 0 deletions
84
plugins/module_utils/network/ios/facts/user_global/user_global.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2023 Red Hat | ||
# GNU General Public License v3.0+ | ||
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
|
||
__metaclass__ = type | ||
|
||
""" | ||
The ios user_global fact class | ||
It is in this file the configuration is collected from the device | ||
for a given resource, parsed, and the facts tree is populated | ||
based on the configuration. | ||
""" | ||
|
||
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import utils | ||
|
||
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.argspec.user_global.user_global import ( | ||
User_globalArgs, | ||
) | ||
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.rm_templates.user_global import ( | ||
User_globalTemplate, | ||
) | ||
|
||
|
||
class User_globalFacts(object): | ||
"""The ios user_global facts class""" | ||
|
||
def __init__(self, module, subspec="config", options="options"): | ||
self._module = module | ||
self.argument_spec = User_globalArgs.argument_spec | ||
|
||
def get_users_data(self, connection): | ||
return connection.get("show running-config | section ^username|^enable") | ||
|
||
def sort_list_dicts(self, objs): | ||
p_key = { | ||
"enable": "level", | ||
"users": "name", | ||
} | ||
for k, _v in p_key.items(): | ||
if k in objs: | ||
if k == "enable": | ||
objs[k] = sorted(objs[k], key=lambda _k: str(_k.get(_v, 15))) | ||
else: | ||
objs[k] = sorted(objs[k], key=lambda _k: str(_k[_v])) | ||
return objs | ||
|
||
def populate_facts(self, connection, ansible_facts, data=None): | ||
"""Populate the facts for User_global network resource | ||
:param connection: the device connection | ||
:param ansible_facts: Facts dictionary | ||
:param data: previously collected conf | ||
:rtype: dictionary | ||
:returns: facts | ||
""" | ||
facts = {} | ||
objs = [] | ||
params = {} | ||
|
||
if not data: | ||
data = self.get_users_data(connection) | ||
|
||
# parse native config using the User_global template | ||
user_global_parser = User_globalTemplate(lines=data.splitlines(), module=self._module) | ||
objs = user_global_parser.parse() | ||
|
||
if objs: | ||
self.sort_list_dicts(objs) | ||
|
||
ansible_facts["ansible_network_resources"].pop("user_global", None) | ||
|
||
params = utils.remove_empties( | ||
user_global_parser.validate_config(self.argument_spec, {"config": objs}, redact=True), | ||
) | ||
|
||
facts["user_global"] = params["config"] | ||
ansible_facts["ansible_network_resources"].update(facts) | ||
|
||
return ansible_facts |
Oops, something went wrong.