当前位置: 首页>>代码示例>>Python>>正文


Python Vlan.get_by_pk方法代码示例

本文整理汇总了Python中networkapi.vlan.models.Vlan.get_by_pk方法的典型用法代码示例。如果您正苦于以下问题:Python Vlan.get_by_pk方法的具体用法?Python Vlan.get_by_pk怎么用?Python Vlan.get_by_pk使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在networkapi.vlan.models.Vlan的用法示例。


在下文中一共展示了Vlan.get_by_pk方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: handle_get

# 需要导入模块: from networkapi.vlan.models import Vlan [as 别名]
# 或者: from networkapi.vlan.models.Vlan import get_by_pk [as 别名]
    def handle_get(self, request, user, *args, **kwargs):
        """Treat GET requests to check if a vlan need confimation to insert

        URL: vlan/confirm/
        """

        try:

            # Get XML data
            ip_version = kwargs.get("ip_version")

            if ip_version == "None":
                is_number = True
                number = kwargs.get("number")
                id_environment = kwargs.get("id_environment")
            else:
                network = kwargs.get("number")
                network = network.replace("net_replace", "/")
                id_vlan = kwargs.get("id_environment")
                if ip_version == "1":
                    version = "v6"
                else:
                    version = "v4"
                is_number = False

            # 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()

            if is_number:
                # Valid number

                if not is_valid_int_greater_zero_param(id_environment):
                    self.log.error(u"Parameter id_environment is invalid. Value: %s.", id_environment)
                    raise InvalidValueError(None, "id_environment", id_environment)

                ambiente = Ambiente.get_by_pk(id_environment)

                equips = list()
                envs = list()
                envs_aux = list()

                for env in ambiente.equipamentoambiente_set.all():
                    equips.append(env.equipamento)

                for equip in equips:
                    for env in equip.equipamentoambiente_set.all():
                        if not env.ambiente_id in envs_aux:
                            envs.append(env.ambiente)
                            envs_aux.append(env.ambiente_id)

                # Valid number
                map = dict()
                map["needs_confirmation"] = True

                for env in envs:
                    for vlan in env.vlan_set.all():
                        if int(vlan.num_vlan) == int(number):
                            if (
                                ambiente.filter_id == None
                                or vlan.ambiente.filter_id == None
                                or int(vlan.ambiente.filter_id) != int(ambiente.filter_id)
                            ):
                                map["needs_confirmation"] = False
                            else:
                                map["needs_confirmation"] = True
                                break
            else:
                # Valid subnet

                if not is_valid_int_greater_zero_param(id_vlan):
                    self.log.error(u"Parameter id_vlan is invalid. Value: %s.", id_vlan)
                    raise InvalidValueError(None, "id_vlan", id_vlan)

                # Get all vlans environments from equipments of the current
                # environment
                vlan = Vlan()
                vlan = vlan.get_by_pk(id_vlan)
                ambiente = vlan.ambiente

                equips = list()
                envs = list()
                envs_aux = list()

                for env in ambiente.equipamentoambiente_set.all():
                    equips.append(env.equipamento)

                for equip in equips:
                    for env in equip.equipamentoambiente_set.all():
                        if not env.ambiente_id in envs_aux:
                            envs.append(env.ambiente)
                            envs_aux.append(env.ambiente_id)

                # Check subnet's
                network = str(network)
                prefix = split(network, "/")
                net_explode = prefix[0]
#.........这里部分代码省略.........
开发者ID:andrewsmedina,项目名称:GloboNetworkAPI,代码行数:103,代码来源:VlanValidateResource.py

示例2: handle_put

