def fileUploadView(request): data = JSONParser().parse(request) data = data['image'] format, imgstr = data.split(';base64,') ext = format.split('/')[-1] data = ContentFile(base64.b64decode(imgstr), name='temp.' + ext) imagePath = 'obs/' + get_random_string(length=32) + '.' + ext path = djangoSettings.STATIC_ROOT + imagePath default_storage.save(path, data) return JsonResponse({"url": djangoSettings.STATIC_URL + imagePath}, safe=False)
def storage_policy_disks(request, storage_policy_id): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) key = "storage-policy:" + storage_policy_id if request.method == 'GET': if r.exists(key): storage_policy = r.hgetall(key) storage_policy['devices'] = json.loads(storage_policy['devices']) all_devices = [] for node_key in r.keys('object_node:*'): node = r.hgetall(node_key) all_devices += [ node_key.split(':')[1] + ':' + device for device in json.loads(node['devices']).keys() ] current_devices = [dev[0] for dev in storage_policy['devices']] available_devices = [ device for device in all_devices if device not in current_devices ] available_devices_detail = [] for device in available_devices: object_node_id, device_id = device.split(':') object_node = r.hgetall('object_node:' + object_node_id) device_detail = json.loads(object_node['devices'])[device_id] device_detail['id'] = device device_detail['region'] = r.hgetall( 'region:' + object_node['region_id'])['name'] device_detail['zone'] = r.hgetall( 'zone:' + object_node['zone_id'])['name'] available_devices_detail.append(device_detail) return JSONResponse(available_devices_detail, status=status.HTTP_200_OK) else: return JSONResponse('Storage policy not found.', status=status.HTTP_404_NOT_FOUND) if request.method == 'PUT': if r.exists(key): disk = JSONParser().parse(request) object_node_id, device_id = disk.split(':') object_node = r.hgetall('object_node:' + object_node_id) # device_detail = json.loads(object_node['devices'])[device_id] region = r.hgetall('region:' + object_node['region_id'])['name'] zone = r.hgetall('zone:' + object_node['zone_id'])['name'] tmp_policy_file = get_policy_file_path(settings.SWIFT_CFG_TMP_DIR, storage_policy_id) ring = RingBuilder.load(tmp_policy_file) ring_dev_id = ring.add_dev({ 'weight': 100, 'region': region, 'zone': zone, 'ip': object_node['ip'], 'port': '6200', 'device': device_id }) ring.save(tmp_policy_file) storage_policy = r.hgetall(key) storage_policy['devices'] = json.loads(storage_policy['devices']) storage_policy['devices'].append((disk, ring_dev_id)) storage_policy['devices'] = json.dumps(storage_policy['devices']) r.hset(key, 'devices', storage_policy['devices']) r.hset(key, 'deployed', False) return JSONResponse('Disk added correctly', status=status.HTTP_200_OK) else: return JSONResponse('Disk could not be added.', status=status.HTTP_400_BAD_REQUEST) return JSONResponse('Method not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def storage_policy_disks(request, storage_policy_id): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) key = "storage-policy:" + storage_policy_id if request.method == 'GET': if r.exists(key): storage_policy = r.hgetall(key) storage_policy['devices'] = json.loads(storage_policy['devices']) all_devices = [] for node_key in r.keys('object_node:*'): node = r.hgetall(node_key) all_devices += [node_key.split(':')[1] + ':' + device for device in json.loads(node['devices']).keys()] current_devices = [dev[0] for dev in storage_policy['devices']] available_devices = [device for device in all_devices if device not in current_devices] available_devices_detail = [] for device in available_devices: object_node_id, device_id = device.split(':') object_node = r.hgetall('object_node:' + object_node_id) device_detail = json.loads(object_node['devices'])[device_id] device_detail['id'] = device device_detail['region'] = r.hgetall('region:' + object_node['region_id'])['name'] device_detail['zone'] = r.hgetall('zone:' + object_node['zone_id'])['name'] available_devices_detail.append(device_detail) return JSONResponse(available_devices_detail, status=status.HTTP_200_OK) else: return JSONResponse('Storage policy not found.', status=status.HTTP_404_NOT_FOUND) if request.method == 'PUT': if r.exists(key): disk = JSONParser().parse(request) object_node_id, device_id = disk.split(':') object_node = r.hgetall('object_node:' + object_node_id) # device_detail = json.loads(object_node['devices'])[device_id] # region = r.hgetall('region:' + object_node['region_id'])['name'] # zone = r.hgetall('zone:' + object_node['zone_id'])['name'] region = int(object_node['region_id']) zone = int(object_node['zone_id']) tmp_policy_file = get_policy_file_path(settings.SWIFT_CFG_TMP_DIR, storage_policy_id) ring = RingBuilder.load(tmp_policy_file) ring_dev_id = ring.add_dev({'weight': 100, 'region': region, 'zone': zone, 'ip': object_node['ip'], 'port': 6200, 'device': str(device_id)}) ring.save(tmp_policy_file) storage_policy = r.hgetall(key) storage_policy['devices'] = json.loads(storage_policy['devices']) storage_policy['devices'].append((disk, ring_dev_id)) storage_policy['devices'] = json.dumps(storage_policy['devices']) r.hset(key, 'devices', storage_policy['devices']) r.hset(key, 'deployed', False) return JSONResponse('Disk added correctly', status=status.HTTP_200_OK) else: return JSONResponse('Disk could not be added.', status=status.HTTP_400_BAD_REQUEST) return JSONResponse('Method not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)