コード例 #1
0
def create(*, db_session, service_in: ServiceCreate) -> Service:
    """Creates a new service."""
    project = project_service.get_by_name(db_session=db_session,
                                          name=service_in.project.name)
    terms = [
        term_service.get_or_create(db_session=db_session, term_in=t)
        for t in service_in.terms
    ]
    incident_priorities = [
        incident_priority_service.get_by_name(db_session=db_session,
                                              project_id=project.id,
                                              name=n.name)
        for n in service_in.incident_priorities
    ]
    incident_types = [
        incident_type_service.get_by_name(db_session=db_session,
                                          project_id=project.id,
                                          name=n.name)
        for n in service_in.incident_types
    ]
    service = Service(
        **service_in.dict(exclude={
            "terms", "incident_priorities", "incident_types", "project"
        }),
        incident_priorities=incident_priorities,
        incident_types=incident_types,
        terms=terms,
        project=project,
    )
    db_session.add(service)
    db_session.commit()
    return service
コード例 #2
0
ファイル: service.py プロジェクト: kangyupeng/dispatch
def create(*, db_session, document_in: DocumentCreate) -> Document:
    """Creates a new document."""
    terms = [
        term_service.get_or_create(db_session=db_session, term_in=t)
        for t in document_in.terms
    ]
    incident_priorities = [
        incident_priority_service.get_by_name(db_session=db_session,
                                              name=n.name)
        for n in document_in.incident_priorities
    ]
    incident_types = [
        incident_type_service.get_by_name(db_session=db_session, name=n.name)
        for n in document_in.incident_types
    ]
    project = project_service.get_by_name(db_session=db_session,
                                          name=document_in.project.name)

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

    document = Document(
        **document_in.dict(exclude={
            "terms", "incident_priorities", "incident_types", "project"
        }),
        incident_priorities=incident_priorities,
        incident_types=incident_types,
        terms=terms,
        project=project,
    )

    db_session.add(document)
    db_session.commit()
    return document
コード例 #3
0
ファイル: views.py プロジェクト: Netflix/dispatch
def report_incident(
    db_session: SessionLocal,
    channel_id: str,
    project_name: str = None,
    title: str = None,
    description: str = None,
):
    """Builds all blocks required for the reporting incident modal."""
    project = project_service.get_by_name(db_session=db_session,
                                          name=project_name)
    modal_template = {
        "type":
        "modal",
        "title": {
            "type": "plain_text",
            "text": "Incident Report"
        },
        "blocks": [
            {
                "type":
                "context",
                "elements": [{
                    "type":
                    "mrkdwn",
                    "text":
                    "If you suspect an incident and require help, "
                    "please fill out the following to the best of your abilities.",
                }],
            },
            title_input_block(initial_value=title),
            description_input_block(initial_value=description),
            project_select_block(db_session=db_session,
                                 initial_option=project),
        ],
        "close": {
            "type": "plain_text",
            "text": "Cancel"
        },
        "submit": {
            "type": "plain_text",
            "text": "Submit"
        },
        "callback_id":
        ReportIncidentCallbackId.update_view,
        "private_metadata":
        json.dumps({"channel_id": str(channel_id)}),
    }

    # switch from update to submit when we have a project
    if project:
        modal_template["callback_id"] = ReportIncidentCallbackId.submit_form
        modal_template["blocks"] += [
            incident_type_select_block(db_session=db_session,
                                       project_id=project.id),
            incident_priority_select_block(db_session=db_session,
                                           project_id=project.id),
            tag_multi_select_block(),
        ]

    return modal_template
コード例 #4
0
def create(*, db_session, team_contact_in: TeamContactCreate) -> TeamContact:
    terms = [
        term_service.get_or_create(db_session=db_session, term_in=t)
        for t in team_contact_in.terms
    ]
    incident_priorities = [
        incident_priority_service.get_by_name(db_session=db_session,
                                              name=n.name)
        for n in team_contact_in.incident_priorities
    ]
    incident_types = [
        incident_type_service.get_by_name(db_session=db_session, name=n.name)
        for n in team_contact_in.incident_types
    ]

    project = project_service.get_by_name(db_session=db_session,
                                          name=team_contact_in.project.name)
    team = TeamContact(
        **team_contact_in.dict(exclude={
            "terms", "incident_priorities", "incident_types", "project"
        }),
        project=project,
        terms=terms,
        incident_types=incident_types,
        incident_priorities=incident_priorities,
    )
    db_session.add(team)
    db_session.commit()
    return team
