Exemple #1
0
    def post(self, request, *args, **kwargs):
        """Create new environment."""

        envs = request.DATA
        json_validate(SPECS.get('environment_post')).validate(envs)
        response = list()
        for env in envs['environments']:

            env_obj = facade.create_environment(env)
            response.append({'id': env_obj.id})

        return Response(response, status=status.HTTP_201_CREATED)
Exemple #2
0
    def post(self, request, *args, **kwargs):
        """Create new environment."""

        envs = request.DATA
        json_validate(SPECS.get('environment_post')).validate(envs)
        response = list()
        for env in envs['environments']:

            env_obj = facade.create_environment(env)
            response.append({'id': env_obj.id})

        return Response(response, status=status.HTTP_201_CREATED)
    def leaf_leaf_envs_save(self):
        log.debug("_create_lflf_envs")

        env_lf = models_env.Ambiente.objects.filter(
            dcroom=int(self.rack.dcroom.id),
            grupo_l3__nome=str(self.rack.dcroom.name),
            ambiente_logico__nome="LEAF-LEAF")
        log.debug("Leaf-leaf environments: " + str(env_lf))

        try:
            id_l3 = models_env.GrupoL3().get_by_name(self.rack.nome).id
        except:
            l3_dict = models_env.GrupoL3()
            l3_dict.nome = self.rack.nome
            l3_dict.save()
            id_l3 = l3_dict.id
            pass

        for env in env_lf:
            config_subnet = []
            for net in env.configs:
                cidr = list(
                    IPNetwork(net.network).subnet(int(
                        net.subnet_mask)))[self.rack.numero]
                network = {
                    'network': str(cidr),
                    'ip_version': str(net.ip_version),
                    'network_type': int(net.id_network_type.id),
                    'subnet_mask': int(net.subnet_mask)
                }
                config_subnet.append(network)

            obj = {
                'grupo_l3': id_l3,
                'ambiente_logico': env.ambiente_logico.id,
                'divisao_dc': env.divisao_dc.id,
                'acl_path': env.acl_path,
                'ipv4_template': env.ipv4_template,
                'ipv6_template': env.ipv6_template,
                'link': env.link,
                'min_num_vlan_2': env.min_num_vlan_1,
                'max_num_vlan_2': env.max_num_vlan_1,
                'min_num_vlan_1': env.min_num_vlan_1,
                'max_num_vlan_1': env.max_num_vlan_1,
                'vrf': env.vrf,
                'father_environment': env.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))
