예제 #1
0
    def handle_put(self, request, user, *args, **kwargs):
        '''
            Treat requests PUT to add a network IPv4 with num hosts param
            URL: network/ipv4/add/
        '''

        # Commons Validations

        # User permission
        if not has_perm(user, AdminPermission.VLAN_MANAGEMENT,
                        AdminPermission.WRITE_OPERATION):
            self.log.error(
                u'User does not have permission to perform the operation.')
            return self.not_authorized()

        # Business Validations

        # Load XML data
        xml_map, _ = loads(request.raw_post_data)

        # XML data format
        networkapi_map = xml_map.get('networkapi')
        if networkapi_map is None:
            msg = u'There is no value to the networkapi tag of XML request.'
            self.log.error(msg)
            return self.response_error(3, msg)
        vlan_map = networkapi_map.get('vlan')
        if vlan_map is None:
            msg = u'There is no value to the vlan tag of XML request.'
            self.log.error(msg)
            return self.response_error(3, msg)

        # Get XML data
        vlan_id = vlan_map.get('id_vlan')
        network_type = vlan_map.get('id_tipo_rede')
        environment_vip = vlan_map.get('id_ambiente_vip')
        num_hosts = vlan_map.get('num_hosts')

        # Valid num_hosts
        if not is_valid_int_greater_zero_param(
                num_hosts) or int(num_hosts) > MAX_IPV4_HOSTS:
            self.log.error(u'Parameter num_hosts is invalid. Value: %s.',
                           num_hosts)
            return self.response_error(269, 'num_hosts', num_hosts)

        num_hosts = int(num_hosts)
        # Get configuration
        conf = Configuration.get()

        num_hosts += conf.IPv4_MIN + conf.IPv4_MAX
        prefix = get_prefix_IPV4(num_hosts)
        self.log.info(u'Prefix for %s hosts: %s' % (num_hosts, prefix))

        return self.network_ipv4_add(user, vlan_id, network_type,
                                     environment_vip, prefix)
    def handle_put(self, request, user, *args, **kwargs):
        '''
            Treat requests PUT to add a network IPv4 with num hosts param
            URL: network/ipv4/add/
        '''

        # Commons Validations

        # User permission
        if not has_perm(user, AdminPermission.VLAN_MANAGEMENT, AdminPermission.WRITE_OPERATION):
            self.log.error(
                u'User does not have permission to perform the operation.')
            return self.not_authorized()

        # Business Validations

        # Load XML data
        xml_map, _ = loads(request.raw_post_data)

        # XML data format
        networkapi_map = xml_map.get('networkapi')
        if networkapi_map is None:
            msg = u'There is no value to the networkapi tag of XML request.'
            self.log.error(msg)
            return self.response_error(3, msg)
        vlan_map = networkapi_map.get('vlan')
        if vlan_map is None:
            msg = u'There is no value to the vlan tag of XML request.'
            self.log.error(msg)
            return self.response_error(3, msg)

        # Get XML data
        vlan_id = vlan_map.get('id_vlan')
        network_type = vlan_map.get('id_tipo_rede')
        environment_vip = vlan_map.get('id_ambiente_vip')
        num_hosts = vlan_map.get('num_hosts')

        # Valid num_hosts
        if not is_valid_int_greater_zero_param(num_hosts) or int(num_hosts) > MAX_IPV4_HOSTS:
            self.log.error(
                u'Parameter num_hosts is invalid. Value: %s.', num_hosts)
            return self.response_error(269, 'num_hosts', num_hosts)

        num_hosts = int(num_hosts)
        # Get configuration
        conf = Configuration.get()

        num_hosts += conf.IPv4_MIN + conf.IPv4_MAX
        prefix = get_prefix_IPV4(num_hosts)
        self.log.info(u'Prefix for %s hosts: %s' % (num_hosts, prefix))

        return self.network_ipv4_add(user, vlan_id, network_type, environment_vip, prefix)