示例#1
0
def sync_publicip_south():
    try:
        account_data = Account.objects.all()
        for account in account_data:

            publicip_all_s = VpcApi(account.tokencn_south_1, 'cn-south-1',
                                    account.pidcn_south_1).get_public_ip()
            publicip_id_all_s = set()

            #add ips,update
            for pubip in publicip_all_s:
                create_time = datetime.datetime.strptime(
                    pubip['create_time'],
                    '%Y-%m-%d %H:%M:%S').replace(tzinfo=datetime.timezone.utc)
                Vpc.objects.get_or_create(
                    publicip_id=pubip['id'],
                    publicip_type=pubip['type'],
                    publicip=pubip['public_ip_address'],
                    project_id=pubip['tenant_id'],
                    #create_time=pubip['create_time'],
                    create_time=create_time,
                    bandwidth_id=pubip['bandwidth_id'],
                    bandwidth_type=pubip['bandwidth_share_type'],
                    region='cn-south-1',
                    account_name=account.account_name,
                    account_id=account.id,
                )

                if 'private_ip_address' in pubip.keys():
                    Vpc.objects.filter(publicip_id=pubip['id']).update(
                        publicip_status=pubip['status'],
                        privateip=pubip['private_ip_address'],
                        bandwidth_size=pubip['bandwidth_size'],
                        bandwidth_name=pubip['bandwidth_name'],
                    )
                else:
                    Vpc.objects.filter(publicip_id=pubip['id']).update(
                        publicip_status=pubip['status'],
                        bandwidth_size=pubip['bandwidth_size'],
                        bandwidth_name=pubip['bandwidth_name'],
                    )

            #delete mysql_publicip
                publicip_id_all_s.add(pubip['id'])
            publicip_id_all_mysql_s = set(
                list(
                    map(
                        lambda x: x.publicip_id,
                        Vpc.objects.filter(account_name=account.account_name,
                                           region='cn-south-1'))))
            delete_publicip_mysql_s = list(publicip_id_all_mysql_s -
                                           publicip_id_all_s)

            for pubip_id in delete_publicip_mysql_s:
                Vpc.objects.filter(publicip_id=pubip_id).delete()
    except Exception as e:
        log.logging.error(e)
示例#2
0
文件: tasks.py 项目: hyperwd/hwcram
def public_ip_north():
    try:
        account_data = Account.objects.all()
        for account in account_data:
            #add public_ip
            ecs_public_ip_n = VpcApi(account.tokencn_north_1,'cn-north-1',account.pidcn_north_1).get_public_ip()
            for i in ecs_public_ip_n:
                if 'private_ip_address' in i.keys():
                    Ecs.objects.filter(private_ip=i['private_ip_address'],public_ip__isnull=True,account__pidcn_north_1__contains=i['tenant_id']).update(public_ip=i['public_ip_address'])
                else:
                    Ecs.objects.filter(public_ip=i['public_ip_address'],account__pidcn_north_1__contains=i['tenant_id']).update(public_ip='')
    except Exception as e:
        log.logging.error(e)
示例#3
0
def createip_task(
    createip_id,
    account_name,
    account_id,
    token,
    region,
    projectid,
    publicip_type,
    bandwidth_share_type,
    publicip,
    bandwidth_name,
    bandwidth_size,
    bandwidth_share_id,
    bandwidth_charge_mode,
):
    try:
        createip_data = Createip.objects.filter(id=createip_id)
        if bandwidth_share_type == 'PER':
            if bandwidth_name and bandwidth_size and bandwidth_charge_mode:
                r = VpcApi(
                    token,
                    region,
                    projectid,
                ).create_public_ip(
                    publicip_type,
                    bandwidth_share_type,
                    publicip,
                    bandwidth_name,
                    bandwidth_size,
                    bandwidth_share_id,
                    bandwidth_charge_mode,
                )

                if r[1] == 200:
                    createip_data.update(publicip_id=r[0]['id'])
                    createip_data.update(publicip=r[0]['public_ip_address'])
                    create_time = datetime.datetime.strptime(
                        r[0]['create_time'], '%Y-%m-%d %H:%M:%S').replace(
                            tzinfo=datetime.timezone.utc)
                    createip_data.update(create_time=create_time)
                    createip_data.update(result=str(r[1]))
                    createip_data.update(account_id=account_id)
                    return r
                else:
                    createip_data.update(result=str(r[1]))
                    createip_data.update(account_id=account_id)
                    return r
            else:
                createip_data.update(result='1')

        if bandwidth_share_type == 'WHOLE':
            if bandwidth_share_id:
                r = VpcApi(
                    token,
                    region,
                    projectid,
                ).create_public_ip(
                    publicip_type,
                    bandwidth_share_type,
                    publicip,
                    bandwidth_name,
                    bandwidth_size,
                    bandwidth_share_id,
                    bandwidth_charge_mode,
                )

                if r[1] == 200:
                    createip_data.update(publicip_id=r[0]['id'])
                    createip_data.update(publicip=r[0]['public_ip_address'])
                    create_time = datetime.datetime.strptime(
                        r[0]['create_time'], '%Y-%m-%d %H:%M:%S').replace(
                            tzinfo=datetime.timezone.utc)
                    createip_data.update(create_time=create_time)
                    createip_data.update(result=str(r[1]))
                    createip_data.update(account_id=account_id)
                    return r
                else:
                    createip_data.update(result=str(r[1]))
                    createip_data.update(account_id=account_id)
                    return r
            else:
                createip_data.update(result='2')
    except Exception as e:
        log.logging.error(e)