Example #1
0
def _create_prod_vlans(rack, user):
    log.debug("_create_prod_vlans")

    try:
        env = models_env.Ambiente.objects.filter(
            dcroom=int(rack.dcroom.id),
            divisao_dc__nome="BE",
            grupo_l3__nome=str(rack.nome),
            ambiente_logico__nome="PRODUCAO").uniqueResult()
        log.debug("BE environments: %s" % env)
    except Exception as e:
        raise Exception("Erro: %s" % e)

    if rack.dcroom.config:
        fabricconfig = rack.dcroom.config
    else:
        log.debug("No frabric configurations %s" % str(rack.dcroom.id))
        fabricconfig = list()

    try:
        fabricconfig = json.loads(fabricconfig)
    except:
        pass

    try:
        fabricconfig = ast.literal_eval(fabricconfig)
        log.debug("config -ast: %s" % str(fabricconfig))
    except:
        pass

    environment = None
    father_id = env.id
    fabenv = None

    for fab in fabricconfig.get("Ambiente"):
        if int(fab.get("id")) == int(env.father_environment.id):
            fabenv = fab.get("details")
    if not fabenv:
        log.debug("No configurations for child environment of env id=%s" %
                  (str(env.id)))
        return 0

    fabenv.sort(key=operator.itemgetter('min_num_vlan_1'))
    log.debug("Order by min_num_vlan: %s" % str(fabenv))

    for idx, amb in enumerate(fabenv):
        try:
            id_div = models_env.DivisaoDc().get_by_name(amb.get("name")).id
        except:
            div_dict = models_env.DivisaoDc()
            div_dict.nome = amb.get("name")
            div_dict.save()
            id_div = div_dict.id
            pass

        config_subnet = []
        for net in env.configs:
            for net_dict in amb.get("config"):

                if net_dict.get("type") == net.ip_version:
                    cidr = IPNetwork(net.network)

                    initial_prefix = 20 if net.ip_version == "v4" else 56
                    prefixo = net_dict.get("mask")
                    if not idx:
                        bloco = list(cidr.subnet(int(prefixo)))[0]
                        log.debug(str(bloco))
                    else:
                        bloco1 = list(cidr.subnet(initial_prefix))[1]
                        bloco = list(bloco1.subnet(int(prefixo)))[idx - 1]
                        log.debug(str(bloco))
                    network = {
                        'network': str(bloco),
                        'ip_version': str(net.ip_version),
                        'network_type': int(net.id_network_type.id),
                        'subnet_mask': int(net_dict.get("new_prefix"))
                    }
                    config_subnet.append(network)

        obj = {
            'grupo_l3': env.grupo_l3.id,
            'ambiente_logico': env.ambiente_logico.id,
            'divisao_dc': id_div,
            'acl_path': env.acl_path,
            'ipv4_template': env.ipv4_template,
            'ipv6_template': env.ipv6_template,
            'link': env.link,
            'min_num_vlan_2': amb.get("min_num_vlan_1"),
            'max_num_vlan_2': amb.get("max_num_vlan_1"),
            'min_num_vlan_1': amb.get("min_num_vlan_1"),
            'max_num_vlan_1': amb.get("max_num_vlan_1"),
            'vrf': env.vrf,
            'father_environment': father_id,
            'default_vrf': env.default_vrf.id,
            'configs': config_subnet,
            'fabric_id': rack.dcroom.id
        }
        environment = facade_env.create_environment(obj)
        log.debug("Environment object: %s" % str(environment))

        for switch in [rack.id_sw1, rack.id_sw2]:
            try:
                equipamento_ambiente = EquipamentoAmbiente()
                equipamento_ambiente.ambiente = environment
                equipamento_ambiente.equipamento = switch
                equipamento_ambiente.is_router = True
                equipamento_ambiente.create(user)
            except EquipamentoAmbienteDuplicatedError:
                pass

    return environment
