def buy_post(self, ctx, member_id: int, payment_method: int, products: List[int]): try: self.product_manager.buy(ctx, member_id, payment_method, products) return None, 204 except Exception as e: return handle_error(ctx, e)
def get_from_number(self, ctx, vlan_number): """ Get the state of a port """ try: return self.vlan_manager.get_from_number( ctx, vlan_number).to_dict(), 200 except Exception as e: return handle_error(ctx, e)
def member_get(self, ctx, id_: int): try: if ctx.get(CTX_ADMIN) != id_ and Roles.ADMIN_WRITE.value not in ctx.get(CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") return self.room_manager.room_from_member(ctx, id_), 200 except Exception as e: return handle_error(ctx, e)
def post(self, ctx, body: Dict[str, Any]): try: return self.api_key_manager.create(ctx=ctx, login=body.get('login', ''), roles=body.get('roles', [])), 200 except Exception as e: return handle_error(ctx, e)
def member_put(self, ctx, id_: int, body: Dict[str, int]): try: if ctx.get(CTX_ADMIN) != id_ and Roles.ADMIN_WRITE.value not in ctx.get(CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") self.mailinglist_manager.update_member_mailinglist(ctx, id_, body['value']) return NoContent, 204 except Exception as e: return handle_error(ctx, e)
def _update(self, ctx, function, klass: Type[Model], body, id: Optional[int] = None): try: body['id'] = 0 # Set a dummy id to pass the initial validation to_update = klass.from_dict(body) the_object, created = function(ctx, to_update, id=id) return the_object.to_dict(), 201 if created else 204 except Exception as e: return handle_error(ctx, e)
def profile(self, ctx): try: member, roles = self.member_manager.get_profile(ctx) if member.id != ctx.get(CTX_ADMIN): raise UnauthorizedError( "Not authorize to access this ressource") return {"member": member.to_dict(), "roles": roles}, 200 except Exception as e: return handle_error(ctx, e)
def post(self, ctx, body: Dict[str, Any]): try: self.role_manager.create(ctx=ctx, auth=body["auth"], identifier=body["identifier"], roles=body["roles"]) return NoContent, 201 except Exception as e: return handle_error(ctx, e)
def member_get(self, ctx, charter_id: int, id_: int): try: if ctx.get(CTX_ADMIN ) != id_ and Roles.ADMIN_READ.value not in ctx.get( CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") return self.charter_manager.get(ctx, charter_id, id_), 200 except Exception as e: return handle_error(ctx, e)
def subscription_patch(self, ctx, id_, body): try: LOG.debug("membership_patch_called", extra=log_extra(ctx, body=body, id=id_)) to_update = SubscriptionBody.from_dict(body) self.member_manager.update_subscription(ctx, id_, to_update) return NoContent, 204 except Exception as e: return handle_error(ctx, e)
def subscription_post(self, ctx, id_: int, body: dict): """ Add a membership record in the database """ LOG.debug("http_member_post_membership_called", extra=log_extra(ctx, id=id_, request=body)) try: created_membership = self.member_manager.create_subscription( ctx, id_, SubscriptionBody.from_dict(body)) return created_membership.to_dict(), 200 # 200 OK except Exception as e: return handle_error(ctx, e)
def statuses_search(self, ctx, id_: int): try: return list( map(lambda x: x.to_dict(), self.member_manager.get_statuses(ctx, id_))), 200 except Exception as e: if isinstance( e, NotFoundError ) and Roles.ADMIN_READ.value not in ctx.get(CTX_ROLES): e = UnauthorizedError("cannot access this resource") return handle_error(ctx, e)
def post(self, ctx, body: dict = {}): try: device_body = DeviceBody.from_dict(body) if ctx.get( CTX_ADMIN ) != device_body.member and Roles.ADMIN_WRITE.value not in ctx.get( CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") return self.device_manager.create(ctx, device_body).id, 201 except Exception as e: return handle_error(ctx, e)
def search(self, ctx, auth: str, id_: Union[str, None] = None): try: result, count = self.role_manager.search(ctx=ctx, auth=auth, identifier=id_) headers = { "X-Total-Count": str(count), 'access-control-expose-headers': 'X-Total-Count' } return list(map(lambda x: x.to_dict(), result)), 200, headers except Exception as e: return handle_error(ctx, e)
def charter_put(self, ctx, id_, charter_id) -> Tuple[Any, int]: try: if ctx.get(CTX_ADMIN ) != id_ and Roles.ADMIN_READ.value not in ctx.get( CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") self.charter_manager.sign(ctx, charter_id=charter_id, member_id=id_) return NoContent, 204 except Exception as e: return handle_error(ctx, e)
def vlan_put(self, ctx, id_, body): LOG.debug("http_port_vlan_put_called", extra=log_extra(ctx, id=id_)) try: if (self.switch_network_manager.get_port_vlan(ctx, port_id=id_) ) == "No Such Instance currently exists at this OID": return 1, 200 self.switch_network_manager.update_port_vlan(ctx, port_id=id_, vlan=int(body)) return int(body), 204 except Exception as e: return handle_error(ctx, e)
def get(self, ctx, id_: int, only: Optional[List[str]]=None): try: def remove(entity: Any) -> Any: if isinstance(entity, dict) and only is not None: entity_cp = entity.copy() for k in entity_cp.keys(): if k not in only + ["id"]: del entity[k] return entity return remove(self.main_manager.get_by_id(ctx, id=id_).to_dict()), 200 except Exception as e: return handle_error(ctx, e)
def password_put(self, ctx, id_, body): """ Set the password of a member. """ try: self.member_manager.change_password(ctx, id_, body.get('password'), body.get("hashedPassword")) return NoContent, 204 # 204 No Content except Exception as e: if isinstance( e, NotFoundError ) and Roles.ADMIN_WRITE.value not in ctx.get(CTX_ROLES): e = UnauthorizedError("cannot access this resource") return handle_error(ctx, e)
def search(self, ctx, limit, offset, login: Union[str, None] = None): try: result, count = self.api_key_manager.search(ctx=ctx, limit=limit, offset=offset, login=login) headers = { "X-Total-Count": str(count), 'access-control-expose-headers': 'X-Total-Count' } return [r.to_dict() for r in result], 200, headers except Exception as e: return handle_error(ctx, e)
def delete(self, ctx, id_: int): try: if ctx.get(CTX_ADMIN) != self.device_manager.get_owner( ctx, id_) and Roles.ADMIN_WRITE.value not in ctx.get( CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") self.main_manager.delete(ctx, id=id_) return NoContent, 204 # 204 No Content except Exception as e: if isinstance( e, NotFoundError ) and Roles.ADMIN_WRITE.value not in ctx.get(CTX_ROLES): e = UnauthorizedError("cannot access this resource") return handle_error(ctx, e)
def vendor_search(self, ctx, id_: int): """ Return the vendor associated with the given device """ try: device = self.device_manager.get_by_id(ctx=ctx, id=id_) if ctx.get( CTX_ADMIN ) != device.member and Roles.ADMIN_READ.value not in ctx.get( CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") return self.device_manager.get_mac_vendor(ctx, id=id_), 200 except Exception as e: if isinstance( e, NotFoundError ) and Roles.ADMIN_READ.value not in ctx.get(CTX_ROLES): e = UnauthorizedError("cannot access this resource") return handle_error(ctx, e)
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, terms=None): try: result, total_count = self.account_type_manager.search( ctx, limit=limit, offset=offset, terms=terms) headers = { "X-Total-Count": str(total_count), 'access-control-expose-headers': 'X-Total-Count' } result = list(map(lambda x: x.to_dict(), result)) return result, 200, headers except Exception as e: return handle_error(ctx, e)
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, terms=None, filter_: Optional[Any] = None, only: Optional[List[str]]=None): try: def remove(entity: Any) -> Any: if isinstance(entity, dict) and only is not None: entity_cp = entity.copy() for k in entity_cp.keys(): if k not in only + ["id"]: del entity[k] return entity filter_ = self.abstract_entity_class.from_dict(filter_) if filter_ else None result, total_count = self.main_manager.search(ctx, limit=limit, offset=offset, terms=terms, filter_=filter_) headers = { "X-Total-Count": str(total_count), 'access-control-expose-headers': 'X-Total-Count' } return list(map(remove, map(lambda x: x.to_dict(), result))), 200, headers except Exception as e: return handle_error(ctx, e)
def get(self, ctx, id_: int, only: Optional[List[str]] = None): try: def remove(entity: Any) -> Any: if isinstance(entity, dict) and only is not None: entity_cp = entity.copy() for k in entity_cp.keys(): if k not in only + ["id"]: del entity[k] return entity return remove(self.main_manager.get_by_id(ctx, id=id_).to_dict()), 200 except Exception as e: if isinstance( e, NotFoundError ) and Roles.ADMIN_READ.value not in ctx.get(CTX_ROLES): e = UnauthorizedError("cannot access this resource") return handle_error(ctx, e)
def search(self, ctx, limit: int = DEFAULT_LIMIT, offset: int = DEFAULT_OFFSET, terms: Union[str, None] = None, filter_: Optional[Any] = None): try: filter_ = MemberFilter.from_dict(filter_) if filter_ else None result, total_count = self.main_manager.search(ctx, limit=limit, offset=offset, terms=terms, filter_=filter_) headers = { "X-Total-Count": str(total_count), 'access-control-expose-headers': 'X-Total-Count' } return result, 200, headers except Exception as e: return handle_error(ctx, e)
def search(self, ctx, limit=DEFAULT_LIMIT, offset=DEFAULT_OFFSET, filter_: dict = {}): try: device_filter = DeviceFilter.from_dict(filter_) if ctx.get( CTX_ADMIN ) != device_filter.member and Roles.ADMIN_READ.value not in ctx.get( CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") result, total_count = self.device_manager.search( ctx, limit=limit, offset=offset, device_filter=device_filter) headers = { "X-Total-Count": str(total_count), 'access-control-expose-headers': 'X-Total-Count' } return result, 200, headers except Exception as e: return handle_error(ctx, e)
def get(self, ctx, id_: int, only: t.Optional[t.List[str]] = None): try: device = self.device_manager.get_by_id(ctx, id=id_) if ctx.get( CTX_ADMIN ) != device.member and Roles.ADMIN_READ.value not in ctx.get( CTX_ROLES, []): raise UnauthorizedError("Unauthorize to access this resource") def remove(entity: t.Any) -> t.Any: if isinstance(entity, dict) and only is not None: entity_cp = entity.copy() for k in entity_cp.keys(): if k not in only + ["id"]: del entity[k] return entity return remove(device.to_dict()), 200 except Exception as e: if isinstance( e, NotFoundError ) and Roles.ADMIN_READ.value not in ctx.get(CTX_ROLES): e = UnauthorizedError("cannot access this resource") return handle_error(ctx, e)
def member_search(self, ctx, id_: int): try: return self.device_manager.get_owner(ctx=ctx, device_id=id_), 200 except Exception as e: return handle_error(ctx, e)
def mab_post(self, ctx, id_: int): """ Return the vendor associated with the given device """ try: return self.device_manager.put_mab(ctx, id=id_), 200 except Exception as e: return handle_error(ctx, e)
def get(self, ctx, id_: int): try: return self.account_type_manager.get_by_id(ctx, id=id_).to_dict(), 200 except Exception as e: return handle_error(ctx, e)