def get_login(self): json_data = read_json_file(get_cfg('keystone__data_login')) json_data['auth']['identity']['password']['user'][ 'name'] = self.keystone_admin json_data['auth']['identity']['password']['user'][ 'password'] = self.keystone_admin_pass url = self.path + get_cfg('keystone__login') return post(self.session, url, None, json_data)
def get_json_data(self, info): if isinstance(info, bool): json_data = read_json_file(get_cfg('keystone__data_patch_user')) json_data['user']['enabled'] = info else: json_data = read_json_file( get_cfg('keystone__data_patch_password_user')) json_data['user']['password'] = info return json_data
def change_password(self, user_id, password): response = self.get_login() url = self.path + get_cfg( 'keystone__create_user') + "/" + user_id + '/password' json_data = read_json_file( get_cfg('keystone__data_change_password_user')) json_data['user']['password'] = password['password'] json_data['user']['original_password'] = password['original_password'] return self.session.post(url, data=json.dumps(json_data), headers=create_keystone_headers( get_keystone_token(response)))
def patch_user(self, user_id, info): response = self.get_login() url = self.path + get_cfg('keystone__create_user') + "/" + user_id return self.session.patch(url, data=json.dumps(self.get_json_data(info)), headers=create_keystone_headers( get_keystone_token(response)))
def __init__(self): self.path = get_cfg('keystone__url') self.user = None self.keystone = Keystone() self.response_msg = create_message_error(409, 'USER_DUPLICATED') self.smm = ServiceManagerMailer() self.session = Session()
def get_project(self, name_project, data): """ Consultamos el proyecto/entidad en Mongo, ya que guardamos cierta información de la entitiy y la API de keystone no ofrece un findByName. :return: """ entity = find_one_in_collection('entities', {"name": name_project}) entity_id = entity['keystone_project_id'] json_data = read_json_file(get_cfg('keystone__data_create_user')) json_data['user']['default_project_id'] = entity_id json_data['user']['name'] = data['email'] json_data['user']['password'] = data['password'] return json_data, entity_id, entity
def save_keystone(self, data): #primero comprobamos que el usuario ha de ser o proveedor o cliente if not data['client_role'] and not data['provider_role']: return respond_json(create_message_error(400, 'PROVIDER_CLIENT'), 400) # login response = self.keystone.get_login() if response.status_code == 201: token = get_keystone_token(response) json_data, entity_id, entity = self.keystone.get_project( get_cfg('keystone__project_name'), data) url = self.path + get_cfg('keystone__create_user') response = post(self.session, url, put_headers_keystone(token), json_data) if response.status_code == 201: user_id = json.loads(response.text)['user']['id'] # grabamos la info en mongo if data['provider_role']: self.user = Provider() else: self.user = Client() try: self.dict_to_mongo_user(data, user_id, entity_id, str(entity['_id'])) self.response_msg = dict(status_code=204) except Exception as e: ''' Cualquier error que se produzca al intentar grabar en BBDD. Hemos de borrar el usuario de Keystone ''' print e url = url + "/" + user_id delete(self.session, url, put_headers_keystone(token)) self.response_msg = create_message_error(400) if self.response_msg['status_code'] == 204: self.smm.notify(self.user.email) return respond_json(self.response_msg, self.response_msg['status_code'])
def get_users(self): response = self.get_login() url = self.path + get_cfg('keystone__create_user') return self.session.get(url, headers=create_keystone_headers( get_keystone_token(response)))
def __init__(self): self.keystone_admin, self.keystone_admin_pass = \ get_keystone_info(read_json_file(get_cfg('keystone__file'))) self.path = get_cfg('keystone__url') self.session = Session()