Exemple #4
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
Exemple #5
0
def _create_prod_envs(rack, user):
    log.debug("_create_prod_envs")

    prod_envs = models_env.Ambiente.objects.filter(
        dcroom=int(rack.dcroom.id),
        father_environment__isnull=True,
        grupo_l3__nome=str(rack.dcroom.name),
        ambiente_logico__nome="PRODUCAO").exclude(divisao_dc__nome="BO_DMZ")

    log.debug(str(prod_envs))

    try:
        id_grupo_l3 = models_env.GrupoL3().get_by_name(rack.nome).id
    except:
        grupo_l3_dict = models_env.GrupoL3()
        grupo_l3_dict.nome = rack.nome
        grupo_l3_dict.save()
        id_grupo_l3 = grupo_l3_dict.id
        pass

    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 = list()
    for env in prod_envs:

        father_id = env.id

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

        config_subnet = list()
        for net in env.configs:
            log.debug(str(net.ip_config.subnet))
            log.debug(str(type(net.ip_config.subnet)))
            cidr = IPNetwork(str(net.ip_config.subnet))
            prefix = int(net.ip_config.new_prefix)
            bloco = list(cidr.subnet(int(prefix)))[int(rack.numero)]
            log.debug(str(details))
            if details[0].get(str(net.ip_config.type)):
                log.debug("if")
                new_prefix = details[0].get(str(
                    net.ip_config.type)).get("new_prefix")
            else:
                new_prefix = 31 if net.ip_config.type == "v4" else 127
            network = {
                'subnet': str(bloco),
                'type': net.ip_config.type,
                'network_type': net.ip_config.network_type.id,
                'new_prefix': new_prefix
            }
            config_subnet.append(network)

        obj = {
            'grupo_l3': id_grupo_l3,
            'ambiente_logico': env.ambiente_logico.id,
            'divisao_dc': env.divisao_dc.id,
            'acl_path': env.acl_path,
            'ipv4_template': env.ipv4_template,
            'ipv6_template': env.ipv6_template,
            'link': env.link,
            'min_num_vlan_2': env.min_num_vlan_1,
            'max_num_vlan_2': env.max_num_vlan_1,
            'min_num_vlan_1': env.min_num_vlan_1,
            'max_num_vlan_1': env.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.append(facade_env.create_environment(obj))
        log.debug("Environment object: %s" % str(environment))

    return environment
Exemple #6
0
def _create_spnlfenv(rack):
    log.debug("_create_spnlfenv")

    envfathers = models_env.Ambiente.objects.filter(
        dcroom=int(rack.dcroom.id),
        father_environment__isnull=True,
        grupo_l3__nome=str(rack.dcroom.name),
        ambiente_logico__nome="SPINES")
    log.debug(str(envfathers))

    environment_spn_lf_list = list()
    spines = int(rack.dcroom.spines)
    fabric = rack.dcroom.name

    try:
        id_grupo_l3 = models_env.GrupoL3().get_by_name(fabric).id
    except:
        grupo_l3_dict = models_env.GrupoL3()
        grupo_l3_dict.nome = fabric
        grupo_l3_dict.save()
        id_grupo_l3 = grupo_l3_dict.id
        pass

    for env in envfathers:
        config_subnet = list()
        for net in env.configs:
            # verificar se o ambiente possui range associado.
            cidr = IPNetwork(net.ip_config.subnet)
            prefix = int(net.ip_config.new_prefix)
            network = {
                'cidr': list(cidr.subnet(prefix)),
                'type': net.ip_config.type,
                'network_type': net.ip_config.network_type.id
            }
            config_subnet.append(network)
        for spn in range(spines):
            amb_log_name = "SPINE0" + str(spn + 1) + "LEAF"
            try:
                id_amb_log = models_env.AmbienteLogico().get_by_name(
                    amb_log_name).id
            except:
                amb_log_dict = models_env.AmbienteLogico()
                amb_log_dict.nome = amb_log_name
                amb_log_dict.save()
                id_amb_log = amb_log_dict.id
                pass
            config = list()
            for sub in config_subnet:
                config_spn = {
                    'subnet':
                    str(sub.get("cidr")[spn]),
                    'new_prefix':
                    str(31) if str(sub.get("type"))[-1] is "4" else str(127),
                    'type':
                    str(sub.get("type")),
                    'network_type':
                    sub.get("network_type")
                }
                config.append(config_spn)
            obj = {
                'grupo_l3': id_grupo_l3,
                'ambiente_logico': id_amb_log,
                'divisao_dc': env.divisao_dc.id,
                'acl_path': env.acl_path,
                'ipv4_template': env.ipv4_template,
                'ipv6_template': env.ipv6_template,
                'link': env.link,
                'min_num_vlan_2': env.min_num_vlan_2,
                'max_num_vlan_2': env.max_num_vlan_2,
                'min_num_vlan_1': env.min_num_vlan_1,
                'max_num_vlan_1': env.max_num_vlan_1,
                'vrf': env.vrf,
                'father_environment': env.id,
                'default_vrf': env.default_vrf.id,
                'configs': config,
                'fabric_id': rack.dcroom.id
            }
            try:
                environment_spn_lf = facade_env.create_environment(obj)
                environment_spn_lf_list.append(environment_spn_lf)
            except:
                log.debug("Environment object: %s" % str(obj))
    return environment_spn_lf_list
Exemple #7
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
Exemple #8
0
def _create_prod_envs(rack, user):
    log.debug("_create_prod_envs")

    prod_envs = models_env.Ambiente.objects.filter(
        dcroom=int(rack.dcroom.id),
        grupo_l3__nome=str(rack.dcroom.name),
        ambiente_logico__nome="PRODUCAO").exclude(divisao_dc__nome="BO_DMZ")

    log.debug("PROD environments: " + str(prod_envs))

    try:
        id_grupo_l3 = models_env.GrupoL3().get_by_name(rack.nome).id
    except:
        grupo_l3_dict = models_env.GrupoL3()
        grupo_l3_dict.nome = rack.nome
        grupo_l3_dict.save()
        id_grupo_l3 = grupo_l3_dict.id
        pass

    if rack.dcroom.config:
        fabricconfig = rack.dcroom.config
    else:
        log.debug("sem configuracoes do fabric %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 = []
    for env in prod_envs:

        father_id = env.id

        details = None
        for fab in fabricconfig.get("Ambiente"):
            if int(fab.get("id")) == int(father_id):
                details = fab.get("details")

        config_subnet = []
        for net in env.configs:
            cidr = IPNetwork(str(net.network))
            prefix = int(net.subnet_mask)
            subnet_list = list(cidr.subnet(int(prefix)))
            try:
                bloco = subnet_list[int(rack.numero)]
            except IndexError:
                msg = "Rack number %d is greater than the maximum number of " \
                      "subnets available with prefix %d from %s subnet" % \
                      (rack.numero, prefix, cidr)
                raise Exception(msg)

            if isinstance(details, list) and len(details) > 0:
                if details[0].get(str(net.ip_version)):
                    new_prefix = details[0].get(str(
                        net.ip_version)).get("new_prefix")
                else:
                    new_prefix = 31 if net.ip_version == "v4" else 127
                network = {
                    'network': str(bloco),
                    'ip_version': net.ip_version,
                    'network_type': net.id_network_type.id,
                    'subnet_mask': new_prefix
                }
                config_subnet.append(network)

        obj = {
            'grupo_l3': id_grupo_l3,
            'ambiente_logico': env.ambiente_logico.id,
            'divisao_dc': env.divisao_dc.id,
            'acl_path': env.acl_path,
            'ipv4_template': env.ipv4_template,
            'ipv6_template': env.ipv6_template,
            'link': env.link,
            'min_num_vlan_2': env.min_num_vlan_1,
            'max_num_vlan_2': env.max_num_vlan_1,
            'min_num_vlan_1': env.min_num_vlan_1,
            'max_num_vlan_1': env.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
        }
        obj_env = facade_env.create_environment(obj)
        environment.append(obj_env)
        log.debug("Environment object: %s" % str(obj_env))

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

    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
    def prod_environment_save(self):
        log.debug("_create_prod_envs")

        prod_envs = models_env.Ambiente.objects.filter(
            dcroom=int(self.rack.dcroom.id),
            grupo_l3__nome=str(self.rack.dcroom.name),
            ambiente_logico__nome="PRODUCAO").exclude(
                divisao_dc__nome="BO_DMZ")

        log.debug("PROD environments: " + str(prod_envs))

        try:
            id_grupo_l3 = models_env.GrupoL3().get_by_name(self.rack.nome).id
        except:
            grupo_l3_dict = models_env.GrupoL3()
            grupo_l3_dict.nome = self.rack.nome
            grupo_l3_dict.save()
            id_grupo_l3 = grupo_l3_dict.id
            pass

        if self.rack.dcroom.config:
            fabricconfig = self.rack.dcroom.config
        else:
            log.debug("sem configuracoes do fabric %s" %
                      str(self.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 = list()
        for env in prod_envs:
            details = None
            for fab in fabricconfig.get("Ambiente"):
                if int(fab.get("id")) == env.id:
                    details = fab.get("details")

            config_subnet = list()
            for net in env.configs:

                if isinstance(details, list) and len(details) > 0:
                    if details[0].get(str(net.ip_version)):
                        new_prefix = details[0].get(str(
                            net.ip_version)).get("new_prefix")
                    else:
                        new_prefix = 27 if net.ip_version == "v4" else 64
                    network = {
                        'ip_version': net.ip_version,
                        'network_type': net.id_network_type.id,
                        'subnet_mask': new_prefix
                    }
                    config_subnet.append(network)

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

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

        return environment
Exemple #11
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("BE environments: "+str(env))

    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_config.type:
                    cidr = IPNetwork(net.ip_config.subnet)

                    initial_prefix  = 20 if net.ip_config.type=="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 = {
                        '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))

        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
Exemple #12
0
def _create_prod_envs(rack, user):
    log.debug("_create_prod_envs")

    prod_envs = models_env.Ambiente.objects.filter(dcroom=int(rack.dcroom.id),
                                                   father_environment__isnull=True,
                                                   grupo_l3__nome=str(rack.dcroom.name),
                                                   ambiente_logico__nome="PRODUCAO"
                                                   ).exclude(divisao_dc__nome="BO_DMZ")

    log.debug("PROD environments: "+str(prod_envs))

    try:
        id_grupo_l3 = models_env.GrupoL3().get_by_name(rack.nome).id
    except:
        grupo_l3_dict = models_env.GrupoL3()
        grupo_l3_dict.nome = rack.nome
        grupo_l3_dict.save()
        id_grupo_l3 = grupo_l3_dict.id
        pass

    if rack.dcroom.config:
        fabricconfig = rack.dcroom.config
    else:
        log.debug("sem configuracoes do fabric %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 = []
    for env in prod_envs:

        father_id = env.id

        details = None
        for fab in fabricconfig.get("Ambiente"):
            if int(fab.get("id"))==int(father_id):
                details = fab.get("details")

        config_subnet = []
        for net in env.configs:
            cidr = IPNetwork(str(net.ip_config.subnet))
            prefix = int(net.ip_config.new_prefix)
            subnet_list = list(cidr.subnet(int(prefix)))

            try:
                bloco = subnet_list[int(rack.numero)]
            except IndexError as err:
                msg = "Rack number %d is greater than the maximum number of " \
                      "subnets available with prefix %d from %s subnet" % (
                    rack.numero, prefix, cidr
                )
                raise Exception(msg)

            if isinstance(details, list) and len(details) > 0:

                if details[0].get(str(net.ip_config.type)):
                    new_prefix = details[0].get(str(net.ip_config.type)).get("new_prefix")
                else:
                    new_prefix = 31 if net.ip_config.type=="v4" else 127
                network = {
                    'subnet': str(bloco),
                    'type': net.ip_config.type,
                    'network_type': net.ip_config.network_type.id,
                    'new_prefix': new_prefix
                }
                config_subnet.append(network)

        obj = {
            'grupo_l3': id_grupo_l3,
            'ambiente_logico': env.ambiente_logico.id,
            'divisao_dc': env.divisao_dc.id,
            'acl_path': env.acl_path,
            'ipv4_template': env.ipv4_template,
            'ipv6_template': env.ipv6_template,
            'link': env.link,
            'min_num_vlan_2': env.min_num_vlan_1,
            'max_num_vlan_2': env.max_num_vlan_1,
            'min_num_vlan_1': env.min_num_vlan_1,
            'max_num_vlan_1': env.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
        }
        obj_env = facade_env.create_environment(obj)
        environment.append(obj_env)
        log.debug("Environment object: %s" % str(obj_env))

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

    return environment