Skip to content

Commit

Permalink
feat(panos_bgp_aggregate): 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 8c2bebe commit 31b92f1
Showing 1 changed file with 44 additions and 94 deletions.
138 changes: 44 additions & 94 deletions plugins/modules/panos_bgp_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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 @@ -174,61 +174,61 @@
)

try:
from panos.errors import PanDeviceError
from panos.network import Bgp, BgpPolicyAggregationAddress, VirtualRouter
except ImportError:
try:
from pandevice.errors import PanDeviceError
from pandevice.network import Bgp, BgpPolicyAggregationAddress, 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"),
prefix=dict(type="str"),
summary=dict(type="bool"),
as_set=dict(type="bool", default=False),
attr_local_preference=dict(type="int"),
attr_med=dict(type="int"),
attr_weight=dict(type="int"),
attr_nexthop=dict(type="list", elements="str"),
attr_origin=dict(
type="str", default="incomplete", choices=["igp", "egp", "incomplete"]
),
attr_as_path_limit=dict(type="int"),
attr_as_path_type=dict(
type="str",
default="none",
choices=["none", "remove", "prepend", "remove-and-prepend"],
),
attr_as_path_prepend_times=dict(type="int"),
attr_community_type=dict(
type="str",
default="none",
choices=["none", "remove-all", "remove-regex", "append", "overwrite"],
),
attr_community_argument=dict(type="str"),
attr_extended_community_type=dict(
type="str",
default="none",
choices=["none", "remove-all", "remove-regex", "append", "overwrite"],
),
attr_extended_community_argument=dict(type="str"),
)


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=BgpPolicyAggregationAddress,
sdk_params=dict(
name=dict(required=True),
enable=dict(type="bool", default=True),
prefix=dict(),
summary=dict(type="bool"),
as_set=dict(type="bool", default=False),
attr_local_preference=dict(type="int"),
attr_med=dict(type="int"),
attr_weight=dict(type="int"),
attr_nexthop=dict(type="list", elements="str"),
attr_origin=dict(
type="str",
default="incomplete",
choices=["igp", "egp", "incomplete"],
),
attr_as_path_limit=dict(type="int"),
attr_as_path_type=dict(
type="str",
default="none",
choices=["none", "remove", "prepend", "remove-and-prepend"],
),
attr_as_path_prepend_times=dict(type="int"),
attr_community_type=dict(
type="str",
default="none",
choices=["none", "remove-all", "remove-regex", "append", "overwrite"],
),
attr_community_argument=dict(),
attr_extended_community_type=dict(
type="str",
default="none",
choices=["none", "remove-all", "remove-regex", "append", "overwrite"],
),
attr_extended_community_argument=dict(),
),
)

module = AnsibleModule(
Expand All @@ -237,57 +237,7 @@ def main():
required_one_of=helper.required_one_of,
)

parent = helper.get_pandevice_parent(module)

spec = {
"name": module.params["name"],
"enable": module.params["enable"],
"prefix": module.params["prefix"],
"summary": module.params["summary"],
"as_set": module.params["as_set"],
"attr_local_preference": module.params["attr_local_preference"],
"attr_med": module.params["attr_med"],
"attr_weight": module.params["attr_weight"],
"attr_nexthop": module.params["attr_nexthop"],
"attr_origin": module.params["attr_origin"],
"attr_as_path_limit": module.params["attr_as_path_limit"],
"attr_as_path_type": module.params["attr_as_path_type"],
"attr_as_path_prepend_times": module.params["attr_as_path_prepend_times"],
"attr_community_type": module.params["attr_community_type"],
"attr_community_argument": module.params["attr_community_argument"],
"attr_extended_community_type": module.params["attr_extended_community_type"],
"attr_extended_community_argument": module.params[
"attr_extended_community_argument"
],
}
obj = BgpPolicyAggregationAddress(**spec)

vr_name = module.params["vr_name"]
commit = module.params["commit"]

vr = VirtualRouter(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 "{0}"'.format(vr.name))

listing = bgp.findall(BgpPolicyAggregationAddress)
bgp.add(obj)

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

# Optional: commit.
if changed and 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 31b92f1

Please sign in to comment.