コード例 #5
0
ファイル: service.py プロジェクト: wuchong718/dispatch
def create(*, db_session, document_in: DocumentCreate) -> Document:
    """Creates a new document."""
    project = None
    if document_in.project:
        project = project_service.get_by_name(db_session=db_session,
                                              name=document_in.project.name)

    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
コード例 #6
0
def create(*, db_session,
           incident_type_in: IncidentTypeCreate) -> IncidentType:
    """Creates an incident type."""
    project = project_service.get_by_name(db_session=db_session,
                                          name=incident_type_in.project.name)
    incident_type = IncidentType(
        **incident_type_in.dict(
            exclude={"commander_service", "template_document", "project"}),
        project=project,
    )

    if incident_type_in.template_document:
        template_document = document_service.get(
            db_session=db_session,
            document_id=incident_type_in.template_document.id)
        incident_type.template_document = 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
コード例 #7
0
ファイル: service.py プロジェクト: Netflix/dispatch
def create_or_update_project_default(*, db_session, user: DispatchUser,
                                     user_project_in: UserProject):
    """Creates a new user project or updates an existing one."""
    if user_project_in.project.id:
        project_id = user_project_in.project.id
    else:
        project = project_service.get_by_name(
            db_session=db_session, name=user_project_in.project.name)
        project_id = project.id

    user_project = (db_session.query(DispatchUserProject).filter(
        DispatchUserProject.dispatch_user_id == user.id, ).filter(
            DispatchUserProject.project_id == project_id).one_or_none())

    if not user_project:
        user_project = DispatchUserProject(
            dispatch_user_id=user.id,
            project_id=project_id,
            default=True,
        )
        db_session.add(user_project)
        return user_project

    user_project.default = user_project_in.default
    return user_project
コード例 #8
0
ファイル: service.py プロジェクト: kangyupeng/dispatch
def create(
        *, db_session,
        individual_contact_in: IndividualContactCreate) -> IndividualContact:
    """Creates an individual."""
    project = project_service.get_by_name(
        db_session=db_session, name=individual_contact_in.project.name)
    terms = [
        term_service.get_or_create(db_session=db_session, term_in=t)
        for t in individual_contact_in.terms
    ]
    incident_priorities = [
        incident_priority_service.get_by_name(db_session=db_session,
                                              name=n.name)
        for n in individual_contact_in.incident_priorities
    ]
    incident_types = [
        incident_type_service.get_by_name(db_session=db_session, name=n.name)
        for n in individual_contact_in.incident_types
    ]
    contact = IndividualContact(
        **individual_contact_in.dict(exclude={
            "terms", "incident_priorities", "incident_types", "project"
        }),
        terms=terms,
        incident_types=incident_types,
        incident_priorities=incident_priorities,
        project=project,
    )
    db_session.add(contact)
    db_session.commit()
    return contact
