Exemple #1
0
def create(*, db_session,
           incident_type_in: IncidentTypeCreate) -> IncidentType:
    """Creates an incident type."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=incident_type_in.project)
    incident_type = IncidentType(
        **incident_type_in.dict(
            exclude={
                "commander_service",
                "incident_template_document",
                "executive_template_document",
                "tracking_template_document",
                "review_template_document",
                "project",
            }),
        project=project,
    )

    if incident_type_in.incident_template_document:
        incident_template_document = document_service.get(
            db_session=db_session,
            document_id=incident_type_in.incident_template_document.id)
        incident_type.incident_template_document = incident_template_document

    if incident_type_in.executive_template_document:
        executive_template_document = document_service.get(
            db_session=db_session,
            document_id=incident_type_in.executive_template_document.id)
        incident_type.executive_template_document = executive_template_document

    if incident_type_in.review_template_document:
        review_template_document = document_service.get(
            db_session=db_session,
            document_id=incident_type_in.review_template_document.id)
        incident_type.review_template_document = review_template_document

    if incident_type_in.tracking_template_document:
        tracking_template_document = document_service.get(
            db_session=db_session,
            document_id=incident_type_in.tracking_template_document.id)
        incident_type.tracking_template_document = tracking_template_document

    if incident_type_in.commander_service:
        commander_service = service_service.get(
            db_session=db_session,
            service_id=incident_type_in.commander_service.id)
        incident_type.commander_service = commander_service

    if incident_type_in.liaison_service:
        liaison_service = service_service.get(
            db_session=db_session,
            service_id=incident_type_in.liaison_service.id)
        incident_type.liaison_service = liaison_service

    db_session.add(incident_type)
    db_session.commit()
    return incident_type
Exemple #2
0
def create(*, db_session, tag_type_in: TagTypeCreate) -> TagType:
    """Creates a new tag type."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=tag_type_in.project)
    tag_type = TagType(**tag_type_in.dict(exclude={"project"}),
                       project=project)
    db_session.add(tag_type)
    db_session.commit()
    return tag_type
Exemple #3
0
def create(*, db_session,
           search_filter_in: SearchFilterCreate) -> SearchFilter:
    """Creates a new search filter."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=search_filter_in.project)
    search_filter = SearchFilter(**search_filter_in.dict(exclude={"project"}),
                                 project=project)
    db_session.add(search_filter)
    db_session.commit()
    return search_filter
Exemple #4
0
def get_by_external_id_and_project_name(
        *, db_session, external_id: str,
        project_name: str) -> Optional[Service]:
    """Gets a service by external id (e.g. PagerDuty service id) and project name."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=ProjectRead(name=project_name))
    service = get_by_external_id_and_project_id(db_session=db_session,
                                                external_id=external_id,
                                                project_id=project.id)
    return service
Exemple #5
0
def create(*, db_session,
           source_status_in: SourceStatusCreate) -> SourceStatus:
    """Creates a new status."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=source_status_in.project)
    source_status = SourceStatus(**source_status_in.dict(exclude={"project"}),
                                 project=project)
    db_session.add(source_status)
    db_session.commit()
    return source_status
Exemple #6
0
def create(*, db_session,
           source_transport_in: SourceTransportCreate) -> SourceTransport:
    """Creates a new source transport."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=source_transport_in.project)
    source_transport = SourceTransport(**source_transport_in.dict(
        exclude={"project"}),
                                       project=project)
    db_session.add(source_transport)
    db_session.commit()
    return source_transport
Exemple #7
0
def create(*, db_session,
           source_data_format_in: SourceDataFormatCreate) -> SourceDataFormat:
    """Creates a new source."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=source_data_format_in.project)
    source_data_format = SourceDataFormat(**source_data_format_in.dict(
        exclude={"project"}),
                                          project=project)
    db_session.add(source_data_format)
    db_session.commit()
    return source_data_format
Exemple #8
0
def create(*, db_session,
           incident_priority_in: IncidentPriorityCreate) -> IncidentPriority:
    """Creates an incident priority."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=incident_priority_in.project)
    incident_priority = IncidentPriority(**incident_priority_in.dict(
        exclude={"project"}),
                                         project=project)
    db_session.add(incident_priority)
    db_session.commit()
    return incident_priority
