def on_put(self, req, resp, **kwargs): # Load update object raw_json = req.stream.read() obj = json.loads(raw_json.decode('utf-8')) if raw_json else None if 'endpoint' in kwargs and kwargs.get( 'endpoint').lower() not in PackageAPI.PACKAGE_PUT_OPTIONS: raise HTTPNotFound() elif 'endpoint' in kwargs and kwargs.get( 'endpoint').lower() in PackageAPI.PACKAGE_PUT_OPTIONS: endpoint = '{}{}/{}'.format( ConfReader().get('APP_CATALOGUE', 'url'), kwargs.get('package_id'), 'action') request_post_patch(endpoint, method='PUT', params=dict(status=kwargs.get('endpoint'))) elif not obj or 'app' not in obj: raise HTTPBadRequest( title='No data', description='No data provided to update package', code='211') else: endpoint = "{}{}".format(ConfReader().get('APP_CATALOGUE', 'url'), kwargs.get('package_id')) request_post_patch(endpoint, method='PUT', json=obj.get('app')) resp.status = HTTP_200
def on_delete(self, req, resp, tenant_id): """ Method to delete a Tenant. This endpoint is only accessible to the Super Admin. Before deleting the tenant the method ensures the tenant is disabled. :param tenant_id: The tenant id to delete :return: 204 No Content - When the tenant is successfully deleted """ # Configure correct endpoint endpoint = TenantAPI.KS_ENDPOINT + '/' + tenant_id # Ensure the tenant is disabled ks_domain = dict(enabled=False) request_post_patch(endpoint, 'PATCH', headers={ 'Content-Type': 'application/json', 'X-Auth-Token': req.headers['X-AUTH-TOKEN'] }, json=dict(domain=ks_domain), service_name=TenantAPI.SERVICE_NAME) request(endpoint, method='DELETE', headers={ 'Content-Type': 'application/json', 'X-Auth-Token': req.headers['X-AUTH-TOKEN'] }, service_name=TenantAPI.SERVICE_NAME) resp.status = HTTP_204
def on_post(self, req, resp, tenant_id, parsed): """ Method to create new user. This endpoint is accessible to Super Admin and tenant admin. The name of each user must be unique within a tenant. :return: 201 Created - When the user is successfully created """ data = parsed.get('user') # Validate Role ID role_endpoint = RoleAPI.KS_ENDPOINT + '/' + data.get('role').get('id') request(role_endpoint, headers={ 'Content-Type': 'application/json', 'X-Auth-Token': req.headers['X-AUTH-TOKEN'] }, service_name=UserAPI.SERVICE_NAME) # Build KS object ks_user = dict() ks_user['description'] = data.get('description', None) ks_user['name'] = data.get('username') ks_user['password'] = data.get('password') ks_user['enabled'] = data.get('enabled', True) ks_user['domain_id'] = tenant_id # Request user creation r = request_post_patch(UserAPI.KS_ENDPOINT, method='POST', headers={ 'Content-Type': 'application/json', 'X-Auth-Token': req.headers['X-AUTH-TOKEN'] }, json=dict(user=ks_user), service_name=UserAPI.SERVICE_NAME) # Get user object user = UserAPI.convert_ks_user_user(req, r=r) # Add specified role endpoint = "{}/{}/users/{}/roles/{}".format(TenantAPI.KS_ENDPOINT, tenant_id, user.get('id'), data.get('role').get('id')) request_post_patch( endpoint, method='PUT', headers={'X-Auth-Token': req.headers['X-AUTH-TOKEN']}, service_name=UserAPI.SERVICE_NAME) user['role'] = dict(id=data.get('role').get('id')) resp.status = HTTP_201 resp.body = self.format_body(dict(user=user))
def on_post(self, req, resp, parsed): """ Creates a Role object in a global context, i.e., accessible to all tenants :return: 201 CREATED: Role created with success """ data = parsed.get('role') request_post_patch( RoleAPI.KS_ENDPOINT, method='POST', headers={'X-Auth-Token': req.headers['X-AUTH-TOKEN']}, json=dict(role=data)) resp.status = HTTP_201
def on_post(self, req, resp, **kwargs): """ :param req: :param resp: :param kwargs: :return: """ tal_script = req.stream.read().decode('utf-8') tal_script = tal_script.replace("encoding='utf8'", 'encoding="UTF-8"') request_post_patch(ConfReader().get('TAL_SERVICE', 'url'), method='POST', headers={'Content-Type': 'application/xml'}, data=tal_script) resp.status = HTTP_201
def on_patch(self, req, resp, tenant_id, parsed): """ Method to update a Tenant. This endpoint is only accessible to the Super Admin. It's not required the full object only the updated fields. :param tenant_id: The tenant id to edit :return: 200 OK - When the tenant is edited successfully """ # Configure correct endpoint endpoint = TenantAPI.KS_ENDPOINT + '/' + tenant_id data = parsed.get('tenant') r = request_post_patch(endpoint, method='PATCH', headers={ 'Content-Type': 'application/json', 'X-Auth-Token': req.headers['X-AUTH-TOKEN'] }, json=dict(domain=data), service_name=TenantAPI.SERVICE_NAME) tenant = TenantAPI.convert_domain_tenant(req, r=r) resp.body = self.format_body(dict(tenant=tenant), from_dict=True) resp.status = HTTP_200
def on_post(self, req, resp, parsed): """ Method to create new tenant. This endpoint is accessible to Super Admin only. The name of each tenant must be unique. :return: 201 Created - When the tenant is successfully created """ data = parsed.get('tenant') # Build KS Request enabled = True # By default a domain is enabled ks_domain = dict(name=data.get('name'), enabled=enabled, description=data.get('description')) # Process request r = request_post_patch(TenantAPI.KS_ENDPOINT, method='POST', headers={ 'Content-Type': 'application/json', 'X-Auth-Token': req.headers['X-AUTH-TOKEN'] }, json=dict(domain=ks_domain), service_name=TenantAPI.SERVICE_NAME) tenant = TenantAPI.convert_domain_tenant(req, r=r) resp.body = self.format_body(dict(tenant=tenant), from_dict=True) resp.status = HTTP_201
def on_post(self, req, resp, parsed): """ Create new token. This will issue a new request to the Keystone service that will launch a new token. :return: 201 Created - When the new token is successfully created and the credentials are correct. An object with a JSON session representation is returned containing the issued_at, expires_at and user fields. """ data = parsed.get('auth') # Build KS request ks_domain = dict( name=data.get('tenant')) # In KS tenants are known as domains ks_user = dict(name=data.get('username'), password=data.get('password'), domain=ks_domain) ks_pw = dict(user=ks_user) ks_identity = dict(methods=['password'], password=ks_pw) ks_scope = dict(domain=ks_domain) ks_auth = dict(identity=ks_identity, scope=ks_scope) # Process request r = request_post_patch(LoginAPI.KS_ENDPOINT, method='POST', headers={'Content-Type': 'application/json'}, json=dict(auth=ks_auth)) resp.set_header('X-Subject-Token', r.headers['X-Subject-Token']) resp.status = HTTP_201 data = json.loads(r.text) data = data['token'] user = data['user'] user['tenant'] = dict(id=user.get('domain').get('id'), name=user.get('domain').get('name')) user['role'] = data.pop('roles', None)[0] # User's will only have one role user.pop('domain', None) data['user'] = user # Pop unwanted keys data.pop('catalog', None) data.pop('methods', None) data.pop('domain', None) self.format_body(dict(session=data), from_dict=True) LoginAPI.logger.info('User {} successfully logged'.format( user.get('id', None)))
def on_post(self, req, resp, **kwargs): """ Creates a new package based on a provided file. """ file = req.get_param('file') # Loads the tar file if file is None: raise HTTPBadRequest(title="No Package", description="No package provided as file", code='215') endpoint = ConfReader().get('APP_CATALOGUE', 'url') r = request_post_patch(endpoint, method='POST', files={'file': file.file.read()}) resp.status = HTTP_201 resp.body = self.format_body(dict(app=dict(id=r.text)), from_dict=True)
def on_patch(self, req, resp, parsed, tenant_id, user_id): """ Method to update a User. This endpoint is only accessible to the Super Admin and tenant admin. It's not required the full object only the updated fields. :return: 200 OK - When the user is edited successfully """ print('Im patching') # Validate incoming data data = parsed.get('user') # Build KS object based on user's role ks_user = dict() ks_user['description'] = data.get('description', None) ks_user['name'] = data.get('username', None) ks_user['enabled'] = data.get('enabled', None) ks_user['password'] = data.get('password', None) ks_user = {k: v for k, v in ks_user.items() if v is not None} # Generator to clear None value keys # Patch or get user when only role is updated endpoint = UserAPI.KS_ENDPOINT + '/' + user_id r = request_post_patch( endpoint, method='PATCH', headers={'X-Auth-Token': req.headers['X-AUTH-TOKEN']}, json=dict(user=ks_user), service_name=UserAPI.SERVICE_NAME) # Converts keystone user and update the role user = UserAPI.convert_ks_user_user(req, r=r) if 'role' not in data: resp.status = HTTP_200 resp.body = self.format_body(dict(user=user)) return # Update role # Validate Role ID role_endpoint = RoleAPI.KS_ENDPOINT + '/' + data.get('role').get('id') request(role_endpoint, headers={ 'Content-Type': 'application/json', 'X-Auth-Token': req.headers['X-AUTH-TOKEN'] }, service_name=RoleAPI.SERVICE_NAME) # Disable current role role_id = user.get('role').get('id') endpoint = "{}/{}/users/{}/roles/{}".format(TenantAPI.KS_ENDPOINT, tenant_id, user_id, role_id) request(endpoint, method='DELETE', headers={'X-Auth-Token': req.headers['X-AUTH-TOKEN']}, service_name=UserAPI.SERVICE_NAME) # Set new role endpoint = "{}/{}/users/{}/roles/{}".format(TenantAPI.KS_ENDPOINT, tenant_id, user_id, data.get('role').get('id')) request_post_patch( endpoint, method='PUT', headers={'X-Auth-Token': req.headers['X-AUTH-TOKEN']}, service_name=RoleAPI.SERVICE_NAME) user['role'] = dict(id=data.get('role').get('id')) resp.status = HTTP_200 resp.body = self.format_body(dict(user=user))