Example #2
0
def _create_prod_vlans(rack, user):
    log.debug("_create_prod_vlans")

    env = models_env.Ambiente.objects.filter(
        dcroom=int(rack.dcroom.id),
        divisao_dc__nome="BE",
        grupo_l3__nome=str(rack.nome),
        ambiente_logico__nome="PRODUCAO").uniqueResult()

    log.debug(str(env))

    if rack.dcroom.config:
        fabricconfig = rack.dcroom.config
        log.debug(str(type(fabricconfig)))
    else:
        log.debug("sem configuracoes do fabric %s" % str(rack.dcroom.id))
        fabricconfig = list()

    try:
        fabricconfig = json.loads(fabricconfig)
        log.debug("type -ast: %s" % str(type(fabricconfig)))
    except:
        pass

    try:
        fabricconfig = ast.literal_eval(fabricconfig)
        log.debug("config -ast: %s" % str(fabricconfig))
    except:
        pass

    environment = None
    father_id = env.id
    fabenv = None

    log.debug("debug")
    for fab in fabricconfig.get("Ambiente"):
        log.debug("debug for fab")
        if int(fab.get("id")) == int(env.father_environment.id):
            fabenv = fab.get("details")
    if not fabenv:
        log.debug(
            "Sem configuracoes para os ambientes filhos - BE, BEFE, BEBO, BECA do ambiente id=%s"
            % str())
        return 0

    log.debug(str(fabenv))
    for amb in fabenv:
        try:
            id_div = models_env.DivisaoDc().get_by_name(amb.get("name")).id
        except:
            div_dict = models_env.DivisaoDc()
            div_dict.nome = amb.get("name")
            div_dict.save()
            id_div = div_dict.id
            pass

        config_subnet = list()
        for net in env.configs:
            for net_dict in amb.get("config"):
                if net_dict.get("type") == net.ip_config.type:
                    cidr = IPNetwork(net.ip_config.subnet)
                    prefixo = net_dict.get("mask")
                    bloco = list(cidr.subnet(int(prefixo)))[-2]
                    network = {
                        'subnet': str(bloco),
                        'type': str(net.ip_config.type),
                        'network_type': int(net.ip_config.network_type.id),
                        'new_prefix': int(net_dict.get("new_prefix"))
                    }
                    config_subnet.append(network)

        obj = {
            'grupo_l3': env.grupo_l3.id,
            'ambiente_logico': env.ambiente_logico.id,
            'divisao_dc': id_div,
            'acl_path': env.acl_path,
            'ipv4_template': env.ipv4_template,
            'ipv6_template': env.ipv6_template,
            'link': env.link,
            'min_num_vlan_2': amb.get("min_num_vlan_1"),
            'max_num_vlan_2': amb.get("max_num_vlan_1"),
            'min_num_vlan_1': amb.get("min_num_vlan_1"),
            'max_num_vlan_1': amb.get("max_num_vlan_1"),
            'vrf': env.vrf,
            'father_environment': father_id,
            'default_vrf': env.default_vrf.id,
            'configs': config_subnet,
            'fabric_id': rack.dcroom.id
        }
        environment = facade_env.create_environment(obj)
        log.debug("Environment object: %s" % str(environment))

    return environment
    def children_prod_environment_save(self):
        log.debug("_create_prod_children")

        try:
            env = models_env.Ambiente.objects.filter(
                dcroom=int(self.rack.dcroom.id),
                divisao_dc__nome="BE",
                grupo_l3__nome=str(self.rack.nome),
                ambiente_logico__nome="PRODUCAO").uniqueResult()
            log.debug("BE environments: %s" % env)
        except Exception as e:
            raise Exception("Erro: %s" % e)

        if self.rack.dcroom.config:
            fabricconfig = self.rack.dcroom.config
        else:
            log.debug("No fabric configurations %s" % str(self.rack.dcroom.id))
            fabricconfig = list()

        try:
            fabricconfig = json.loads(fabricconfig)
            fabricconfig = ast.literal_eval(fabricconfig)
            log.debug("config -ast: %s" % str(fabricconfig))
        except:
            log.debug("Error loading fabric json.")

        environment = None
        father_id = env.id
        fabenv = None

        for fab in fabricconfig.get("Ambiente"):
            if int(fab.get("id")) == int(env.father_environment.id):
                fabenv = fab.get("details")

        if not fabenv:
            log.debug("No configurations for child environment of env id=%s" %
                      (str(env.id)))
            return False

        fabenv.sort(key=operator.itemgetter('min_num_vlan_1'))
        log.debug("Order by min_num_vlan: %s" % str(fabenv))

        for amb in fabenv:
            log.debug("amb: %s" % amb)
            try:
                id_div = models_env.DivisaoDc().get_by_name(amb.get("name")).id
            except:
                div_dict = models_env.DivisaoDc()
                div_dict.nome = amb.get("name")
                div_dict.save()
                id_div = div_dict.id
                pass

            config_subnet = list()
            for net_dict in amb.get("config"):
                network = {
                    'ip_version': net_dict.get("type"),
                    'network_type': int(net_dict.get("network_type")),
                    'subnet_mask': int(net_dict.get("new_prefix")),
                    'network_mask': int(net_dict.get("mask"))
                }
                config_subnet.append(network)
            log.debug("config: %s" % config_subnet)
            obj = {
                'grupo_l3': env.grupo_l3.id,
                'ambiente_logico': env.ambiente_logico.id,
                'divisao_dc': id_div,
                'acl_path': env.acl_path,
                'ipv4_template': env.ipv4_template,
                'ipv6_template': env.ipv6_template,
                'link': env.link,
                'min_num_vlan_2': amb.get("min_num_vlan_1"),
                'max_num_vlan_2': amb.get("max_num_vlan_1"),
                'min_num_vlan_1': amb.get("min_num_vlan_1"),
                'max_num_vlan_1': amb.get("max_num_vlan_1"),
                'vrf': env.vrf,
                'father_environment': father_id,
                'default_vrf': env.default_vrf.id,
                'configs': config_subnet,
                'fabric_id': self.rack.dcroom.id
            }
            environment = facade_env.create_environment(obj)
            log.debug("Environment object: %s" % str(environment))

            for switch in [self.rack.id_sw1, self.rack.id_sw2]:
                try:
                    equipamento_ambiente = EquipamentoAmbiente()
                    equipamento_ambiente.ambiente = environment
                    equipamento_ambiente.equipamento = switch
                    equipamento_ambiente.is_router = True
                    equipamento_ambiente.create(self.user)
                except EquipamentoAmbienteDuplicatedError:
                    pass
                except Exception as e:
                    log.debug("error %s" % e)

        return environment