def add_metric(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=500) 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=200) if request.method == 'POST': data = JSONParser().parse(request) name = data.pop("name", None) if not name: return JSONResponse('Metric must have a name', status=400) r.hmset('metric:' + str(name), data) return JSONResponse('Metric has been added in the registry', status=201) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=405)
def node_restart(request, node_id): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) logger.debug('Node id: ' + str(node_id)) if request.method == 'PUT': node = r.hgetall('node:' + str(node_id)) logger.debug('Node data: ' + str(node)) data = { 'node_ip': node['ip'], 'ssh_username': node['ssh_username'], 'ssh_password': node['ssh_password'] } restart_command = 'sshpass -p {ssh_password} ssh {ssh_username}@{node_ip} sudo swift-init main restart'.format( **data) logger.debug('Command: ' + str(restart_command)) ret = os.system(restart_command) if ret != 0: logger.error('An error occurred restarting Swift nodes') raise FileSynchronizationException( "An error occurred restarting Swift nodes") logger.debug('Node ' + str(node_id) + ' 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 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']) 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 dynamic_policy_detail(request, policy_id): """ Delete a dynamic policy. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=500) if request.method == 'DELETE': create_local_host() try: rule_actors[int(policy_id)].stop_actor() del rule_actors[int(policy_id)] except Exception as e: logger.error(str(e)) print e r.delete('policy:' + policy_id) policies_ids = r.keys('policy:*') if len(policies_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 add_tenants_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("G:*") gtenants = {} for key in keys: gtenant = r.lrange(key, 0, -1) gtenant_id = key.split(":")[1] gtenants[gtenant_id] = gtenant # gtenants.extend(eval(gtenant[0])) return JSONResponse(gtenants, 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("gtenant:id") r.rpush('G:' + 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_storage_node(request): """ Add a storage node or list all the storage nodes saved in the registry. :param request: :return: JSONResponse """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=500) if request.method == "GET": keys = r.keys("SN:*") storage_nodes = [] for k in keys: sn = r.hgetall(k) sn["id"] = k.split(":")[1] storage_nodes.append(sn) sorted_list = sorted(storage_nodes, key=itemgetter('name')) return JSONResponse(sorted_list, status=200) if request.method == "POST": sn_id = r.incr("storage_nodes:id") data = JSONParser().parse(request) r.hmset('SN:' + str(sn_id), data) return JSONResponse('Storage node has been added to the registry', status=201) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=405)
def add_dynamic_filter(request): """ Add a filter with its default parameters in the registry (redis). List all the dynamic filters registered. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=500) if request.method == 'GET': keys = r.keys("dsl_filter:*") dynamic_filters = [] for key in keys: dynamic_filter = r.hgetall(key) dynamic_filter["name"] = key.split(":")[1] dynamic_filters.append(dynamic_filter) return JSONResponse(dynamic_filters, status=200) if request.method == 'POST': data = JSONParser().parse(request) name = data.pop("name", None) if not name: return JSONResponse('Filter must have a name', status=400) r.hmset('dsl_filter:' + str(name), data) return JSONResponse('Filter has been added to the registy', status=201) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=405)
def tenants_list(request): """ List swift tenants. """ token = get_token_connection(request) if request.method == 'GET': r = requests.get(settings.KEYSTONE_URL + "/tenants", headers={'X-Auth-Token': token}) return HttpResponse(r.content, content_type='application/json', status=r.status_code) if request.method == "POST": data = JSONParser().parse(request) try: sds_project.add_new_sds_project(data["tenant_name"]) except Exception: return JSONResponse('Error creating a new project.', status=status.HTTP_500_INTERNAL_SERVER_ERROR) return JSONResponse('Account created successfully', status=status.HTTP_201_CREATED) return JSONResponse('Only HTTP GET /tenants/ requests 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 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 tenants_group_detail(request, gtenant_id): """ Get, update or delete a tenants 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 = 'G:' + str(gtenant_id) if r.exists(key): gtenant = r.lrange(key, 0, -1) return JSONResponse(gtenant, status=status.HTTP_200_OK) else: return JSONResponse('The tenant group with id: ' + str(gtenant_id) + ' does not exist.', status=status.HTTP_404_NOT_FOUND) if request.method == 'PUT': key = 'G:' + str(gtenant_id) if r.exists(key): data = JSONParser().parse(request) if not data: return JSONResponse('Tenant group cannot be empty', 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 members of the tenants group with id: ' + str(gtenant_id) + ' has been updated', status=status.HTTP_201_CREATED) 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(gtenant_id) + ' does not exist.', status=status.HTTP_404_NOT_FOUND) if request.method == 'DELETE': key = 'G:' + str(gtenant_id) if r.exists(key): r.delete("G:" + str(gtenant_id)) return JSONResponse('Tenants group has been deleted', status=status.HTTP_204_NO_CONTENT) else: return JSONResponse('The tenant group with id: ' + str(gtenant_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 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 dependency_list_deployed(request, account): if request.method == 'GET': try: r = get_redis_connection() except RedisError: return JSONResponse('Problems to connect with the DB', status=500) result = r.lrange("AUTH_" + str(account) + ":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 load_policies(): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=500) dynamic_policies = r.keys("policy:*") if dynamic_policies: logger.info("Starting dynamic rules stored in redis") host = create_local_host() for policy in dynamic_policies: policy_data = r.hgetall(policy) if policy_data['alive'] == 'True': _, rule_parsed = dsl_parser.parse( policy_data['policy_description']) target = rule_parsed.target[0][1] # Tenant ID or tenant+container for action_info in rule_parsed.action_list: if action_info.transient: logger.info("Transient rule: " + policy_data['policy_description']) rule_actors[policy] = host.spawn_id( str(policy), settings.RULE_TRANSIENT_MODULE, settings.RULE_TRANSIENT_CLASS, [rule_parsed, action_info, target, host]) rule_actors[policy].start_rule() else: logger.info("Rule: " + policy_data['policy_description']) rule_actors[policy] = host.spawn_id( str(policy), settings.RULE_MODULE, settings.RULE_CLASS, [rule_parsed, action_info, target, host]) rule_actors[policy].start_rule()
def get(self, request, metric_module_id): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if r.exists('workload_metric:' + str(metric_module_id)): workload_metric_path = os.path.join( settings.WORKLOAD_METRICS_DIR, str( r.hget('workload_metric:' + str(metric_module_id), 'metric_name'))) if os.path.exists(workload_metric_path): workload_metric_name = os.path.basename(workload_metric_path) workload_metric_size = os.stat(workload_metric_path).st_size # Generate response response = StreamingHttpResponse( FileWrapper(open(workload_metric_path), workload_metric_size), content_type=mimetypes.guess_type(workload_metric_path)[0]) response['Content-Length'] = workload_metric_size response[ 'Content-Disposition'] = "attachment; filename=%s" % workload_metric_name return response else: return HttpResponse(status=status.HTTP_404_NOT_FOUND) else: return HttpResponse(status=status.HTTP_404_NOT_FOUND)
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.GLOBAL_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 get(self, request, storlet_id, format=None): try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if r.exists('filter:' + str(storlet_id)): filter_path = r.hget('filter:' + str(storlet_id), 'path') if os.path.exists(filter_path): filter_name = os.path.basename(filter_path) filter_size = os.stat(filter_path).st_size # Generate response response = StreamingHttpResponse( FileWrapper(open(filter_path), filter_size), content_type=mimetypes.guess_type(filter_path)[0]) response['Content-Length'] = filter_size response[ 'Content-Disposition'] = "attachment; filename=%s" % filter_name return response else: return HttpResponse(status=status.HTTP_404_NOT_FOUND) else: return HttpResponse(status=status.HTTP_404_NOT_FOUND)
def gtenants_tenant_detail(request, gtenant_id, tenant_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': r.lrem("G:" + str(gtenant_id), str(tenant_id), 1) return JSONResponse('Tenant ' + str(tenant_id) + ' has been deleted from group with the id: ' + str(gtenant_id), 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, node_id): """ GET: Retrieve node details. PUT: Update node. :param request: :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 = "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: r.hmset(key, data) return JSONResponse("Data updated", status=status.HTTP_201_CREATED) except RedisError: return JSONResponse("Error updating 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): node = 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 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 sort_detail(request, sort_id): """ Retrieve, update or delete a Proxy Sorting. """ try: r = get_redis_connection() except RedisError: return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) if request.method == 'GET': proxy_sorting = r.hgetall("proxy_sorting:" + str(sort_id)) return JSONResponse(proxy_sorting, status=status.HTTP_200_OK) elif request.method == 'PUT': try: data = JSONParser().parse(request) r.hmset('proxy_sorting:' + str(sort_id), data) return JSONResponse("Data updated", status=status.HTTP_201_CREATED) except redis.exceptions.DataError: return JSONResponse("Error updating data", status=status.HTTP_400_BAD_REQUEST) except ParseError: return JSONResponse("Invalid format or empty request", status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': r.delete("proxy_sorting:" + str(sort_id)) return JSONResponse('Proxy sorting 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 filter_undeploy(request, filter_id, account, container=None, swift_object=None): """ Undeploy a filter from a specific swift account. """ token = get_token_connection(request) 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 not r.exists("AUTH_" + str(account) + ":" + str(filter_data["filter_name"])): return JSONResponse('Filter ' + str(filter_data["filter_name"]) + ' has not been deployed already', status=status.HTTP_404_NOT_FOUND) if container and swift_object: target = account + "/" + container + "/" + swift_object elif container: target = account + "/" + container else: target = account # print target return unset_filter(r, target, filter_data, token) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def process_request(request): # Example of the django logging # logger.info('Remote address: ' + str(request.META['REMOTE_ADDR'])) # logger.info('User agent: ' + str(request.META['HTTP_USER_AGENT'])) # logger.info('X-Auth-Token: ' + str(request.META['HTTP_X_AUTH_TOKEN'])) if 'HTTP_X_AUTH_TOKEN' in request.META: token = request.META['HTTP_X_AUTH_TOKEN'] else: return JSONResponse('You must be authenticated as admin.', status=status.HTTP_401_UNAUTHORIZED) is_admin = False now = datetime.utcnow() if token not in valid_tokens: keystone = get_keystone_admin_auth() try: token_data = keystone.tokens.validate(token) except exceptions.base.ClientException: return JSONResponse('You must be authenticated as admin.', status=status.HTTP_401_UNAUTHORIZED) token_expiration = datetime.strptime(token_data.expires, '%Y-%m-%dT%H:%M:%SZ') token_roles = token_data.user['roles'] for role in token_roles: if role['name'] == 'admin': is_admin = True if token_expiration > now and is_admin: valid_tokens[token] = token_expiration return None else: token_expiration = valid_tokens[token] if token_expiration > now: return None else: valid_tokens.pop(token, None) return JSONResponse('You must be authenticated as admin.', status=status.HTTP_401_UNAUTHORIZED)
def do_action(request, r, rule_parsed): token = get_token_connection(request) for target in rule_parsed.target: for action_info in rule_parsed.action_list: logger.info("TARGET RULE: " + action_info) dynamic_filter = r.hgetall("dsl_filter:" + str(action_info.filter)) filter_data = r.hgetall("filter:" + dynamic_filter["identifier"]) if not filter_data: return JSONResponse("Filter does not exist", status=status.HTTP_404_NOT_FOUND) if action_info.action == "SET": # Get an identifier of this new policy policy_id = r.incr("policies:id") # Set the policy data policy_data = { "policy_id": policy_id, "object_type": "", "object_size": "", "execution_order": policy_id, "params": "", "callable": False } # Rewrite default values if rule_parsed.object_list: if rule_parsed.object_list.object_type: policy_data[ "object_type"] = rule_parsed.object_list.object_type.object_value if rule_parsed.object_list.object_size: policy_data["object_size"] = [ rule_parsed.object_list.object_size.operand, rule_parsed.object_list.object_size.object_value ] if action_info.server_execution: policy_data[ "execution_server"] = action_info.server_execution if action_info.params: policy_data["params"] = action_info.params if action_info.callable: policy_data["callable"] = True # Deploy (an exception is raised if something goes wrong) set_filter(r, target[1], filter_data, policy_data, token) elif action_info.action == "DELETE": undeploy_response = unset_filter(r, target[1], filter_data, token) if undeploy_response != status.HTTP_204_NO_CONTENT: return undeploy_response
def storage_policy_list(request): """ List all storage policies. """ 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("storage-policy:*") storage_policy_list = [] for key in keys: storage_policy = r.hgetall(key) storage_policy['id'] = str(key).split(':')[-1] storage_policy_list.append(storage_policy) return JSONResponse(storage_policy_list, status=status.HTTP_200_OK) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def dependency_deploy(request, dependency_id, account): token = get_token_connection(request) 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) metadata = { 'X-Object-Meta-Storlet-Dependency-Version': str(dependency["version"]) } if "path" not in dependency.keys(): return JSONResponse('Dependency path does not exist', status=404) try: dependency_file = open(dependency["path"], 'r') content_length = None response = dict() url = settings.SWIFT_URL + settings.SWIFT_API_VERSION + "/AUTH_" + str( account) swift_client.put_object(url, token, 'dependency', dependency["name"], dependency_file, content_length, None, None, "application/octet-stream", metadata, None, None, None, response) except ClientException: return JSONResponse(response.get("reason"), status=response.get('status')) finally: dependency_file.close() status = response.get('status') if status == 201: if r.exists("AUTH_" + str(account) + ":dependency:" + str(dependency['name'])): return JSONResponse("Already deployed", status=200) if r.lpush("AUTH_" + str(account) + ":dependencies", str(dependency['name'])): return JSONResponse("Deployed", status=201) return JSONResponse("error", status=400) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=405)
def global_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 storlet_list_deployed(request, account): """ List all the storlets deployed. """ if request.method == 'GET': try: r = get_redis_connection() except RedisError: return JSONResponse('Problems to connect with the DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR) result = r.lrange("AUTH_" + str(account), 0, -1) if result: return JSONResponse(result, status=status.HTTP_200_OK) else: return JSONResponse('Any Storlet deployed', status=status.HTTP_404_NOT_FOUND) 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 storlet_list(request): """ List all storlets, or create a new storlet. """ 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("filter:*") storlets = [] for key in keys: storlet = r.hgetall(key) storlets.append(storlet) sorted_list = sorted(storlets, key=lambda x: int(itemgetter('id')(x))) return JSONResponse(sorted_list, status=status.HTTP_200_OK) if request.method == 'POST': try: data = JSONParser().parse(request) except ParseError: return JSONResponse("Invalid format or empty request", status=status.HTTP_400_BAD_REQUEST) if (('filter_type' not in data) or ((data['filter_type'] == 'storlet' or data['filter_type'] == 'native') and not check_keys(data.keys(), FILTER_KEYS[2:-1])) or ((data['filter_type'] == 'global') and not check_keys(data.keys(), GLOBAL_FILTER_KEYS[2:-1]))): return JSONResponse("Invalid parameters in request", status=status.HTTP_400_BAD_REQUEST) storlet_id = r.incr("filters:id") try: data['id'] = storlet_id r.hmset('filter:' + str(storlet_id), data) if data['filter_type'] == 'global': if data['enabled'] is True or data[ 'enabled'] == 'True' or data['enabled'] == 'true': to_json_bools(data, 'has_reverse', 'is_pre_get', 'is_post_get', 'is_pre_put', 'is_post_put', 'enabled') r.hset("global_filters", str(storlet_id), json.dumps(data)) return JSONResponse(data, status=status.HTTP_201_CREATED) except DataError: return JSONResponse("Error to save the object", status=status.HTTP_400_BAD_REQUEST) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)