def disk_save(request): if not request.user.is_authenticated(): json_r = json.dumps({"result":"no login"}) return HttpResponse(json_r) elif not request.user.has_perm('cmdb.change_disk'): json_r = json.dumps({"result":"no permission"}) return HttpResponse(json_r) json_str = request.body data = json.loads(json_str) if data['id']: i = Disk.objects.filter(id=data['id']) message = cmdb_log.cmp(list(i.values())[0],data) i.update(Disk_Type = data['Disk_Type']) cmdb_log.log_change(request,i[0],data['Disk_Type'],message) else: i = Disk(Disk_Type = data['Disk_Type']) i.save() cmdb_log.log_addition(request,i,data['Disk_Type'],data) json_r = json.dumps({"result":"save sucess"}) return HttpResponse(json_r)
def get_hosts_from_aliyun(): """ 从阿里云获取ECS实例并入库 :return: """ ecs = ECSHandler('L', 'V7', 'cn-beijing') page = 1 while True: instances, exception, next_page = ecs.get_instances(page) for instance in instances: disks = instance.pop('disks') instance['cloud_type'] = 'aliyun' try: host = Host.objects.create(**instance) for disk in disks: instance = Disk() instance.host = host instance.disk_id = disk['disk_id'] instance.device = disk['device'] instance.size = disk['size'] instance.type = disk['type'] instance.creation_time = disk['creation_time'] instance.expired_time = disk['expired_time'] instance.save() except Exception as e: host = Host.objects.filter( instance_id=instance['instance_id']).update(**instance) for disk in disks: instance = Disk.objects.filter(host=host).update(**disk) page += 1 if not next_page: break return True
def get_hosts_from_qcloud(): """ 从腾讯云获取ECS实例并入库 :return: """ ecs = TenHandler('', '', '', 'cvm') instances, is_success, next_page = ecs.get_instances(1) if is_success: page = 1 while True: for instance in instances: disks = instance.pop('disks') instance['cloud_type'] = 'qcloud' try: host = Host.objects.create(**instance) for disk in disks: instance = Disk() instance.host = host instance.disk_id = disk['disk_id'] instance.device = disk['device'] instance.size = disk['size'] instance.type = disk['type'] instance.creation_time = disk['creation_time'] instance.expired_time = disk['expired_time'] instance.save() except Exception as e: host = Host.objects.filter( instance_id=instance['instance_id']).update(**instance) for disk in disks: instance = Disk.objects.filter(host=host).update( **disk) page += 1 if not next_page: break return True
def get_hosts_from_qcloud(): """ 从腾讯云获取ECS实例并入库 :return: """ ecs = TenHandler('ap-guangzhou', 'AKIDmRuFSU61qUwHQOGkBQSIxRjpjSzKRQBk', 'Vr7GLJBwa8Yr251qXAg6PbgljaJuOl08', "cvm") instances, is_success, next_page = ecs.get_instances(1) if is_success: page = 1 while True: for instance in instances: disks = instance.pop('disks') instance['cloud_type'] = 'qcloud' try: host = Host.objects.create(**instance) for disk in disks: instance = Disk() instance.host = host instance.disk_id = disk['disk_id'] instance.device = disk['device'] instance.size = disk['size'] instance.type = disk['type'] instance.creation_time = disk['creation_time'] instance.expired_time = disk['expired_time'] instance.save() except Exception as e: host = Host.objects.filter( instance_id=instance['instance_id']).update(**instance) for disk in disks: instance = Disk.objects.filter(host=host).update( **disk) page += 1 if not next_page: break return True