Commit afb6ddeb authored by Dionysis Grigoropoulos's avatar Dionysis Grigoropoulos
Browse files

cyclades: Add a function to check name legnth

* Add a function logic/utils.py to check the length of a string and
raise an API fault with the corresponding message.
* Check the length of the name of servers/ports/networks/subnets
parent 40b14f81
...@@ -295,7 +295,10 @@ class VirtualMachine(models.Model): ...@@ -295,7 +295,10 @@ class VirtualMachine(models.Model):
'DESTROYED': 'DELETED', 'DESTROYED': 'DELETED',
} }
name = models.CharField('Virtual Machine Name', max_length=255) VIRTUAL_MACHINE_NAME_LENGTH = 255
name = models.CharField('Virtual Machine Name',
max_length=VIRTUAL_MACHINE_NAME_LENGTH)
userid = models.CharField('User ID of the owner', max_length=100, userid = models.CharField('User ID of the owner', max_length=100,
db_index=True, null=False) db_index=True, null=False)
backend = models.ForeignKey(Backend, null=True, backend = models.ForeignKey(Backend, null=True,
......
...@@ -42,6 +42,7 @@ from synnefo.db.models import Network, Backend ...@@ -42,6 +42,7 @@ from synnefo.db.models import Network, Backend
from synnefo.db.utils import validate_mac from synnefo.db.utils import validate_mac
from synnefo.db.pools import EmptyPool from synnefo.db.pools import EmptyPool
from synnefo.logic import backend as backend_mod from synnefo.logic import backend as backend_mod
from synnefo.logic import utils
from logging import getLogger from logging import getLogger
log = getLogger(__name__) log = getLogger(__name__)
...@@ -76,6 +77,9 @@ def create(userid, name, flavor, link=None, mac_prefix=None, mode=None, ...@@ -76,6 +77,9 @@ def create(userid, name, flavor, link=None, mac_prefix=None, mode=None,
if link is not None and flavor == "PHYSICAL_VLAN": if link is not None and flavor == "PHYSICAL_VLAN":
raise faults.BadRequest("Cannot override PHYSICAL_VLAN link") raise faults.BadRequest("Cannot override PHYSICAL_VLAN link")
utils.check_name_length(name, Network.NETWORK_NAME_LENGTH, "Network name "
"is too long")
try: try:
fmode, flink, fmac_prefix, ftags = util.values_from_flavor(flavor) fmode, flink, fmac_prefix, ftags = util.values_from_flavor(flavor)
except EmptyPool: except EmptyPool:
......
...@@ -39,7 +39,7 @@ from snf_django.lib.api import faults ...@@ -39,7 +39,7 @@ from snf_django.lib.api import faults
from django.conf import settings from django.conf import settings
from synnefo import quotas from synnefo import quotas
from synnefo.api import util from synnefo.api import util
from synnefo.logic import backend, ips from synnefo.logic import backend, ips, utils
from synnefo.logic.backend_allocator import BackendAllocator from synnefo.logic.backend_allocator import BackendAllocator
from synnefo.db.models import (NetworkInterface, VirtualMachine, from synnefo.db.models import (NetworkInterface, VirtualMachine,
VirtualMachineMetadata, IPAddressLog, Network) VirtualMachineMetadata, IPAddressLog, Network)
...@@ -170,6 +170,9 @@ def create(userid, name, password, flavor, image, metadata={}, ...@@ -170,6 +170,9 @@ def create(userid, name, password, flavor, image, metadata={},
# Allocate server to a Ganeti backend # Allocate server to a Ganeti backend
use_backend = allocate_new_server(userid, flavor) use_backend = allocate_new_server(userid, flavor)
utils.check_name_length(name, VirtualMachine.VIRTUAL_MACHINE_NAME_LENGTH,
"Server name is too long")
# Create the ports for the server # Create the ports for the server
ports = create_instance_ports(userid, networks) ports = create_instance_ports(userid, networks)
...@@ -453,6 +456,9 @@ def _create_port(userid, network, machine=None, use_ipaddress=None, ...@@ -453,6 +456,9 @@ def _create_port(userid, network, machine=None, use_ipaddress=None,
raise faults.Conflict("Cannot create port while network %s is in" raise faults.Conflict("Cannot create port while network %s is in"
" 'SNF:DRAINED' status" % network.id) " 'SNF:DRAINED' status" % network.id)
utils.check_name_length(name, NetworkInterface.NETWORK_IFACE_NAME_LENGTH,
"Port name is too long")
ipaddress = None ipaddress = None
if use_ipaddress is not None: if use_ipaddress is not None:
# Use an existing IPAddress object. # Use an existing IPAddress object.
......
...@@ -41,6 +41,7 @@ from django.db.models import Q ...@@ -41,6 +41,7 @@ from django.db.models import Q
from snf_django.lib import api from snf_django.lib import api
from snf_django.lib.api import faults from snf_django.lib.api import faults
from synnefo.logic import utils
from synnefo.db.models import Subnet, Network, IPPoolTable from synnefo.db.models import Subnet, Network, IPPoolTable
...@@ -108,7 +109,8 @@ def _create_subnet(network_id, user_id, cidr, name, ipversion=4, gateway=None, ...@@ -108,7 +109,8 @@ def _create_subnet(network_id, user_id, cidr, name, ipversion=4, gateway=None,
else: else:
validate_subnet_params(subnet=cidr, gateway=gateway) validate_subnet_params(subnet=cidr, gateway=gateway)
name = check_name_length(name) name = utils.check_name_length(name, Subnet.SUBNET_NAME_LENGTH, "Subnet "
"name is too long")
sub = Subnet.objects.create(name=name, network=network, cidr=cidr, sub = Subnet.objects.create(name=name, network=network, cidr=cidr,
ipversion=ipversion, gateway=gateway, ipversion=ipversion, gateway=gateway,
dhcp=dhcp, host_routes=host_routes, dhcp=dhcp, host_routes=host_routes,
...@@ -178,7 +180,8 @@ def update_subnet(sub_id, name, user_id): ...@@ -178,7 +180,8 @@ def update_subnet(sub_id, name, user_id):
if user_id != subnet.network.userid: if user_id != subnet.network.userid:
raise api.faults.Unauthorized("Unauthorized operation") raise api.faults.Unauthorized("Unauthorized operation")
check_name_length(name) utils.check_name_length(name, Subnet.SUBNET_NAME_LENGTH, "Subnet name is "
" too long")
subnet.name = name subnet.name = name
subnet.save() subnet.save()
...@@ -207,13 +210,6 @@ def check_number_of_subnets(network, version): ...@@ -207,13 +210,6 @@ def check_number_of_subnets(network, version):
"network is allowed") "network is allowed")
def check_name_length(name):
"""Check if the length of a name is within acceptable value"""
if len(str(name)) > Subnet.SUBNET_NAME_LENGTH:
raise api.faults.BadRequest("Subnet name too long")
return name
def validate_pools(pool_list, cidr, gateway): def validate_pools(pool_list, cidr, gateway):
"""Validate IP Pools """Validate IP Pools
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
# Utility functions # Utility functions
from synnefo.db.models import VirtualMachine, Network from synnefo.db.models import VirtualMachine, Network
from snf_django.lib.api import faults
from django.conf import settings from django.conf import settings
from copy import deepcopy from copy import deepcopy
...@@ -187,3 +188,9 @@ def hide_pass(kw): ...@@ -187,3 +188,9 @@ def hide_pass(kw):
return kw1 return kw1
else: else:
return kw return kw
def check_name_length(name, max_length, message):
"""Check if a string is within acceptable value length"""
if len(str(name)) > max_length:
raise faults.BadRequest(message)
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 to comment