Beispiel #1
0
def update(*, db_session, service: Service, service_in: ServiceUpdate) -> Service:
    service_data = jsonable_encoder(service)

    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, name=n.name)
        for n in service_in.incident_priorities
    ]
    incident_types = [
        incident_type_service.get_by_name(db_session=db_session, name=n.name)
        for n in service_in.incident_types
    ]
    update_data = service_in.dict(
        skip_defaults=True, exclude={"terms", "incident_priorities", "incident_types"}
    )

    for field in service_data:
        if field in update_data:
            setattr(service, field, update_data[field])

    service.terms = terms
    service.incident_priorities = incident_priorities
    service.incident_types = incident_types
    db_session.add(service)
    db_session.commit()
    return service
Beispiel #2
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)
    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
Beispiel #3
0
def update(
    *,
    db_session,
    individual_contact: IndividualContact,
    individual_contact_in: IndividualContactUpdate,
) -> IndividualContact:
    individual_contact_data = jsonable_encoder(individual_contact_in)

    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
    ]
    update_data = individual_contact_in.dict(
        skip_defaults=True,
        exclude={"terms", "incident_priorities", "incident_types"})

    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
    db_session.add(individual_contact)
    db_session.commit()
    return individual_contact
Beispiel #4
0
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
    ]
    document = Document(
        **document_in.dict(
            exclude={"terms", "incident_priorities", "incident_types"}),
        incident_priorities=incident_priorities,
        incident_types=incident_types,
        terms=terms,
    )
    db_session.add(document)
    db_session.commit()
    return document
Beispiel #5
0
def update(*, db_session, document: Document,
           document_in: DocumentUpdate) -> Document:
    """Updates a document."""
    document_data = jsonable_encoder(document)
    update_data = document_in.dict(
        skip_defaults=True,
        exclude={"terms", "incident_priorities", "incident_types"})

    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
    ]

    for field in document_data:
        if field in update_data:
            setattr(document, field, update_data[field])

    document.terms = terms
    document.incident_priorities = incident_priorities
    document.incident_types = incident_types

    db_session.add(document)
    db_session.commit()
    return document
Beispiel #6
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
Beispiel #7
0
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
Beispiel #8
0
def update(*, db_session, team_contact: TeamContact,
           team_contact_in: TeamContactUpdate) -> TeamContact:
    team_contact_data = jsonable_encoder(team_contact)

    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,
            project_id=team_contact.project.id,
            name=n.name) for n in team_contact_in.incident_priorities
    ]
    incident_types = [
        incident_type_service.get_by_name(db_session=db_session,
                                          project_id=team_contact.project.id,
                                          name=n.name)
        for n in team_contact_in.incident_types
    ]
    update_data = team_contact_in.dict(
        skip_defaults=True,
        exclude={"terms", "incident_priorities", "incident_types"})

    for field in team_contact_data:
        if field in update_data:
            setattr(team_contact, field, update_data[field])

    team_contact.terms = terms
    team_contact.incident_priorities = incident_priorities
    team_contact.incident_types = incident_types
    db_session.add(team_contact)
    db_session.commit()
    return team_contact
Beispiel #9
0
def update(*, db_session, service: Service, service_in: ServiceUpdate) -> Service:
    """Updates an existing service."""
    service_data = jsonable_encoder(service)

    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, name=n.name)
        for n in service_in.incident_priorities
    ]
    incident_types = [
        incident_type_service.get_by_name(db_session=db_session, name=n.name)
        for n in service_in.incident_types
    ]
    update_data = service_in.dict(
        skip_defaults=True, exclude={"terms", "incident_priorities", "incident_types"}
    )

    if service_in.is_active:  # user wants to enable the service
        oncall_plugin = plugin_service.get_by_slug(db_session=db_session, slug=service_in.type)
        if not oncall_plugin.enabled:
            raise InvalidConfiguration(
                f"Cannot enable service: {service.name}. Its associated plugin {oncall_plugin.title} is not enabled."
            )

    for field in service_data:
        if field in update_data:
            setattr(service, field, update_data[field])

    service.terms = terms
    service.incident_priorities = incident_priorities
    service.incident_types = incident_types
    db_session.add(service)
    db_session.commit()
    return service
