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

Feat/PBRs module #100

Merged
merged 20 commits into from
Feb 17, 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
44 changes: 44 additions & 0 deletions examples/create_pbr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
- name: Auto Generated Playbook
hosts: localhost
gather_facts: false
collections:
- nutanix.ncp
tasks:
- name: Setting Variables
set_fact:
ip: ""
username: ""
password: ""
cluster_name: ""
cluster_uuid: ""
priority: ""
vpc_uuid: ""

- name: create PBR with vpc uuid with any source or destination or protocol with deny action
ntnx_pbrs:
validate_certs: False
state: present
nutanix_host: "{{ ip }}"
nutanix_username: "{{ username }}"
nutanix_password: "{{ password }}"
priority: "{{ priority }}"
vpc:
uuid: "{{ vpc_uuid }}"
source:
any: True
destination:
any: True
action:
deny: True
protocol:
any: True
register: result
- name: Delete pbrs
ntnx_pbrs:
state: absent
nutanix_host: "{{ ip }}"
nutanix_username: "{{ username }}"
nutanix_password: "{{ password }}"
validate_certs: false
pbr_uuid: "{{ result.pbr_uuid }}"
7 changes: 5 additions & 2 deletions plugins/module_utils/prism/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ def __init__(self, module):
resource_type = "/groups"
super(Groups, self).__init__(module, resource_type=resource_type)

def get_uuid(self, entity_type, filter):
data = {"entity_type": entity_type, "filter_criteria": filter}
def get_uuid(self, value, key="name", entity_type=""):
data = {
"entity_type": entity_type,
"filter_criteria": "{0}=={1}".format(key, value),
}
resp, status = self.list(data, use_base_url=True)
if resp.get("group_results"):
return resp["group_results"][0]["entity_results"][0]["entity_id"]
Expand Down
143 changes: 143 additions & 0 deletions plugins/module_utils/prism/pbrs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# 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 .prism import Prism
from .vpcs import get_vpc_uuid


class Pbr(Prism):
def __init__(self, module):
resource_type = "/routing_policies"
super(Pbr, self).__init__(module, resource_type=resource_type)
self.build_spec_methods = {
"priority": self._build_spec_priority,
# "pbr_uuid": self.build_spec_pbr_uuid,
"vpc": self._build_spec_vpc,
"source": self._build_spec_source,
"destination": self._build_spec_destination,
"protocol": self._build_spec_protocol,
"action": self._build_spec_action,
}

def _get_default_spec(self):
return deepcopy(
{
"api_version": "3.1.0",
"metadata": {"kind": "routing_policy"},
"spec": {"resources": {}},
}
)

def _build_spec_priority(self, payload, config):
payload["spec"]["resources"]["priority"] = config
payload["spec"]["name"] = "Policy with priority{0}".format(config)

return payload, None

def _build_spec_vpc(self, payload, config):
uuid, error = get_vpc_uuid(config, self.module)
if error:
return None, error
payload["spec"]["resources"]["vpc_reference"] = self._get_vpc_ref(uuid)
return payload, None

def _get_vpc_ref(self, uuid):
return deepcopy({"kind": "vpc", "uuid": uuid})

def _build_spec_source(self, payload, config):
source = {}
if config.get("any"):
source["address_type"] = "ALL"
elif config.get("external"):
source["address_type"] = "INTERNET"
elif config.get("network"):
source["ip_subnet"] = {
"ip": config["network"].get("ip"),
"prefix_length": int(config["network"].get("prefix")),
}

payload["spec"]["resources"]["source"] = source

return payload, None

def _build_spec_destination(self, payload, config):
destination = {}
if config.get("any"):
destination["address_type"] = "ALL"
elif config.get("external"):
destination["address_type"] = "INTERNET"
elif config.get("network"):
destination["ip_subnet"] = {
"ip": config["network"].get("ip"),
"prefix_length": int(config["network"].get("prefix")),
}

payload["spec"]["resources"]["destination"] = destination

return payload, None

def _build_spec_protocol(self, payload, config):
protocol_parameters = {}
if config.get("tcp") or config.get("udp"):
key = "tcp" if config.get("tcp") else "udp"
value = {}
protocol_type = key.upper()
src_port_range_list = []
if "*" not in config[key]["src"]:
for port in config[key]["src"]:
port = port.split("-")
src_port_range_list.append(
{"start_port": int(port[0]), "end_port": int(port[-1])}
)
dest_port_range_list = []
if "*" not in config[key]["dst"]:
for port in config[key]["dst"]:
port = port.split("-")
dest_port_range_list.append(
{"start_port": int(port[0]), "end_port": int(port[-1])}
)
if src_port_range_list:
value["source_port_range_list"] = src_port_range_list
if dest_port_range_list:
value["destination_port_range_list"] = dest_port_range_list
if value:
protocol_parameters[key] = value

elif config.get("icmp"):
protocol_type = "ICMP"
if config["icmp"].get("code"):
protocol_parameters["icmp"] = {"icmp_code": config["icmp"]["code"]}
if config["icmp"].get("type"):
protocol_parameters["icmp"]["icmp_type"] = config["icmp"]["type"]

elif config.get("number"):
protocol_type = "PROTOCOL_NUMBER"
protocol_parameters["protocol_number"] = config["number"]

else:
protocol_type = "ALL"

payload["spec"]["resources"]["protocol_type"] = protocol_type
if protocol_parameters:
payload["spec"]["resources"]["protocol_parameters"] = protocol_parameters

return payload, None

def _build_spec_action(self, payload, config):
action = {}
if config.get("deny"):
action["action"] = "DENY" # TODO check
elif config.get("reroute"):
action["action"] = "REROUTE"
action["service_ip_list"] = [config.get("reroute")]
elif config.get("allow"):
action["action"] = "PERMIT"

payload["spec"]["resources"]["action"] = action

return payload, None
2 changes: 1 addition & 1 deletion plugins/module_utils/prism/virtual_switches.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get_dvs_uuid(config, module):
if "name" in config:
groups = Groups(module)
name = config["name"]
uuid = groups.get_uuid("distributed_virtual_switch", "name=={0}".format(name))
uuid = groups.get_uuid(value=name, entity_type="distributed_virtual_switch")
if not uuid:
error = "Virtual Switch {0} not found.".format(name)
return None, error
Expand Down
3 changes: 2 additions & 1 deletion plugins/module_utils/prism/vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ def _build_spec_disks(self, payload, vdisks):
groups = Groups(self.module)
name = vdisk["storage_container"]["name"]
uuid = groups.get_uuid(
value=name,
key="container_name",
entity_type="storage_container",
filter="container_name=={0}".format(name),
)
if not uuid:
error = "Storage container {0} not found.".format(name)
Expand Down
Loading