mirror of
https://github.com/peeringdb/peeringdb.git
synced 2024-05-11 05:55:09 +00:00
* fix issue where ix-f import would raise suggestions ipaddresses not that ixlan (#764) * IX-F Suggestions: Leaving the editor and returning to it via back button issues (#765) * IX-F importer: Clicking "Preview" (IXP Update Tools) on /net/ page resulted in 170 ticket resolutions (#769) More robust testing * black formatting (was lost after pyupgrade) * Add regex searching to deskpro ticket subjects * Change operational error * IX-F suggestions: consolidate delete+add (#770) * Add reset functions to commandline tool * Fix commandline tool bugs * Fix reset commandline tool bugs * add commandline tool * Ixlan needs to be set for import commandline tool * Add email model * Add admin view to emails * Allow network and ix to be null * save emails as part of ixf import * Add email model * Add email delete * add iregex search and better comments * fix ixlan selection for import * redefine migration dependencies for this branch * only enable search w start and end char * Add caption to regex search * Remove delete all ixfmemberdata option * [beta] IX-F importer: don't bother about missing IPv{4,6} address when network is not doing IPv{4,6} (#771) * Add cmdline tests * Resolve email conflicts * Add cmd tool reset tests * add autocomplete to commandline tool * Fix email bugs * Fix email migrations * Fix typos * [beta] IX-F importer: prevent Admin Committee overload by initially limiting importer to IXes enabled by AC (#772) * Finalize regex search for emails and deskprotickets * Fix keyword bug * fix typo * protocol-conflict will now be handled in the notification consolidation 771 changes where if the network indicates neither ipv4 nor ipv6 support, it is handled as supporting both (eg the network didnt configure these at all) realised that the importer command re instantiates the `Importer` class for each ixlan it processes, so moved the sending of consolidated notifications (#772) out of the `update` function and into the command itself after its done processing all the ixlans. This means for tests you will need to call `importer.notify_proposals` after `importer.update` to test the consolidated notifications. fixed several MultipleObjectsReturned errors when network switch protocol support in between imports * should be checking for "ix" in the form data (#773) * Fix cmd ixf tests * fix issue in log_peer * Add commit check for reset tool * fix importer bugs * remove dupe IXFImportEmail definition * ixfimportemail support ix__name and net__name searching * ticket resolution responses * Add commit to command ixf import changes * fix modify entry header * remove whitespace in notification about remote data changes * Begin updating tests * ixf-import command line tool to queue * refactor conflict inserts * Update import protocol tests, including tests for 770 * More test edits * Change cmd tests * better ixfmemberdata error handling and fix some test data * dont reset the same ixfmemberdata requirement * fix many bugs add many tests * remove debug message * fix bug during import when consolidating delete+add * fix perfomance issue in IXFMemberData listing * dont show reset flags on prod env * Add regex search tests * Add 772 tests * remove debug output * fix `test_resolve_deskpro_ticket` test * black formatting * remove dupe import * fix issue with unique constraint error handling * add test for ixp / network ip protocol notification * add missing test data Co-authored-by: Stefan Pratter <stefan@20c.com> Co-authored-by: Elliot Frank <elliot@20c.com>
281 lines
7.3 KiB
Python
281 lines
7.3 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:
|
|
ticket_response = self.create(
|
|
"tickets",
|
|
{
|
|
"subject": ticket.subject,
|
|
"person": {"id": person["id"]},
|
|
"status": "awaiting_agent",
|
|
},
|
|
)
|
|
|
|
ticket.deskpro_ref = ticket_response["ref"]
|
|
ticket.deskpro_id = ticket_response["id"]
|
|
|
|
self.create(
|
|
"tickets/{}/messages".format(ticket.deskpro_id),
|
|
{
|
|
"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 = "{}".format(uuid.uuid4())
|
|
return {"ref": ref[:16], "id": self.ticket_count}
|
|
return {}
|
|
|
|
|
|
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 dont 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,
|
|
)
|