Beispiel #10
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
Beispiel #11
0
def update(*, db_session, incident: Incident,
           incident_in: IncidentUpdate) -> Incident:
    """Updates an existing incident."""
    incident_priority = incident_priority_service.get_by_name(
        db_session=db_session, name=incident_in.incident_priority.name)

    incident_type = incident_type_service.get_by_name(
        db_session=db_session, name=incident_in.incident_type.name)

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

    terms = []
    for t in incident_in.terms:
        terms.append(
            term_service.get_or_create(db_session=db_session,
                                       term_in=TermUpdate(**t)))

    duplicates = []
    for d in incident_in.duplicates:
        duplicates.append(get(db_session=db_session, incident_id=d.id))

    update_data = incident_in.dict(
        skip_defaults=True,
        exclude={
            "incident_type",
            "incident_priority",
            "commander",
            "reporter",
            "status",
            "visibility",
            "tags",
            "terms",
            "duplicates",
        },
    )

    for field in update_data.keys():
        setattr(incident, field, update_data[field])

    incident.terms = terms
    incident.tags = tags
    incident.duplicates = duplicates

    incident.status = incident_in.status
    incident.visibility = incident_in.visibility

    incident.incident_priority = incident_priority
    incident.incident_type = incident_type

    db_session.add(incident)
    db_session.commit()

    return incident
Beispiel #12
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
    ]
    definition = Definition(**definition_in.dict(exclude={"terms"}),
                            terms=terms)
    db_session.add(definition)
    db_session.commit()
    return definition
Beispiel #13
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
Beispiel #14
0
def update(*, db_session, definition: Definition,
           definition_in: DefinitionUpdate) -> Definition:
    definition_data = jsonable_encoder(definition)

    terms = [
        term_service.get_or_create(db_session=db_session, term_in=t)
        for t in definition_in.terms
    ]
    update_data = definition_in.dict(skip_defaults=True, exclude={"terms"})

    for field in definition_data:
        if field in update_data:
            setattr(definition, field, update_data[field])

    definition.terms = terms
    db_session.add(definition)
    db_session.commit()
    return definition
Beispiel #15
0
def create(*, db_session, service_in: ServiceCreate) -> Service:
    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, name=n.name)
        for n in service_in.incident_priorities
    ]
    incident_types = [
        incident_type_service.get_by_name(db_session=db_session, name=n.name)
        for n in service_in.incident_types
    ]
    service = Service(
        **service_in.dict(exclude={"terms", "incident_priorities", "incident_types"}),
        incident_priorities=incident_priorities,
        incident_types=incident_types,
        terms=terms,
    )
    db_session.add(service)
    db_session.commit()
    return service
Beispiel #16
0
def update(*, db_session, document: Document,
           document_in: DocumentUpdate) -> Document:
    """Updates a document."""
    # reset the last reminder to now
    if document_in.evergreen:
        if not document.evergreen:
            document_in.evergreen_last_reminder_at = datetime.utcnow()

    document_data = jsonable_encoder(document)
    update_data = document_in.dict(
        skip_defaults=True,
        exclude={"terms", "incident_priorities", "incident_types"})

    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,
                                              project_id=n.project.id,
                                              name=n.name)
        for n in document_in.incident_priorities
    ]
    incident_types = [
        incident_type_service.get_by_name(db_session=db_session,
                                          project_id=n.project.id,
                                          name=n.name)
        for n in document_in.incident_types
    ]

    for field in document_data:
        if field in update_data:
            setattr(document, field, update_data[field])

    document.terms = terms
    document.incident_priorities = incident_priorities
    document.incident_types = incident_types

    db_session.add(document)
    db_session.commit()
    return document
Beispiel #17
0
def update(*, db_session, incident: Incident, incident_in: IncidentUpdate) -> Incident:
    """Updates an existing incident."""
    incident_priority = incident_priority_service.get_by_name(
        db_session=db_session,
        project_id=incident.project.id,
        name=incident_in.incident_priority.name,
    )

    if not incident_priority.enabled:
        raise Exception("Incident priority must be enabled.")

    incident_type = incident_type_service.get_by_name(
        db_session=db_session, project_id=incident.project.id, name=incident_in.incident_type.name
    )

    if not incident_type.enabled:
        raise Exception("Incident type must be enabled.")

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

    terms = []
    for t in incident_in.terms:
        terms.append(term_service.get_or_create(db_session=db_session, term_in=TermUpdate(**t)))

    duplicates = []
    for d in incident_in.duplicates:
        duplicates.append(get(db_session=db_session, incident_id=d.id))

    incident_costs = []
    for incident_cost in incident_in.incident_costs:
        incident_costs.append(
            incident_cost_service.get_or_create(
                db_session=db_session, incident_cost_in=incident_cost
            )
        )

    update_data = incident_in.dict(
        skip_defaults=True,
        exclude={
            "commander",
            "duplicates",
            "incident_costs",
            "incident_priority",
            "incident_type",
            "reporter",
            "status",
            "tags",
            "terms",
            "visibility",
            "project",
        },
    )

    for field in update_data.keys():
        setattr(incident, field, update_data[field])

    incident.duplicates = duplicates
    incident.incident_costs = incident_costs
    incident.incident_priority = incident_priority
    incident.incident_type = incident_type
    incident.status = incident_in.status
    incident.tags = tags
    incident.terms = terms
    incident.visibility = incident_in.visibility

    db_session.add(incident)
    db_session.commit()

    return incident