コード例 #9
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(db_session=db_session,
                                          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
コード例 #10
0
def create(*, db_session,
           search_filter_in: SearchFilterCreate) -> SearchFilter:
    """Creates a new search filter."""
    project = project_service.get_by_name(db_session=db_session,
                                          name=search_filter_in.project.name)
    search_filter = SearchFilter(**search_filter_in.dict(exclude={"project"}),
                                 project=project)
    db_session.add(search_filter)
    db_session.commit()
    return search_filter
コード例 #11
0
ファイル: service.py プロジェクト: wuchong718/dispatch
def create(*, db_session,
           incident_priority_in: IncidentPriorityCreate) -> IncidentPriority:
    """Creates an incident priority."""
    project = project_service.get_by_name(
        db_session=db_session, name=incident_priority_in.project.name)
    incident_priority = IncidentPriority(**incident_priority_in.dict(
        exclude={"project"}),
                                         project=project)
    db_session.add(incident_priority)
    db_session.commit()
    return incident_priority
コード例 #12
0
def create(*, db_session, tag_in: TagCreate) -> Tag:
    tag_type = tag_type_service.get_by_name(db_session=db_session,
                                            name=tag_in.tag_type.name)
    project = project_service.get_by_name(db_session=db_session,
                                          name=tag_in.project.name)
    tag = Tag(**tag_in.dict(exclude={"tag_type", "project"}),
              project=project,
              tag_type=tag_type)
    db_session.add(tag)
    db_session.commit()
    return tag
コード例 #13
0
def create(*, db_session, tag_type_in: TagTypeCreate) -> TagType:
    """
    Creates a new tag type.
    """
    project = project_service.get_by_name(db_session=db_session,
                                          name=tag_type_in.project.name)
    tag_type = TagType(**tag_type_in.dict(exclude={"project"}),
                       project=project)
    db_session.add(tag_type)
    db_session.commit()
    return tag_type
コード例 #14
0
def create(*, db_session, term_in: TermCreate) -> Term:
    project = project_service.get_by_name(db_session=db_session, name=term_in.project.name)
    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
コード例 #15
0
ファイル: service.py プロジェクト: kangyupeng/dispatch
def create(*, db_session, workflow_in: WorkflowCreate) -> Workflow:
    """Creates a new workflow."""
    plugin = plugin_service.get(db_session=db_session, plugin_id=workflow_in.plugin.id)
    project = project_service.get_by_name(db_session=db_session, name=workflow_in.project.name)
    workflow = Workflow(
        **workflow_in.dict(exclude={"plugin", "project"}),
        plugin=plugin,
        project=project,
    )

    db_session.add(workflow)
    db_session.commit()
    return workflow
コード例 #16
0
ファイル: service.py プロジェクト: kangyupeng/dispatch
def create(*, db_session,
           incident_cost_type_in: IncidentCostTypeCreate) -> IncidentCostType:
    """
    Creates a new incident cost type.
    """
    project = project_service.get_by_name(
        db_session=db_session, name=incident_cost_type_in.project.name)
    incident_cost_type = IncidentCostType(**incident_cost_type_in.dict(
        exclude={"project"}),
                                          project=project)
    db_session.add(incident_cost_type)
    db_session.commit()
    return incident_cost_type
コード例 #17
0
def create(*, db_session, definition_in: DefinitionCreate) -> 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(db_session=db_session,
                                          name=definition_in.project.name)
    definition = Definition(**definition_in.dict(exclude={"terms", "project"}),
                            project=project,
                            terms=terms)
    db_session.add(definition)
    db_session.commit()
    return definition
コード例 #18
0
def create_instance(
        *, db_session,
        plugin_instance_in: PluginInstanceCreate) -> PluginInstance:
    """Creates a new plugin instance."""
    project = project_service.get_by_name(db_session=db_session,
                                          name=plugin_instance_in.project.name)
    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
コード例 #19
0
def create(*, db_session, team_contact_in: TeamContactCreate) -> TeamContact:
    project = project_service.get_by_name(db_session=db_session,
                                          name=team_contact_in.project.name)
    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
コード例 #20
0
def create(*, db_session, service_in: ServiceCreate) -> Service:
    """Creates a new service."""
    project = project_service.get_by_name(db_session=db_session,
                                          name=service_in.project.name)

    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
コード例 #21
0
def update(
    *,
    db_session,
    individual_contact: IndividualContact,
    individual_contact_in: IndividualContactUpdate,
) -> IndividualContact:
    individual_contact_data = jsonable_encoder(individual_contact_in)

    project = project_service.get_by_name(
        db_session=db_session, name=individual_contact_in.project.name)

    terms = [
        term_service.get_or_create(db_session=db_session, term_in=t)
        for t in individual_contact_in.terms
    ]
    incident_priorities = [
        incident_priority_service.get_by_name(db_session=db_session,
                                              project_id=project.id,
                                              name=n.name)
        for n in individual_contact_in.incident_priorities
    ]
    incident_types = [
        incident_type_service.get_by_name(db_session=db_session,
                                          project_id=project.id,
                                          name=n.name)
        for n in individual_contact_in.incident_types
    ]
    update_data = individual_contact_in.dict(
        skip_defaults=True,
        exclude={"terms", "incident_priorities", "incident_types", "project"})

    for field in individual_contact_data:
        if field in update_data:
            setattr(individual_contact, field, update_data[field])

    individual_contact.terms = terms
    individual_contact.incident_types = incident_types
    individual_contact.incident_priorities = incident_priorities
    individual_contact.project = project
    db_session.add(individual_contact)
    db_session.commit()
    return individual_contact
コード例 #22
0
def create(
        *, db_session,
        individual_contact_in: IndividualContactCreate) -> IndividualContact:
    """Creates an individual."""
    project = project_service.get_by_name(
        db_session=db_session, name=individual_contact_in.project.name)

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

    contact = IndividualContact(
        **individual_contact_in.dict(exclude={"project", "filters"}),
        filters=filters,
        project=project,
    )
    db_session.add(contact)
    db_session.commit()
    return contact
コード例 #23
0
def create(*, db_session,
           incident_cost_in: IncidentCostCreate) -> IncidentCost:
    """
    Creates a new incident cost.
    """
    project = project_service.get_by_name(db_session=db_session,
                                          name=incident_cost_in.project.name)
    incident_cost_type = incident_cost_type_service.get_by_name(
        db_session=db_session,
        project_id=project.id,
        incident_cost_type_name=incident_cost_in.incident_cost_type.name,
    )
    incident_cost = IncidentCost(
        **incident_cost_in.dict(exclude={"incident_cost_type", "project"}),
        incident_cost_type=incident_cost_type,
        project=project,
    )
    db_session.add(incident_cost)
    db_session.commit()
    return incident_cost
コード例 #24
0
def create(*, db_session, notification_in: NotificationCreate) -> Notification:
    """Creates a new notification."""
    filters = []
    if notification_in.filters:
        filters = [
            search_service.get(db_session=db_session, search_filter_id=f.id)
            for f in notification_in.filters
        ]

    project = project_service.get_by_name(db_session=db_session,
                                          name=notification_in.project.name)

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

    db_session.add(notification)
    db_session.commit()
    return notification
コード例 #25
0
ファイル: service.py プロジェクト: Netflix/dispatch
def create_or_update_project_role(*, db_session, user: DispatchUser,
                                  role_in: UserProject):
    """Creates a new project role or updates an existing role."""
    if not role_in.project.id:
        project = project_service.get_by_name(db_session=db_session,
                                              name=role_in.project.name)
        project_id = project.id
    else:
        project_id = role_in.project.id

    project_role = (db_session.query(DispatchUserProject).filter(
        DispatchUserProject.dispatch_user_id == user.id, ).filter(
            DispatchUserProject.project_id == project_id).one_or_none())

    if not project_role:
        return DispatchUserProject(
            project_id=project_id,
            role=role_in.role,
        )
    project_role.role = role_in.role
    return project_role
コード例 #26
0
ファイル: service.py プロジェクト: mohamadalipour/dispatch
def create(
    *,
    db_session,
    project: str,
    incident_priority: str,
    incident_type: str,
    reporter_email: str,
    title: str,
    status: str,
    description: str,
    tags: List[dict],
    visibility: str = None,
) -> Incident:
    """Creates a new incident."""
    # We get the incident type by name
    if not project:
        project = project_service.get_default(db_session=db_session)
        if not project:
            raise Exception("No project specificed and no default has been defined.")

    else:
        project = project_service.get_by_name(db_session=db_session, name=project["name"])

    if not incident_type:
        incident_type = incident_type_service.get_default(db_session=db_session)
        if not incident_type:
            raise Exception("No incident type specified and no default has been defined.")
    else:
        incident_type = incident_type_service.get_by_name(
            db_session=db_session, name=incident_type["name"]
        )

    # We get the incident priority by name
    if not incident_priority:
        incident_priority = incident_priority_service.get_default(db_session=db_session)
        if not incident_priority:
            raise Exception("No incident priority specified and no default has been defined.")
    else:
        incident_priority = incident_priority_service.get_by_name(
            db_session=db_session, name=incident_priority["name"]
        )

    if not visibility:
        visibility = incident_type.visibility

    tag_objs = []
    for t in tags:
        tag_objs.append(tag_service.get_or_create(db_session=db_session, tag_in=TagCreate(**t)))

    # We create the incident
    incident = Incident(
        title=title,
        description=description,
        status=status,
        incident_type=incident_type,
        incident_priority=incident_priority,
        visibility=visibility,
        tags=tag_objs,
        project=project,
    )
    db_session.add(incident)
    db_session.commit()

    event_service.log(
        db_session=db_session,
        source="Dispatch Core App",
        description="Incident created",
        incident_id=incident.id,
    )

    # Add other incident roles (e.g. commander and liaison)
    assign_incident_role(db_session, incident, reporter_email, ParticipantRoleType.reporter)

    assign_incident_role(
        db_session, incident, reporter_email, ParticipantRoleType.incident_commander
    )

    assign_incident_role(db_session, incident, reporter_email, ParticipantRoleType.liaison)

    return incident
コード例 #27
0
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,
    )