Commit a011c0fa by Arnau Comas Codina

Network Interfaces

parent 36ed0e99
Pipeline #1101 failed with stage
in 47 seconds
from interface.network.interfaces import NetworkInterfacesInterface
from interface.network.resources import Port
from interface.network.resources import Interface
from implementation.utils.connection import OpenStackConnection
from implementation.utils.decorators import OpenStackError
......@@ -13,7 +13,7 @@ class OSNetworkInterfacesManager(NetworkInterfacesInterface, OpenStackConnection
@OpenStackError
def create_interface(self):
def create_interface(self, name):
""" Creates a set of OpenStack resources to provide the functionality of
a "vlan". These resources consists of a router connected to public
network, a network and a subnet, where the instance gets the local ip.
......@@ -34,7 +34,7 @@ class OSNetworkInterfacesManager(NetworkInterfacesInterface, OpenStackConnection
# create the network which will hold the subnets
network = self.openstack.network.create_network(project_id=project_id,
is_admin_state_up=True)
is_admin_state_up=True, name=name)
# create the IPv4 subnet
subnet_v4 = self.openstack.network.create_subnet(project_id=project_id,
......@@ -50,14 +50,15 @@ class OSNetworkInterfacesManager(NetworkInterfacesInterface, OpenStackConnection
self.openstack.network.add_interface_to_router(router, subnet_v4.id)
self.openstack.network.add_interface_to_router(router, subnet_v6.id)
interface = Interface(network)
# return the equivalent "interface id"
return network.id
return interface
@OpenStackError
def delete_interface(self, interface_id):
def delete_interface(self, interface):
""" Deletes the set of OpenStack resources that represent the specified
"vlan" identified by the "interface_id" param. These are conformed by a
"vlan" identified by the "interface.id" param. These are conformed by a
router, a network and a subnet, if and only if there are no devices
attached.
"""
......@@ -69,7 +70,7 @@ class OSNetworkInterfacesManager(NetworkInterfacesInterface, OpenStackConnection
# obtain router_id and remove+delete all their interfaces
router_id = None
for port in self.openstack.network.ports(network_id=interface_id):
for port in self.openstack.network.ports(network_id=interface.id):
if port.device_owner == "network:router_interface":
router_id = port.device_id
......@@ -80,20 +81,20 @@ class OSNetworkInterfacesManager(NetworkInterfacesInterface, OpenStackConnection
self.openstack.network.delete_port(port)
# clean subnets
for subnet in self.openstack.network.subnets(network_id=interface_id):
for subnet in self.openstack.network.subnets(network_id=interface.id):
self.openstack.network.delete_subnet(subnet)
# delete the router and its port to the network
self.openstack.network.delete_router(router_id)
# delete the network, which should be free of ports (except the network:dhcp)
self.openstack.network.delete_network(interface_id)
self.openstack.network.delete_network(interface.id)
@OpenStackError
def add_vm_to_interface(self, device_id, interface_id):
def add_vm_to_interface(self, device_id, interface):
""" Attaches the specified device to a "vlan" identified by the
"interface_id" param. This translates on creating a network port into
"interface.id" param. This translates on creating a network port into
the corresponding "vlan" and thus assign an ip to the device.
Note that further actions may be required depending on the "vm" to
......@@ -104,36 +105,49 @@ class OSNetworkInterfacesManager(NetworkInterfacesInterface, OpenStackConnection
project_id = self.openstack.auth['project_id']
#for subnet in self.openstack.network.subnets(network_id=interface_id):
#for subnet in self.openstack.network.subnets(network_id=interface.id):
port = self.openstack.network.create_port(project_id=project_id,
network_id=interface_id, is_admin_state_up=True,
network_id=interface.id, is_admin_state_up=True,
device_id=device_id, device_owner="compute:nova",
is_port_security_enabled=True, binding_vnic_type="normal")
#fixed_ips=[{
# 'subnet_id': subnet.id,
# 'ip_address': '29.29.29.29'}]
# adds a fixed IP address from the network (interface_id) to a server
# adds a fixed IP address from the network (interface.id) to a server
# instance (device_id)
#self.openstack.compute.add_fixed_ip_to_server(device_id, interface_id)
#self.openstack.compute.add_fixed_ip_to_server(device_id, interface.id)
return port.id
interface.set_port(port)
return interface
@OpenStackError
def remove_vm_from_interface(self, port_id):
""" Detaches the specified device from the "vlan" identified by the
"interface_id" param.
def remove_vm_from_interface(self, interface):
""" Detaches the specified device from the "vlan".
Note that further actions may be required depending on the "vm" to
activate and configure the new interface.
deactivate and configure the old interface.
"""
self.openstack.network.delete_port(port_id)
self.openstack.network.delete_port(interface.port.id)
@OpenStackError
def list_all_vm_interfaces(self):
"""
def list_all_vm_interfaces(self, device_id):
""" Fetches network ports from the vm instance, to find the networks
that the device is attached to.
Returns the list of networks (also known as "vm interfaces") associated.
"""
pass
device_ports = self.openstack.network.ports(device_id=device_id)
interfaces = []
for port in device_ports:
interface = Interface(self.openstack.network.find_network(port.network_id))
interface.set_port(port)
interfaces.append(interface)
return interfaces
......@@ -86,3 +86,37 @@ class PortForwarding:
def JSON(self):
return json.dumps(self.__dict__)
class Interface:
""" From the OpenStack Network source data, which is the equivalent concept
for the Circle "interface". In OpenStack, to achieve a "vlan", is required
a specific Router where the Network is attached to, and two Subnets inside
of the Network, one for IPv4 and one for IPv6. Also:
- Some methods regarding class representation
"""
def __init__(self, data):
self.id = data['id'] # str
self.name = data['name'] # str
self.port = None # Port see below
self.is_external = data['is_router_external'] # bool
self.created_at = data['created_at'] # str
self.updated_at = data['updated_at'] # str
def set_port(self, data):
""" This attribute is set when a device is added to the interface, not
when the interface is created.
Specifies the "connection" from the device to the network
"""
self.port = Port(data)
def __repr__(self):
return(f"Interface({self.name})")
def __str__(self):
return(f"Interface({self.name})")
def JSON(self):
return json.dumps(self.__dict__)
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