Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and info module for static routes #221

Merged
merged 9 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ ansible-playbook examples/iaas/iaas.yml
| ntnx_image_placement_policies_info | List existing image placement policies. |
| ntnx_pbrs | Create or delete a PBR. |
| ntnx_pbrs_info | List existing PBRs. |
| ntnx_static_routes | Update static routes of a vpc. |
| ntnx_static_routes_info | List existing static routes of a vpc. |
| ntnx_subnets | Create or delete a Subnet. |
| ntnx_subnets_info | List existing Subnets. |
| ntnx_vms | Create or delete a VM. |
Expand Down
2 changes: 2 additions & 0 deletions meta/runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ action_groups:
- ntnx_vms_clone
- ntnx_vms
- ntnx_vpcs
- ntnx_static_routes
- ntnx_floating_ips_info
- ntnx_images_info
- ntnx_image_placement_policies_info
- ntnx_pbrs_info
- ntnx_subnets_info
- ntnx_vms_info
- ntnx_vpcs_info
- ntnx_static_routes_info
- ntnx_foundation_aos_packages_info
- ntnx_foundation_bmc_ipmi_config
- ntnx_foundation_discover_nodes_info
Expand Down
90 changes: 90 additions & 0 deletions plugins/module_utils/prism/static_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# This file is part of Ansible
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function

__metaclass__ = type

from copy import deepcopy

from .subnets import get_subnet_uuid
from .vpn_connections import get_vpn_connection_uuid
from .vpcs import Vpc


class StaticRoutes(Vpc):
default_route_dest = "0.0.0.0/0"

def __init__(self, module):
super(StaticRoutes, self).__init__(module)
self.build_spec_methods = {
"static_routes": self._build_spec_static_routes,
"remove_all_routes": self._build_spec_remove_all_routes,
}

def update_static_routes(self, data, vpc_uuid):
return self.update(data=data, uuid=vpc_uuid, endpoint="route_tables")

def get_static_routes(self, vpc_uuid):
return self.read(uuid=vpc_uuid, endpoint="route_tables")

def _get_default_spec(self):
return deepcopy(
{
"metadata": {"kind": "vpc_route_table"},
"spec": {
"resources": {
"static_routes_list": [],
"default_route_nexthop": None,
}
},
}
)

def _build_default_route_spec(self, payload, next_hop):
if payload["spec"]["resources"].get("default_route_nexthop"):
error = "More than one default routes are not allowed"
return None, error
payload["spec"]["resources"]["default_route_nexthop"] = next_hop
return payload, None

def _build_spec_static_routes(self, payload, inp_static_routes):
# since static route list has to be overriden
if payload["spec"]["resources"].get("default_route_nexthop"):
payload["spec"]["resources"].pop("default_route_nexthop")
static_routes_list = []
for route in inp_static_routes:
next_hop = {}
if route["next_hop"].get("external_subnet_ref"):
subnet_ref = route["next_hop"]["external_subnet_ref"]
uuid, err = get_subnet_uuid(subnet_ref, self.module)
if err:
return None, err
next_hop["external_subnet_reference"] = {"kind": "subnet", "uuid": uuid}
elif route["next_hop"].get("vpn_connection_ref"):
vpn_ref = route["next_hop"]["vpn_connection_ref"]
uuid, err = get_vpn_connection_uuid(self.module, vpn_ref)
if err:
return None, err
next_hop["vpn_connection_reference"] = {
"kind": "vpn_connection",
"uuid": uuid,
}

if route["destination"] == self.default_route_dest:
_, err = self._build_default_route_spec(payload, next_hop)
if err:
return None, err
else:
static_routes_list.append(
{"nexthop": next_hop, "destination": route["destination"]}
)

payload["spec"]["resources"]["static_routes_list"] = static_routes_list
return payload, None

def _build_spec_remove_all_routes(self, payload, remove_all_routes):
if remove_all_routes:
if payload["spec"]["resources"].get("default_route_nexthop"):
payload["spec"]["resources"].pop("default_route_nexthop")
payload["spec"]["resources"]["static_routes_list"] = []
return payload, None
33 changes: 33 additions & 0 deletions plugins/module_utils/prism/vpn_connections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This file is part of Ansible
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function

__metaclass__ = type

from .prism import Prism


class VpnConnection(Prism):
def __init__(self, module):
resource_type = "/vpn_connections"
super(VpnConnection, self).__init__(module, resource_type=resource_type)


# Helper functions


def get_vpn_connection_uuid(module, config):
if "name" in config:
vpn_obj = VpnConnection(module)
name = config.get("name")
uuid = vpn_obj.get_uuid(name)
if not uuid:
error = "VPN connection {0} not found.".format(name)
return None, error
elif "uuid" in config:
uuid = config.get("uuid")
else:
error = "Config {0} doesn't have name or uuid key".format(config)
None, error

return uuid, None
Loading