Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
RECIRCLE
/
interface-openstack
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
2
Merge Requests
4
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5ed6f6b9
authored
Apr 24, 2020
by
Arnau Comas Codina
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial commit for Port Forwarding implementation
parent
4d34907b
Pipeline
#1088
failed with stage
in 58 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
94 additions
and
0 deletions
+94
-0
implementation/network/port_forwarding.py
+94
-0
No files found.
implementation/network/port_forwarding.py
0 → 100644
View file @
5ed6f6b9
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment