def get_default_or_raise(*, db_session, project_id: int) -> IncidentType: """Returns the default incident_type or raise a ValidationError if one doesn't exist.""" incident_type = get_default(db_session=db_session, project_id=project_id) if not incident_type: raise ValidationError( [ ErrorWrapper( NotFoundError(msg="No default incident_type defined."), loc="incident_type", ) ], model=IncidentTypeRead, ) return incident_type
def get_by_name_or_raise( *, db_session, project_id, source_status_in=SourceStatusRead) -> SourceStatusRead: """Returns the status specified or raises ValidationError.""" status = get_by_name(db_session=db_session, project_id=project_id, name=source_status_in.name) if not status: raise ValidationError( [ ErrorWrapper( NotFoundError( msg="SourceStatus not found.", status=source_status_in.name, ), loc="status", ) ], model=SourceStatusRead, ) return status
def get_by_email_and_project_id_or_raise( *, db_session, project_id: int, individual_contact_in=IndividualContactRead) -> IndividualContactRead: """Returns the individual specified or raises ValidationError.""" individual_contact = get_by_email_and_project( db_session=db_session, project_id=project_id, email=individual_contact_in.email) if not individual_contact: raise ValidationError( [ ErrorWrapper( NotFoundError( msg="Indivdual not found.", individual=individual_contact_in.email, ), loc="individual", ) ], model=IndividualContactRead, ) return individual_contact
def get_by_name_or_raise( *, db_session, project_id, source_data_format_in=SourceDataFormatRead) -> SourceDataFormatRead: """Returns the source specified or raises ValidationError.""" data_format = get_by_name(db_session=db_session, project_id=project_id, name=source_data_format_in.name) if not data_format: raise ValidationError( [ ErrorWrapper( NotFoundError( msg="SourceDataFormat not found.", source=source_data_format_in.name, ), loc="dataFormat", ) ], model=SourceDataFormatRead, ) return data_format
def get_by_external_id_and_project_id_or_raise(*, db_session, project_id: int, service_in=ServiceRead ) -> Service: """Returns the service specified or raises ValidationError.""" service = get_by_external_id_and_project_id( db_session=db_session, project_id=project_id, external_id=service_in.external_id) if not service: raise ValidationError( [ ErrorWrapper( NotFoundError( msg="Service not found.", incident_priority=service.external_id, ), loc="service", ) ], model=ServiceRead, ) return service
def get_organization_scope_from_slug(slug: str) -> SessionLocal: """Iterate all organizations looking for a relevant channel_id.""" db_session = SessionLocal() organization = organization_service.get_by_slug(db_session=db_session, slug=slug) db_session.close() if organization: schema_engine = engine.execution_options( schema_translate_map={ None: f"dispatch_organization_{slug}", }) return sessionmaker(bind=schema_engine)() raise ValidationError( [ ErrorWrapper( NotFoundError( msg= f"Organization slug '{slug}' not found. Check your spelling." ), loc="organization", ) ], model=BaseModel, )
def get_by_name_or_raise( *, db_session, project_id: int, incident_priority_in=IncidentPriorityRead) -> IncidentPriority: """Returns the incident_priority specified or raises ValidationError.""" incident_priority = get_by_name(db_session=db_session, project_id=project_id, name=incident_priority_in.name) if not incident_priority: raise ValidationError( [ ErrorWrapper( NotFoundError( msg="IncidentPriority not found.", incident_priority=incident_priority_in.name, ), loc="incident_priority", ) ], model=IncidentPriorityRead, ) return incident_priority
def get_by_name_or_raise( *, db_session, project_id, source_transport_in=SourceTransportRead) -> SourceTransportRead: """Returns the source transport specified or raises ValidationError.""" source = get_by_name(db_session=db_session, project_id=project_id, name=source_transport_in.name) if not source: raise ValidationError( [ ErrorWrapper( NotFoundError( msg="SourceTransport not found.", source=source_transport_in.name, ), loc="source", ) ], model=SourceTransportRead, ) return source
def get_class_by_tablename(table_fullname: str) -> Any: """Return class reference mapped to table.""" def _find_class(name): for c in Base._decl_class_registry.values(): if hasattr(c, "__table__"): if c.__table__.fullname.lower() == name.lower(): return c mapped_name = resolve_table_name(table_fullname) mapped_class = _find_class(mapped_name) # try looking in the 'dispatch_core' schema if not mapped_class: mapped_class = _find_class(f"dispatch_core.{mapped_name}") if not mapped_class: raise ValidationError( [ ErrorWrapper( NotFoundError( msg="Model not found. Check the name of your model."), loc="filter", ) ], model=BaseModel, ) return mapped_class
def create_workflow(*, db_session: Session = Depends(get_db), workflow_in: WorkflowCreate): """Create a new workflow.""" plugin_instance = plugin_service.get_instance( db_session=db_session, plugin_instance_id=workflow_in.plugin_instance.id ) if not plugin_instance: raise ValidationError( [ErrorWrapper(NotFoundError(msg="No plugin instance found."), loc="plugin_instance")], model=WorkflowCreate, ) workflow = create(db_session=db_session, workflow_in=workflow_in) return workflow
def get_default_or_raise(*, db_session) -> Organization: """Returns the default organization or raise a ValidationError if one doesn't exist.""" organization = get_default(db_session=db_session) if not organization: raise ValidationError( [ ErrorWrapper( NotFoundError(msg="No default organization defined."), loc="organization", ) ], model=OrganizationRead, ) return organization
def get_default_or_raise(*, db_session) -> Project: """Returns the default project or raise a ValidationError if one doesn't exist.""" project = get_default(db_session=db_session) if not project: raise ValidationError( [ ErrorWrapper( NotFoundError(msg="No default project defined."), loc="project", ) ], model=ProjectRead, ) return project
def get_by_name_or_raise(*, db_session, project_in=ProjectRead) -> Project: """Returns the project specified or raises ValidationError.""" project = get_by_name(db_session=db_session, name=project_in.name) if not project: raise ValidationError( [ ErrorWrapper( NotFoundError(msg="Project not found.", name=project_in.name), loc="name", ) ], model=ProjectRead, ) return project
def get_by_name_or_raise(*, db_session, project_id: int, incident_in: IncidentRead) -> Incident: """Returns an incident based on a given name or raises ValidationError""" incident = get_by_name(db_session=db_session, project_id=project_id, name=incident_in.name) if not incident: raise ValidationError( [ ErrorWrapper( NotFoundError( msg="Incident not found.", query=incident_in.name, ), loc="incident", ) ], model=IncidentRead, ) return incident
def get_by_name_or_raise(*, db_session, alert_in=AlertRead) -> AlertRead: """Returns the alert specified or raises ValidationError.""" alert = get_by_name(db_session=db_session, name=alert_in.name) if not alert: raise ValidationError( [ ErrorWrapper( NotFoundError( msg="Alert not found.", alert=alert_in.name, ), loc="alert", ) ], model=AlertRead, ) return alert
def get_by_slug_or_raise(*, db_session, organization_in=OrganizationRead) -> Organization: """Returns the organization specified or raises ValidationError.""" organization = get_by_name(db_session=db_session, name=organization_in.name) if not organization: raise ValidationError( [ ErrorWrapper( NotFoundError(msg="Organization not found.", organization=organization_in.name), loc="organization", ) ], model=OrganizationRead, ) return organization
def get_by_name_or_raise(*, db_session, project_id: int, incident_type_in=IncidentTypeRead) -> IncidentType: """Returns the incident_type specified or raises ValidationError.""" incident_type = get_by_name(db_session=db_session, project_id=project_id, name=incident_type_in.name) if not incident_type: raise ValidationError( [ ErrorWrapper( NotFoundError(msg="IncidentType not found.", incident_type=incident_type_in.name), loc="incident_type", ) ], model=IncidentTypeRead, ) return incident_type
def get_by_name_or_raise(*, db_session, project_id: int, tag_type_in=TagTypeRead) -> TagType: """Returns the tag_type specified or raises ValidationError.""" tag_type = get_by_name(db_session=db_session, project_id=project_id, name=tag_type_in.name) if not tag_type: raise ValidationError( [ ErrorWrapper( NotFoundError(msg="TagType not found.", tag_type=tag_type_in.name), loc="tag_type", ) ], model=TagTypeRead, ) return tag_type
def list_incidents( user_id: str, user_email: str, channel_id: str, incident_id: int, config: SlackConversationConfiguration = None, command: dict = None, db_session=None, slack_client=None, ): """Returns the list of current active and stable incidents, and closed incidents in the last 24 hours.""" projects = [] incidents = [] args = command["text"].split(" ") # scopes reply to the current incident's project incident = incident_service.get(db_session=db_session, incident_id=incident_id) if incident: # command was run in an incident conversation projects.append(incident.project) else: # command was run in a non-incident conversation if len(args) == 2: project = project_service.get_by_name(db_session=db_session, name=args[1]) if project: projects.append() else: raise ValidationError( [ ErrorWrapper( NotFoundError( msg=f"Project name '{args[1]}' in organization '{args[0]}' not found. Check your spelling." ), loc="project", ) ], model=BaseModel, ) else: projects = project_service.get_all(db_session=db_session) for project in projects: # we fetch active incidents incidents.extend( incident_service.get_all_by_status( db_session=db_session, project_id=project.id, status=IncidentStatus.active ) ) # We fetch stable incidents incidents.extend( incident_service.get_all_by_status( db_session=db_session, project_id=project.id, status=IncidentStatus.stable, ) ) # We fetch closed incidents in the last 24 hours incidents.extend( incident_service.get_all_last_x_hours_by_status( db_session=db_session, project_id=project.id, status=IncidentStatus.closed, hours=24, ) ) blocks = [] blocks.append({"type": "header", "text": {"type": "plain_text", "text": "List of Incidents"}}) if incidents: for incident in incidents: if incident.visibility == Visibility.open: ticket_weblink = resolve_attr(incident, "ticket.weblink") try: blocks.append( { "type": "section", "text": { "type": "mrkdwn", "text": ( f"*<{ticket_weblink}|{incident.name}>*\n" f"*Title*: {incident.title}\n" f"*Type*: {incident.incident_type.name}\n" f"*Priority*: {incident.incident_priority.name}\n" f"*Status*: {incident.status}\n" f"*Incident Commander*: <{incident.commander.individual.weblink}|{incident.commander.individual.name}>\n" f"*Project*: {incident.project.name}" ), }, } ) except Exception as e: log.exception(e) dispatch_slack_service.send_ephemeral_message( slack_client, channel_id, user_id, "Incident List", blocks=blocks, )
def create_or_update( *, db_session, project_in: ProjectRead, role: ParticipantRoleType, incident_roles_in: List[IncidentRoleCreateUpdate], ) -> List[IncidentRole]: """Updates a list of incident role policies.""" role_policies = [] project = project_service.get_by_name_or_raise(db_session=db_session, project_in=project_in) # update/create everybody else for role_policy_in in incident_roles_in: if role_policy_in.id: role_policy = get(db_session=db_session, incident_role_id=role_policy_in.id) if not role_policy: raise ValidationError( [ ErrorWrapper( NotFoundError(msg="Role policy not found."), loc="id", ) ], model=IncidentRoleCreateUpdate, ) else: role_policy = IncidentRole(role=role, project=project) db_session.add(role_policy) role_policy_data = role_policy.dict() update_data = role_policy_in.dict( skip_defaults=True, exclude={ "role", # we don't allow role to be updated "tags", "incident_types", "incident_priorities", "service", "individual", "project", }, ) for field in role_policy_data: if field in update_data: setattr(role_policy, field, update_data[field]) if role_policy_in.tags: tags = [ tag_service.get_by_name_or_raise(db_session=db_session, project_id=project.id, tag_in=t) for t in role_policy_in.tags ] role_policy.tags = tags if role_policy_in.incident_types: incident_types = [ incident_type_service.get_by_name_or_raise( db_session=db_session, project_id=project.id, incident_type_in=i) for i in role_policy_in.incident_types ] role_policy.incident_types = incident_types if role_policy_in.incident_priorities: incident_priorities = [ incident_priority_service.get_by_name_or_raise( db_session=db_session, project_id=project.id, incident_priority_in=i, ) for i in role_policy_in.incident_priorities ] role_policy.incident_priorities = incident_priorities if role_policy_in.service: service = service_service.get_by_external_id_and_project_id_or_raise( db_session=db_session, project_id=project.id, service_in=role_policy_in.service, ) role_policy.service = service if role_policy_in.individual: individual = individual_contact_service.get_by_email_and_project_id_or_raise( db_session=db_session, project_id=project.id, individual_contact_in=role_policy_in.individual, ) role_policy.individual = individual role_policies.append(role_policy) # TODO Add projects # get all current policies in order to detect deletions existing_incident_roles = get_all_by_role(db_session=db_session, role=role, project_id=project.id) for existing_role_policy in existing_incident_roles: for current_role_policy in role_policies: if existing_role_policy.id == current_role_policy.id: break else: db_session.delete(existing_role_policy) db_session.commit() return role_policies