Commit 5ed6f6b9 by Arnau Comas Codina

Initial commit for Port Forwarding implementation

parent 4d34907b
Pipeline #1088 failed with stage
in 58 seconds
from openstack.exceptions import ResourceNotFound
from interface.network.port_forwarding import PortForwardingInterface
from interface.network.resources import FloatingIP, PortForwarding
from implementation.utils.connection import OpenStackConnection
class OSPortForwardingManager(PortForwardingInterface, OpenStackConnection):
# TODO: init ha d'obtenir el rang de floating ips i els ports forwardings
# actuals per gestionar els rangs de ports lliures/ocupats
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.floating_ips = [
FloatingIP(item) for item in self.openstack.list_floating_ips()]
for floating_ip in self.floating_ips:
items = self.openstack.network.floating_ip_port_forwardings(floating_ip.id)
floating_ip.port_forwardings = [PortForwarding(item) for item in items]
#self.port_forwardings = [
# PortForwarding(item) for item in self.openstack.network.port_forwardings(self.floating_ips[0].id)]
def create_port_forwarding(self, instance_id, internal_port_number, protocol):
"""DUBTE hem de gestionar condicions com ara que no estigui creat ja
un port forwarding igual, que la floating_ip no estigui activa, que no
estigui associat a un router, etc.?
"""
project_id = self.openstack.auth['project_id']
# fetch internal Neutron port id of the instance by instance_id
internal_port_object = [item for item in self.openstack.network.ports(
device_id=instance_id)].pop() # pop() is used because returns a list
internal_port_id = internal_port_object.id
internal_ip_address = internal_port_object.fixed_ips[0]['ip_address']
# recorrer cada floating ip i cridar una funció que ens torni un port lliure o None, sinó crear nova ip
floating_ip_id = self.floating_ips[0].id
external_port = "8000" #get_free_port() from list of ports
# floating_ip_id = "3179d648-9bb0-4871-854c-efffceb74fb0" #get_floating_ip_id
self.openstack.network.create_port_forwarding(
floatingip_id=floating_ip_id,
internal_port_id=internal_port_id,
internal_ip_address=internal_ip_address,
internal_port=internal_port_number,
external_port=external_port,
protocol=protocol,
description='')
port_forwardings = [
PortForwarding(item) for item in self.openstack.network.port_forwardings(self.floating_ips[0].id)]
# DUBTE: l'instància de la connection sempre s'ha d'inicialitzar amb l'objecte auth.
# no es podria compartir? Perquè aquí necessito accedir a funcions del port manager
# (internal_port_id no coincideix amb la instance_id)
# sinó s'haurà de fer a un nivell superior? de cercar-lo i passar-lo per paràmetre aquí
# on ha d'estar aquesta lògica doncs?
payload = {
"port_forwarding": {
"project_id": f"{project_id}",
"protocol": f"{protocol}",
"internal_ip_address": f"{instance_id}",
"internal_port": f"{internal_port_number}",
"internal_port_id": f"{internal_port_id}",
"external_port": f"{external_port}"
}
}
# create a ks client and get token or do a post directly with client
#ks = self.obtain_keystone_client()
#print(ks)
#r = requests.post("http://vm.niif.cloud.bme.hu:18686/v2.0/floatingips" \
# f"/{floating_ip_id}/port_forwardings", data = payload)
#r.json()
def remove_port_forwarding(self, instance_id, internal_port_number):
"""
Removes port forwarding rule
"""
pass
def list_instance_port_forwardings(self, instance_id):
"""
Lists all port forwarding rules defined from a vm instance
# NOTE: shall we return all rules or filtered by floating ip for example?
"""
pass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment