Ejemplo n.º 1
0
def post_cidr(obj):

    from netaddr import IPNetwork

    try:
        cidr = EnvCIDR()

        data = dict()
        data['id'] = obj.get('id')
        data['ip_version'] = obj.get('ip_version')
        data['subnet_mask'] = obj.get('subnet_mask')
        data['network_type'] = obj.get('network_type')
        data['environment'] = obj.get('environment')
        data['network'] = obj.get('network')

        try:
            network = IPNetwork(obj.get('network'))
        except Exception as e:
            raise ValidationAPIException(str(e))

        environment = Ambiente().get_by_pk(int(obj.get('environment')))
        msg = list()
        if not cidr.check_cidr(environment, obj.get('network')):
            message = "The network is not a subnet of the father environment."
            msg.append(
                dict(message=message, environment_id=obj.get('environment')))
            log.info(message)

        duplicated_cidr = cidr.check_duplicated_cidr(environment,
                                                     obj.get('network'))

        duplicated_ids = [ids.id_env.id for ids in duplicated_cidr]

        if duplicated_cidr:
            message = "CIDR %s overlaps with networks from environments: %s" % \
                      (obj.get('network'), duplicated_ids)
            msg.append(
                dict(message=message, environment_id=obj.get('environment')))
            log.info(message)

        data['network_first_ip'] = int(network.ip)
        data['network_last_ip'] = int(network.broadcast)
        data['network_mask'] = network.prefixlen

        response = cidr.post(data)

    except CIDRErrorV3 as e:
        raise ValidationAPIException(str(e))
    except ValidationAPIException as e:
        raise ValidationAPIException(str(e))
    except Exception as e:
        raise NetworkAPIException(str(e))

    return response, msg
Ejemplo n.º 2
0
def delete_cidr(cidr=None, environment=None):
    """Delete CIDR."""

    try:
        env_cidr = EnvCIDR()
        cidr_obj = env_cidr.get(cidr_id=cidr, env_id=environment)
        for cidr in cidr_obj:
            cidr.delete()
    except CIDRErrorV3 as e:
        raise ValidationAPIException(str(e))
    except ValidationAPIException as e:
        raise ObjectDoesNotExistException(str(e))
    except Exception as e:
        raise NetworkAPIException(str(e))
Ejemplo n.º 3
0
def get_cidr(cidr=None, environment=None):
    """Return a list of CIDR."""

    try:
        env_cidr = EnvCIDR()
        cidr = env_cidr.get(cidr_id=cidr, env_id=environment)
    except CIDRErrorV3 as e:
        raise ValidationAPIException(str(e))
    except ValidationAPIException as e:
        raise ValidationAPIException(str(e))
    except Exception as e:
        raise NetworkAPIException(str(e))
    else:
        return cidr
Ejemplo n.º 4
0
def post_cidr_auto(obj):
    try:
        cidr = EnvCIDR()
        subnet, _ = cidr.checkAvailableCIDR(obj.get('environment'),
                                            obj.get('ip_version'))
        obj["network"] = subnet
        response, msg = post_cidr(obj)

    except CIDRErrorV3 as e:
        raise ValidationAPIException(str(e))
    except ValidationAPIException as e:
        raise ValidationAPIException(str(e))
    except Exception as e:
        raise NetworkAPIException(str(e))

    return response, msg
Ejemplo n.º 5
0
    def test_allocateFirstSubnetCIDR(self):
        """Test of Success allocate the first subnet."""

        from networkapi.ambiente.models import EnvCIDR

        response, _ = EnvCIDR().checkAvailableCIDR(12, "v4")

        self.compare_values("10.224.0.0/27", response)
Ejemplo n.º 6
0
    def test_checkAvailableCIDR(self):
        """Test of Success to get the next available CIDR."""

        from networkapi.ambiente.models import EnvCIDR

        response, _ = EnvCIDR().checkAvailableCIDR(2, "v4")

        self.compare_values("10.0.6.0/24", response)
Ejemplo n.º 7
0
    def test_AvailableCIDR(self):
        """Test of Success when the method has to find the
        subnet that was not allocate yeat."""

        from networkapi.ambiente.models import EnvCIDR

        response, _ = EnvCIDR().checkAvailableCIDR(10, "v4")

        self.compare_values("10.143.0.4/31", response)
Ejemplo n.º 8
0
    def test_checkAvailableCIDRWithTwoBlocks(self):
        """Test of Success to get the next available CIDR
        when the environment father has two cidr and just
        one of them with subnet available."""

        from networkapi.ambiente.models import EnvCIDR

        response, _ = EnvCIDR().checkAvailableCIDR(7, "v4")

        self.compare_values("201.7.1.0/24", response)
    def handle_get(self, request, user, *args, **kwargs):
        """Treat requests GET to list all Environments.

        URL: /environment/configuration/list/environment_id/
        """
        try:

            environment_id = kwargs.get('environment_id')

            self._validate_permission(user)

            self._validate_environment_id(environment_id)

            configurations_prefix = EnvCIDR().get(env_id=environment_id)

            lists_configuration = list()

            for configuration in configurations_prefix:

                network_type = configuration.id_network_type.tipo_rede \
                    if configuration.id_network_type else None

                configuration_dict = dict(id=configuration.id,
                                          subnet=configuration.network,
                                          new_prefix=configuration.subnet_mask,
                                          type=configuration.ip_version,
                                          network_type=network_type)

                lists_configuration.append(configuration_dict)

            return self.response(
                dumps_networkapi({'lists_configuration': lists_configuration}))

        except PermissionError:
            return self.not_authorized()
        except InvalidValueError as e:
            self.log.error('Parameter %s is invalid. Value: %s.', e.param,
                           e.value)
            return self.response_error(269, e.param, e.value)
        except AmbienteNotFoundError:
            return self.response_error(112)
        except AmbienteError:
            return self.response_error(1)