def list(uid=None): result = [] if uid: u = User.query.get(uid) if u is not None: user_data = { 'id': u.id, 'email': u.email, 'active': u.active, 'role': get_detailed_roles(u.roles) } result.append(user_data) else: users = User.query.all() for u in users: result.append({ 'id': u.id, 'email': u.email, 'active': u.active, 'role': get_detailed_roles(u.roles) }) result = Result(200, "SUCCESS", "SUCCESS", extra_fields={"results": result}) return result.http_response()
def api_login(): data = request.json if 'email' not in data or 'password' not in data: result = InvalidParameterResult( errors=["Both email and password are required"]) return result.http_response() r = requests.post(request.url_root + "/auth", data=json.dumps({ 'email': data['email'], 'password': data['password'] }), headers={'content-type': 'application/json'}) response = r.json() if response['meta']['code'] == 200: extra_fiends = { 'authentication_token': response['response']['user']['authentication_token'] } result = Result(200, "SUCCESS", "SUCCESS", extra_fields=extra_fiends) else: result = Result(response['meta']['code'], "FAILED", "FAILED", extra_fields=response['response']) return result.http_response()
def role(rid): """ Args: rid: Role id Returns: List of pgAdmin4 users roles or single role if rid is provided. """ result = [] if rid: r = Role.query.get(rid) if r is not None: result.append({'id': r.id, 'name': r.name}) else: roles = Role.query.all() for r in roles: result.append({'id': r.id, 'name': r.name}) result = Result(200, "SUCCESS", "SUCCESS", extra_fields={"results": result}) return result.http_response()
def update(uid): """ Args: uid: Returns: """ usr = User.query.get(uid) if not usr: return NotFoundResult(message="User with id {0} is not found".format( uid)).http_response() data = request.form if request.form else json.loads(request.data, encoding='utf-8') try: new_data = validate_user(data) if 'roles' in new_data: roles_obj = [] for r in new_data['roles']: roles_obj.append(Role.query.get(r)) new_data['roles'] = roles_obj except Exception as e: return InvalidParameterResult(errors=[str(e)]).http_response() try: for k, v in new_data.items(): setattr(usr, k, v) db.session.commit() res = { 'id': usr.id, 'email': usr.email, 'active': usr.active, 'role': get_detailed_roles(usr.roles) } result = Result(200, "SUCCESS", "SUCCESS", extra_fields={"results": [res]}) return result.http_response() except Exception as e: return ServerErrorResult(message=str(e)).http_response()
def create(): """ Returns: """ data = request.form if request.form else json.loads(request.data, encoding='utf-8') for f in ('email', 'role', 'active', 'newPassword', 'confirmPassword'): if f in data and data[f] != '': continue else: return InvalidParameterResult( errors=["Missing field: '{0}'".format(f)]).http_response() try: new_data = validate_user(data) if 'roles' in new_data: roles_obj = [] for r in new_data['roles']: roles_obj.append(Role.query.get(r)) new_data['roles'] = roles_obj except Exception as e: return InvalidParameterResult(errors=[str(e)]).http_response() try: usr = User(email=new_data['email'], roles=new_data['roles'], active=new_data['active'], password=new_data['password']) db.session.add(usr) db.session.commit() # Add default server group for new user. server_group = ServerGroup(user_id=usr.id, name="Servers") db.session.add(server_group) db.session.commit() except Exception as e: return ServerErrorResult(message=str(e)).http_response() res = { 'id': usr.id, 'email': usr.email, 'active': usr.active, 'role': get_detailed_roles(usr.roles) } result = Result(200, "SUCCESS", "SUCCESS", extra_fields={"results": [res]}) return result.http_response()
def get(self, cmd): pg_response = pgc.get_data('instances '+cmd) if len(pg_response) == 0: return ServerErrorResult().http_response() if pg_response[0]['state'] != 'completed': return ServerErrorResult(state=pg_response[0]['state'],message=pg_response[0].get('msg')).http_response() extra_fields = pg_response[0]['data'] return Result(200,pg_response[0]['state'],'SUCCESS',extra_fields={"data":extra_fields}).http_response(pretty=1)
def get(self): pg_response = pgc.get_cmd_data('credentials --list') return Result(200, 'SUCCESS', 'SUCCESS', extra_fields={ "data": pg_response }).http_response()
def put(self): json_dict = {} args = request.json.get('params') pgcCmd = self.constructCmd(args, method_type="UPDATE") data = pgc.get_cmd_data(pgcCmd) if len(data) == 0: return ServerErrorResult().http_response() if data[0]['state'] != 'info' or data[0]['state'] == 'completed': return ServerErrorResult( state=data[0]['state'], message=data[0].get('msg')).http_response() return Result(200, data[0]['state'], data[0]['msg']).http_response()
def post(self): result = {} args = request.json if not validate_backup_fields(args): return InvalidParameterResult().http_response() try: from BackupRestore import BackupRestore backuprestore = BackupRestore() ctime = get_current_time(format='%y%m%d%H%M%S%f') import util if args['host'] in ['localhost', '127.0.0.1']: args['host'] = util.get_host_ip() result = backuprestore.backup_restore(ctime, 'restore', args['host'], args['port'], args['username'], args['dbName'], args['sshServer'], args['backupDirectory'], args['fileName'], args['format'], args.get('advOptions', ""), password=args.get( 'password', None)) process_log_dir = result['log_dir'] process_status = get_process_status(process_log_dir) result['pid'] = process_status.get('pid') result['exit_code'] = process_status.get('exit_code') result['process_log_id'] = result["process_log_id"] if process_status.get('exit_code') is None: result['in_progress'] = True try: j = Process(pid=int(result["process_log_id"]), command=result['cmd'], logdir=result["log_dir"], desc=dumps(str(args['action'])), user_id=current_user.id, acknowledge='pgDevOps') db_session.add(j) db_session.commit() except Exception as e: print str(e) pass if result['error']: response = Result(500, "error", result['error'], extra_fields={'data': result}) else: response = Result(200, "Success", "Success", extra_fields={'data': result}) except Exception as e: import traceback response = ServerErrorResult(message=str(e)) time.sleep(1) return response.http_response()
def delete(self, uuids): json_dict = {} result = {} for cred_uuid in uuids.split(','): pgcCmd = "credentials DELETE " if cred_uuid: pgcCmd = pgcCmd + " --cred_uuid \"" + cred_uuid + "\"" data = pgc.get_cmd_data(pgcCmd) if len(data) == 0: return ServerErrorResult().http_response() if data[0]['state'] != 'info' or data[0]['state'] == 'completed': return ServerErrorResult( state=data[0]['state'], message=data[0].get('msg')).http_response() return Result(200, data[0]['state'], data[0]['msg']).http_response()
def post(self): try: json_body = request.json pgcCmd = "create " + json_body.get( "type") + " --cloud " + json_body.get("cloud") pgcCmd = pgcCmd + " --params '" + str( json.dumps(json_body.get("params"))) + "'" data = pgc.get_cmd_data(pgcCmd) if len(data) == 0: return ServerErrorResult().http_response() if data[0]['state'] != 'complete': return ServerErrorResult( state=data[0]['state'], message=data[0].get('msg')).http_response() return Result(200, 'SUCCESS', message=data[0].get('msg')).http_response() except Exception as ex: return ServerErrorResult(message=str(ex)).http_response()
def post(self): payload = request.get_json()['params'] if not set(("cloud_type", "credentials")).issubset(payload): return InvalidParameterResult( errors=["Both cloud_type and credentials are required" ]).http_response() if payload["cloud_type"] not in ('aws', 'azure', 'vmware'): return InvalidParameterResult( errors=["Possible values for cloud_type are aws/azure/vmware" ]).http_response() pgcCmd = "test-cred --cloud=" + payload["cloud_type"] pgcCmd = pgcCmd + " --credentials \'" + json.dumps( payload["credentials"]) + "\'" data = pgc.get_cmd_data(pgcCmd) if len(data) == 0: return ServerErrorResult().http_response() if data[0]['state'] != 'info' or data[0]['state'] == 'completed': return ServerErrorResult( state=data[0]['state'], message=data[0].get('msg')).http_response() return Result(200, data[0]['state'], data[0]['msg']).http_response()
def delete(uid): """ Args: uid: Returns: """ usr = User.query.get(uid) if not usr: return NotFoundResult( message="User with id {0} is not found".format(uid)) try: Setting.query.filter_by(user_id=uid).delete() UserPreference.query.filter_by(uid=uid).delete() Server.query.filter_by(user_id=uid).delete() ServerGroup.query.filter_by(user_id=uid).delete() Process.query.filter_by(user_id=uid).delete() # Finally delete user db.session.delete(usr) db.session.commit() return Result(200, "SUCCESS", "User Deleted Successfully").http_response() except Exception as e: return ServerErrorResult(message=str(e)).http_response()