Пример #1
0
def filter_subnet_pubips(ip_set):
    ip_list = list()
    for item in ip_set:
        ip_addr = item.get("floating_ip_address")
        ip_status = item.get("status")
        ip_uuid = item.get("id")
        binding_resource = item.get("binding_resource")
        ip_id = 'Unkonw'
        ip_name = 'Unknow'
        bandwidth = -1
        create_datetime = 0

        # 根据 ip 地址的 uuid 查询数据库表 ips ,可以获得 ID、名称、计费模式、带宽、分配时间
        ip_obj = IpsModel.get_ip_by_uuid(ip_uuid)
        if ip_obj:
            ip_id = ip_obj.ip_id
            ip_name = ip_obj.name
            bandwidth = ip_obj.bandwidth
            create_datetime = datetime_to_timestamp(ip_obj.create_datetime)

        instance_uuid = binding_resource.get("instance_id")
        if instance_uuid:
            instance_inst = InstancesModel.get_instance_by_uuid(instance_uuid)
            if instance_inst:
                instance_id = instance_inst.instance_id
                instance_name = instance_inst.name
                binding_resource["instance_id"] = instance_id
                binding_resource["instance_name"] = instance_name

        router_uuid = binding_resource.get("router_id")
        if router_uuid:
            router_inst = RoutersModel.get_router_by_uuid(router_uuid)
            if router_inst:
                router_id = router_inst.router_id
                router_name = router_inst.name
                binding_resource["router_id"] = router_id
                binding_resource["router_name"] = router_name

        ip_list.append({
            "ip_id": ip_id,
            "ip_name": ip_name,
            "ip_addr": ip_addr,
            "ip_status": ip_status,
            "bandwidth": bandwidth,
            "binding_resource": json.dumps(binding_resource),
            "create_datetime": create_datetime
        })
    return ip_list
Пример #2
0
def get_ip_detail_by_uuid(uuid):
    detail = {}
    try:
        info = IpsModel.get_ip_by_uuid(uuid)
        detail["ip_id"] = info.ip_id
        detail["ip_name"] = info.name
        detail["bandwidth"] = info.bandwidth
        detail["is_normal"] = info.is_normal
        detail["billing_mode"] = info.billing_mode
        detail["create_datetime"] = datetime_to_timestamp(info.create_datetime)
        detail["charge_mode"] = getattr(info, "charge_mode")
    except IpsModel.DoesNotExist:
        # TODO: logging
        pass

    return detail
Пример #3
0
def filter_needed_ip_set(ip_set, ip_ids):
    ip_ret = []
    if ip_ids:
        for ip in ip_set:
            ip_ist = IpsModel.get_ip_by_uuid(ip["id"])
            if ip_ist:
                ip_id = ip_ist.ip_id
            else:
                ip_id = None

            if ip_id and ip_id in ip_ids:
                ip_ret.append(ip)
    else:
        ip_ret = ip_set

    return ip_ret
Пример #4
0
    def filter(zone, owner):
        payload = {
            'owner': owner,
            'zone': zone,
            'action': 'DescribeLoadbalancers',
        }
        lb_details = LoadbalancerTrash.objects.filter(
            lb__zone__name=zone,
            lb__user__username=owner,
            delete_datetime=None,
            restore_datetime=None).values('lb__lb_id', 'create_datetime')
        lb_delete_datetimes = {
            lb_detail['lb__lb_id']: lb_detail['create_datetime']
            for lb_detail in lb_details
        }
        resp = describe_loadbalancers_api(payload)
        lb_set = resp.get("data", {}).get("ret_set", [])

        lb_list = []
        for single in lb_set:
            lb_id = single.get("name", None)
            if lb_id not in lb_delete_datetimes:
                continue

            raw_status = single.get("provisioning_status", None)
            if lb_id and LoadbalancerModel.lb_exists_by_id(lb_id,
                                                           deleted=True):
                lb = LoadbalancerModel.get_lb_by_id(lb_id, deleted=True)
            else:
                continue

            info = {
                "lb_id": lb_id,
                "lb_name": lb.name,
                "create_datetime": datetime_to_timestamp(lb.create_datetime),
                "status": transfer_lb_status(raw_status),
            }

            net_info = {"is_basenet": lb.is_basenet}
            if not lb.is_basenet:
                net_payload = {
                    "zone": zone,
                    "owner": owner,
                    "action": "DescribeNets",
                    "subnet_id": lb.net_id
                }
                resp = api.get(net_payload)
                net_data = resp['data']['ret_set'][0]
                net_type = 'private' if net_data.get(
                    'gateway_ip') is None else 'public'
                net_info.update({"net_type": net_type})
                net_info.update({"net_id": lb.net_id})
                net_info.update({"net_name": net_data['name']})
            info.update({"net": net_info})

            ip_info = {"vip_addr": single.get("vip_address", None)}
            fip_info = single.get("fip_info")
            if fip_info:
                fip_uuid = fip_info["ip_uuid"]
                fip_address = fip_info["ip_address"]
                ip = IpsModel.get_ip_by_uuid(fip_uuid)
                ip_info.update({"ip_id": ip.ip_id})
                ip_info.update({"fip_addr": fip_address})
                ip_info.update({"bandwidth": ip.bandwidth})
            info.update({"ip": ip_info})
            info['delete_datetime'] = datetime_to_timestamp(
                lb_delete_datetimes[lb_id])

            lb_list.append(info)

        return lb_list
