def storage_policies(request): """ Creates a storage policy to swift with an specific ring. Allows create replication storage policies and erasure code storage policies """ if request.method == "GET": try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) keys = r.keys("storage-policy:*") storage_policy_list = [] for key in keys: storage_policy = r.hgetall(key) to_json_bools(storage_policy, 'deprecated', 'default', 'deployed') storage_policy['id'] = str(key).split(':')[-1] storage_policy['devices'] = json.loads(storage_policy['devices']) storage_policy_list.append(storage_policy) return JSONResponse(storage_policy_list, status=status.HTTP_200_OK) if request.method == "POST": try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) data = JSONParser().parse(request) if data['policy_type'] == 'EC': data['replicas'] = int(data['ec_num_data_fragments']) + int( data['ec_num_parity_fragments']) try: sp_id = str(r.incr('storage-policies:id')) key = 'storage-policy:' + sp_id ring = RingBuilder(int(data['partition_power']), int(data['replicas']), int(data['time'])) ring.save(get_policy_file_path(settings.SWIFT_CFG_TMP_DIR, sp_id)) r.hmset(key, data) except: return JSONResponse('Error creating the Storage Policy', status=status.HTTP_500_INTERNAL_SERVER_ERROR) return JSONResponse('Account created successfully', status=status.HTTP_201_CREATED) return JSONResponse('Only HTTP POST requests allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def slo_detail(request, dsl_filter, slo_name, target): """ Retrieve, update or delete SLO. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) slo_key = ':'.join(['SLO', dsl_filter, slo_name, target]) if request.method == 'GET': if r.exists(slo_key): value = r.get(slo_key) slo = {'dsl_filter': dsl_filter, 'slo_name': slo_name, 'target': target, 'value': value} return JSONResponse(slo, status=status.HTTP_200_OK) else: return JSONResponse("SLO not found.", status=status.HTTP_404_NOT_FOUND) elif request.method == 'PUT': data = JSONParser().parse(request) try: r.set(slo_key, data['value']) return JSONResponse('Data updated', status=status.HTTP_201_CREATED) except DataError: return JSONResponse('Error updating data', status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': r.delete(slo_key) return JSONResponse('SLA has been deleted', status=status.HTTP_204_NO_CONTENT) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def slo_list(request): """ List all SLOs, or create an SLO. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': slos = [] keys = r.keys('SLO:*') for key in keys: _, dsl_filter, slo_name, target = key.split(':') value = r.get(key) slos.append({'dsl_filter': dsl_filter, 'slo_name': slo_name, 'target': target, 'value': value}) return JSONResponse(slos, status=status.HTTP_200_OK) elif request.method == 'POST': data = JSONParser().parse(request) try: slo_key = ':'.join(['SLO', data['dsl_filter'], data['slo_name'], data['target']]) r.set(slo_key, data['value']) return JSONResponse(data, status=status.HTTP_201_CREATED) except DataError: return JSONResponse('Error saving SLA.', status=status.HTTP_400_BAD_REQUEST) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def dependency_detail(request, dependency_id): """ Retrieve, update or delete a Dependency. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=500) if request.method == 'GET': dependency = r.hgetall("dependency:" + str(dependency_id)) return JSONResponse(dependency, status=200) elif request.method == 'PUT': data = JSONParser().parse(request) try: r.hmset('dependency:' + str(dependency_id), data) return JSONResponse("Data updated", status=201) except DataError: return JSONResponse("Error updating data", status=400) elif request.method == 'DELETE': r.delete("dependency:" + str(dependency_id)) return JSONResponse('Dependency has been deleted', status=204) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=405)
def zones(request): """ GET: List all zones """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': keys = r.keys("zone:*") zone_items = [] for key in keys: zone = r.hgetall(key) zone['id'] = key.split(':')[1] zone['region_name'] = r.hgetall('region:' + zone['region'])['name'] zone_items.append(zone) return JSONResponse(zone_items, status=status.HTTP_200_OK) if request.method == 'POST': key = "zone:" + str(r.incr('zones:id')) data = JSONParser().parse(request) try: r.hmset(key, data) return JSONResponse("Data inserted correctly", status=status.HTTP_201_CREATED) except RedisError: return JSONResponse("Error inserting data", status=status.HTTP_400_BAD_REQUEST) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def put(self, request, metric_module_id): data = {} try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) try: file_obj = request.FILES['file'] make_sure_path_exists(settings.WORKLOAD_METRICS_DIR) path = save_file(file_obj, settings.WORKLOAD_METRICS_DIR) data['metric_name'] = os.path.basename(path) # synchronize metrics directory with all nodes try: rsync_dir_with_nodes(settings.WORKLOAD_METRICS_DIR, settings.WORKLOAD_METRICS_DIR) except FileSynchronizationException as e: # print "FileSynchronizationException", e # TODO remove return JSONResponse(e.message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) r.hmset('workload_metric:' + str(metric_module_id), data) return JSONResponse("Data updated", status=status.HTTP_201_CREATED) except DataError: return JSONResponse("Error updating data", status=status.HTTP_400_BAD_REQUEST) except ValueError: return JSONResponse("Error starting controller", status=status.HTTP_400_BAD_REQUEST)
def add_projects_group(request): """ Add a tenant group or list all the tenants groups saved in the registry. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': keys = r.keys("project_group:*") project_groups = [] for key in keys: group = r.hgetall(key) group['id'] = key.split(':')[1] group['attached_projects'] = json.loads(group['attached_projects']) project_groups.append(group) return JSONResponse(project_groups, status=status.HTTP_200_OK) if request.method == 'POST': data = JSONParser().parse(request) if not data: return JSONResponse('Tenant group cannot be empty', status=status.HTTP_400_BAD_REQUEST) gtenant_id = r.incr("project_groups:id") r.hmset('project_group:' + str(gtenant_id), data) return JSONResponse('Tenant group has been added to the registry', status=status.HTTP_201_CREATED) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def get(self, request, controller_id): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if r.exists('controller:' + str(controller_id)): global_controller_path = os.path.join( settings.CONTROLLERS_DIR, str( r.hget('controller:' + str(controller_id), 'controller_name'))) if os.path.exists(global_controller_path): global_controller_name = os.path.basename( global_controller_path) global_controller_size = os.stat( global_controller_path).st_size # Generate response response = StreamingHttpResponse( FileWrapper(open(global_controller_path), global_controller_size), content_type=mimetypes.guess_type( global_controller_path)[0]) response['Content-Length'] = global_controller_size response[ 'Content-Disposition'] = "attachment; filename=%s" % global_controller_name return response else: return HttpResponse(status=status.HTTP_404_NOT_FOUND) else: return HttpResponse(status=status.HTTP_404_NOT_FOUND)
def zone_detail(request, zone_id): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) key = 'zone:' + str(zone_id) if request.method == 'GET': if r.exists(key): zone = r.hgetall(key) return JSONResponse(zone, status=status.HTTP_200_OK) else: return JSONResponse('Zone not found.', status=status.HTTP_404_NOT_FOUND) if request.method == 'DELETE': # Deletes the key. If the node is alive, the metric middleware will recreate this key again. if r.exists(key): r.delete(key) return JSONResponse('Zone has been deleted', status=status.HTTP_204_NO_CONTENT) else: return JSONResponse('Zone not found.', status=status.HTTP_404_NOT_FOUND) if request.method == 'PUT': data = JSONParser().parse(request) key = "zone:" + str(data['zone_id']) try: r.hmset(key, data) return JSONResponse("Zone Data updated correctly", status=status.HTTP_204_NO_CONTENT) except RedisError: return JSONResponse("Error updating zone data", status=status.HTTP_400_BAD_REQUEST) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def deployed_storage_policies(request): if request.method == "GET": try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) keys = r.keys("storage-policy:*") swift_file = os.path.join(settings.SWIFT_CFG_DEPLOY_DIR, 'swift.conf') config_parser = ConfigParser.RawConfigParser() config_parser.read(swift_file) deployed_storage_policy_list = [sp for sp in keys if config_parser.has_section(sp)] storage_policy_list = [] for key in deployed_storage_policy_list: storage_policy = r.hgetall(key) to_json_bools(storage_policy, 'deprecated', 'default', 'deployed') storage_policy['id'] = str(key).split(':')[-1] storage_policy['devices'] = json.loads(storage_policy['devices']) storage_policy_list.append(storage_policy) return JSONResponse(storage_policy_list, status=status.HTTP_200_OK) return JSONResponse('Only HTTP POST requests allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def post(self, request): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=500) data = json.loads(request.POST['metadata']) # json data is in metadata parameter for this request if not data: return JSONResponse("Invalid format or empty request", status=status.HTTP_400_BAD_REQUEST) controller_id = r.incr("controllers:id") try: data['id'] = controller_id file_obj = request.FILES['file'] make_sure_path_exists(settings.CONTROLLERS_DIR) path = save_file(file_obj, settings.CONTROLLERS_DIR) data['controller_name'] = os.path.basename(path) r.hmset('controller:' + str(controller_id), data) return JSONResponse(data, status=status.HTTP_201_CREATED) except DataError: return JSONResponse("Error to save the object", status=status.HTTP_400_BAD_REQUEST) except ValueError: return JSONResponse("Error starting/stoping controller", status=status.HTTP_400_BAD_REQUEST) except Exception: return JSONResponse("Error uploading file", status=status.HTTP_400_BAD_REQUEST)
def deployed_storage_policies(request): if request.method == "GET": try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) keys = r.keys("storage-policy:*") swift_file = os.path.join(settings.SWIFT_CFG_DEPLOY_DIR, 'swift.conf') config_parser = ConfigParser.RawConfigParser() config_parser.read(swift_file) deployed_storage_policy_list = [ sp for sp in keys if config_parser.has_section(sp) ] storage_policy_list = [] for key in deployed_storage_policy_list: storage_policy = r.hgetall(key) to_json_bools(storage_policy, 'deprecated', 'default', 'deployed') storage_policy['id'] = str(key).split(':')[-1] storage_policy['devices'] = json.loads(storage_policy['devices']) storage_policy_list.append(storage_policy) return JSONResponse(storage_policy_list, status=status.HTTP_200_OK) return JSONResponse('Only HTTP POST requests allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def node_restart(request, server_type, node_id): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) key = server_type + "_node:" + node_id logger.debug('Restarting node: ' + str(key)) if request.method == 'PUT': node = r.hgetall(key) ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(node['ip'], username=node['ssh_username'], password=node['ssh_password']) try: ssh_client.exec_command('sudo swift-init main restart') except SSHException: ssh_client.close() logger.error('An error occurred restarting Swift nodes') raise FileSynchronizationException( "An error occurred restarting Swift nodes") ssh_client.close() logger.debug('Node ' + str(key) + ' was restarted!') return JSONResponse('The node was restarted successfully.', status=status.HTTP_200_OK) logger.error('Method ' + str(request.method) + ' not allowed.') return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def add_projects_group(request): """ Add a tenant group or list all the tenants groups saved in the registry. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': keys = r.keys("project_group:*") project_groups = [] for key in keys: group = r.hgetall(key) group['id'] = key.split(':')[1] group['attached_projects'] = json.loads(group['attached_projects']) project_groups.append(group) return JSONResponse(project_groups, status=status.HTTP_200_OK) if request.method == 'POST': data = JSONParser().parse(request) if not data: return JSONResponse('Tenant group cannot be empty', status=status.HTTP_400_BAD_REQUEST) gtenant_id = r.incr("project_groups:id") r.hmset('project_group:' + str(gtenant_id), data) return JSONResponse('Tenant group has been added to the registry', status=status.HTTP_201_CREATED) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def list_activated_metrics(request): """ Get all registered workload metrics (GET) or add a new metric workload in the registry (POST). :param request: The http request. :type request: HttpRequest :return: A JSON list with all registered metrics (GET) or a success/error message depending on the result of the function. :rtype: JSONResponse """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': keys = r.keys("metric:*") metrics = [] for key in keys: metric = r.hgetall(key) metric["name"] = key.split(":")[1] metrics.append(metric) return JSONResponse(metrics, status=status.HTTP_200_OK) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def filter_undeploy(request, filter_id, project_id, container=None, swift_object=None): """ Undeploy a filter from a specific swift project. """ if request.method == 'PUT': try: r = get_redis_connection() except RedisError: return JSONResponse('Problems to connect with the DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) filter_data = r.hgetall("filter:" + str(filter_id)) if not filter_data: return JSONResponse('Filter does not exist', status=status.HTTP_404_NOT_FOUND) if container and swift_object: target = project_id + "/" + container + "/" + swift_object elif container: target = project_id + "/" + container else: target = project_id token = get_token_connection(request) return unset_filter(r, target, filter_data, token) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def dependency_undeploy(request, dependency_id, project_id): if request.method == 'PUT': try: r = get_redis_connection() except RedisError: return JSONResponse('Problems to connect with the DB', status=500) dependency = r.hgetall("dependency:" + str(dependency_id)) if not dependency: return JSONResponse('Dependency does not exist', status=404) if not r.exists(str(project_id) + ":dependency:" + str(dependency["name"])): return JSONResponse('Dependency ' + str(dependency["name"]) + ' has not been deployed already', status=404) try: token = get_token_connection(request) url = settings.SWIFT_URL + "/AUTH_" + project_id swift_response = dict() swift_client.delete_object(url, token, 'dependency', dependency["name"], None, None, None, None, swift_response) except ClientException: return JSONResponse(swift_response.get("reason"), status=swift_response.get('status')) swift_status = swift_response.get('status') if 200 <= swift_status < 300: r.delete(str(project_id) + ":dependency:" + str(dependency["name"])) r.lrem(str(project_id) + ":dependencies", str(dependency["name"]), 1) return JSONResponse('The dependency has been deleted', status=swift_status) return JSONResponse(swift_response.get("reason"), status=swift_status) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=405)
def zone_detail(request, zone_id): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) key = 'zone:' + str(zone_id) if request.method == 'GET': if r.exists(key): zone = r.hgetall(key) return JSONResponse(zone, status=status.HTTP_200_OK) else: return JSONResponse('Zone not found.', status=status.HTTP_404_NOT_FOUND) if request.method == 'DELETE': # Deletes the key. If the node is alive, the metric middleware will recreate this key again. if r.exists(key): r.delete(key) return JSONResponse('Zone has been deleted', status=status.HTTP_204_NO_CONTENT) else: return JSONResponse('Zone not found.', status=status.HTTP_404_NOT_FOUND) if request.method == 'PUT': data = JSONParser().parse(request) key = "zone:" + str(data['zone_id']) try: r.hmset(key, data) return JSONResponse("Zone Data updated correctly", status=status.HTTP_204_NO_CONTENT) except RedisError: return JSONResponse("Error updating zone data", status=status.HTTP_400_BAD_REQUEST) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def zones(request): """ GET: List all zones """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': keys = r.keys("zone:*") zone_items = [] for key in keys: zone = r.hgetall(key) zone['id'] = key.split(':')[1] zone['region_name'] = r.hgetall('region:' + zone['region'])['name'] zone_items.append(zone) return JSONResponse(zone_items, status=status.HTTP_200_OK) if request.method == 'POST': key = "zone:" + str(r.incr('zones:id')) data = JSONParser().parse(request) try: r.hmset(key, data) return JSONResponse("Data inserted correctly", status=status.HTTP_201_CREATED) except RedisError: return JSONResponse("Error inserting data", status=status.HTTP_400_BAD_REQUEST) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def dependency_list(request): """ List all dependencies, or create a Dependency. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=500) if request.method == 'GET': keys = r.keys("dependency:*") dependencies = [] for key in keys: dependencies.append(r.hgetall(key)) return JSONResponse(dependencies, status=200) elif request.method == 'POST': data = JSONParser().parse(request) dependency_id = r.incr("dependencies:id") try: data["id"] = dependency_id r.hmset('dependency:' + str(dependency_id), data) return JSONResponse(data, status=201) except DataError: return JSONResponse("Error to save the filter", status=400) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=405)
def node_restart(request, server_type, node_id): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) key = server_type+"_node:" + node_id logger.debug('Restarting node: ' + str(key)) if request.method == 'PUT': node = r.hgetall(key) ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect(node['ip'], username=node['ssh_username'], password=node['ssh_password']) try: ssh_client.exec_command('sudo swift-init main restart') except SSHException: ssh_client.close() logger.error('An error occurred restarting Swift nodes') raise FileSynchronizationException("An error occurred restarting Swift nodes") ssh_client.close() logger.debug('Node ' + str(key) + ' was restarted!') return JSONResponse('The node was restarted successfully.', status=status.HTTP_200_OK) logger.error('Method ' + str(request.method) + ' not allowed.') return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def storage_policies(request): """ Creates a storage policy to swift with an specific ring. Allows create replication storage policies and erasure code storage policies """ if request.method == "GET": try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) keys = r.keys("storage-policy:*") storage_policy_list = [] for key in keys: storage_policy = r.hgetall(key) to_json_bools(storage_policy, 'deprecated', 'default', 'deployed') storage_policy['id'] = str(key).split(':')[-1] storage_policy['devices'] = json.loads(storage_policy['devices']) storage_policy_list.append(storage_policy) return JSONResponse(storage_policy_list, status=status.HTTP_200_OK) if request.method == "POST": try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) data = JSONParser().parse(request) if data['policy_type'] == 'EC': data['replicas'] = int(data['ec_num_data_fragments']) + int(data['ec_num_parity_fragments']) try: sp_id = str(r.incr('storage-policies:id')) key = 'storage-policy:' + sp_id ring = RingBuilder(int(data['partition_power']), int(data['replicas']), int(data['time'])) ring.save(get_policy_file_path(settings.SWIFT_CFG_TMP_DIR, sp_id)) r.hmset(key, data) except: return JSONResponse('Error creating the Storage Policy', status=status.HTTP_500_INTERNAL_SERVER_ERROR) return JSONResponse('Account created successfully', status=status.HTTP_201_CREATED) return JSONResponse('Only HTTP POST requests allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def static_policy_detail(request, policy_id): """ Retrieve, update or delete a static policy. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) target = str(policy_id).split(':')[:-1] target = ':'.join(target) policy = str(policy_id).split(':')[-1] if request.method == 'GET': project_list = get_project_list() project_list['global'] = 'Global' policy_redis = r.hget("pipeline:" + str(target), policy) data = json.loads(policy_redis) filter_data = r.hgetall('filter:' + str(data['dsl_name'])) to_json_bools(filter_data, 'get', 'put', 'post', 'head', 'delete') data['get'] = filter_data['get'] data['put'] = filter_data['put'] if 'post' in filter_data: data['post'] = filter_data['post'] if 'head' in filter_data: data['head'] = filter_data['head'] if 'delete' in filter_data: data['delete'] = filter_data['delete'] data["id"] = policy data["target_id"] = target data["target_name"] = project_list[target.split(':')[0]] return JSONResponse(data, status=200) elif request.method == 'PUT': data = JSONParser().parse(request) try: policy_redis = r.hget("pipeline:" + str(target), policy) json_data = json.loads(policy_redis) json_data.update(data) json_data['object_name'] = ', '.join(r.lrange('object_type:' + json_data['object_type'], 0, -1)) json_data['execution_order'] = int(json_data['execution_order']) r.hset("pipeline:" + str(target), policy, json.dumps(json_data)) return JSONResponse("Data updated", status=201) except DataError: return JSONResponse("Error updating data", status=400) elif request.method == 'DELETE': r.hdel('pipeline:' + target, policy) policies_ids = r.keys('policy:*') pipelines_ids = r.keys('pipeline:*') if len(policies_ids) == 0 and len(pipelines_ids) == 0: r.set('policies:id', 0) # token = get_token_connection(request) # unset_filter(r, target, filter_data, token) return JSONResponse('Policy has been deleted', status=status.HTTP_204_NO_CONTENT) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def node_detail(request, server_type, node_id): """ GET: Retrieve node details. PUT: Update node. :param request: :param server_type: :param node_id: :return: """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) key = server_type+"_node:" + node_id if request.method == 'GET': if r.exists(key): node = r.hgetall(key) node.pop("ssh_password", None) # password is not returned node['devices'] = json.loads(node['devices']) return JSONResponse(node, status=status.HTTP_200_OK) else: return JSONResponse('Node not found.', status=status.HTTP_404_NOT_FOUND) if request.method == 'PUT': if r.exists(key): data = JSONParser().parse(request) try: ssh_user = data['ssh_username'] ssh_password = data['ssh_password'] node = r.hgetall(key) ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: ssh_client.connect(node['ip'], username=ssh_user, password=ssh_password) ssh_client.close() data['ssh_access'] = True except AuthenticationException: data['ssh_access'] = False r.hmset(key, data) return JSONResponse("Node Data updated", status=status.HTTP_201_CREATED) except RedisError: return JSONResponse("Error updating node data", status=status.HTTP_400_BAD_REQUEST) else: return JSONResponse('Node not found.', status=status.HTTP_404_NOT_FOUND) if request.method == 'DELETE': # Deletes the key. If the node is alive, the metric middleware will recreate this key again. if r.exists(key): r.delete(key) return JSONResponse('Node has been deleted', status=status.HTTP_204_NO_CONTENT) else: return JSONResponse('Node not found.', status=status.HTTP_404_NOT_FOUND) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def node_detail(request, server_type, node_id): """ GET: Retrieve node details. PUT: Update node. :param request: :param server_type: :param node_id: :return: """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) key = server_type+"_node:" + node_id if request.method == 'GET': if r.exists(key): node = r.hgetall(key) node.pop("ssh_password", None) # password is not returned node['devices'] = json.loads(node['devices']) return JSONResponse(node, status=status.HTTP_200_OK) else: return JSONResponse('Node not found.', status=status.HTTP_404_NOT_FOUND) if request.method == 'PUT': if r.exists(key): data = JSONParser().parse(request) try: ssh_user = data['ssh_username'] ssh_password = data['ssh_password'] node = r.hgetall(key) ssh_client = paramiko.SSHClient() ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: ssh_client.connect(node['ip'], username=ssh_user, password=ssh_password) ssh_client.close() data['ssh_access'] = True except AuthenticationException: data['ssh_access'] = False r.hmset(key, data) return JSONResponse("Node Data updated", status=status.HTTP_201_CREATED) except RedisError: return JSONResponse("Error updating node data", status=status.HTTP_400_BAD_REQUEST) else: return JSONResponse('Node not found.', status=status.HTTP_404_NOT_FOUND) if request.method == 'DELETE': # Deletes the key. If the node is alive, the metric middleware will recreate this key again. if r.exists(key): r.delete(key) return JSONResponse('Node has been deleted', status=status.HTTP_204_NO_CONTENT) else: return JSONResponse('Node not found.', status=status.HTTP_404_NOT_FOUND) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def projects_group_detail(request, group_id): """ Get, update or delete a projects group from the registry. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': key = 'project_group:' + str(group_id) if r.exists(key): group = r.hgetall(key) group['attached_projects'] = json.loads(group['attached_projects']) return JSONResponse(group, status=status.HTTP_200_OK) else: return JSONResponse('The tenant group with id: ' + str(group_id) + ' does not exist.', status=status.HTTP_404_NOT_FOUND) if request.method == 'PUT': key = 'project_group:' + str(group_id) if r.exists(key): data = JSONParser().parse(request) try: r.hmset(key, data) return JSONResponse( 'The members of the tenants group with id: ' + str(group_id) + ' has been updated', status=status.HTTP_201_CREATED) except: return JSONResponse( 'Error storing the tenant group in the DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: return JSONResponse('The tenant group with id: ' + str(group_id) + ' does not exist.', status=status.HTTP_404_NOT_FOUND) if request.method == 'DELETE': key = 'project_group:' + str(group_id) if r.exists(key): r.delete("project_group:" + str(group_id)) gtenants_ids = r.keys('project_group:*') if len(gtenants_ids) == 0: r.set('project_groups:id', 0) return JSONResponse('Tenants group has been deleted', status=status.HTTP_204_NO_CONTENT) else: return JSONResponse('The tenant group with id: ' + str(group_id) + ' does not exist.', status=status.HTTP_404_NOT_FOUND) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def deploy_storage_policy(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 == "POST": if r.exists(key): try: tmp_policy_file = get_policy_file_path( settings.SWIFT_CFG_TMP_DIR, storage_policy_id) deploy_policy_file = get_policy_file_path( settings.SWIFT_CFG_DEPLOY_DIR, storage_policy_id) deploy_gzip_filename = deploy_policy_file.replace( 'builder', 'ring.gz') ring = RingBuilder.load(tmp_policy_file) ring.rebalance() ring.save(tmp_policy_file) ringdata = ring.get_ring() ringdata.save(deploy_gzip_filename) data = r.hgetall(key) update_sp_files( settings.SWIFT_CFG_DEPLOY_DIR, storage_policy_id, { 'name': data['name'], 'deprecated': data['deprecated'], 'default': data['default'] }) copyfile(tmp_policy_file, deploy_policy_file) rsync_dir_with_nodes(settings.SWIFT_CFG_DEPLOY_DIR, '/etc/swift') r.hset(key, 'deployed', 'True') return JSONResponse('Storage policy deployed correctly', status=status.HTTP_200_OK) except RedisError: return JSONResponse('Storage policy could not be deployed', status=status.HTTP_400_BAD_REQUEST) except exceptions.RingBuilderError, e: return JSONResponse( 'Storage policy could not be deployed. Error message: %s' % e.message, status=status.HTTP_400_BAD_REQUEST) else: return JSONResponse('Storage policy not found.', status=status.HTTP_404_NOT_FOUND)
def put(self, request, dependency_id, format=None): try: r = get_redis_connection() except RedisError: return JSONResponse('Problems to connect with the DB', status=500) if r.exists("dependency:" + str(dependency_id)): file_obj = request.FILES['file'] path = save_file(file_obj, settings.DEPENDENCY_DIR) r.hset("dependency:" + str(dependency_id), "path", str(path)) return JSONResponse('Dependency has been updated', status=201) return JSONResponse('Dependency does not exist', status=404)
def controller_detail(request, controller_id): """ Retrieve, update or delete a global controller. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': controller = r.hgetall('controller:' + str(controller_id)) # to_json_bools(controller, 'enabled') return JSONResponse(controller, status=status.HTTP_200_OK) elif request.method == 'PUT': try: data = JSONParser().parse(request) except ParseError: return JSONResponse("Invalid format or empty request", status=status.HTTP_400_BAD_REQUEST) try: r.hmset('controller:' + str(controller_id), data) return JSONResponse("Data updated", status=status.HTTP_200_OK) except DataError: return JSONResponse("Error updating data", status=status.HTTP_408_REQUEST_TIMEOUT) elif request.method == 'DELETE': try: controller = r.hgetall('controller:' + str(controller_id)) if controller['instances'] not in ['0', 0]: return JSONResponse( "Controller could not be deleted because it has one or more instances.", status=status.HTTP_400_BAD_REQUEST) else: delete_file(controller['controller_name'], settings.CONTROLLERS_DIR) r.delete("controller:" + str(controller_id)) except: return JSONResponse("Error deleting controller", status=status.HTTP_400_BAD_REQUEST) # If this is the last controller, the counter is reset keys = r.keys('controller:*') if not keys: r.delete('controllers:id') return JSONResponse('Controller has been deleted', status=status.HTTP_204_NO_CONTENT) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def deploy_storage_policy(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 == "POST": if r.exists(key): try: tmp_policy_file = get_policy_file_path(settings.SWIFT_CFG_TMP_DIR, storage_policy_id) deploy_policy_file = get_policy_file_path(settings.SWIFT_CFG_DEPLOY_DIR, storage_policy_id) deploy_gzip_filename = deploy_policy_file.replace('builder', 'ring.gz') ring = RingBuilder.load(tmp_policy_file) ring.rebalance() ring.save(tmp_policy_file) ringdata = ring.get_ring() ringdata.save(deploy_gzip_filename) data = r.hgetall(key) policy = {'name': data['name'], 'deprecated': data['deprecated'], 'default': data['default']} if data['policy_type'] == 'EC': policy.update({'policy_type': 'erasure_coding', 'ec_type': data['ec_type'], 'ec_num_data_fragments': data['ec_num_data_fragments'], 'ec_num_parity_fragments': data['ec_num_parity_fragments'], 'ec_object_segment_size': data['ec_object_segment_size'], 'ec_duplication_factor': data['ec_duplication_factor']}) update_sp_files(settings.SWIFT_CFG_DEPLOY_DIR, storage_policy_id, policy) else: update_sp_files(settings.SWIFT_CFG_DEPLOY_DIR, storage_policy_id, policy) copyfile(tmp_policy_file, deploy_policy_file) rsync_dir_with_nodes(settings.SWIFT_CFG_DEPLOY_DIR, '/etc/swift') r.hset(key, 'deployed', 'True') return JSONResponse('Storage policy deployed correctly', status=status.HTTP_200_OK) except RedisError: return JSONResponse('Storage policy could not be deployed', status=status.HTTP_400_BAD_REQUEST) except exceptions.RingBuilderError as e: return JSONResponse('Storage policy could not be deployed. Error message: %s' % e.message, status=status.HTTP_400_BAD_REQUEST) else: return JSONResponse('Storage policy not found.', status=status.HTTP_404_NOT_FOUND) return JSONResponse('Only HTTP POST requests allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def filter_detail(request, filter_id): """ Retrieve, update or delete a Filter. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if not r.exists("filter:" + str(filter_id)): return JSONResponse('Object does not exist!', status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': my_filter = r.hgetall("filter:" + str(filter_id)) to_json_bools(my_filter, 'put', 'get', 'post', 'head', 'delete') return JSONResponse(my_filter, status=status.HTTP_200_OK) elif request.method == 'PUT': try: data = JSONParser().parse(request) except ParseError: return JSONResponse("Invalid format or empty request", status=status.HTTP_400_BAD_REQUEST) try: if 'dsl_name' in data and str(filter_id) != data['dsl_name']: # Check for possible activated policies policies = r.keys('policy:*') for policy_key in policies: policy = r.hgetall(policy_key) dsl_filter = policy['filter'] if dsl_filter == str(filter_id): return JSONResponse("It is not possible to change the DSL Name, "+str(filter_id)+ " is associated with some Dynamic Policy", status=status.HTTP_400_BAD_REQUEST) filter_data = r.hgetall("filter:" + str(filter_id)) r.hmset('filter:' + str(data['dsl_name']), filter_data) r.delete("filter:" + str(filter_id)) r.hmset('filter:' + str(data['dsl_name']), data) else: r.hmset('filter:' + str(filter_id), data) return JSONResponse("Data updated", status=status.HTTP_200_OK) except DataError: return JSONResponse("Error updating data", status=status.HTTP_408_REQUEST_TIMEOUT) elif request.method == 'DELETE': try: r.delete("filter:" + str(filter_id)) return JSONResponse('Filter has been deleted', status=status.HTTP_204_NO_CONTENT) except DataError: return JSONResponse("Error deleting filter", status=status.HTTP_408_REQUEST_TIMEOUT) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def instance_detail(request, instance_id): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': try: controller = r.hgetall('controller_instance:' + str(instance_id)) return JSONResponse(controller, status=status.HTTP_200_OK) except Exception: return JSONResponse("Error retrieving data", status=status.HTTP_400_BAD_REQUEST) elif request.method == 'PUT': data = JSONParser().parse(request) try: if 'status' in data and data['status'] == 'Running': instance_data = r.hgetall('controller_instance:' + str(instance_id)) controller_data = r.hgetall('controller:' + str(instance_data['controller'])) controller_name = controller_data['controller_name'].split('.')[0] class_name = controller_data['class_name'] parameters = instance_data['parameters'] start_controller_instance(instance_id, controller_name, class_name, parameters) else: stop_controller_instance(instance_id) r.hmset('controller_instance:' + str(instance_id), data) return JSONResponse("Data updated", status=status.HTTP_201_CREATED) except DataError: return JSONResponse("Error updating data", status=status.HTTP_400_BAD_REQUEST) except Exception as e: return JSONResponse("Error starting the controller: "+str(e), status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': try: controller_id = 'controller:' + r.hgetall('controller_instance:' + instance_id)['controller'] r.hincrby(controller_id, 'instances', -1) r.delete("controller_instance:" + str(instance_id)) except: return JSONResponse("Error deleting controller", status=status.HTTP_400_BAD_REQUEST) # If this is the last controller, the counter is reset keys = r.keys('controller_instance:*') if not keys: r.delete('controller_instances:id') return JSONResponse('Instance has been deleted', status=status.HTTP_204_NO_CONTENT) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def region_detail(request, region_id): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) region_key = 'region:' + str(region_id) if request.method == 'GET': if r.exists(region_key): region = r.hgetall(region_key) return JSONResponse(region, status=status.HTTP_200_OK) else: return JSONResponse('Region not found.', status=status.HTTP_404_NOT_FOUND) if request.method == 'DELETE': # Deletes the key. If the node is alive, the metric middleware will recreate this key again. if r.exists(region_key): keys = r.keys("zone:*") if 'zone:id' in keys: keys.remove('zone:id') for key in keys: zone = r.hgetall(key) if zone['region'] == region_id: return JSONResponse( "Region couldn't be deleted because the zone with id: " + region_id + ' has this region assigned.', status=status.HTTP_400_BAD_REQUEST) r.delete(region_key) return JSONResponse('Region has been deleted', status=status.HTTP_204_NO_CONTENT) else: return JSONResponse('Region not found.', status=status.HTTP_404_NOT_FOUND) if request.method == 'PUT': data = JSONParser().parse(request) try: r.hmset(region_key, data) return JSONResponse("Data updated correctly", status=status.HTTP_204_NO_CONTENT) except RedisError: return JSONResponse("Error updating data", status=status.HTTP_400_BAD_REQUEST) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def dependency_list_deployed(request, project_id): if request.method == 'GET': try: r = get_redis_connection() except RedisError: return JSONResponse('Problems to connect with the DB', status=500) result = r.lrange(str(project_id) + ":dependencies", 0, -1) if result: return JSONResponse(result, status=200) else: return JSONResponse('Any Storlet deployed', status=404) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=405)
def object_type_items_detail(request, object_type_name, item_name): """ Delete an extension from an object type definition. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=500) if request.method == 'DELETE': r.lrem("object_type:" + str(object_type_name), str(item_name), 1) return JSONResponse('Extension ' + str(item_name) + ' has been deleted from object type ' + str(object_type_name), status=204) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=405)
def delete_storage_policy_disks(request, storage_policy_id, disk_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 == 'DELETE': if r.exists(key): try: tmp_policy_file = get_policy_file_path( settings.SWIFT_CFG_TMP_DIR, storage_policy_id) found = False storage_policy = r.hgetall(key) storage_policy['devices'] = json.loads( storage_policy['devices']) for i, disk in enumerate(storage_policy['devices']): if disk_id == disk[0]: found = True ring = RingBuilder.load(tmp_policy_file) ring.remove_dev(disk[1]) ring.save(tmp_policy_file) del storage_policy['devices'][i] storage_policy['devices'] = json.dumps( storage_policy['devices']) r.hset(key, 'devices', storage_policy['devices']) r.hset(key, 'deployed', False) return JSONResponse("Disk removed", status=status.HTTP_204_NO_CONTENT) if not found: return JSONResponse('Disk not found', status=status.HTTP_404_NOT_FOUND) except RedisError: return JSONResponse("Error updating storage policy", status=status.HTTP_400_BAD_REQUEST) else: return JSONResponse('Storage policy not found.', status=status.HTTP_404_NOT_FOUND) return JSONResponse('Method not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def dynamic_policy_detail(request, policy_id): """ Delete a dynamic policy. """ http_host = request.META['HTTP_HOST'] try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=500) key = 'policy:' + str(policy_id) if request.method == 'PUT': data = JSONParser().parse(request) try: if data['status'] == 'Stopped': policy_id = int(policy_id) if policy_id in rule_actors: rule_actors[policy_id].stop_actor() del rule_actors[policy_id] else: policy_data = r.hgetall(key) try: start_dynamic_policy_actor(policy_data, http_host) except Exception as e: return JSONResponse(str(e), status=400) data['object_name'] = ', '.join(r.lrange('object_type:' + data['object_type'], 0, -1)) r.hmset(key, data) return JSONResponse("Data updated", status=201) except DataError: return JSONResponse("Error updating data", status=400) elif request.method == 'DELETE': try: policy_id = int(policy_id) if policy_id in rule_actors: rule_actors[policy_id].stop_actor() del rule_actors[policy_id] except: logger.info("Error stopping the rule actor: "+str(policy_id)) r.delete(key) policies_ids = r.keys('policy:*') pipelines_ids = r.keys('pipeline:*') if len(policies_ids) == 0 and len(pipelines_ids) == 0: r.set('policies:id', 0) return JSONResponse('Policy has been deleted', status=204) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=405)
def put(self, request, controller_id): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=500) try: file_obj = request.FILES['file'] make_sure_path_exists(settings.CONTROLLERS_DIR) path = save_file(file_obj, settings.CONTROLLERS_DIR) r.hmset('controller:' + str(controller_id), {'controller_name': os.path.basename(path)}) return JSONResponse("Data updated", status=status.HTTP_201_CREATED) except DataError: return JSONResponse("Error updating data", status=status.HTTP_400_BAD_REQUEST) except ValueError: return JSONResponse("Error starting controller", status=status.HTTP_400_BAD_REQUEST)
def create_instance(request): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'POST': data = JSONParser().parse(request) try: r.hincrby('controller:' + data['controller'], 'instances', 1) r.hmset('controller_instance:' + str(r.incr('controller_instances:id')), data) return JSONResponse("Instance created", status=status.HTTP_201_CREATED) except Exception: return JSONResponse("Error creating instance", status=status.HTTP_400_BAD_REQUEST) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def controller_detail(request, controller_id): """ Retrieve, update or delete a global controller. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': controller = r.hgetall('controller:' + str(controller_id)) # to_json_bools(controller, 'enabled') return JSONResponse(controller, status=status.HTTP_200_OK) elif request.method == 'PUT': try: data = JSONParser().parse(request) except ParseError: return JSONResponse("Invalid format or empty request", status=status.HTTP_400_BAD_REQUEST) try: r.hmset('controller:' + str(controller_id), data) return JSONResponse("Data updated", status=status.HTTP_200_OK) except DataError: return JSONResponse("Error updating data", status=status.HTTP_408_REQUEST_TIMEOUT) elif request.method == 'DELETE': try: controller = r.hgetall('controller:' + str(controller_id)) if controller['instances'] not in ['0', 0]: return JSONResponse("Controller could not be deleted because it has one or more instances.", status=status.HTTP_400_BAD_REQUEST) else: delete_file(controller['controller_name'], settings.CONTROLLERS_DIR) r.delete("controller:" + str(controller_id)) except: return JSONResponse("Error deleting controller", status=status.HTTP_400_BAD_REQUEST) # If this is the last controller, the counter is reset keys = r.keys('controller:*') if not keys: r.delete('controllers:id') return JSONResponse('Controller has been deleted', status=status.HTTP_204_NO_CONTENT) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def projects_groups_detail(request, group_id, project_id): """ Delete a member from a tenants group. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'DELETE': key = 'project_group:' + str(group_id) group = r.hgetall(key) attached_projects = json.loads(group['attached_projects']) attached_projects.remove(str(project_id)) group['attached_projects'] = json.dumps(attached_projects) r.hmset(key, group) return JSONResponse('Tenant ' + str(project_id) + ' has been deleted from group with the id: ' + str(group_id), status=status.HTTP_204_NO_CONTENT) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def controller_list(request): """ List all global controllers. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': keys = r.keys('controller:*') controller_list = [] for key in keys: controller = r.hgetall(key) # to_json_bools(controller, 'enabled') controller_list.append(controller) return JSONResponse(controller_list, status=status.HTTP_200_OK) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def put(self, request, controller_id): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=500) try: file_obj = request.FILES['file'] make_sure_path_exists(settings.CONTROLLERS_DIR) path = save_file(file_obj, settings.CONTROLLERS_DIR) r.hmset('controller:' + str(controller_id), {'controller_name': os.path.basename(path)}) return JSONResponse("Data updated", status=status.HTTP_201_CREATED) except DataError: return JSONResponse("Error updating data", status=status.HTTP_400_BAD_REQUEST) except ValueError: return JSONResponse("Error starting controller", status=status.HTTP_400_BAD_REQUEST)
def region_detail(request, region_id): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) region_key = 'region:' + str(region_id) if request.method == 'GET': if r.exists(region_key): region = r.hgetall(region_key) return JSONResponse(region, status=status.HTTP_200_OK) else: return JSONResponse('Region not found.', status=status.HTTP_404_NOT_FOUND) if request.method == 'DELETE': # Deletes the key. If the node is alive, the metric middleware will recreate this key again. if r.exists(region_key): keys = r.keys("zone:*") if 'zone:id' in keys: keys.remove('zone:id') for key in keys: zone = r.hgetall(key) if zone['region'] == region_id: return JSONResponse("Region couldn't be deleted because the zone with id: " + region_id + ' has this region assigned.', status=status.HTTP_400_BAD_REQUEST) r.delete(region_key) return JSONResponse('Region has been deleted', status=status.HTTP_204_NO_CONTENT) else: return JSONResponse('Region not found.', status=status.HTTP_404_NOT_FOUND) if request.method == 'PUT': data = JSONParser().parse(request) try: r.hmset(region_key, data) return JSONResponse("Data updated correctly", status=status.HTTP_204_NO_CONTENT) except RedisError: return JSONResponse("Error updating data", status=status.HTTP_400_BAD_REQUEST) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def node_list(request): """ GET: List all nodes ordered by name """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': keys = r.keys("*_node:*") nodes = [] for key in keys: node = r.hgetall(key) node.pop("ssh_username", None) # username & password are not returned in the list node.pop("ssh_password", None) node['devices'] = json.loads(node['devices']) r_id = node['region_id'] z_id = node['zone_id'] if r.exists('region:' + r_id): node['region_name'] = r.hgetall('region:' + r_id)['name'] else: node['region_name'] = r_id if r.exists('zone:' + z_id): node['zone_name'] = r.hgetall('zone:' + z_id)['name'] else: node['zone_name'] = z_id if 'ssh_access' not in node: node['ssh_access'] = False nodes.append(node) sorted_list = sorted(nodes, key=itemgetter('name')) return JSONResponse(sorted_list, status=status.HTTP_200_OK) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def projects_group_detail(request, group_id): """ Get, update or delete a projects group from the registry. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': key = 'project_group:' + str(group_id) if r.exists(key): group = r.hgetall(key) group['attached_projects'] = json.loads(group['attached_projects']) return JSONResponse(group, status=status.HTTP_200_OK) else: return JSONResponse('The tenant group with id: ' + str(group_id) + ' does not exist.', status=status.HTTP_404_NOT_FOUND) if request.method == 'PUT': key = 'project_group:' + str(group_id) if r.exists(key): data = JSONParser().parse(request) try: r.hmset(key, data) return JSONResponse('The members of the tenants group with id: ' + str(group_id) + ' has been updated', status=status.HTTP_201_CREATED) except: return JSONResponse('Error storing the tenant group in the DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: return JSONResponse('The tenant group with id: ' + str(group_id) + ' does not exist.', status=status.HTTP_404_NOT_FOUND) if request.method == 'DELETE': key = 'project_group:' + str(group_id) if r.exists(key): r.delete("project_group:" + str(group_id)) gtenants_ids = r.keys('project_group:*') if len(gtenants_ids) == 0: r.set('project_groups:id', 0) return JSONResponse('Tenants group has been deleted', status=status.HTTP_204_NO_CONTENT) else: return JSONResponse('The tenant group with id: ' + str(group_id) + ' does not exist.', status=status.HTTP_404_NOT_FOUND) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def metric_module_list(request): """ List all metric modules """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': keys = r.keys("workload_metric:*") workload_metrics = [] for key in keys: metric = r.hgetall(key) to_json_bools(metric, 'put', 'get', 'replicate') workload_metrics.append(metric) sorted_workload_metrics = sorted(workload_metrics, key=lambda x: int(itemgetter('id')(x))) return JSONResponse(sorted_workload_metrics, status=status.HTTP_200_OK) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def instances_list(request): """ List all global controllers. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': keys = r.keys('controller_instance:*') controller_list = [] for key in keys: controller = r.hgetall(key) controller['id'] = key.split(':')[1] controller['controller'] = r.hgetall('controller:' + controller['controller'])['controller_name'] controller_list.append(controller) return JSONResponse(controller_list, status=status.HTTP_200_OK) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def delete_storage_policy_disks(request, storage_policy_id, disk_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 == 'DELETE': if r.exists(key): try: tmp_policy_file = get_policy_file_path(settings.SWIFT_CFG_TMP_DIR, storage_policy_id) found = False storage_policy = r.hgetall(key) storage_policy['devices'] = json.loads(storage_policy['devices']) for i, disk in enumerate(storage_policy['devices']): if disk_id == disk[0]: found = True ring = RingBuilder.load(tmp_policy_file) ring.remove_dev(disk[1]) ring.save(tmp_policy_file) del storage_policy['devices'][i] storage_policy['devices'] = json.dumps(storage_policy['devices']) r.hset(key, 'devices', storage_policy['devices']) r.hset(key, 'deployed', False) return JSONResponse("Disk removed", status=status.HTTP_204_NO_CONTENT) if not found: return JSONResponse('Disk not found', status=status.HTTP_404_NOT_FOUND) except RedisError: return JSONResponse("Error updating storage policy", status=status.HTTP_400_BAD_REQUEST) else: return JSONResponse('Storage policy not found.', status=status.HTTP_404_NOT_FOUND) return JSONResponse('Method not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def object_type_detail(request, object_type_name): """ GET: List extensions allowed about an object type word registered. PUT: Update the object type word registered. DELETE: Delete the object type word registered. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) key = "object_type:" + object_type_name if request.method == 'GET': if r.exists(key): types_list = r.lrange(key, 0, -1) object_type = {"name": object_type_name, "types_list": types_list} return JSONResponse(object_type, status=status.HTTP_200_OK) return JSONResponse("Object type not found", status=status.HTTP_404_NOT_FOUND) if request.method == "PUT": if not r.exists(key): return JSONResponse('The object type with name: ' + object_type_name + ' does not exist.', status=status.HTTP_404_NOT_FOUND) data = JSONParser().parse(request) if not data: return JSONResponse('Object type must have a types_list defining the valid object types', status=status.HTTP_400_BAD_REQUEST) pipe = r.pipeline() # the following commands are buffered in a single atomic request (to replace current contents) if pipe.delete(key).rpush(key, *data).execute(): return JSONResponse('The object type ' + str(object_type_name) + ' has been updated', status=status.HTTP_201_CREATED) return JSONResponse('Error storing the object type in the DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == "DELETE": if r.exists(key): object_type = r.delete(key) return JSONResponse(object_type, status=status.HTTP_200_OK) return JSONResponse("Object type not found", status=status.HTTP_404_NOT_FOUND) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def deploy_storage_policy(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 == "POST": if r.exists(key): try: tmp_policy_file = get_policy_file_path(settings.SWIFT_CFG_TMP_DIR, storage_policy_id) deploy_policy_file = get_policy_file_path(settings.SWIFT_CFG_DEPLOY_DIR, storage_policy_id) deploy_gzip_filename = deploy_policy_file.replace('builder', 'ring.gz') ring = RingBuilder.load(tmp_policy_file) ring.rebalance() ring.save(tmp_policy_file) ringdata = ring.get_ring() ringdata.save(deploy_gzip_filename) data = r.hgetall(key) update_sp_files(settings.SWIFT_CFG_DEPLOY_DIR, storage_policy_id, {'name': data['name'], 'deprecated': data['deprecated'], 'default': data['default']}) copyfile(tmp_policy_file, deploy_policy_file) rsync_dir_with_nodes(settings.SWIFT_CFG_DEPLOY_DIR, '/etc/swift') r.hset(key, 'deployed', 'True') return JSONResponse('Storage policy deployed correctly', status=status.HTTP_200_OK) except RedisError: return JSONResponse('Storage policy could not be deployed', status=status.HTTP_400_BAD_REQUEST) except exceptions.RingBuilderError, e: return JSONResponse('Storage policy could not be deployed. Error message: %s' % e.message, status=status.HTTP_400_BAD_REQUEST) else: return JSONResponse('Storage policy not found.', status=status.HTTP_404_NOT_FOUND)
def node_list(request): """ GET: List all nodes ordered by name """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': keys = r.keys("*_node:*") nodes = [] for key in keys: node = r.hgetall(key) node.pop("ssh_username", None) # username & password are not returned in the list node.pop("ssh_password", None) node['devices'] = json.loads(node['devices']) r_id = node['region_id'] z_id = node['zone_id'] if r.exists('region:' + r_id): node['region_name'] = r.hgetall('region:' + r_id)['name'] else: node['region_name'] = r_id if r.exists('zone:' + z_id): node['zone_name'] = r.hgetall('zone:' + z_id)['name'] else: node['zone_name'] = z_id if 'ssh_access' not in node: node['ssh_access'] = False nodes.append(node) sorted_list = sorted(nodes, key=itemgetter('name')) return JSONResponse(sorted_list, status=status.HTTP_200_OK) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def post(self, request): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=500) data = json.loads(request.POST['metadata']) # json data is in metadata parameter for this request if not data: return JSONResponse("Invalid format or empty request", status=status.HTTP_400_BAD_REQUEST) workload_metric_id = r.incr("workload_metrics:id") try: data['id'] = workload_metric_id file_obj = request.FILES['file'] make_sure_path_exists(settings.WORKLOAD_METRICS_DIR) path = save_file(file_obj, settings.WORKLOAD_METRICS_DIR) data['metric_name'] = os.path.basename(path) # synchronize metrics directory with all nodes try: rsync_dir_with_nodes(settings.WORKLOAD_METRICS_DIR, settings.WORKLOAD_METRICS_DIR) except FileSynchronizationException as e: # print "FileSynchronizationException", e # TODO remove return JSONResponse(e.message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) r.hmset('workload_metric:' + str(workload_metric_id), data) return JSONResponse(data, status=status.HTTP_201_CREATED) except DataError: return JSONResponse("Error to save the object", status=status.HTTP_400_BAD_REQUEST) except Exception as e: print e logger.error(str(e)) return JSONResponse("Error uploading file", status=status.HTTP_400_BAD_REQUEST)