Revision a4658bbe snf-cyclades-app/synnefo/logic/networks.py

b/snf-cyclades-app/synnefo/logic/networks.py
30 30
# documentation are those of the authors and should not be
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33
import ipaddr
34

  
33 35
from functools import wraps
34 36
from django.db import transaction
35 37

  
38
from django.conf import settings
36 39
from snf_django.lib.api import faults
37 40
from synnefo.api import util
38 41
from synnefo import quotas
......
80 83
        raise faults.BadRequest("IPv6 only networks can not be"
81 84
                                " pools.")
82 85
    # Check that network parameters are valid
83
    util.validate_network_params(subnet, gateway, subnet6, gateway6)
86
    validate_network_params(subnet, gateway, subnet6, gateway6)
84 87

  
85 88
    try:
86 89
        fmode, flink, fmac_prefix, ftags = util.values_from_flavor(flavor)
......
165 168
        # If network does not exist in any backend, update the network state
166 169
        backend_mod.update_network_state(network)
167 170
    return network
171

  
172

  
173
def validate_network_params(subnet=None, gateway=None, subnet6=None,
174
                            gateway6=None):
175
    if (subnet is None) and (subnet6 is None):
176
        raise faults.BadRequest("subnet or subnet6 is required")
177

  
178
    if subnet:
179
        try:
180
            # Use strict option to not all subnets with host bits set
181
            network = ipaddr.IPv4Network(subnet, strict=True)
182
        except ValueError:
183
            raise faults.BadRequest("Invalid network IPv4 subnet")
184

  
185
        # Check that network size is allowed!
186
        prefixlen = network.prefixlen
187
        if not prefixlen <= 29 and prefixlen > settings.MAX_CIDR_BLOCK:
188
            raise faults.OverLimit(message="Unsupported network size",
189
                                   details="Network mask must be in range"
190
                                           " (%s, 29]"
191
                                           % settings.MAX_CIDR_BLOCK)
192
        if gateway:  # Check that gateway belongs to network
193
            try:
194
                gateway = ipaddr.IPv4Address(gateway)
195
            except ValueError:
196
                raise faults.BadRequest("Invalid network IPv4 gateway")
197
            if not gateway in network:
198
                raise faults.BadRequest("Invalid network IPv4 gateway")
199

  
200
    if subnet6:
201
        try:
202
            # Use strict option to not all subnets with host bits set
203
            network6 = ipaddr.IPv6Network(subnet6, strict=True)
204
        except ValueError:
205
            raise faults.BadRequest("Invalid network IPv6 subnet")
206
        if gateway6:
207
            try:
208
                gateway6 = ipaddr.IPv6Address(gateway6)
209
            except ValueError:
210
                raise faults.BadRequest("Invalid network IPv6 gateway")
211
            if not gateway6 in network6:
212
                raise faults.BadRequest("Invalid network IPv6 gateway")

Also available in: Unified diff