# 需要导入模块: from networkapi.vlan.models import Vlan [as 别名]
# 或者: from networkapi.vlan.models.Vlan import get_by_pk [as 别名]
    def handle_put(self, request, user, *args, **kwargs):
        '''Treat PUT requests to activate a vlan 
           Set column ativada = 1

        URL: vlan/create/
        '''

        try:

            # 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()

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

            # XML data format
            networkapi_map = xml_map.get('networkapi')
            vlan_map = networkapi_map.get('vlan')

            id_vlan = vlan_map.get('vlan_id')

            vlan = Vlan()
            vlan = vlan.get_by_pk(id_vlan)

            # Check permission group equipments
            equips_from_ipv4 = Equipamento.objects.filter(
                ipequipamento__ip__networkipv4__vlan=id_vlan, equipamentoambiente__is_router=1)
            equips_from_ipv6 = Equipamento.objects.filter(
                ipv6equipament__ip__networkipv6__vlan=id_vlan, equipamentoambiente__is_router=1)
            for equip in equips_from_ipv4:
                # User permission
                if not has_perm(user, AdminPermission.EQUIPMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION, None, equip.id, AdminPermission.EQUIP_WRITE_OPERATION):
                    self.log.error(
                        u'User does not have permission to perform the operation.')
                    return self.not_authorized()
            for equip in equips_from_ipv6:
                # User permission
                if not has_perm(user, AdminPermission.EQUIPMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION, None, equip.id, AdminPermission.EQUIP_WRITE_OPERATION):
                    self.log.error(
                        u'User does not have permission to perform the operation.')
                    return self.not_authorized()

            if vlan.ativada:
                return self.response_error(122)

            # Make command
            vlan_command = settings.VLAN_CREATE % int(id_vlan)

            # Execute command
            code, stdout, stderr = exec_script(vlan_command)

            # if command was successfully executed
            if code == 0:

                # After execute script, change to activated
                vlan.activate(user)
            else:
                return self.response_error(2, stdout + stderr)

            # Send to Queue
            queue_manager = QueueManager()

            serializer = VlanSerializer(vlan)
            data_to_queue = serializer.data
            data_to_queue.update({'description': queue_keys.VLAN_ACTIVATE})
            queue_manager.append({'action': queue_keys.VLAN_ACTIVATE,'kind': queue_keys.VLAN_KEY,'data': data_to_queue})

            queue_manager.send()

            return self.response(dumps_networkapi({}))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)
开发者ID:itnihao,项目名称:GloboNetworkAPI,代码行数:78,代码来源:VlanEditResource.py

示例3: handle_post

# 需要导入模块: from networkapi.vlan.models import Vlan [as 别名]
# 或者: from networkapi.vlan.models.Vlan import get_by_pk [as 别名]
    def handle_post(self, request, user, *args, **kwargs):
        '''Treat POST requests to edit a vlan 

        URL: vlan/edit/
        '''

        try:

            network_version = kwargs.get('network_version')

            # 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, attrs_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
            environment_id = vlan_map.get('environment_id')
            number = vlan_map.get('number')
            name = vlan_map.get('name')
            acl_file = vlan_map.get('acl_file')
            acl_file_v6 = vlan_map.get('acl_file_v6')
            description = vlan_map.get('description')
            id_vlan = vlan_map.get('vlan_id')

            # Valid vlan ID
            if not is_valid_int_greater_zero_param(id_vlan):
                self.log.error(
                    u'Parameter id_vlan is invalid. Value: %s.', id_vlan)
                raise InvalidValueError(None, 'id_vlan', id_vlan)

            # Valid environment_id ID
            if not is_valid_int_greater_zero_param(environment_id):
                self.log.error(
                    u'Parameter environment_id is invalid. Value: %s.', environment_id)
                raise InvalidValueError(None, 'environment_id', environment_id)

            # Valid number of Vlan
            if not is_valid_int_greater_zero_param(number):
                self.log.error(
                    u'Parameter number is invalid. Value: %s', number)
                raise InvalidValueError(None, 'number', number)

            # Valid name of Vlan
            if not is_valid_string_minsize(name, 3) or not is_valid_string_maxsize(name, 50):
                self.log.error(u'Parameter name is invalid. Value: %s', name)
                raise InvalidValueError(None, 'name', name)

            p = re.compile("^[A-Z0-9-_]+$")
            m = p.match(name)

            if not m:
                name = name.upper()
                m = p.match(name)

                if not m:
                    raise InvalidValueError(None, 'name', name)

            # Valid description of Vlan
            if not is_valid_string_minsize(description, 3, False) or not is_valid_string_maxsize(description, 200, False):
                self.log.error(
                    u'Parameter description is invalid. Value: %s', description)
                raise InvalidValueError(None, 'description', description)

            vlan = Vlan()
            vlan = vlan.get_by_pk(id_vlan)

            with distributedlock(LOCK_VLAN % id_vlan):

                # Valid acl_file Vlan
                if acl_file is not None:
                    if not is_valid_string_minsize(acl_file, 3) or not is_valid_string_maxsize(acl_file, 200):
                        self.log.error(
                            u'Parameter acl_file is invalid. Value: %s', acl_file)
                        raise InvalidValueError(None, 'acl_file', acl_file)
                    p = re.compile("^[A-Z0-9-_]+$")
                    m = p.match(acl_file)
                    if not m:
                        raise InvalidValueError(None, 'acl_file', acl_file)

                    # VERIFICA SE VLAN COM MESMO ACL JA EXISTE OU NAO
