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 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 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 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 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 project_groups_list(request, project_id): if request.method == 'GET': keystone_client = keystone_client = get_keystone_admin_auth() groups = keystone_client.groups.list() roles = keystone_client.roles.list() for role in roles: if role.name == 'user': user_role_id = role.id break role_assignments = keystone_client.role_assignments.list() valid_groups = list() for ra in role_assignments: if hasattr(ra, 'group') and 'project' in ra.scope and ra.scope['project']['id'] == project_id \ and ra.role['id'] == user_role_id: valid_groups.append(ra.group['id']) groups_list = list() for group in groups: if group.id in valid_groups: group_data = {} group_data['id'] = group.id group_data['name'] = group.name groups_list.append(group_data) return JSONResponse(groups_list, status=status.HTTP_200_OK) return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
def project_users_list(request, project_id): if request.method == 'GET': keystone_client = get_keystone_admin_auth() users = keystone_client.users.list() roles = keystone_client.roles.list() for role in roles: if role.name == 'user': user_role_id = role.id break role_assignments = keystone_client.role_assignments.list() valid_users = list() for ra in role_assignments: if hasattr(ra, 'user') and 'project' in ra.scope and ra.scope['project']['id'] == project_id \ and ra.role['id'] == user_role_id: valid_users.append(ra.user['id']) user_list = list() for user in users: if user.id in valid_users: user_data = {} user_data['id'] = user.id user_data['name'] = user.name user_list.append(user_data) return JSONResponse(user_list, status=status.HTTP_200_OK) 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 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 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, 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 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 containers_list(request, project_id): if request.method == 'GET': token = get_token_connection(request) url = settings.SWIFT_URL + "/AUTH_" + project_id _, containers = swift_client.get_account(url, token) for c_id in reversed(range(len(containers))): if containers[c_id]['name'] in ('.dependency', '.storlet'): del containers[c_id] return JSONResponse(containers, status=status.HTTP_200_OK) return JSONResponse('Method ' + str(request.method) + ' not 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 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 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 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 create_container(request, project_id, container_name): if request.method == 'POST': try: headers = JSONParser().parse(request) token = get_token_connection(request) url = settings.SWIFT_URL + "/AUTH_" + project_id swift_client.put_container(url, token, container_name, headers) except Exception as e: return JSONResponse(e.message, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return JSONResponse("Container Policy updated correctly", status=status.HTTP_201_CREATED) 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 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 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 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 update_container(request, project_id, container_name): if request.method == 'PUT': sp = JSONParser().parse(request) token = get_token_connection(request) url = settings.SWIFT_URL + "/AUTH_" + project_id headers, obj_list = swift_client.get_container(url, token, container_name) headers['X-Storage-Policy'] = sp path_container = settings.SWIFT_CFG_TMP_DIR + "/" + container_name os.mkdir(path_container) for obj in obj_list: file = open(path_container + "/" + obj["name"], "w") obj_headers, obj_body = swift_client.get_object( url, token, container_name, obj["name"]) file.write(obj_body) file.close() obj["headers"] = obj_headers swift_client.delete_object(url, token, container_name, obj["name"]) swift_client.delete_container(url, token, container_name) swift_client.put_container(url, token, container_name, headers) for obj in obj_list: obj_path = os.path.join(path_container, obj["name"]) obj_body = open(obj_path, "r") content_length = os.stat(obj_path).st_size swift_response = {} swift_client.put_object(url, token, container_name, obj["name"], obj_body, content_length, None, None, obj['content_type'], obj["headers"], None, None, None, swift_response) obj_body.close() os.remove(obj_path) os.rmdir(path_container) return JSONResponse("Container Policy updated correctly", status=status.HTTP_201_CREATED) return JSONResponse('Method ' + str(request.method) + ' not 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 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 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 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 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)