def get_users(ctx, id, email, user_access_token, admin_access_token, output_format): """Return user information. Requires the token of an administrator.""" try: response = _get_users(id, email, user_access_token, admin_access_token) headers = ['id', 'email', 'access_token'] data = [] for user in response: data.append((str(user.id_), user.email, user.access_token)) if output_format: tablib_data = tablib.Dataset() tablib_data.headers = headers for row in data: tablib_data.append(row) click.echo(tablib_data.export(output_format)) else: click_table_printer(headers, [], data) except Exception as e: logging.debug(traceback.format_exc()) logging.debug(str(e)) click.echo( click.style('User could not be retrieved: \n{}' .format(str(e)), fg='red'), err=True)
def list_users(ctx, id, email, user_access_token, admin_access_token, output_format): """List users according to the search criteria.""" try: response = _get_users(id, email, user_access_token, admin_access_token) headers = ["id", "email", "access_token", "access_token_status"] data = [] for user in response: data.append(( str(user.id_), user.email, str(user.access_token), str(user.access_token_status), )) if output_format: tablib_data = tablib.Dataset() tablib_data.headers = headers for row in data: tablib_data.append(row) click.echo(tablib_data.export(output_format)) else: click_table_printer(headers, [], data) except Exception as e: logging.debug(traceback.format_exc()) logging.debug(str(e)) click.echo( click.style("User could not be retrieved: \n{}".format(str(e)), fg="red"), err=True, )
def list_quota_usage(ctx, id, email, user_access_token, admin_access_token, output_format, human_readable): """List quota usage of users.""" try: response = _get_users(id, email, user_access_token, admin_access_token) headers = [ "id", "email", "cpu-used", "cpu-limit", "disk-used", "disk-limit" ] health_order = { QuotaHealth.healthy.name: 0, QuotaHealth.warning.name: 1, QuotaHealth.critical.name: 2, } data = [] colours = [] health = [] for user in response: quota_usage = user.get_quota_usage() disk, cpu = quota_usage.get("disk"), quota_usage.get("cpu") data.append(( str(user.id_), user.email, cpu.get("usage").get(human_readable), cpu.get("limit", {}).get(human_readable) or "-", disk.get("usage").get(human_readable), disk.get("limit", {}).get(human_readable) or "-", )) health_ordered = max( [ disk.get("health", QuotaHealth.healthy.name), cpu.get("health", QuotaHealth.healthy.name), ], key=lambda key: health_order[key], ) colours.append(REANA_RESOURCE_HEALTH_COLORS[health_ordered]) health.append(health_ordered) if data and colours and health: data, colours, _ = (list(t) for t in zip(*sorted( zip(data, colours, health), key=lambda t: health_order[t[2]], reverse=True, ))) if output_format: tablib_data = tablib.Dataset() tablib_data.headers = headers for row in data: tablib_data.append(row) click.echo(tablib_data.export(output_format)) else: click_table_printer(headers, [], data, colours) except Exception as e: logging.debug(traceback.format_exc()) logging.debug(str(e)) click.echo( click.style("User could not be retrieved: \n{}".format(str(e)), fg="red"), err=True, )
def get_user(): # noqa r"""Endpoint to get user information from the server. --- get: summary: Get user information. Requires the admin api key. description: >- Get user information. operationId: get_user produces: - application/json parameters: - name: email in: query description: Not required. The email of the user. required: false type: string - name: id_ in: query description: Not required. UUID of the user. required: false type: string - name: user_token in: query description: Not required. API key of the admin. required: false type: string - name: access_token in: query description: Required. API key of the admin. required: true type: string responses: 200: description: >- Users matching criteria were found. Returns all stored user information. schema: type: array items: type: object properties: id_: type: string email: type: string access_token: type: string examples: application/json: [ { "id": "00000000-0000-0000-0000-000000000000", "email": "*****@*****.**", "access_token": "Drmhze6EPcv0fN_81Bj-nA", }, { "id": "00000000-0000-0000-0000-000000000001", "email": "*****@*****.**", "access_token": "Drmhze6EPcv0fN_81Bj-nB", }, ] 403: description: >- Request failed. The incoming payload seems malformed. 404: description: >- Request failed. User does not exist. examples: application/json: { "message": "User 00000000-0000-0000-0000-000000000000 does not exist." } 500: description: >- Request failed. Internal server error. examples: application/json: { "message": "Error while querying." } """ try: user_id = request.args.get('id_') user_email = request.args.get('email') user_token = request.args.get('user_token') access_token = request.args.get('access_token') users = _get_users(user_id, user_email, user_token, access_token) if users: users_response = [] for user in users: user_response = dict(id_=user.id_, email=user.email, access_token=user.access_token) users_response.append(user_response) return jsonify(users_response), 200 else: return jsonify( {"message": "User {} does not exist.".format(user_id)}, 404) except ValueError: return jsonify({"message": "Action not permitted."}), 403 except Exception as e: logging.error(traceback.format_exc()) return jsonify({"message": str(e)}), 500