51 lines
2.4 KiB
Python
51 lines
2.4 KiB
Python
"""AWS-Rechnungen per Invoicing-API (ListInvoiceSummaries + GetInvoicePDF), kein Konsolen-Login nötig.
|
|
|
|
IAM-User mit nur: invoicing:ListInvoiceSummaries, invoicing:GetInvoicePDF. Keys im Vault:
|
|
INVOICING_AWS_ACCESS_KEY_ID / INVOICING_AWS_SECRET_ACCESS_KEY. Ohne Keys ist der Adapter aus.
|
|
Endpoint us-east-1 ist bei AWS fest vorgegeben (globaler Billing-Dienst); übertragen wird nichts, AWS liefert
|
|
nur die eigene Rechnung an uns aus.
|
|
"""
|
|
|
|
import os
|
|
import urllib.request
|
|
from datetime import date
|
|
from typing import Iterator, Tuple
|
|
|
|
ENABLED = bool(os.getenv("INVOICING_AWS_ACCESS_KEY_ID") and os.getenv("INVOICING_AWS_SECRET_ACCESS_KEY"))
|
|
SENDER_DOMAINS = ("aws.com", "amazonaws.com", "aws.amazon.com") # deren Hinweis-Mails deckt die API ab
|
|
|
|
|
|
def _months(since: date) -> Iterator[Tuple[int, int]]:
|
|
y, m = since.year, since.month
|
|
today = date.today()
|
|
while (y, m) <= (today.year, today.month):
|
|
yield y, m
|
|
y, m = (y + 1, 1) if m == 12 else (y, m + 1)
|
|
|
|
|
|
def fetch_invoices(since: date, known: set) -> Iterator[dict]:
|
|
"""-> {invoice_id, issued, period, pdf} für jede noch unbekannte Rechnung seit since."""
|
|
import boto3
|
|
|
|
s = boto3.Session(aws_access_key_id=os.environ["INVOICING_AWS_ACCESS_KEY_ID"],
|
|
aws_secret_access_key=os.environ["INVOICING_AWS_SECRET_ACCESS_KEY"], region_name="us-east-1")
|
|
account = s.client("sts").get_caller_identity()["Account"]
|
|
inv = s.client("invoicing")
|
|
for y, m in _months(since):
|
|
kw = {"Selector": {"ResourceType": "ACCOUNT_ID", "Value": account},
|
|
"Filter": {"BillingPeriod": {"Year": y, "Month": m}}}
|
|
while True:
|
|
page = inv.list_invoice_summaries(**kw)
|
|
for summ in page.get("InvoiceSummaries", []):
|
|
iid = summ["InvoiceId"]
|
|
if iid in known:
|
|
continue
|
|
url = inv.get_invoice_pdf(InvoiceId=iid)["InvoicePDF"]["DocumentUrl"] # presigned, kurz gültig
|
|
with urllib.request.urlopen(url, timeout=60) as r:
|
|
pdf = r.read()
|
|
if pdf.startswith(b"%PDF-"):
|
|
yield {"invoice_id": iid, "issued": summ.get("IssuedDate"), "period": f"{y}-{m:02d}",
|
|
"entity": (summ.get("Entity") or {}).get("BillingEntity", "AWS"), "pdf": pdf}
|
|
if not page.get("NextToken"):
|
|
break
|
|
kw["NextToken"] = page["NextToken"]
|