Skip to content

Commit

Permalink
fix sanity issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshrn committed Jul 2, 2021
1 parent 1691bc2 commit c59a65b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 52 deletions.
32 changes: 24 additions & 8 deletions plugins/action/network_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
except ImportError:
HAS_YAML = False

from ansible.errors import AnsibleActionFail
from ansible.errors import AnsibleActionFail, AnsibleError
from ansible.module_utils._text import to_text

from ansible_collections.ansible.netcommon.plugins.action.network import (
ActionModule as ActionNetworkModule,
Expand Down Expand Up @@ -85,7 +86,14 @@ def run(self, task_vars=None):
if run_mode == RunMode.RM_LIST:
result = self._list_resource_modules()
elif run_mode in [RunMode.RM_GET, RunMode.RM_CONFIG]:
result = self._run_resource_module()
try:
result = self._run_resource_module()
except AnsibleError as exc:
# handle short name redirection not working for ansible-2.9
if "was not found" in to_text(exc):
result = self._run_resource_module(prefix_os_name=True)
else:
raise

result.update(
{
Expand All @@ -99,10 +107,10 @@ def run(self, task_vars=None):
)
return result

def _run_resource_module(self):
def _run_resource_module(self, prefix_os_name=False):
new_task = self._task.copy()

self._module = self._get_resource_module()
self._module = self._get_resource_module(prefix_os_name=prefix_os_name)
if not self._module:
msg = "Could not find resource module '%s' for os name '%s'" % (
self._name,
Expand All @@ -111,7 +119,6 @@ def _run_resource_module(self):
raise AnsibleActionFail(msg)

new_task.action = self._module

action = self._shared_loader_obj.action_loader.get(
self._rm_play_context.network_os,
task=new_task,
Expand All @@ -130,7 +137,7 @@ def _run_resource_module(self):
result.update({"resource_module_name": self._module})
return result

def _get_resource_module(self):
def _get_resource_module(self, prefix_os_name=False):
if "." in self._name:
if len(self._name.split(".")) != 3:
msg = (
Expand All @@ -140,8 +147,13 @@ def _get_resource_module(self):
raise AnsibleActionFail(msg)
fqcn_module_name = self._name
else:
if prefix_os_name:
module_name = self._os_name.split(".")[1] + "_" + self._name
else:
module_name = self._name

fqcn_module_name = ".".join(
self._os_name.split(".")[:2] + [self._name]
self._os_name.split(".")[:2] + [module_name]
)

return fqcn_module_name
Expand Down Expand Up @@ -230,7 +242,11 @@ def _list_resource_modules(self):
module_dir_path = os.path.dirname(
import_module(modulelib).__file__
)
module_paths = glob.glob(f"{module_dir_path}/[!_]*.py")
module_paths = glob.glob(
"{module_dir_path}/[!_]*.py".format(
module_dir_path=module_dir_path
)
)

for module_path in module_paths:
module_name = os.path.basename(module_path).split(".")[0]
Expand Down
86 changes: 42 additions & 44 deletions plugins/modules/network_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

DOCUMENTATION = """
module: network_resource
author:
- Ganesh B. Nalawade (@ganeshrn)
author: Ganesh B. Nalawade (@ganeshrn)
short_description: Manage resource modules
description:
- Get list of available resource modules for given os name
Expand Down Expand Up @@ -41,27 +40,26 @@
structure of this option refer the individual resource module
platform documentation.
type: raw
running_config:
description:
- This option is used only with state I(parsed).
- The value of this option should be the output received from the host device
by executing the cli command to get the resource configuration on host.
- The state I(parsed) reads the configuration from C(running_config) option and
transforms it into Ansible structured data as per the resource module's argspec
and the value is then returned in the I(parsed) key within the result.
state:
description:
- The state the configuration should be left in.
- For supported values refer the individual resource module
platform documentation.
running_config:
description:
- This option is used only with state I(parsed).
- The value of this option should be the output received from the host device
by executing the cli command to get the resource configuration on host.
- The state I(parsed) reads the configuration from C(running_config) option and
transforms it into Ansible structured data as per the resource module's argspec
and the value is then returned in the I(parsed) key within the result.
state:
description:
- The state the configuration should be left in.
- For supported values refer the individual resource module
platform documentation.
version_added: 2.2.0
notes:
- Refer the individual module documentation for the valid inputs of I(state)
and I(config) modules.
"""


EXAMPLES = r"""
EXAMPLES = """
- name: get list of resource modules for given network_os
ansible.netcommon.network_resource:
register: result
Expand All @@ -72,35 +70,35 @@
name: acls
state: gathered
- name: manage acl config for cisco.ios.ios network os.
ansible.netcommon.network_resource:
name: acls
config:
- afi: ipv4
acls:
- name: test_acl
acl_type: extended
aces:
- grant: deny
protocol_options:
tcp:
fin: true
source:
address: 192.0.2.0
wildcard_bits: 0.0.0.255
destination:
address: 192.0.3.0
wildcard_bits: 0.0.0.255
port_protocol:
eq: www
option:
traceroute: true
ttl:
eq: 10
state: merged
- name: manage acl config for cisco.ios.ios network os.
ansible.netcommon.network_resource:
name: acls
config:
- afi: ipv4
acls:
- name: test_acl
acl_type: extended
aces:
- grant: deny
protocol_options:
tcp:
fin: true
source:
address: 192.0.2.0
wildcard_bits: 0.0.0.255
destination:
address: 192.0.3.0
wildcard_bits: 0.0.0.255
port_protocol:
eq: www
option:
traceroute: true
ttl:
eq: 10
state: merged
"""

RETURN = r"""
RETURN = """
modules:
description: List of resource modules supported for given OS.
returned: When only I(os_name) or I(ansible_network_os) is set
Expand Down

0 comments on commit c59a65b

Please sign in to comment.