Revision 53b9ba10 snf-cyclades-app/synnefo/api/management/commands/_common.py

b/snf-cyclades-app/synnefo/api/management/commands/_common.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
import ipaddr
34 35
from datetime import datetime
35 36

  
36 37
from django.utils.timesince import timesince, timeuntil
38
from django.core.management import CommandError
39

  
40
from synnefo.api.util import validate_network_size
41
from synnefo.settings import MAX_CIDR_BLOCK
37 42

  
38 43

  
39 44
def format_bool(b):
......
55 60
        return "BUILD(" + str(vm.buildpercentage) + "%)"
56 61
    else:
57 62
        return vm.operstate
63

  
64

  
65
def validate_network_info(options):
66
    subnet = options['subnet']
67
    gateway = options['gateway']
68
    subnet6 = options['subnet6']
69
    gateway6 = options['gateway6']
70

  
71
    try:
72
        net = ipaddr.IPv4Network(subnet)
73
        prefix = net.prefixlen
74
        if not validate_network_size(prefix):
75
            raise CommandError("Unsupport network mask %d."
76
                               " Must be in range (%s,29] "
77
                               % (prefix, MAX_CIDR_BLOCK))
78
    except ValueError:
79
        raise CommandError('Malformed subnet')
80
    try:
81
        gateway and ipaddr.IPv4Address(gateway) or None
82
    except ValueError:
83
        raise CommandError('Malformed gateway')
84

  
85
    try:
86
        subnet6 and ipaddr.IPv6Network(subnet6) or None
87
    except ValueError:
88
        raise CommandError('Malformed subnet6')
89

  
90
    try:
91
        gateway6 and ipaddr.IPv6Address(gateway6) or None
92
    except ValueError:
93
        raise CommandError('Malformed gateway6')
94

  
95
    return subnet, gateway, subnet6, gateway6

Also available in: Unified diff