-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from nutanix/feat/pbr
#99 PBRs module
- Loading branch information
Showing
9 changed files
with
979 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.