Exemple #9
0
def create(
        *, db_session,
        source_environment_in: SourceEnvironmentCreate) -> SourceEnvironment:
    """Creates a new source."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=source_environment_in.project)
    source_environment = SourceEnvironment(**source_environment_in.dict(
        exclude={"project"}),
                                           project=project)
    db_session.add(source_environment)
    db_session.commit()
    return source_environment
Exemple #10
0
def get_incident_roles(
        *,
        db_session: Session = Depends(get_db),
        role: ParticipantRoleType,
        project_name: str = Query(..., alias="projectName"),
):
    """Get all incident role mappings."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=ProjectRead(name=project_name))
    policies = get_all_by_role(db_session=db_session,
                               role=role,
                               project_id=project.id)
    return {"policies": policies}
Exemple #11
0
def create_instance(
        *, db_session,
        plugin_instance_in: PluginInstanceCreate) -> PluginInstance:
    """Creates a new plugin instance."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=plugin_instance_in.project)
    plugin = get(db_session=db_session, plugin_id=plugin_instance_in.plugin.id)
    plugin_instance = PluginInstance(**plugin_instance_in.dict(
        exclude={"project", "plugin"}),
                                     project=project,
                                     plugin=plugin)
    db_session.add(plugin_instance)
    db_session.commit()
    return plugin_instance
Exemple #12
0
def create(*, db_session, tag_in: TagCreate) -> Tag:
    """Creates a new tag."""
    project = project_service.get_by_name_or_raise(db_session=db_session,
                                                   project_in=tag_in.project)
    tag_type = tag_type_service.get_or_create(db_session=db_session,
                                              tag_type_in=tag_in.tag_type)
    tag = Tag(**tag_in.dict(exclude={"tag_type", "project"}),
              project=project,
              tag_type=tag_type)
    tag.tag_type = tag_type
    tag.project = project
    db_session.add(tag)
    db_session.commit()
    return tag
Exemple #13
0
def create(*, db_session, definition_in: DefinitionCreate) -> Definition:
    """Creates a new definition."""
    terms = [
        term_service.get_or_create(db_session=db_session, term_in=t)
        for t in definition_in.terms
    ]

    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=definition_in.project)
    definition = Definition(**definition_in.dict(exclude={"terms", "project"}),
                            project=project,
                            terms=terms)
    db_session.add(definition)
    db_session.commit()
    return definition
Exemple #14
0
def create(*, db_session, workflow_in: WorkflowCreate) -> Workflow:
    """Creates a new workflow."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=workflow_in.project)
    plugin_instance = plugin_service.get_instance(
        db_session=db_session,
        plugin_instance_id=workflow_in.plugin_instance.id)
    workflow = Workflow(
        **workflow_in.dict(exclude={"plugin_instance", "project"}),
        plugin_instance=plugin_instance,
        project=project,
    )

    db_session.add(workflow)
    db_session.commit()
    return workflow
Exemple #15
0
def create(*, db_session, term_in: TermCreate) -> Term:
    project = project_service.get_by_name_or_raise(db_session=db_session,
                                                   project_in=term_in.project)

    definitions = [
        definition_service.upsert(db_session=db_session, definition_in=d)
        for d in term_in.definitions
    ]

    term = Term(**term_in.dict(exclude={"definitions", "project"}),
                project=project,
                definitions=definitions)

    db_session.add(term)
    db_session.commit()
    return term
Exemple #16
0
def create(*, db_session, team_contact_in: TeamContactCreate) -> TeamContact:
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=team_contact_in.project)
    filters = [
        search_filter_service.get(db_session=db_session, search_filter_id=f.id)
        for f in team_contact_in.filters
    ]

    team = TeamContact(
        **team_contact_in.dict(exclude={"project", "filters"}),
        filters=filters,
        project=project,
    )
    db_session.add(team)
    db_session.commit()
    return team
Exemple #17
0
def create(*, db_session, service_in: ServiceCreate) -> Service:
    """Creates a new service."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=service_in.project)

    filters = [
        search_filter_service.get(db_session=db_session, search_filter_id=f.id)
        for f in service_in.filters
    ]

    service = Service(
        **service_in.dict(exclude={"filters", "project"}),
        filters=filters,
        project=project,
    )
    db_session.add(service)
    db_session.commit()
    return service
Exemple #18
0
def create(*, db_session, notification_in: NotificationCreate) -> Notification:
    """Creates a new notification."""
    filters = []
    if notification_in.filters:
        filters = [
            search_filter_service.get(db_session=db_session,
                                      search_filter_id=f.id)
            for f in notification_in.filters
        ]

    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=notification_in.project)

    notification = Notification(**notification_in.dict(
        exclude={"filters", "project"}),
                                filters=filters,
                                project=project)

    db_session.add(notification)
    db_session.commit()
    return notification
Exemple #19
0
def create(*, db_session, document_in: DocumentCreate) -> Document:
    """Creates a new document."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=document_in.project)

    filters = [
        search_filter_service.get(db_session=db_session, search_filter_id=f.id)
        for f in document_in.filters
    ]

    # set the last reminder to now
    if document_in.evergreen:
        document_in.evergreen_last_reminder_at = datetime.utcnow()

    document = Document(
        **document_in.dict(exclude={"project", "filters"}),
        filters=filters,
        project=project,
    )

    db_session.add(document)
    db_session.commit()
    return document
