Skip to content

Commit e3791b5

Browse files
fix: panos_logical_router_vrf_static_route now supports "none" nexthop type
1 parent 1632e2b commit e3791b5

3 files changed

Lines changed: 44 additions & 41 deletions

File tree

plugins/module_utils/panos.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,3 +2008,42 @@ def get_nested_key(d, key_list):
20082008
"""
20092009

20102010
return reduce(lambda val, key: val.get(key) if val else None, key_list, d)
2011+
2012+
2013+
2014+
class StaticRouteHelper(ConnectionHelper):
2015+
"""Helper class that handles next-hop types with static routes
2016+
"""
2017+
def spec_handling(self, spec, module):
2018+
if module.params["state"] == "present" and spec["nexthop_type"] is None:
2019+
# need this because we dont have the default assignment in sdk-params and
2020+
# `None` value params are being removed in ParamPath.element method (called via VersionedPanObject.element)
2021+
spec["nexthop_type"] = "ip-address"
2022+
2023+
# default to ip-address when nexthop is set in merged state
2024+
# we dont know if object exists or not in merged state, and we dont set default values in module invocation
2025+
# in order to avoid unintended updates to non-provided params, but if nexthop is given, type must be ip-address
2026+
if (
2027+
module.params["state"] == "merged"
2028+
and spec["nexthop_type"] is None
2029+
and spec["nexthop"] is not None
2030+
):
2031+
spec["nexthop_type"] = "ip-address"
2032+
2033+
# NOTE merged state have a lot of API issues for updating nexthop we will let the API return it..
2034+
# from None to IP address - "Failed update nexthop_type: Edit breaks config validity"
2035+
# from IP address to next-vr - "Failed update nexthop_type: Edit breaks config validity"
2036+
2037+
# applies for updating existing routes from IP/next-vr/discard to none
2038+
# however it works for new objects, we ignore this as this is the existing implementation
2039+
if module.params["state"] == "merged" and spec["nexthop_type"] == "none":
2040+
msg = [
2041+
"Nexthop cannot be set to None with state='merged'.",
2042+
"You will need to use either state='present' or state='replaced'.",
2043+
]
2044+
module.fail_json(msg=" ".join(msg))
2045+
2046+
def object_handling(self, obj, module):
2047+
super().object_handling(obj, module)
2048+
if module.params.get("nexthop_type") == "none":
2049+
setattr(obj, "nexthop_type", None)

plugins/modules/panos_logical_router_vrf_static_route.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,12 @@
120120
from ansible.module_utils.basic import AnsibleModule
121121
from ansible_collections.paloaltonetworks.panos.plugins.module_utils.panos import (
122122
get_connection,
123+
StaticRouteHelper
123124
)
124125

125-
126126
def main():
127127
helper = get_connection(
128+
helper_cls=StaticRouteHelper,
128129
template=True,
129130
template_stack=True,
130131
with_network_resource_module_state=True,
@@ -139,7 +140,7 @@ def main():
139140
sdk_params=dict(
140141
name=dict(required=True),
141142
destination=dict(required=True),
142-
nexthop_type=dict(choices=["ip-address", "discard", "next-lr"]),
143+
nexthop_type=dict(choices=["ip-address", "discard", "next-lr", "none"]),
143144
nexthop=dict(),
144145
interface=dict(),
145146
admin_dist=dict(),

plugins/modules/panos_static_route.py

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -149,50 +149,13 @@
149149

150150
from ansible.module_utils.basic import AnsibleModule
151151
from ansible_collections.paloaltonetworks.panos.plugins.module_utils.panos import (
152-
ConnectionHelper,
152+
StaticRouteHelper,
153153
get_connection,
154154
)
155155

156-
157-
class Helper(ConnectionHelper):
158-
def spec_handling(self, spec, module):
159-
if module.params["state"] == "present" and spec["nexthop_type"] is None:
160-
# need this because we dont have the default assignment in sdk-params and
161-
# `None` value params are being removed in ParamPath.element method (called via VersionedPanObject.element)
162-
spec["nexthop_type"] = "ip-address"
163-
164-
# default to ip-address when nexthop is set in merged state
165-
# we dont know if object exists or not in merged state, and we dont set default values in module invocation
166-
# in order to avoid unintended updates to non-provided params, but if nexthop is given, type must be ip-address
167-
if (
168-
module.params["state"] == "merged"
169-
and spec["nexthop_type"] is None
170-
and spec["nexthop"] is not None
171-
):
172-
spec["nexthop_type"] = "ip-address"
173-
174-
# NOTE merged state have a lot of API issues for updating nexthop we will let the API return it..
175-
# from None to IP address - "Failed update nexthop_type: Edit breaks config validity"
176-
# from IP address to next-vr - "Failed update nexthop_type: Edit breaks config validity"
177-
178-
# applies for updating existing routes from IP/next-vr/discard to none
179-
# however it works for new objects, we ignore this as this is the existing implementation
180-
if module.params["state"] == "merged" and spec["nexthop_type"] == "none":
181-
msg = [
182-
"Nexthop cannot be set to None with state='merged'.",
183-
"You will need to use either state='present' or state='replaced'.",
184-
]
185-
module.fail_json(msg=" ".join(msg))
186-
187-
def object_handling(self, obj, module):
188-
super().object_handling(obj, module)
189-
if module.params.get("nexthop_type") == "none":
190-
setattr(obj, "nexthop_type", None)
191-
192-
193156
def main():
194157
helper = get_connection(
195-
helper_cls=Helper,
158+
helper_cls=StaticRouteHelper,
196159
template=True,
197160
template_stack=True,
198161
with_network_resource_module_state=True,

0 commit comments

Comments
 (0)