Skip to content

Commit

Permalink
feat(panos_bgp_dampening): 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 d93247e commit 536d3e1
Showing 1 changed file with 18 additions and 56 deletions.
74 changes: 18 additions & 56 deletions plugins/modules/panos_bgp_dampening.py
Expand Up @@ -39,7 +39,7 @@
extends_documentation_fragment:
- paloaltonetworks.panos.fragments.transitional_provider
- paloaltonetworks.panos.fragments.full_template_support
- paloaltonetworks.panos.fragments.state
- paloaltonetworks.panos.fragments.network_resource_module_state
- paloaltonetworks.panos.fragments.deprecated_commit
options:
vr_name:
Expand Down Expand Up @@ -98,37 +98,35 @@
)

try:
from panos.errors import PanDeviceError
from panos.network import Bgp, BgpDampeningProfile, VirtualRouter
except ImportError:
try:
from pandevice.errors import PanDeviceError
from pandevice.network import Bgp, BgpDampeningProfile, VirtualRouter
except ImportError:
pass


def setup_args():
return dict(
commit=dict(type="bool", default=False),
vr_name=dict(default="default"),
name=dict(type="str", required=True),
enable=dict(default=True, type="bool"),
cutoff=dict(type="float"),
reuse=dict(type="float"),
max_hold_time=dict(type="int"),
decay_half_life_reachable=dict(type="int"),
decay_half_life_unreachable=dict(type="int"),
)


def main():
helper = get_connection(
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", "default"),
(Bgp, None),
),
sdk_cls=BgpDampeningProfile,
sdk_params=dict(
name=dict(type="str", required=True),
enable=dict(default=True, type="bool"),
cutoff=dict(type="float"),
reuse=dict(type="float"),
max_hold_time=dict(type="int"),
decay_half_life_reachable=dict(type="int"),
decay_half_life_unreachable=dict(type="int"),
),
)

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

# Verify libs, initialize pandevice object tree.
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))

bgp = vr.find("", Bgp)
if bgp is None:
module.fail_json(
msg='BGP is not configured for virtual router "{0}"'.format(vr.name)
)

listing = bgp.findall(BgpDampeningProfile)
spec = {
"name": module.params["name"],
"enable": module.params["enable"],
"cutoff": module.params["cutoff"],
"reuse": module.params["reuse"],
"max_hold_time": module.params["max_hold_time"],
"decay_half_life_reachable": module.params["decay_half_life_reachable"],
"decay_half_life_unreachable": module.params["decay_half_life_unreachable"],
}
obj = BgpDampeningProfile(**spec)
bgp.add(obj)

# Apply the requested state.
changed, diff = helper.apply_state(obj, listing, module)

# Optional commit.
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 536d3e1

Please sign in to comment.