示例#1
0
def update(*, db_session, service: Service,
           service_in: ServiceUpdate) -> Service:
    """Updates an existing service."""
    service_data = jsonable_encoder(service)

    update_data = service_in.dict(skip_defaults=True, exclude={"filters"})

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

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

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

    service.filters = filters
    db_session.add(service)
    db_session.commit()
    return service
示例#2
0
    def get(
        self,
        incident_type: IncidentType,
        incident_priority: IncidentPriority,
        incident_description: str,
        project: Project,
        db_session=None,
    ):
        """Fetches participants from Dispatch."""
        route_in = {
            "text": incident_description,
            "context": {
                "incident_priorities": [incident_priority],
                "incident_types": [incident_type],
                "terms": [],
                "project": project,
            },
        }

        route_in = RouteRequest(**route_in)
        recommendation = route_service.get(db_session=db_session,
                                           route_in=route_in)

        log.debug(f"Recommendation: {recommendation}")
        individual_contacts = [(x, None)
                               for x in recommendation.individual_contacts]
        # we need to resolve our service contacts to individuals
        for s in recommendation.service_contacts:
            plugin_instance = plugin_service.get_active_instance_by_slug(
                db_session=db_session, slug=s.type, project_id=project.id)

            if plugin_instance:
                if plugin_instance.enabled:
                    log.debug(
                        f"Resolving service contact. ServiceContact: {s}")
                    individual_email = plugin_instance.instance.get(
                        s.external_id)

                    individual = individual_service.get_or_create(
                        db_session=db_session, email=individual_email)
                    individual_contacts.append((individual, s))
                    recommendation.individual_contacts.append(individual)
                else:
                    log.warning(
                        f"Skipping service contact. Service: {s.name} Reason: Associated service plugin not enabled."
                    )
            else:
                log.warning(
                    f"Skipping service contact. Service: {s.name} Reason: Associated service plugin not found."
                )

        db_session.commit()
        return list(individual_contacts), list(recommendation.team_contacts)
示例#3
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,
                                              project_id=service.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=service.project.id,
                                          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_instance = plugin_service.get_active_instance_by_slug(
            db_session=db_session,
            slug=service_in.type,
            project_id=service.project.id)
        if not oncall_plugin_instance.enabled:
            raise InvalidConfiguration(
                f"Cannot enable service: {service.name}. Its associated plugin {oncall_plugin_instance.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
示例#4
0
    def get(
        self,
        incident: Incident,
        db_session=None,
    ):
        """Fetches participants from Dispatch."""
        models = [
            (IndividualContact, IndividualContactRead),
            (Service, ServiceRead),
            (TeamContact, TeamContactRead),
        ]
        recommendation = route_service.get(db_session=db_session,
                                           incident=incident,
                                           models=models)

        log.debug(f"Recommendation: {recommendation}")

        individual_contacts = []
        team_contacts = []
        for match in recommendation.matches:
            if match.resource_type == TeamContact.__name__:
                team = team_service.get_or_create(
                    db_session=db_session,
                    email=match.resource_state["email"],
                    incident=incident)
                team_contacts.append(team)

            if match.resource_type == IndividualContact.__name__:
                individual = individual_service.get_or_create(
                    db_session=db_session,
                    email=match.resource_state["email"],
                    incident=incident)

                individual_contacts.append((individual, None))

            # we need to do more work when we have a service
            if match.resource_type == Service.__name__:
                plugin_instance = plugin_service.get_active_instance_by_slug(
                    db_session=db_session,
                    slug=match.resource_state["type"],
                    project_id=incident.project.id,
                )

                if plugin_instance:
                    if plugin_instance.enabled:
                        log.debug(
                            f"Resolving service contact. ServiceContact: {match.resource_state}"
                        )
                        # ensure that service is enabled
                        service = service_service.get_by_external_id_and_project_id(
                            db_session=db_session,
                            external_id=match.resource_state["external_id"],
                            project_id=incident.project_id,
                        )
                        if service.is_active:
                            individual_email = plugin_instance.instance.get(
                                match.resource_state["external_id"])

                            individual = individual_service.get_or_create(
                                db_session=db_session,
                                email=individual_email,
                                incident=incident)

                            individual_contacts.append(
                                (individual, match.resource_state["id"]))
                    else:
                        log.warning(
                            f"Skipping service contact. Service: {match.resource_state['name']} Reason: Associated service plugin not enabled."
                        )
                else:
                    log.warning(
                        f"Skipping service contact. Service: {match.resource_state['name']} Reason: Associated service plugin not found."
                    )

        db_session.commit()
        return individual_contacts, team_contacts