#.........这里部分代码省略.........
开发者ID:itnihao,项目名称:GloboNetworkAPI,代码行数:103,代码来源:VlanEditResource.py

示例4: handle_put

# 需要导入模块: from networkapi.vlan.models import Vlan [as 别名]
# 或者: from networkapi.vlan.models.Vlan import get_by_pk [as 别名]
    def handle_put(self, request, user, *args, **kwargs):
        '''Handles PUT requests to create Network and Vlan.

        URL: network/create/
        '''

        try:

            # 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()

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

            # XML data format
            networkapi_map = xml_map.get('networkapi')
            network_map = networkapi_map.get('network')
            ids = network_map.get('ids')
            id_vlan = network_map.get('id_vlan')

            if not is_valid_int_greater_zero_param(id_vlan):
                self.log.error(
                    u'The id network parameter is invalid. Value: %s.', id_vlan)
                raise InvalidValueError(None, 'id_network', id_vlan)

            vlan = Vlan()
            vlan = vlan.get_by_pk(id_vlan)

            # Check permission group equipments
            equips_from_ipv4 = Equipamento.objects.filter(
                ipequipamento__ip__networkipv4__vlan=id_vlan, equipamentoambiente__is_router=1).distinct()
            equips_from_ipv6 = Equipamento.objects.filter(
                ipv6equipament__ip__networkipv6__vlan=id_vlan, equipamentoambiente__is_router=1).distinct()
            for equip in equips_from_ipv4:
                # User permission
                if not has_perm(user, AdminPermission.EQUIPMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION, None, equip.id, AdminPermission.EQUIP_WRITE_OPERATION):
                    self.log.error(
                        u'User does not have permission to perform the operation.')
                    return self.not_authorized()
            for equip in equips_from_ipv6:
                # User permission
                if not has_perm(user, AdminPermission.EQUIPMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION, None, equip.id, AdminPermission.EQUIP_WRITE_OPERATION):
                    self.log.error(
                        u'User does not have permission to perform the operation.')
                    return self.not_authorized()

            with distributedlock(LOCK_VLAN % id_vlan):
                if vlan.ativada == 0:
                    # Make command - VLAN'
                    vlan_command = VLAN_CREATE % int(id_vlan)

                    # Execute command
                    code, stdout, stderr = exec_script(vlan_command)

                    # code = 0 means OK
                    if code == 0:
                        vlan.activate(user)
                    else:
                        return self.response_error(2, stdout + stderr)

                # if 'ids' is a list
                if isinstance(ids, list):
                    for id in ids:
                        code, stdout, stderr = self.activate_network(user, id)
                else:
                    code, stdout, stderr = self.activate_network(user, ids)

                if code != 0:
                    return self.response_error(2, stdout + stderr)

                return self.response(dumps_networkapi({'network': network_map}))

        except InvalidValueError, e:
            return self.response_error(269, e.param, e.value)
开发者ID:marcelometal,项目名称:GloboNetworkAPI,代码行数:79,代码来源:NetworkEditResource.py


注:本文中的networkapi.vlan.models.Vlan.get_by_pk方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。