def criar_ambiente(user, ambientes, ranges, acl_path=None, filter=None): #ambiente cloud environment = Ambiente() environment.grupo_l3 = GrupoL3() environment.ambiente_logico = AmbienteLogico() environment.divisao_dc = DivisaoDc() environment.grupo_l3.id = environment.grupo_l3.get_by_name(ambientes.get('L3')).id environment.ambiente_logico.id = environment.ambiente_logico.get_by_name(ambientes.get('LOG')).id environment.divisao_dc.id = environment.divisao_dc.get_by_name(ambientes.get('DC')).id environment.acl_path = acl_path environment.ipv4_template = None environment.ipv6_template = None environment.max_num_vlan_1 = ranges.get('MAX') environment.min_num_vlan_1 = ranges.get('MIN') environment.max_num_vlan_2 = ranges.get('MAX') environment.min_num_vlan_2 = ranges.get('MIN') environment.link = " " if filter is not None: try: filter_obj = Filter.objects.get(name__iexact=filter) environment.filter = filter_obj except ObjectDoesNotExist, e: pass
def criar_ambiente(user, ambientes, ranges, acl_path=None, filter=None, vrf=None): # ambiente cloud environment = Ambiente() environment.grupo_l3 = GrupoL3() environment.ambiente_logico = AmbienteLogico() environment.divisao_dc = DivisaoDc() environment.grupo_l3.id = environment.grupo_l3.get_by_name( ambientes.get('L3')).id environment.ambiente_logico.id = environment.ambiente_logico.get_by_name( ambientes.get('LOG')).id environment.divisao_dc.id = environment.divisao_dc.get_by_name( ambientes.get('DC')).id environment.acl_path = acl_path environment.ipv4_template = None environment.ipv6_template = None if vrf is not None: environment.vrf = vrf environment.max_num_vlan_1 = ranges.get('MAX') environment.min_num_vlan_1 = ranges.get('MIN') environment.max_num_vlan_2 = ranges.get('MAX') environment.min_num_vlan_2 = ranges.get('MIN') environment.link = ' ' if filter is not None: try: filter_obj = Filter.objects.get(name__iexact=filter) environment.filter = filter_obj except ObjectDoesNotExist: pass environment.create(user) return environment
def handle_post(self, request, user, *args, **kwargs): """Trata requisições POST para inserir novo Ambiente. URL: ambiente/ or ambiente/ipconfig/ """ try: if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION): return self.not_authorized() xml_map, attrs_map = loads(request.raw_post_data) self.log.debug('XML_MAP: %s', xml_map) networkapi_map = xml_map.get('networkapi') if networkapi_map is None: return self.response_error( 3, u'Não existe valor para a tag networkapi do XML de requisição.' ) environment_map = networkapi_map.get('ambiente') if environment_map is None: return self.response_error( 3, u'Não existe valor para a tag ambiente do XML de requisição.' ) link = environment_map.get('link') if not is_valid_string_maxsize(link, 200, False): self.log.error(u'Parameter link is invalid. Value: %s', link) raise InvalidValueError(None, 'link', link) l3_group_id = environment_map.get('id_grupo_l3') if not is_valid_int_greater_zero_param(l3_group_id): self.log.error( u'The l3_group_id parameter is not a valid value: %s.', l3_group_id) raise InvalidValueError(None, 'l3_group_id', l3_group_id) else: l3_group_id = int(l3_group_id) logic_environment_id = environment_map.get('id_ambiente_logico') if not is_valid_int_greater_zero_param(logic_environment_id): self.log.error( u'The logic_environment_id parameter is not a valid value: %s.', logic_environment_id) raise InvalidValueError(None, 'logic_environment_id', logic_environment_id) else: logic_environment_id = int(logic_environment_id) dc_division_id = environment_map.get('id_divisao') if not is_valid_int_greater_zero_param(dc_division_id): self.log.error( u'The dc_division_id parameter is not a valid value: %s.', dc_division_id) raise InvalidValueError(None, 'dc_division_id', dc_division_id) else: dc_division_id = int(dc_division_id) filter_id = environment_map.get('id_filter') if filter_id is not None: if not is_valid_int_greater_zero_param(filter_id): self.log.error( u'Parameter filter_id is invalid. Value: %s.', filter_id) raise InvalidValueError(None, 'filter_id', filter_id) acl_path = environment_map.get('acl_path') if not is_valid_string_maxsize(acl_path, 250, False): self.log.error(u'Parameter acl_path is invalid. Value: %s', acl_path) raise InvalidValueError(None, 'acl_path', acl_path) ipv4_template = environment_map.get('ipv4_template') if not is_valid_string_maxsize(ipv4_template, 250, False): self.log.error( u'Parameter ipv4_template is invalid. Value: %s', ipv4_template) raise InvalidValueError(None, 'ipv4_template', ipv4_template) ipv6_template = environment_map.get('ipv6_template') if not is_valid_string_maxsize(ipv6_template, 250, False): self.log.error( u'Parameter ipv6_template is invalid. Value: %s', ipv6_template) raise InvalidValueError(None, 'ipv6_template', ipv6_template) max_num_vlan_1 = environment_map.get('max_num_vlan_1') min_num_vlan_1 = environment_map.get('min_num_vlan_1') max_num_vlan_2 = environment_map.get('max_num_vlan_2') min_num_vlan_2 = environment_map.get('min_num_vlan_2') # validate max_num_vlan_1 and min_num_vlan_1 if (max_num_vlan_1 is not None and min_num_vlan_1 is None) or ( min_num_vlan_1 is not None and max_num_vlan_1 is None): self.log.error( u'Parameters min_num_vlan_1, max_num_vlan_1 is invalid. Values: %s, %s', (min_num_vlan_1, max_num_vlan_1)) raise InvalidValueError(None, 'min_num_vlan_1, max_num_vlan_1', min_num_vlan_1 + ',' + max_num_vlan_1) if max_num_vlan_1 is not None and min_num_vlan_1 is not None: max_num_vlan_1 = int(max_num_vlan_1) min_num_vlan_1 = int(min_num_vlan_1) if max_num_vlan_1 < 1 or min_num_vlan_1 < 1: self.log.error( u'Parameters min_num_vlan_1, max_num_vlan_1 is invalid. Values: %s, %s', (min_num_vlan_1, max_num_vlan_1)) raise InvalidValueError( None, 'min_num_vlan_1, max_num_vlan_1', min_num_vlan_1 + ',' + max_num_vlan_1) if max_num_vlan_1 <= min_num_vlan_1: self.log.error( u'Parameters min_num_vlan_1, max_num_vlan_1 is invalid. Values: %s, %s', (min_num_vlan_1, max_num_vlan_1)) raise InvalidValueError( None, 'min_num_vlan_1, max_num_vlan_1', min_num_vlan_1 + ',' + max_num_vlan_1) else: max_num_vlan_1 = max_num_vlan_2 min_num_vlan_1 = min_num_vlan_2 # validate max_num_vlan_1 and min_num_vlan_1 # validate max_num_vlan_2 and min_num_vlan_2 if (max_num_vlan_2 is not None and min_num_vlan_2 is None) or ( min_num_vlan_2 is not None and max_num_vlan_2 is None): self.log.error( u'Parameters min_num_vlan_2, max_num_vlan_2 is invalid. Values: %s, %s', (min_num_vlan_2, max_num_vlan_2)) raise InvalidValueError(None, 'min_num_vlan_2, max_num_vlan_2', min_num_vlan_2 + ',' + max_num_vlan_1) if max_num_vlan_2 is not None and min_num_vlan_2 is not None: max_num_vlan_2 = int(max_num_vlan_2) min_num_vlan_2 = int(min_num_vlan_2) max_num_vlan_1 = int(max_num_vlan_1) min_num_vlan_1 = int(min_num_vlan_1) if max_num_vlan_2 < 1 or min_num_vlan_2 < 1: self.log.error( u'Parameters min_num_vlan_2, max_num_vlan_2 is invalid. Values: %s, %s', (min_num_vlan_2, max_num_vlan_2)) raise InvalidValueError( None, 'min_num_vlan_2, max_num_vlan_2', min_num_vlan_2 + ',' + max_num_vlan_1) if max_num_vlan_2 <= min_num_vlan_2: self.log.error( u'Parameters min_num_vlan_2, max_num_vlan_2 is invalid. Values: %s, %s', (min_num_vlan_2, max_num_vlan_2)) raise InvalidValueError( None, 'min_num_vlan_2, max_num_vlan_2', min_num_vlan_2 + ',' + max_num_vlan_1) else: max_num_vlan_2 = max_num_vlan_1 min_num_vlan_2 = min_num_vlan_1 # validate max_num_vlan_2 and min_num_vlan_2 vrf = environment_map.get('vrf') if not is_valid_string_maxsize(vrf, 100, False): self.log.error(u'Parameter vrf is invalid. Value: %s', vrf) raise InvalidValueError(None, 'link', vrf) environment = Ambiente() environment.grupo_l3 = GrupoL3() environment.ambiente_logico = AmbienteLogico() environment.divisao_dc = DivisaoDc() environment.grupo_l3.id = l3_group_id environment.ambiente_logico.id = logic_environment_id environment.divisao_dc.id = dc_division_id environment.acl_path = fix_acl_path(acl_path) environment.ipv4_template = ipv4_template environment.ipv6_template = ipv6_template environment.max_num_vlan_1 = max_num_vlan_1 environment.min_num_vlan_1 = min_num_vlan_1 environment.max_num_vlan_2 = max_num_vlan_2 environment.min_num_vlan_2 = min_num_vlan_2 environment.vrf = vrf if filter_id is not None: environment.filter = Filter() environment.filter.id = filter_id environment.link = link environment.create(user) # IP Config ip_config = kwargs.get('ip_config') # If ip config is set if ip_config: # Add this to environment id_ip_config = environment_map.get('id_ip_config') # Valid ip config if not is_valid_int_greater_zero_param(id_ip_config): raise InvalidValueError(None, 'id_ip_config', id_ip_config) # Ip config must exists ip_conf = IPConfig().get_by_pk(id_ip_config) # Makes the relationship config = ConfigEnvironment() config.environment = environment config.ip_config = ip_conf config.save() environment_map = dict() environment_map['id'] = environment.id return self.response( dumps_networkapi({'ambiente': environment_map})) except GrupoError: return self.response_error(1) except XMLError, x: self.log.error(u'Erro ao ler o XML da requisicao.') return self.response_error(3, x)
def handle_post(self, request, user, *args, **kwargs): """Trata requisições POST para inserir novo Ambiente. URL: ambiente/ or ambiente/ipconfig/ """ try: if not has_perm(user, AdminPermission.ENVIRONMENT_MANAGEMENT, AdminPermission.WRITE_OPERATION): return self.not_authorized() xml_map, attrs_map = loads(request.raw_post_data) self.log.debug('XML_MAP: %s', xml_map) networkapi_map = xml_map.get('networkapi') if networkapi_map is None: return self.response_error(3, u'Não existe valor para a tag networkapi do XML de requisição.') environment_map = networkapi_map.get('ambiente') if environment_map is None: return self.response_error(3, u'Não existe valor para a tag ambiente do XML de requisição.') link = environment_map.get('link') if not is_valid_string_maxsize(link, 200, False): self.log.error(u'Parameter link is invalid. Value: %s', link) raise InvalidValueError(None, 'link', link) l3_group_id = environment_map.get('id_grupo_l3') if not is_valid_int_greater_zero_param(l3_group_id): self.log.error( u'The l3_group_id parameter is not a valid value: %s.', l3_group_id) raise InvalidValueError(None, 'l3_group_id', l3_group_id) else: l3_group_id = int(l3_group_id) logic_environment_id = environment_map.get('id_ambiente_logico') if not is_valid_int_greater_zero_param(logic_environment_id): self.log.error( u'The logic_environment_id parameter is not a valid value: %s.', logic_environment_id) raise InvalidValueError( None, 'logic_environment_id', logic_environment_id) else: logic_environment_id = int(logic_environment_id) dc_division_id = environment_map.get('id_divisao') if not is_valid_int_greater_zero_param(dc_division_id): self.log.error( u'The dc_division_id parameter is not a valid value: %s.', dc_division_id) raise InvalidValueError(None, 'dc_division_id', dc_division_id) else: dc_division_id = int(dc_division_id) filter_id = environment_map.get('id_filter') if filter_id is not None: if not is_valid_int_greater_zero_param(filter_id): self.log.error( u'Parameter filter_id is invalid. Value: %s.', filter_id) raise InvalidValueError(None, 'filter_id', filter_id) acl_path = environment_map.get('acl_path') if not is_valid_string_maxsize(acl_path, 250, False): self.log.error( u'Parameter acl_path is invalid. Value: %s', acl_path) raise InvalidValueError(None, 'acl_path', acl_path) ipv4_template = environment_map.get('ipv4_template') if not is_valid_string_maxsize(ipv4_template, 250, False): self.log.error( u'Parameter ipv4_template is invalid. Value: %s', ipv4_template) raise InvalidValueError(None, 'ipv4_template', ipv4_template) ipv6_template = environment_map.get('ipv6_template') if not is_valid_string_maxsize(ipv6_template, 250, False): self.log.error( u'Parameter ipv6_template is invalid. Value: %s', ipv6_template) raise InvalidValueError(None, 'ipv6_template', ipv6_template) max_num_vlan_1 = environment_map.get('max_num_vlan_1') min_num_vlan_1 = environment_map.get('min_num_vlan_1') max_num_vlan_2 = environment_map.get('max_num_vlan_2') min_num_vlan_2 = environment_map.get('min_num_vlan_2') # validate max_num_vlan_1 and min_num_vlan_1 if (max_num_vlan_1 is not None and min_num_vlan_1 is None) or (min_num_vlan_1 is not None and max_num_vlan_1 is None): self.log.error( u'Parameters min_num_vlan_1, max_num_vlan_1 is invalid. Values: %s, %s', (min_num_vlan_1, max_num_vlan_1)) raise InvalidValueError( None, 'min_num_vlan_1, max_num_vlan_1', min_num_vlan_1 + ',' + max_num_vlan_1) if max_num_vlan_1 is not None and min_num_vlan_1 is not None: max_num_vlan_1 = int(max_num_vlan_1) min_num_vlan_1 = int(min_num_vlan_1) if max_num_vlan_1 < 1 or min_num_vlan_1 < 1: self.log.error( u'Parameters min_num_vlan_1, max_num_vlan_1 is invalid. Values: %s, %s', (min_num_vlan_1, max_num_vlan_1)) raise InvalidValueError( None, 'min_num_vlan_1, max_num_vlan_1', min_num_vlan_1 + ',' + max_num_vlan_1) if max_num_vlan_1 <= min_num_vlan_1: self.log.error( u'Parameters min_num_vlan_1, max_num_vlan_1 is invalid. Values: %s, %s', (min_num_vlan_1, max_num_vlan_1)) raise InvalidValueError( None, 'min_num_vlan_1, max_num_vlan_1', min_num_vlan_1 + ',' + max_num_vlan_1) else: max_num_vlan_1 = max_num_vlan_2 min_num_vlan_1 = min_num_vlan_2 # validate max_num_vlan_1 and min_num_vlan_1 # validate max_num_vlan_2 and min_num_vlan_2 if (max_num_vlan_2 is not None and min_num_vlan_2 is None) or (min_num_vlan_2 is not None and max_num_vlan_2 is None): self.log.error( u'Parameters min_num_vlan_2, max_num_vlan_2 is invalid. Values: %s, %s', (min_num_vlan_2, max_num_vlan_2)) raise InvalidValueError( None, 'min_num_vlan_2, max_num_vlan_2', min_num_vlan_2 + ',' + max_num_vlan_1) if max_num_vlan_2 is not None and min_num_vlan_2 is not None: max_num_vlan_2 = int(max_num_vlan_2) min_num_vlan_2 = int(min_num_vlan_2) max_num_vlan_1 = int(max_num_vlan_1) min_num_vlan_1 = int(min_num_vlan_1) if max_num_vlan_2 < 1 or min_num_vlan_2 < 1: self.log.error( u'Parameters min_num_vlan_2, max_num_vlan_2 is invalid. Values: %s, %s', (min_num_vlan_2, max_num_vlan_2)) raise InvalidValueError( None, 'min_num_vlan_2, max_num_vlan_2', min_num_vlan_2 + ',' + max_num_vlan_1) if max_num_vlan_2 <= min_num_vlan_2: self.log.error( u'Parameters min_num_vlan_2, max_num_vlan_2 is invalid. Values: %s, %s', (min_num_vlan_2, max_num_vlan_2)) raise InvalidValueError( None, 'min_num_vlan_2, max_num_vlan_2', min_num_vlan_2 + ',' + max_num_vlan_1) else: max_num_vlan_2 = max_num_vlan_1 min_num_vlan_2 = min_num_vlan_1 # validate max_num_vlan_2 and min_num_vlan_2 vrf = environment_map.get('vrf') if not is_valid_string_maxsize(vrf, 100, False): self.log.error(u'Parameter vrf is invalid. Value: %s', vrf) raise InvalidValueError(None, 'link', vrf) environment = Ambiente() environment.grupo_l3 = GrupoL3() environment.ambiente_logico = AmbienteLogico() environment.divisao_dc = DivisaoDc() environment.grupo_l3.id = l3_group_id environment.ambiente_logico.id = logic_environment_id environment.divisao_dc.id = dc_division_id environment.acl_path = fix_acl_path(acl_path) environment.ipv4_template = ipv4_template environment.ipv6_template = ipv6_template environment.max_num_vlan_1 = max_num_vlan_1 environment.min_num_vlan_1 = min_num_vlan_1 environment.max_num_vlan_2 = max_num_vlan_2 environment.min_num_vlan_2 = min_num_vlan_2 environment.vrf = vrf if filter_id is not None: environment.filter = Filter() environment.filter.id = filter_id environment.link = link environment.create(user) # IP Config ip_config = kwargs.get('ip_config') # If ip config is set if ip_config: # Add this to environment id_ip_config = environment_map.get('id_ip_config') # Valid ip config if not is_valid_int_greater_zero_param(id_ip_config): raise InvalidValueError(None, 'id_ip_config', id_ip_config) # Ip config must exists ip_conf = IPConfig().get_by_pk(id_ip_config) # Makes the relationship config = ConfigEnvironment() config.environment = environment config.ip_config = ip_conf config.save() environment_map = dict() environment_map['id'] = environment.id return self.response(dumps_networkapi({'ambiente': environment_map})) except GrupoError: return self.response_error(1) except XMLError, x: self.log.error(u'Erro ao ler o XML da requisicao.') return self.response_error(3, x)