Skip to content

Commit

Permalink
feat(panos_bgp_peer): 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 536d3e1 commit 17e31b0
Showing 1 changed file with 38 additions and 120 deletions.
158 changes: 38 additions & 120 deletions plugins/modules/panos_bgp_peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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:
address_family_identifier:
Expand Down Expand Up @@ -193,68 +193,55 @@
)

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


def setup_args():
return dict(
name=dict(type="str", required=True),
enable=dict(default=True, type="bool"),
peer_as=dict(type="str"),
enable_mp_bgp=dict(type="bool"),
address_family_identifier=dict(type="str", choices=["ipv4", "ipv6"]),
subsequent_address_unicast=dict(type="bool"),
subsequent_address_multicast=dict(type="bool"),
local_interface=dict(type="str"),
local_interface_ip=dict(type="str"),
peer_address_ip=dict(type="str"),
connection_authentication=dict(type="str"),
connection_keep_alive_interval=dict(type="int"),
connection_min_route_adv_interval=dict(type="int"),
connection_multihop=dict(type="int"),
connection_open_delay_time=dict(type="int"),
connection_hold_time=dict(type="int"),
connection_idle_hold_time=dict(type="int"),
connection_incoming_allow=dict(type="bool"),
connection_outgoing_allow=dict(type="bool"),
connection_incoming_remote_port=dict(type="int"),
connection_outgoing_local_port=dict(type="int"),
enable_sender_side_loop_detection=dict(type="bool"),
reflector_client=dict(
type="str", choices=["non-client", "client", "meshed-client"]
),
peering_type=dict(type="str", choices=["unspecified", "bilateral"]),
# aggregated_confed_as_path=dict(
# type='bool',
# help='This peer understands aggregated confederation AS path'),
max_prefixes=dict(type="int"),
# max_orf_entries=dict(
# type='int',
# help='Maximum of ORF entries accepted from peer'),
# soft_reset_with_stored_info=dict(
# type='bool',
# help='Enable soft reset with stored info'),
bfd_profile=dict(type="str"),
peer_group=dict(required=True),
vr_name=dict(default="default"),
commit=dict(type="bool", default=False),
)


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),
(BgpPeerGroup, "peer_group"),
),
sdk_cls=BgpPeer,
sdk_params=dict(
name=dict(required=True),
enable=dict(default=True, type="bool"),
peer_as=dict(),
enable_mp_bgp=dict(type="bool"),
address_family_identifier=dict(choices=["ipv4", "ipv6"]),
subsequent_address_unicast=dict(type="bool"),
subsequent_address_multicast=dict(type="bool"),
local_interface=dict(),
local_interface_ip=dict(),
peer_address_ip=dict(),
connection_authentication=dict(),
connection_keep_alive_interval=dict(type="int"),
connection_min_route_adv_interval=dict(type="int"),
connection_multihop=dict(type="int"),
connection_open_delay_time=dict(type="int"),
connection_hold_time=dict(type="int"),
connection_idle_hold_time=dict(type="int"),
connection_incoming_allow=dict(type="bool"),
connection_outgoing_allow=dict(type="bool"),
connection_incoming_remote_port=dict(type="int"),
connection_outgoing_local_port=dict(type="int"),
enable_sender_side_loop_detection=dict(type="bool"),
reflector_client=dict(choices=["non-client", "client", "meshed-client"]),
peering_type=dict(choices=["unspecified", "bilateral"]),
max_prefixes=dict(type="int"),
bfd_profile=dict(),
),
)

module = AnsibleModule(
Expand All @@ -263,76 +250,7 @@ def main():
required_one_of=helper.required_one_of,
)

# Verify libs, setup 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 "{0}".'.format(vr.name))

group = bgp.find(module.params["peer_group"], BgpPeerGroup)
if group is None:
module.fail_json(
msg="BGP peer group does not exist: {0}.".format(
module.params["peer_group"]
)
)

listing = group.findall(BgpPeer)
spec = {
"name": module.params["name"],
"enable": module.params["enable"],
"peer_as": module.params["peer_as"],
"enable_mp_bgp": module.params["enable_mp_bgp"],
"address_family_identifier": module.params["address_family_identifier"],
"subsequent_address_unicast": module.params["subsequent_address_unicast"],
"subsequent_address_multicast": module.params["subsequent_address_multicast"],
"local_interface": module.params["local_interface"],
"local_interface_ip": module.params["local_interface_ip"],
"peer_address_ip": module.params["peer_address_ip"],
"connection_authentication": module.params["connection_authentication"],
"connection_keep_alive_interval": module.params[
"connection_keep_alive_interval"
],
"connection_min_route_adv_interval": module.params[
"connection_min_route_adv_interval"
],
"connection_multihop": module.params["connection_multihop"],
"connection_open_delay_time": module.params["connection_open_delay_time"],
"connection_hold_time": module.params["connection_hold_time"],
"connection_idle_hold_time": module.params["connection_idle_hold_time"],
"connection_incoming_allow": module.params["connection_incoming_allow"],
"connection_outgoing_allow": module.params["connection_outgoing_allow"],
"connection_incoming_remote_port": module.params[
"connection_incoming_remote_port"
],
"connection_outgoing_local_port": module.params[
"connection_outgoing_local_port"
],
"enable_sender_side_loop_detection": module.params[
"enable_sender_side_loop_detection"
],
"reflector_client": module.params["reflector_client"],
"peering_type": module.params["peering_type"],
"max_prefixes": module.params["max_prefixes"],
"bfd_profile": module.params["bfd_profile"],
}
obj = BgpPeer(**spec)
group.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 17e31b0

Please sign in to comment.