def action() -> SubjectForIndicator: subject: Optional[Subject] = subject_service.find_by_id(subject_id) if subject is None: raise_404() if subject.tenantId != principal_service.get_tenant_id(): raise_403() connected_space_service = get_connected_space_service(subject_service) connected_space: Optional[ ConnectedSpace] = connected_space_service.find_by_id( subject.connectId) if connected_space is None: raise IndicatorKernelException( f'Connected space not found for subject[id={subject_id}].') space_service = get_space_service(subject_service) space: Optional[Space] = space_service.find_by_id( connected_space.spaceId) if space is None: raise IndicatorKernelException( f'Space not found for subject[id={subject_id}].') topic_service = get_topic_service(subject_service) topics = topic_service.find_by_ids(space.topicIds, principal_service.get_tenant_id()) topic_map: Dict[TopicId, Topic] = ArrayHelper(topics).to_map( lambda x: x.topicId, lambda x: x) all_topic_ids = space.topicIds all_topics = ArrayHelper(all_topic_ids).map( lambda x: topic_map[x]).to_list() return SubjectForIndicator(**subject.to_dict(), topics=all_topics)
def action() -> DataSource: # noinspection PyTypeChecker data_source: DataSource = data_source_service.find_by_id( data_source_id) if data_source is None: raise_404() return data_source
async def init_tenant( tenant_id: Optional[TenantId], principal_service: PrincipalService = Depends(get_any_admin_principal) ) -> None: if is_blank(tenant_id): if principal_service.is_super_admin(): raise_400('Tenant id is required.') elif principal_service.is_tenant_admin(): tenant_id = principal_service.get_tenant_id() else: if principal_service.get_tenant_id( ) != tenant_id and principal_service.is_tenant_admin(): raise_400(f'Tenant[{tenant_id}] does not match principal.') elif principal_service.is_super_admin(): tenant: Optional[Tenant] = get_tenant_service( principal_service).find_by_id(tenant_id) if tenant is None: raise_404(f'Tenant[id={tenant_id}] not found.') meta_tenant_service = get_meta_tenant_service(principal_service) def action() -> None: topics = ask_pipeline_monitor_topics() create_topics_and_pipelines( topics, lambda source_topics: ask_pipeline_monitor_pipelines( source_topics), tenant_id, meta_tenant_service, principal_service) topics = ask_dqc_topics() create_topics_and_pipelines( topics, lambda source_topics: ask_dqc_pipelines(source_topics), tenant_id, meta_tenant_service, principal_service) trans(meta_tenant_service, action)
def action() -> LastSnapshot: # noinspection PyTypeChecker last_snapshot: LastSnapshot = last_snapshot_service.delete_by_id( user_id) if last_snapshot is None: raise_404() return last_snapshot
def action() -> ExternalWriter: # noinspection PyTypeChecker external_writer: ExternalWriter = external_writer_service.find_by_id( writer_id) if external_writer is None: raise_404() return external_writer
def action() -> DataSource: # noinspection PyTypeChecker data_source: DataSource = data_source_service.delete(data_source_id) if data_source is None: raise_404() CacheService.data_source().remove(data_source_id) return data_source
def action() -> Topic: # noinspection PyTypeChecker topic: Topic = topic_service.delete(topic_id) if topic is None: raise_404() post_delete_topic(topic.topicId, topic_service) return topic
def action() -> ConnectedSpaceGraphic: # noinspection PyTypeChecker connected_space_graphic: ConnectedSpaceGraphic = connected_space_graphic_service.delete( connect_id) if connected_space_graphic is None: raise_404() return connected_space_graphic
def action() -> Tenant: # noinspection PyTypeChecker tenant: Tenant = tenant_service.delete(tenant_id) if tenant is None: raise_404() CacheService.tenant().remove(tenant_id) return tenant
def get_topic_schema(topic_name: str, tenant_id: Optional[TenantId], principal_service: PrincipalService) -> TopicSchema: schema = get_topic_service(principal_service).find_schema_by_name( topic_name, tenant_id) if schema is None: raise_404('Topic not found.') return schema
async def delete_enum_by_id_by_super_admin( enum_id: Optional[EnumId] = None, principal_service: PrincipalService = Depends(get_super_admin_principal) ) -> Enum: if not ask_tuple_delete_enabled(): raise_404('Not Found') if is_blank(enum_id): raise_400('Enumeration id is required.') enum_service = get_enum_service(principal_service) def action() -> Enum: # noinspection PyTypeChecker an_enum: Enum = enum_service.delete(enum_id) if an_enum is None: raise_404() enum_item_service = get_enum_item_service(enum_service) enum_items = enum_item_service.find_by_enum_id(an_enum.enumId) if enum_items is None: an_enum.items = [] else: an_enum.items = enum_items enum_item_service.delete_by_enum_id(an_enum.enumId) return an_enum return trans(enum_service, action)
def find_enum( import_items: ImportEnumItems) -> Tuple[Enum, List[EnumItem]]: enum_id = import_items.enumId name = import_items.name if is_blank(enum_id) and is_blank(name): raise_400( 'At least one of enumeration id and name must be provided.') enumeration = None if is_not_blank(enum_id): # both provided, find by id enumeration = enum_service.find_by_id(enum_id) elif is_not_blank(name): enumeration = enum_service.find_by_name( name, principal_service.get_tenant_id()) if enumeration is not None: # found if enumeration.tenantId != principal_service.get_tenant_id(): raise_404(f'Enumeration[id={enum_id}, name={name}] not found.') elif is_not_blank(name): # not found, but name is given, create one enumeration = Enum(enumId=enum_id, name=name, tenantId=principal_service.get_tenant_id(), items=[]) enum_service.create(enumeration) else: raise_404(f'Enumeration[id={enum_id}, name={name}] not found.') return enumeration, import_items.items
async def delete_user_by_id_by_super_admin( user_id: Optional[UserId] = None, principal_service: PrincipalService = Depends(get_super_admin_principal) ) -> User: if not ask_tuple_delete_enabled(): raise_404('Not Found') if is_blank(user_id): raise_400('User id is required.') user_service = get_user_service(principal_service) def action() -> User: # noinspection PyTypeChecker user: User = user_service.delete(user_id) if user is None: raise_404() user_group_ids = user.groupIds if user_group_ids is not None and len(user_group_ids) != 0: user_group_ids = ArrayHelper(user_group_ids).filter( lambda x: is_not_blank(x)).to_list() remove_user_from_groups(user_service, user.userId, user_group_ids, user.tenantId) return user return trans(user_service, action)
def action() -> Pipeline: # noinspection PyTypeChecker pipeline: Pipeline = pipeline_service.delete(pipeline_id) if pipeline is None: raise_404() post_delete_pipeline(pipeline.pipelineId, pipeline_service) return pipeline
def action() -> None: # noinspection PyTypeChecker existing_scheduler: Optional[ TopicSnapshotScheduler] = scheduler_service.find_by_id( scheduler_id) if existing_scheduler is None: raise_404() scheduler_service.delete(scheduler_id)
def action() -> SubjectWithReports: # noinspection PyTypeChecker subject: Subject = subject_service.delete(subject_id) if subject is None: raise_404() report_service: ReportService = get_report_service(subject_service) reports: List[Report] = report_service.delete_by_subject_id(subject_id) return SubjectWithReports(**subject.dict(), reports=reports)
def run_topics_rules( topic_name: Optional[str] = None, frequency: Optional[MonitorRuleStatisticalInterval] = None, process_date: Optional[str] = None, tenant_id: Optional[TenantId] = None, principal_service: PrincipalService = Depends(get_any_admin_principal) ) -> None: principal_service = ask_principal_service(principal_service, tenant_id) if is_not_blank(topic_name): schema = get_topic_service(principal_service).find_schema_by_name( topic_name, principal_service.get_tenant_id()) if schema is None: raise_404(f'Topic[name={topic_name}] not found.') topic_id = schema.get_topic().topicId else: topic_id = None if is_not_blank(process_date): parsed, parsed_date = is_date(process_date, ask_all_date_formats()) if not parsed: raise_400(f'Given process date[{process_date}] cannot be parsed.') process_date = parsed_date else: process_date = get_current_time_in_seconds() process_date = truncate_time(process_date) now = truncate_time(get_current_time_in_seconds()) if process_date.year > now.year: raise_400(f'Given process date[{process_date}] cannot be in future.') if process_date.year == now.year and process_date.month > now.month: raise_400(f'Given process date[{process_date}] cannot be in future.') if process_date.year == now.year and process_date.month == now.month and process_date.day > now.day: raise_400(f'Given process date[{process_date}] cannot be in future.') if frequency == MonitorRuleStatisticalInterval.MONTHLY: # given process date is in this month, run previous month # otherwise, run the given month if process_date.year == now.year and process_date.month == now.month: process_date = to_previous_month(process_date) SelfCleaningMonitorRulesRunner(principal_service) \ .run(process_date, topic_id, MonitorRuleStatisticalInterval.MONTHLY) elif frequency == MonitorRuleStatisticalInterval.WEEKLY: # given process date is in this week, run previous week # otherwise, run the given week if process_date.year == now.year and int( process_date.strftime('%U')) == int(now.strftime('%U')): process_date = to_previous_week(process_date) SelfCleaningMonitorRulesRunner(principal_service) \ .run(process_date, topic_id, MonitorRuleStatisticalInterval.WEEKLY) elif frequency == MonitorRuleStatisticalInterval.DAILY: # given process date is today, run yesterday # otherwise, run the given day if process_date.year == now.year and process_date.month == now.month and process_date.day == now.day: process_date = to_yesterday(process_date) SelfCleaningMonitorRulesRunner(principal_service) \ .run(process_date, topic_id, MonitorRuleStatisticalInterval.DAILY) else: raise_400(f'Given frequency[{frequency}] is not supported.')
def action() -> None: # noinspection PyTypeChecker existing_catalog: Optional[Catalog] = catalog_service.find_by_id( catalog_id) if existing_catalog is None: raise_404() if existing_catalog.tenantId != principal_service.get_tenant_id(): raise_403() catalog_service.delete(catalog_id)
def action() -> Pipeline: # noinspection PyTypeChecker pipeline: Pipeline = pipeline_service.find_by_id(pipeline_id) if pipeline is None: raise_404() # tenant id must match current principal's if pipeline.tenantId != principal_service.get_tenant_id(): raise_404() return pipeline
def action() -> Bucket: # noinspection PyTypeChecker bucket: Bucket = bucket_service.find_by_id(bucket_id) if bucket is None: raise_404() # tenant id must match current principal's if bucket.tenantId != principal_service.get_tenant_id(): raise_404() return bucket
def action() -> UserGroup: # noinspection PyTypeChecker user_group: UserGroup = user_group_service.find_by_id(user_group_id) if user_group is None: raise_404() # tenant id must match current principal's if user_group.tenantId != principal_service.get_tenant_id(): raise_404() return user_group
def action() -> Space: # noinspection PyTypeChecker space: Space = space_service.find_by_id(space_id) if space is None: raise_404() # tenant id must match current principal's if space.tenantId != principal_service.get_tenant_id(): raise_404() return space
def action() -> Catalog: # noinspection PyTypeChecker catalog: Catalog = catalog_service.find_by_id(catalog_id) if catalog is None: raise_404() # tenant id must match current principal's if catalog.tenantId != principal_service.get_tenant_id(): raise_404() return catalog
def action() -> Topic: # noinspection PyTypeChecker topic: Topic = topic_service.find_by_id(topic_id) if topic is None: raise_404() # tenant id must match current principal's if topic.tenantId != principal_service.get_tenant_id(): raise_404() return topic
def action() -> None: # noinspection PyTypeChecker existing_dashboard: Optional[Dashboard] = dashboard_service.find_by_id(dashboard_id) if existing_dashboard is None: raise_404() if existing_dashboard.tenantId != principal_service.get_tenant_id(): raise_403() if not principal_service.is_tenant_admin() and existing_dashboard.userId != principal_service.get_user_id(): raise_403() dashboard_service.delete(dashboard_id)
def action() -> List[Indicator]: # noinspection PyTypeChecker indicator: Indicator = indicator_service.find_by_id(indicator_id) if indicator is None: raise_404() # tenant id must match current principal's if indicator.tenantId != principal_service.get_tenant_id(): raise_404() # TODO find relevant indicators return []
def action() -> Space: # noinspection PyTypeChecker space: Space = space_service.delete(space_id) if space is None: raise_404() user_group_ids = space.groupIds if user_group_ids is not None and len(user_group_ids) != 0: user_group_ids = ArrayHelper(user_group_ids).filter(lambda x: is_not_blank(x)).to_list() remove_space_from_groups(space_service, space.spaceId, user_group_ids, space.tenantId) return space
def action() -> None: # noinspection PyTypeChecker existing_report: Optional[Report] = report_service.find_by_id(report_id) if existing_report is None: raise_404() if existing_report.tenantId != principal_service.get_tenant_id(): raise_403() if not principal_service.is_tenant_admin() and existing_report.userId != principal_service.get_user_id(): raise_403() report_service.delete(report_id)
async def token_exchange(token: TokenExchange) -> SamlToken: if ask_saml2_enabled(): if verify_signature(token.data, token.algorithm, token.signature, token.relayState, ask_saml2_settings()): user_name = get_user_name_in_saml_body(token.data) user = find_user(user_name) return SamlToken(**build_token(user).dict(), accountName=user_name) else: raise_401('Invalid signature') else: raise_404()
def action() -> None: existing_tenant_id: Optional[ TenantId] = pipeline_service.find_tenant_id(pipeline_id) if existing_tenant_id is None: raise_404() elif existing_tenant_id != principal_service.get_tenant_id(): raise_403() # noinspection PyTypeChecker pipeline: Pipeline = pipeline_service.update_enablement( pipeline_id, enabled, principal_service.get_tenant_id()) post_update_pipeline_enablement(pipeline, pipeline_service)