Пример #5
0
def describe_loadbalancers(payload):
    zone = payload.get("zone")
    owner = payload.get("owner")
    lb_id = payload.pop("lb_id", None)
    features = payload.pop("features", [])

    if lb_id:
        lb = LoadbalancerModel.get_lb_by_id(lb_id)
        lb_uuid = lb.uuid
    else:
        lb_uuid = None

    # call backend
    resp = describe_loadbalancers_api(payload, loadbalancer_id=lb_uuid)

    api_code = resp.get("data", {}).get("ret_code")
    code = resp.get("code", -1)
    msg = resp.get("msg", "failed")
    if code != Code.OK:
        code = convert_api_code(api_code)
        return console_response(code=code, msg=msg)

    lb_set = resp.get("data", {}).get("ret_set", [])
    lb_list = []
    for single in lb_set:
        lb_id = single.get("name", None)
        raw_status = single.get("provisioning_status", None)
        if lb_id and LoadbalancerModel.lb_exists_by_id(lb_id):
            lb = LoadbalancerModel.get_lb_by_id(lb_id)
        else:
            continue

        info = {
            "lb_id": lb_id,
            "lb_name": lb.name,
            "create_datetime": datetime_to_timestamp(lb.create_datetime),
            "status": transfer_lb_status(raw_status),
        }

        net_info = {"is_basenet": lb.is_basenet}
        if not lb.is_basenet:

            net_payload = {
                "zone": zone,
                "owner": owner,
                "action": "DescribeNets",
                "subnet_id": lb.net_id
            }
            resp = api.get(net_payload)
            net_data = resp['data']['ret_set'][0]
            net_type = 'private' if net_data.get(
                'gateway_ip') is None else 'public'
            net_info.update({"net_type": net_type})
            net_info.update({"net_id": lb.net_id})
            net_info.update({"net_name": net_data['name']})
        info.update({"net": net_info})

        ip_info = {"vip_addr": single.get("vip_address", None)}
        logger.debug('single: ')
        logger.debug(single)
        fip_info = single.get("fip_info")
        if fip_info:
            fip_uuid = fip_info["ip_uuid"]
            fip_address = fip_info["ip_address"]
            ip = IpsModel.get_ip_by_uuid(fip_uuid)
            ip_info.update({"ip_id": ip.ip_id})
            ip_info.update({"fip_addr": fip_address})
            ip_info.update({"bandwidth": ip.bandwidth})
        info.update({"ip": ip_info})

        for feature in features:
            if 'elastic' == feature:
                elastic = ElasticGroupService.get_by_loadbalance_id(lb_id)
                if elastic:
                    serializer = DescribeLoadbalancerSerializer(elastic)
                    info.update({'elastic': serializer.data})

        lb_list.append(info)
    lb_list = sorted(lb_list,
                     key=lambda x: x.get("create_datetime"),
                     reverse=True)

    return console_response(code=Code.OK,
                            msg=msg,
                            total_count=len(lb_list),
                            ret_set=lb_list)