Exemple #20
0
def create(
        *, db_session,
        individual_contact_in: IndividualContactCreate) -> IndividualContact:
    """Creates an individual."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=individual_contact_in.project)

    contact = IndividualContact(
        **individual_contact_in.dict(exclude={"project", "filters"}),
        project=project,
    )

    if individual_contact_in.filters is not None:
        filters = [
            search_filter_service.get(db_session=db_session,
                                      search_filter_id=f.id)
            for f in individual_contact_in.filters
        ]
        contact.filters = filters

    db_session.add(contact)
    db_session.commit()
    return contact
Exemple #21
0
def run_slack_websocket(organization: str, project: str):
    """Runs the slack websocket process."""
    import asyncio
    from sqlalchemy import true
    from dispatch.project.models import ProjectRead
    from dispatch.project import service as project_service
    from dispatch.plugins.dispatch_slack import socket_mode
    from dispatch.plugins.dispatch_slack.decorators import get_organization_scope_from_slug
    from dispatch.common.utils.cli import install_plugins

    install_plugins()

    session = get_organization_scope_from_slug(organization)

    project = project_service.get_by_name_or_raise(
        db_session=session, project_in=ProjectRead(name=project))

    instances = (session.query(PluginInstance).filter(
        PluginInstance.enabled == true()).filter(
            PluginInstance.project_id == project.id).all())

    instance = None
    for i in instances:
        if i.plugin.slug == "slack-conversation":
            instance = i
            break

    if not instance:
        click.secho(
            f"No slack plugin has been configured for this organization/plugin. Organization: {organization} Project: {project}",
            fg="red",
        )
        return

    click.secho("Slack websocket process started...", fg="blue")
    asyncio.run(socket_mode.run_websocket_process(instance.configuration))
Exemple #22
0
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
Exemple #23
0
def create(*, db_session, source_in: SourceCreate) -> Source:
    """Creates a new source."""
    project = project_service.get_by_name_or_raise(
        db_session=db_session, project_in=source_in.project)

    source = Source(
        **source_in.dict(
            exclude={
                "project",
                "owner",
                "tags",
                "incidents",
                "queries",
                "source_environment",
                "source_data_format",
                "source_transport",
                "source_status",
                "source_type",
            }),
        project=project,
    )

    if source_in.owner:
        source.owner = service_service.get_by_name_or_raise(
            db_session=db_session,
            project_id=project.id,
            service_in=source_in.owner)

    tags = []
    for t in source_in.tags:
        tags.append(
            tag_service.get_by_name_or_raise(db_session=db_session,
                                             tag_in=t,
                                             project_id=project.id))
    source.tags = tags

    incidents = []
    for i in source_in.incidents:
        incidents.append(
            incident_service.get_by_name_or_raise(db_session=db_session,
                                                  incident_in=i,
                                                  project_id=project.id))
    source.incidents = incidents

    queries = []
    for q in source_in.queries:
        queries.append(
            query_service.get_by_name_or_raise(db_session=db_session,
                                               query_in=q,
                                               project_id=project.id))
    source.queries = queries

    if source_in.source_environment:
        source.source_environment = environment_service.get_by_name_or_raise(
            db_session=db_session,
            project_id=project.id,
            source_environment_in=source_in.source_environment,
        )

    if source_in.source_type:
        source.source_type = type_service.get_by_name_or_raise(
            db_session=db_session,
            project_id=project.id,
            source_type_in=source_in.source_type,
        )

    if source_in.source_transport:
        source.source_transport = transport_service.get_by_name_or_raise(
            db_session=db_session,
            project_id=project.id,
            source_transport_in=source_in.source_transport,
        )

    if source_in.source_data_format:
        source.source_data_format = data_format_service.get_by_name_or_raise(
            db_session=db_session,
            project_id=project.id,
            source_data_format_in=source_in.source_data_format,
        )

    if source_in.source_status:
        source.source_status = status_service.get_by_name_or_raise(
            db_session=db_session,
            project_id=project.id,
            source_status_in=source_in.source_status,
        )

    db_session.add(source)
    db_session.commit()
    return source