Skip to content

Commit

Permalink
feat(panos_redistribution): Add network resource module states
Browse files Browse the repository at this point in the history
  • Loading branch information
shinmog committed Jul 26, 2022
1 parent 7bb8b84 commit 390ac8d
Showing 1 changed file with 32 additions and 60 deletions.
92 changes: 32 additions & 60 deletions plugins/modules/panos_redistribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
- Panorama is supported.
extends_documentation_fragment:
- paloaltonetworks.panos.fragments.transitional_provider
- paloaltonetworks.panos.fragments.state
- paloaltonetworks.panos.fragments.network_resource_module_state
- paloaltonetworks.panos.fragments.full_template_support
- paloaltonetworks.panos.fragments.deprecated_commit
options:
Expand Down Expand Up @@ -133,18 +133,17 @@
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.paloaltonetworks.panos.plugins.module_utils.panos import (
get_connection,
ConnectionHelper,
)

try:
from panos.errors import PanDeviceError
from panos.network import (
RedistributionProfile,
RedistributionProfileIPv6,
VirtualRouter,
)
except ImportError:
try:
from pandevice.errors import PanDeviceError
from pandevice.network import (
RedistributionProfile,
RedistributionProfileIPv6,
Expand All @@ -154,33 +153,42 @@
pass


def setup_args():
return dict(
commit=dict(type="bool", default=False),
vr_name=dict(default="default"),
type=dict(type="str", default="ipv4", choices=["ipv4", "ipv6"]),
name=dict(type="str", required=True),
priority=dict(type="int"),
action=dict(type="str", default="no-redist", choices=["no-redist", "redist"]),
filter_type=dict(type="list", elements="str"),
filter_interface=dict(type="list", elements="str"),
filter_destination=dict(type="list", elements="str"),
filter_nexthop=dict(type="list", elements="str"),
ospf_filter_pathtype=dict(type="list", elements="str"),
ospf_filter_area=dict(type="list", elements="str"),
ospf_filter_tag=dict(type="list", elements="str"),
bgp_filter_community=dict(type="list", elements="str"),
bgp_filter_extended_community=dict(type="list", elements="str"),
)
class Helper(ConnectionHelper):
def spec_handling(self, spec, module):
if module.params["type"] == "ipv4":
self.sdk_cls = RedistributionProfile
else:
self.sdk_cls = RedistributionProfileIPv6


def main():
helper = get_connection(
helper_cls=Helper,
template=True,
template_stack=True,
with_state=True,
with_network_resource_module_state=True,
with_classic_provider_spec=True,
argument_spec=setup_args(),
with_commit=True,
parents=((VirtualRouter, "vr_name"),),
sdk_params=dict(
name=dict(type="str", required=True),
priority=dict(type="int"),
action=dict(
type="str", default="no-redist", choices=["no-redist", "redist"]
),
filter_type=dict(type="list", elements="str"),
filter_interface=dict(type="list", elements="str"),
filter_destination=dict(type="list", elements="str"),
filter_nexthop=dict(type="list", elements="str"),
ospf_filter_pathtype=dict(type="list", elements="str"),
ospf_filter_area=dict(type="list", elements="str"),
ospf_filter_tag=dict(type="list", elements="str"),
bgp_filter_community=dict(type="list", elements="str"),
bgp_filter_extended_community=dict(type="list", elements="str"),
),
extra_params=dict(
type=dict(type="str", default="ipv4", choices=["ipv4", "ipv6"]),
),
)

module = AnsibleModule(
Expand All @@ -189,43 +197,7 @@ def main():
required_one_of=helper.required_one_of,
)

parent = helper.get_pandevice_parent(module)

vr = VirtualRouter(module.params["vr_name"])
parent.add(vr)
try:
vr.refresh()
except PanDeviceError as e:
module.fail_json(msg="Failed refresh: {0}".format(e))

spec = {
"name": module.params["name"],
"priority": module.params["priority"],
"action": module.params["action"],
"filter_type": module.params["filter_type"],
"filter_interface": module.params["filter_interface"],
"filter_destination": module.params["filter_destination"],
"filter_nexthop": module.params["filter_nexthop"],
"ospf_filter_pathtype": module.params["ospf_filter_pathtype"],
"ospf_filter_area": module.params["ospf_filter_area"],
"ospf_filter_tag": module.params["ospf_filter_tag"],
"bgp_filter_community": module.params["bgp_filter_community"],
"bgp_filter_extended_community": module.params["bgp_filter_extended_community"],
}

if module.params["type"] == "ipv4":
obj = RedistributionProfile(**spec)
else:
obj = RedistributionProfileIPv6(**spec)

listing = vr.findall(obj.__class__)
vr.add(obj)

changed, diff = helper.apply_state(obj, listing, module)
if changed and module.params["commit"]:
helper.commit(module)

module.exit_json(changed=changed, diff=diff, msg="done")
helper.process(module)


if __name__ == "__main__":
Expand Down

0 comments on commit 390ac8d

Please sign in to comment.