1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/peeringdb_server/deskpro.py
Matt Griswold b07baf3092 Support 202011 (#917)
* install django-grainy

* nsp to grainy first iteration

* Fix validation error message overflow

* Add migration, update views.py and template to add help_text to UI

* nsp to grainy second iteration

* grainy and django-grainy pinned to latest releases

* deskpro ticket cc (#875)

* black formatting

* move ac link to bottom for ticket body

* Fix typo

* Update djangorestframework, peeringdb, django-ratelimit

* Rewrite login view ratelimit decorator

* Relock pipfile

* add list() to make copy of dictionaries before iterating

* respect ix-f url visibilty in ix-f conflict emails

* Add type coercion to settings taken from environment variables

* Add bool handling

* relock pipfile with python3.9
change docker to use python3.9

* Check bool via isinstance

* add ordering to admin search queryset for deskproticket and email

* update settings with envvar_type option

* Add tooltips to add ix and add exchange views (in org)

* Add tooltip to suggest fac view

* get phone information in view

* add missing migration

* add migration and make org a geo model

* Wire normalization to put/create requests for Facility

* Update admin with new address fields

* Refactor serializer using mixin

* Add floor and suite to address API

* Write command to geonormalize existing entries

* Remove unnecessary method from model

* Add floor and suite to views

* Add ignore geo status

* Force refresh for fac and org updates

* adjust frontend typo

* add checking if update needs geosync

* redo error handling for geosync

* remove save keyword from geonormalize command script

* change raw_id_fields

* alternate autocomplete lookup field depending on where inline is called

* remove unnecessary error handling

* Add  csv option

* Fix bug
 with None vs empty string

* add regex parsing for suite and floor conversion

* Add migration that removes geo error as a field

* add geostatus update to command

* Ignore suite floor and address2 changes for api normalization

* update geomodel by removing geo_error

* Black models.py

* Black serializers.py

* remove geocode error from admin

* Add function for reversing pretty speed

* add conversion to export method

* fix typo

* fix speed value feedback after submit

* remove conditional

* Add error handling to create endpoint

* Refine floor and suite parsing regex

* Add geocoding tests

* Add json for tests

* IX-F Importer: Bogus output of "Preview" tool #896

* remove cruft

* black formatting

* IX-F Importer: history of changes per ixlan & netixlan #893

* 6 add geocode to org view

* 4 update geocode without refresh

* Update error display

* Fix bug with formatting translated string

* Add DateTimeFields to model

* Add update signals

* add last updated fields to views and serializers

* Add last updated model migration

* Add the data migration for last updated fields

* add test that tests a normal org user with create org permissions

* grainy to 1.7
django grainy to 1.9.1

* Fix formatting issues

* Adjust var names

* Refactor signals

* Temporary: save override from network model

* Empty vlan lists no longer cause error

* typo in ixf.py

* typo in admin

* Typos in model verbose names

* Add serializer IXLAN validation for ixf_ixp_import_enabled

* Add model validation to IXLan

* relock pipfile

* relock pipfile

* begin signal test file

* Remove full clean from save in ixlan

* use post_reversion_commit signal instead

* remove redundant save override

* remove cruft / debug code

* Add signal tests

* exclude organizations with city missing from commandline geosync

* Skip geosync if the only address information we have is a country

* initial commit for vlan matcher in importer

* Add more tests and remove unused imports

* update tests

* Actually add vlan matching to importer

* Add type checking for speed list and state

* Change how we register connection.state

* add bootstrap options

* add rdap cache command

* remove outdated perm docs

* rdap from master and relock

* propagate rdap settings to peeringdb.settings

* add loaddata for initial fixtures

* user friendly error message on RdapNotFound errors (#497)

* update rdap errors

* django-peeringdb to 2.5.0 and relock

* rdap to 1.2.0 and relock

* fix migration hierarchy

* add ignore_recurse_errors option

* add missing fields to mock
remove cruft missed during merge

* rdap to 1.2.1

* dont geo validate during api tests

* fix tests

* Add test file

* fix merge

* RDAP_SELF_BOOTSTRAP to False while running tests

* black formatted

* run black

* add github actions

* add runs on

Co-authored-by: Stefan Pratter <stefan@20c.com>
Co-authored-by: Elliot Frank <elliot@20c.com>
2021-01-13 14:35:07 -06:00

316 lines
8.0 KiB
Python

"""
DeskPro API Client
"""
import uuid
import re
import requests
import datetime
from django.template import loader
from django.conf import settings
import django.urls
from peeringdb_server.models import DeskProTicket
from peeringdb_server.inet import RdapNotFoundError
def ticket_queue(subject, body, user):
""" queue a deskpro ticket for creation """
ticket = DeskProTicket.objects.create(
subject=f"{settings.EMAIL_SUBJECT_PREFIX}{subject}",
body=body,
user=user,
)
class APIError(IOError):
def __init__(self, msg, data):
super().__init__(msg)
self.data = data
def ticket_queue_asnauto_skipvq(user, org, net, rir_data):
"""
queue deskro ticket creation for asn automation action: skip vq
"""
if isinstance(net, dict):
net_name = net.get("name")
else:
net_name = net.name
if isinstance(org, dict):
org_name = org.get("name")
else:
org_name = org.name
ticket_queue(
f"[ASNAUTO] Network '{net_name}' approved for existing Org '{org_name}'",
loader.get_template("email/notify-pdb-admin-asnauto-skipvq.txt").render(
{"user": user, "org": org, "net": net, "rir_data": rir_data}
),
user,
)
def ticket_queue_asnauto_affil(user, org, net, rir_data):
"""
queue deskro ticket creation for asn automation action: affil
"""
ticket_queue(
"[ASNAUTO] Ownership claim granted to Org '%s' for user '%s'"
% (org.name, user.username),
loader.get_template("email/notify-pdb-admin-asnauto-affil.txt").render(
{"user": user, "org": org, "net": net, "rir_data": rir_data}
),
user,
)
def ticket_queue_asnauto_create(
user, org, net, rir_data, asn, org_created=False, net_created=False
):
"""
queue deskro ticket creation for asn automation action: create
"""
subject = []
if org_created:
subject.append("Organization '%s'" % org.name)
if net_created:
subject.append("Network '%s'" % net.name)
if not subject:
return
subject = ", ".join(subject)
ticket_queue(
"[ASNAUTO] %s created" % subject,
loader.get_template(
"email/notify-pdb-admin-asnauto-entity-creation.txt"
).render(
{
"user": user,
"org": org,
"net": net,
"asn": asn,
"org_created": org_created,
"net_created": net_created,
"rir_data": rir_data,
}
),
user,
)
def ticket_queue_rdap_error(user, asn, error):
if isinstance(error, RdapNotFoundError):
return
error_message = f"{error}"
if re.match("(.+) returned 400", error_message):
return
subject = f"[RDAP_ERR] {user.username} - AS{asn}"
ticket_queue(
subject,
loader.get_template("email/notify-pdb-admin-rdap-error.txt").render(
{"user": user, "asn": asn, "error_details": error_message}
),
user,
)
class APIClient:
def __init__(self, url, key):
self.key = key
self.url = url
@property
def auth_headers(self):
return {"Authorization": f"key {self.key}"}
def parse_response(self, response, many=False):
r_json = response.json()
if "status" in r_json:
if r_json["status"] >= 400:
raise APIError(r_json["message"], r_json)
else:
response.raise_for_status()
data = r_json["data"]
if isinstance(data, list):
if many:
return r_json["data"]
elif data:
return data[0]
else:
return data
def get(self, endpoint, param):
response = requests.get(
f"{self.url}/{endpoint}", params=param, headers=self.auth_headers
)
return self.parse_response(response)
def create(self, endpoint, param):
response = requests.post(
f"{self.url}/{endpoint}", json=param, headers=self.auth_headers
)
return self.parse_response(response)
def require_person(self, user):
person = self.get("people", {"primary_email": user.email})
if not person:
person = self.create(
"people",
{
"primary_email": user.email,
"first_name": user.first_name,
"last_name": user.last_name,
"name": user.full_name,
},
)
return person
def create_ticket(self, ticket):
person = self.require_person(ticket.user)
if not ticket.deskpro_id:
cc = []
for _cc in ticket.cc_set.all():
cc.append(_cc.email)
ticket_response = self.create(
"tickets",
{
"subject": ticket.subject,
"person": {"id": person["id"]},
"status": "awaiting_agent",
"cc": cc,
},
)
ticket.deskpro_ref = ticket_response["ref"]
ticket.deskpro_id = ticket_response["id"]
self.create(
f"tickets/{ticket.deskpro_id}/messages",
{
"message": ticket.body.replace("\n", "<br />\n"),
"person": person["id"],
"format": "html",
},
)
class MockAPIClient(APIClient):
"""
A mock api client for the deskpro API
The IX-F importer uses this when
IXF_SEND_TICKETS=False
"""
def __init__(self, *args, **kwargs):
self.ticket_count = 0
def get(self, endpoint, param):
if endpoint == "people":
return {"id": 1}
return {}
def create(self, endpoint, param):
if endpoint == "tickets":
self.ticket_count += 1
ref = f"{uuid.uuid4()}"
return {"ref": ref[:16], "id": self.ticket_count}
return {}
class FailingMockAPIClient(MockAPIClient):
"""
A mock api client for the deskpro API
that returns an error on post
We use this in our tests, for example
with issue 856.
"""
def __init__(self, *args, **kwargs):
self.ticket_count = 0
def get(self, endpoint, param):
return {"error": "API error with get."}
def create(self, endpoint, param):
return {"error": "API error with create."}
def create_ticket(self, ticket=None):
raise APIError(
"API error when creating ticket.",
{"error": "API error when creating ticket."},
)
def ticket_queue_deletion_prevented(user, instance):
"""
queue deskpro ticket to notify about the prevented
deletion of an object #696
"""
subject = (
f"[PROTECTED] Deletion prevented: "
f"{instance.HandleRef.tag}-{instance.id} "
f"{instance}"
)
# we don't want to spam DeskPRO with tickets when a user
# repeatedly clicks the delete button for an object
#
# so we check if a ticket has recently been sent for it
# and opt out if it falls with in the spam protection
# period defined in settings
period = settings.PROTECTED_OBJECT_NOTIFICATION_PERIOD
now = datetime.datetime.now(datetime.timezone.utc)
max_age = now - datetime.timedelta(hours=period)
ticket = DeskProTicket.objects.filter(
subject=f"{settings.EMAIL_SUBJECT_PREFIX}{subject}"
)
ticket = ticket.filter(created__gt=max_age)
# recent ticket for object exists, bail
if ticket.exists():
return
model_name = instance.__class__.__name__.lower()
# create ticket
ticket_queue(
subject,
loader.get_template("email/notify-pdb-admin-deletion-prevented.txt").render(
{
"user": user,
"instance": instance,
"admin_url": settings.BASE_URL
+ django.urls.reverse(
f"admin:peeringdb_server_{model_name}_change", args=(instance.id,)
),
}
),
user,
)