|
| 1 | +"""AquaComms parser.""" |
| 2 | +import hashlib |
| 3 | +import logging |
| 4 | +import quopri |
| 5 | +import re |
| 6 | + |
| 7 | +import bs4 # type: ignore |
| 8 | + |
| 9 | +from dateutil import parser |
| 10 | + |
| 11 | +from circuit_maintenance_parser.parser import CircuitImpact, EmailSubjectParser, Impact, Status, Text |
| 12 | + |
| 13 | +# pylint: disable=too-many-nested-blocks, too-many-branches |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +class SubjectParserAWS1(EmailSubjectParser): |
| 19 | + """Subject parser for AWS notifications.""" |
| 20 | + |
| 21 | + def parse_subject(self, subject): |
| 22 | + """Parse subject. |
| 23 | +
|
| 24 | + Example: AWS Direct Connect Planned Maintenance Notification [AWS Account: 00000001] |
| 25 | + """ |
| 26 | + data = {} |
| 27 | + search = re.search(r"\[AWS Account ?I?D?: ([0-9]+)\]", subject) |
| 28 | + if search: |
| 29 | + data["account"] = search.group(1) |
| 30 | + return [data] |
| 31 | + |
| 32 | + |
| 33 | +class TextParserAWS1(Text): |
| 34 | + """Parse text body of email.""" |
| 35 | + |
| 36 | + @staticmethod |
| 37 | + def get_text_hook(raw): |
| 38 | + """Modify soup before entering `parse_text`.""" |
| 39 | + soup = bs4.BeautifulSoup(quopri.decodestring(raw), features="lxml") |
| 40 | + return soup.text |
| 41 | + |
| 42 | + def parse_text(self, text): |
| 43 | + """Parse text. |
| 44 | +
|
| 45 | + Example: |
| 46 | + Hello, |
| 47 | +
|
| 48 | + Planned maintenance has been scheduled on an AWS Direct Connect router in A= |
| 49 | + Block, New York, NY from Thu, 20 May 2021 08:00:00 GMT to Thu, 20 Ma= |
| 50 | + y 2021 14:00:00 GMT for 6 hours. During this maintenance window, your AWS D= |
| 51 | + irect Connect services listed below may become unavailable. |
| 52 | +
|
| 53 | + aaaaa-00000001 |
| 54 | + aaaaa-00000002 |
| 55 | + aaaaa-00000003 |
| 56 | + aaaaa-00000004 |
| 57 | + aaaaa-00000005 |
| 58 | + aaaaa-00000006 |
| 59 | +
|
| 60 | + This maintenance is scheduled to avoid disrupting redundant connections at = |
| 61 | + the same time. |
| 62 | + """ |
| 63 | + data = {"circuits": []} |
| 64 | + impact = Impact.OUTAGE |
| 65 | + maintenace_id = "" |
| 66 | + status = Status.CONFIRMED |
| 67 | + for line in text.splitlines(): |
| 68 | + if "planned maintenance" in line.lower(): |
| 69 | + data["summary"] = line |
| 70 | + search = re.search( |
| 71 | + r"([A-Z][a-z]{2}, [0-9]{1,2} [A-Z][a-z]{2,9} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} [A-Z]{2,3}) to ([A-Z][a-z]{2}, [0-9]{1,2} [A-Z][a-z]{2,9} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} [A-Z]{2,3})", |
| 72 | + line, |
| 73 | + ) |
| 74 | + if search: |
| 75 | + data["start"] = self.dt2ts(parser.parse(search.group(1))) |
| 76 | + data["end"] = self.dt2ts(parser.parse(search.group(2))) |
| 77 | + maintenace_id += str(data["start"]) |
| 78 | + maintenace_id += str(data["end"]) |
| 79 | + if "may become unavailable" in line.lower(): |
| 80 | + impact = Impact.OUTAGE |
| 81 | + elif "has been cancelled" in line.lower(): |
| 82 | + status = Status.CANCELLED |
| 83 | + elif re.match(r"[a-z]{5}-[a-z0-9]{8}", line): |
| 84 | + maintenace_id += line |
| 85 | + data["circuits"].append(CircuitImpact(circuit_id=line, impact=impact)) |
| 86 | + # No maintenance ID found in emails, so a hash value is being generated using the start, |
| 87 | + # end and IDs of all circuits in the notification. |
| 88 | + data["maintenance_id"] = hashlib.md5(maintenace_id.encode("utf-8")).hexdigest() # nosec |
| 89 | + data["status"] = status |
| 90 | + return [data] |
0 commit comments