Commit c0739359 by Belákovics Ádám

Merge changes from network

parents ec28d270 f998eb34
Pipeline #734 failed with stage
in 37 seconds
from typing import Optional
from openstack.exceptions import ResourceNotFound
from interface.network.FloatingIP import FloatingIP
from interface.network.FloatingIPManager import FloatingIPManager
class OpenstackFloatingIPManager(FloatingIPManager):
def __init__(self, openstack) -> None:
super().__init__()
self.openstack = openstack
@staticmethod
def os_floating_ip_to_rc_floating_ip(os_floating_ip) -> FloatingIP:
return FloatingIP(
os_floating_ip.id,
os_floating_ip.router_id,
os_floating_ip.floating_network_id,
os_floating_ip.subnet_id,
os_floating_ip.port_id,
os_floating_ip.name,
os_floating_ip.fixed_ip_address,
os_floating_ip.floating_ip_address,
os_floating_ip.status,
os_floating_ip.created_at,
os_floating_ip.updated_at
)
def create(self, floating_network_id) -> FloatingIP:
os_floating_ip = self.openstack.network.create_ip(floating_network_id=floating_network_id)
return self.os_floating_ip_to_rc_floating_ip(os_floating_ip)
def get(self, id) -> Optional[FloatingIP]:
try:
os_floating_ip = self.openstack.network.get_ip(id)
except ResourceNotFound:
return None
return self.os_floating_ip_to_rc_floating_ip(os_floating_ip)
def update(self, id, params) -> Optional[FloatingIP]:
try:
os_floating_ip = self.openstack.network.update_ip(id, **params)
except ResourceNotFound:
return None
return self.os_floating_ip_to_rc_floating_ip(os_floating_ip)
def delete(self, id) -> bool:
try:
self.openstack.network.delete_ip(id)
except ResourceNotFound:
return False
return True
def list(self) -> []:
floating_ips = []
for os_floating_ip in self.openstack.floating_ip.ips():
floating_ips.append(self.os_floating_ip_to_rc_floating_ip(os_floating_ip))
return floating_ips
from typing import Optional
from openstack.exceptions import ResourceNotFound
from interface.network.Network import Network
from interface.network.NetworkManager import NetworkManager
class OpenstackNetworkManager(NetworkManager):
def __init__(self, openstack) -> None:
super().__init__()
self.openstack = openstack
@staticmethod
def os_network_to_rc_network(os_network) -> Network:
return Network(
os_network.id,
os_network.name,
os_network.status,
os_network.subnet_ids,
os_network.created_at
)
def create(self) -> Network:
os_network = self.openstack.network.create_network()
return self.os_network_to_rc_network(os_network)
def get(self, id) -> Optional[Network]:
try:
os_network = self.openstack.network.get_network(id)
except ResourceNotFound:
return None
return self.os_network_to_rc_network(os_network)
def update(self, id, params) -> Optional[Network]:
try:
os_network = self.openstack.network.update_network(id, **params)
except ResourceNotFound:
return None
return self.os_network_to_rc_network(os_network)
def delete(self, id) -> bool:
try:
self.openstack.network.delete_network(id)
except ResourceNotFound:
return False
return True
def list(self) -> []:
networks = []
for os_network in self.openstack.network.networks():
networks.append(self.os_network_to_rc_network(os_network))
return networks
from typing import Optional
from openstack.exceptions import ResourceNotFound
from interface.network.Port import Port
from interface.network.PortManager import PortManager
class OpenstackPortManager(PortManager):
def __init__(self, openstack) -> None:
super().__init__()
self.openstack = openstack
@staticmethod
def os_port_to_rc_port(os_port):
return Port(
os_port.id,
os_port.network_id,
os_port.device_id,
os_port.security_group_ids,
os_port.name,
os_port.status,
os_port.created_at
)
def create(self, network_id) -> Port:
os_port = self.openstack.network.create_port(network_id=network_id)
return self.os_port_to_rc_port(os_port)
def get(self, id) -> Optional[Port]:
try:
os_port = self.openstack.network.get_port(id)
except ResourceNotFound:
return None
return self.os_port_to_rc_port(os_port)
def update(self, id, params) -> Optional[Port]:
try:
os_port = self.openstack.network.update_port(id, **params)
except ResourceNotFound:
return None
return self.os_port_to_rc_port(os_port)
def delete(self, id) -> bool:
try:
self.openstack.network.delete_port(id)
except ResourceNotFound:
return False
return True
def list(self) -> []:
ports = []
for os_port in self.openstack.network.ports():
ports.append(self.os_port_to_rc_port(os_port))
return ports
from typing import Optional
from openstack.exceptions import ResourceNotFound
from interface.network.Router import Router
from interface.network.RouterManager import RouterManager
class OpenstackRouterManager(RouterManager):
def __init__(self, openstack) -> None:
super().__init__()
self.openstack = openstack
@staticmethod
def os_router_to_rc_router(os_router) -> Router:
return Router(
os_router.id,
os_router.name,
os_router.status,
os_router.external_gateway_info,
os_router.created_at,
os_router.updated_at
)
def create(self) -> Router:
os_router = self.openstack.network.create_router()
return self.os_router_to_rc_router(os_router)
def get(self, id) -> Optional[Router]:
try:
os_router = self.openstack.network.get_router(id)
except ResourceNotFound:
return None
return self.os_router_to_rc_router(os_router)
def update(self, id, params) -> Optional[Router]:
try:
os_router = self.openstack.network.update_router(id, **params)
except ResourceNotFound:
return None
return self.os_router_to_rc_router(os_router)
def add_interface(self, id, subnet_id):
try:
os_router = self.openstack.network.add_interface_to_router(id, subnet_id) # TODO: can't get the id of the router from dict
except ResourceNotFound:
return None
return self.os_router_to_rc_router(os_router)
def add_gateway(self, id, network_id):
#try:
os_router = self.openstack.network.add_gateway_to_router(id, external_gateway_info=network_id)
#except ResourceNotFound:
# return None
print(os_router)
return self.os_router_to_rc_router(os_router)
def delete(self, id) -> bool:
try:
self.openstack.network.delete_router(id)
except ResourceNotFound:
return False
return True
def list(self) -> []:
routers = []
for os_router in self.openstack.network.routers():
routers.append(self.os_router_to_rc_router(os_router))
return routers
from typing import Optional
from openstack.exceptions import ResourceNotFound
from interface.network.Subnet import Subnet
from interface.network.SubnetManager import SubnetManager
class OpenstackSubnetManager(SubnetManager):
def __init__(self, openstack) -> None:
super().__init__()
self.openstack = openstack
@staticmethod
def os_subnet_to_rc_subnet(os_subnet):
return Subnet(
os_subnet.id,
os_subnet.network_id,
os_subnet.name,
os_subnet.cidr,
os_subnet.ip_version,
os_subnet.gateway_ip,
os_subnet.is_dhcp_enabled,
os_subnet.created_at
)
def create(self, network_id, ip_version, cidr) -> Subnet:
os_subnet = self.openstack.network.create_subnet(
network_id=network_id,
ip_version=ip_version,
cidr=cidr
)
return self.os_subnet_to_rc_subnet(os_subnet)
def get(self, id) -> Optional[Subnet]:
try:
os_subnet = self.openstack.network.get_subnet(id)
except ResourceNotFound:
return None
return self.os_subnet_to_rc_subnet(os_subnet)
def update(self, params) -> Optional[Subnet]:
try:
os_subnet = self.openstack.network.update_subnet(id, **params)
except ResourceNotFound:
return None
return self.os_subnet_to_rc_subnet(os_subnet)
def delete(self, id) -> bool:
try:
self.openstack.network.delete_subnet(id=id)
except ResourceNotFound:
return False
return True
def list(self) -> []:
subnets = []
for os_subnet in self.openstack.network.subnets():
subnets.append(self.os_subnet_to_rc_subnet(os_subnet))
return subnets
import json
class FloatingIP:
def __init__(self,
id,
router_id,
floating_network_id,
subnet_id,
port_id,
name,
fixed_ip_address,
floating_ip_address,
status,
created_at,
updated_at
) -> None:
super().__init__()
self.id = id
self.router_id = router_id
self.floating_network_id = floating_network_id
self.subnet_id = subnet_id
self.port_id = port_id
self.name = name
self.fixed_ip_address = fixed_ip_address
self.floating_ip_address = floating_ip_address
self.status = status
self.created_at = created_at
self.updated_at = updated_at
def __str__(self) -> str:
return self.toJSON()
def toJSON(self):
return json.dumps(self.__dict__)
from interface.network.FloatingIP import FloatingIP
class FloatingIPManager:
def __init__(self) -> None:
super().__init__()
def create(self, floating_network_id) -> FloatingIP:
raise NotImplementedError
def get(self, id) -> FloatingIP:
raise NotImplementedError
def update(self, id, params) -> FloatingIP:
raise NotImplementedError
def delete(self, id) -> bool:
raise NotImplementedError
def list(self) -> []:
raise NotImplementedError
import json
class Network:
def __init__(self,
id,
name,
status,
subnet_ids,
created_at
) -> None:
super().__init__()
self.id = id
self.name = name
self.status = status
self.subnet_ids = subnet_ids
self.created_at = created_at
def __str__(self) -> str:
return self.toJSON()
def toJSON(self):
return json.dumps(self.__dict__)
from interface.network.Network import Network
class NetworkManager:
def __init__(self) -> None:
super().__init__()
def create(self) -> Network:
raise NotImplementedError
def get(self, id) -> Network:
raise NotImplementedError
def update(self, id, params) -> Network:
raise NotImplementedError
def delete(self, id) -> bool:
raise NotImplementedError
def list(self) -> []:
raise NotImplementedError
import json
class Port:
def __init__(self,
id,
network_id,
device_id,
security_group_ids,
name,
status,
created_at
) -> None:
super().__init__()
self.id = id
self.network_id = network_id
self.device_id = device_id
self.security_group_ids = security_group_ids
self.name = name
self.status = status
self.created_at = created_at
def __str__(self) -> str:
return self.toJSON()
def toJSON(self):
return json.dumps(self.__dict__)
from interface.network.Port import Port
class PortManager:
def __init__(self) -> None:
super().__init__()
def create(self, network_id) -> Port:
raise NotImplementedError
def get(self, id) -> Port:
raise NotImplementedError
def update(self, id, params) -> Port:
raise NotImplementedError
def delete(self, id) -> bool:
raise NotImplementedError
def list(self) -> []:
raise NotImplementedError
import json
class Router:
def __init__(self,
id,
name,
status,
external_gateway_info,
created_at,
updated_at
) -> None:
super().__init__()
self.id = id
self.name = name
self.status = status
self.external_gateway_info = external_gateway_info
self.created_at = created_at
self.updated_at = updated_at
def __str__(self) -> str:
return self.toJSON()
def toJSON(self):
return json.dumps(self.__dict__)
from interface.network.Router import Router
class RouterManager:
def __init__(self) -> None:
super().__init__()
def create(self) -> Router:
raise NotImplementedError
def get(self, id) -> Router:
raise NotImplementedError
def update(self, id, params) -> Router:
raise NotImplementedError
def add_interface(self, id, subnet_id) -> Router:
raise NotImplementedError
def add_gateway(self, id, network_id) -> Router:
raise NotImplementedError
def delete(self, id) -> bool:
raise NotImplementedError
def list(self) -> []:
raise NotImplementedError
import json
class Subnet:
def __init__(self,
id,
network_id,
name,
cidr,
ip_version,
gateway_ip,
is_dhcp_enabled,
created_at
) -> None:
super().__init__()
self.id = id
self.network_id = network_id
self.name = name
self.cidr = cidr
self.ip_version = ip_version
self.gateway_ip = gateway_ip
self.is_dhcp_enabled = is_dhcp_enabled
self.created_at = created_at
def __str__(self) -> str:
return self.toJSON()
def toJSON(self):
return json.dumps(self.__dict__)
from interface.network.Subnet import Subnet
class SubnetManager:
def __init__(self) -> None:
super().__init__()
def create(self, network_id, ip_version, cidr) -> Subnet:
raise NotImplementedError
def get(self, id) -> Subnet:
raise NotImplementedError
def update(self, params) -> Subnet:
raise NotImplementedError
def delete(self, id) -> bool:
raise NotImplementedError
def list(self) -> []:
raise NotImplementedError
...@@ -2,6 +2,11 @@ ...@@ -2,6 +2,11 @@
from implementation.vm.instance import OSVirtualMachineManager from implementation.vm.instance import OSVirtualMachineManager
from interface.vm.resources import BlockDeviceMapping, Flavor from interface.vm.resources import BlockDeviceMapping, Flavor
import openstack import openstack
from implementation.network.OpenstackFloatingIPManager import OpenstackFloatingIPManager
from implementation.network.OpenstackNetworkManager import OpenstackNetworkManager
from implementation.network.OpenstackPortManager import OpenstackPortManager
from implementation.network.OpenstackRouterManager import OpenstackRouterManager
from implementation.network.OpenstackSubnetManager import OpenstackSubnetManager
# openstack.enable_logging(debug=True) # openstack.enable_logging(debug=True)
...@@ -62,3 +67,70 @@ print(interface.create_base_vm("BaseTest", "2", networks, block_dev_map).JSON()) ...@@ -62,3 +67,70 @@ print(interface.create_base_vm("BaseTest", "2", networks, block_dev_map).JSON())
# interface.wake_up_vm(server.id) # interface.wake_up_vm(server.id)
# interface.destroy_vm(server.id) # interface.destroy_vm(server.id)
# ==============================================================================
# Network test
# ==============================================================================
os = openstack.connect(cloud='openstack')
router_manager = OpenstackRouterManager(os)
network_manager = OpenstackNetworkManager(os)
subnet_manager = OpenstackSubnetManager(os)
port_manager = OpenstackPortManager(os)
floating_ip_manager = OpenstackFloatingIPManager(os)
'''
Network setup steps:
- Create inner network
- Create subnet for inner network
- Create router
- Add public network to router as gateway
- Add inner subnet to router as interface
- Add interface (or port?) to vm
- Get the port of the vm
- Create floating ip to public network
- Associate floating ip and vm port
'''
#router = router_manager.create()
router = router_manager.get('3ee87bcd-b1b5-4e14-86a1-ce691bd3e5a2')
print('Router:')
print('\t%s' % router)
#network = network_manager.create()
public_network = network_manager.get('3b349c93-8b2c-4abd-b8ce-22b7db4cca98')
inner_network = network_manager.get('8df0d7ab-7d56-4496-aca0-317af66b3ebc')
print('Network:')
print('\tPublic: %s' % public_network)
print('\tInner: %s' % inner_network)
#subnet = subnet_manager.create(network.id, 4, '10.34.0.0/16')
public_subnet = subnet_manager.get('8a7e05f8-6358-48f7-9104-990d350425a0')
# create(inner_network.id, 4, '10.0.0.0/24')
inner_subnet = subnet_manager.get('34911571-c929-4433-b4e6-305fcb585a91')
print('Subnet:')
print('\tPublic: %s' % public_subnet)
print('\tInner: %s' % inner_subnet)
vm_port = port_manager.get('85202d6d-7aa0-44bc-a5eb-78b0519066c5') # inner
print('Port:')
print('\tVM: %s' % vm_port)
#floating_ip = floating_ip_manager.create(public_network.id)
floating_ip = floating_ip_manager.get('fe1e329f-aedb-4b3e-a649-9fec8eb3ece5')
print('Floating IP:')
print('\tFloating IP: %s' % floating_ip)
# print(router_manager.add_interface(router.id, inner_subnet.id)) # TODO: fix error msg
# print(router_manager.add_gateway(router.id, public_network.id)) # TODO: fix it!
# print(floating_ip_manager.update(floating_ip.id, {
# 'port_id': vm_port.id
# }))
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