Initial import from bf-network-r6.opencode — opencode migration
This commit is contained in:
121
nautobot/ingest-wifi-access-list-rule.py
Normal file
121
nautobot/ingest-wifi-access-list-rule.py
Normal file
@@ -0,0 +1,121 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
ComplianceRule `wifi-access-list` anlegen (idempotent).
|
||||
|
||||
Die Luecke, die das schliesst
|
||||
-----------------------------
|
||||
Es gibt ComplianceRules fuer wifi-configuration/-datapath/-security/-steering/
|
||||
-provisioning/-channel/-cap/-capsman -- aber **keine** fuer
|
||||
`/interface wifi access-list`. Eintraege dort erzeugen deshalb KEINEN Drift und
|
||||
lassen sich auch nicht als Config-Transaktion fuehren: `gc-transaktion.py`
|
||||
verlangt eine ComplianceRule als `--scope`.
|
||||
|
||||
Seit 2026-08-22 ist das nicht mehr theoretisch. `docs/tp25-pin-s22.sh` schreibt
|
||||
bei jedem Messrundgang access-list-Regeln auf den Manager, und TP25 **Fund E**
|
||||
(`signal-range`-Boden gegen klebende Clients) wuerde dauerhaft genau dort landen
|
||||
-- in einem Abschnitt, den niemand prueft.
|
||||
|
||||
Der Soll-Zustand ist heute **leer**: am Geraet steht keine access-list, im
|
||||
Template steht keine. Beide Seiten leer = compliant. Genau das ist der Sinn --
|
||||
ab jetzt faellt jeder Eintrag dort auf, statt unsichtbar zu bleiben.
|
||||
|
||||
export NAUTOBOT_TOKEN=... # aus docs/nas-vlan20/stacks/210-nautobot.yml
|
||||
python3 nautobot/ingest-wifi-access-list-rule.py # Dry-Run
|
||||
python3 nautobot/ingest-wifi-access-list-rule.py --apply
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
BASE = "http://10.40.20.30:8080/api"
|
||||
TOKEN = os.environ.get("NAUTOBOT_TOKEN", "")
|
||||
APPLY = "--apply" in sys.argv
|
||||
|
||||
FEATURE = "wifi-access-list"
|
||||
BESCHREIBUNG = ("TP25 Fund E / tp25-pin-s22.sh: signal-range-Boden und Mess-Pins "
|
||||
"landen hier. Soll ist leer, solange Fund E nicht entschieden ist.")
|
||||
MATCH = "/interface wifi access-list"
|
||||
PLATTFORM = "RouterOS"
|
||||
|
||||
|
||||
def req(method, path, data=None):
|
||||
url = path if path.startswith("http") else BASE + path
|
||||
body = json.dumps(data).encode() if data is not None else None
|
||||
r = urllib.request.Request(url, data=body, method=method)
|
||||
r.add_header("Authorization", "Token " + TOKEN)
|
||||
r.add_header("Content-Type", "application/json")
|
||||
r.add_header("Accept", "application/json")
|
||||
try:
|
||||
with urllib.request.urlopen(r, timeout=60) as resp:
|
||||
raw = resp.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
except urllib.error.HTTPError as e:
|
||||
# 🔴 Fehlerkoerper ausgeben: Nautobot begruendet 400er praezise, und ohne den
|
||||
# Text raet man an Feldnamen herum (TP22-Erfahrung).
|
||||
sys.exit("!! %s %s -> %s\n %s" % (method, url, e.code, e.read().decode()[:600]))
|
||||
|
||||
|
||||
def eins(pfad, **filter):
|
||||
q = urllib.parse.urlencode(filter)
|
||||
res = req("GET", "%s?%s" % (pfad, q)).get("results", [])
|
||||
if len(res) > 1:
|
||||
sys.exit("FEHLER: %s?%s liefert %d Treffer, erwartet 0 oder 1" % (pfad, q, len(res)))
|
||||
return res[0] if res else None
|
||||
|
||||
|
||||
def main():
|
||||
if not TOKEN:
|
||||
sys.exit("NAUTOBOT_TOKEN fehlt.")
|
||||
print("Modus:", "APPLY" if APPLY else "DRY-RUN (nichts wird geschrieben)")
|
||||
|
||||
plat = eins("/dcim/platforms/", name=PLATTFORM)
|
||||
if not plat:
|
||||
sys.exit("Plattform %r nicht gefunden." % PLATTFORM)
|
||||
print("Plattform : %s (%s)" % (plat["display"], plat["id"]))
|
||||
|
||||
feat = eins("/plugins/golden-config/compliance-feature/", name=FEATURE)
|
||||
if feat:
|
||||
print("Feature : existiert bereits (%s)" % feat["id"])
|
||||
else:
|
||||
print("Feature : anlegen -> %s" % FEATURE)
|
||||
if APPLY:
|
||||
feat = req("POST", "/plugins/golden-config/compliance-feature/",
|
||||
{"name": FEATURE, "slug": FEATURE, "description": BESCHREIBUNG})
|
||||
print(" angelegt (%s)" % feat["id"])
|
||||
|
||||
if not feat:
|
||||
print("\nDry-Run: ohne Feature kann die Rule nicht geprueft werden. Mit --apply erneut.")
|
||||
return
|
||||
|
||||
rule = eins("/plugins/golden-config/compliance-rule/",
|
||||
feature=feat["id"], platform=plat["id"])
|
||||
if rule:
|
||||
print("Rule : existiert bereits (%s), match_config=%r"
|
||||
% (rule["id"], rule.get("match_config")))
|
||||
if rule.get("match_config") != MATCH:
|
||||
print(" ⚠️ match_config weicht ab -- erwartet %r" % MATCH)
|
||||
return
|
||||
|
||||
nutzlast = {
|
||||
"feature": feat["id"],
|
||||
"platform": plat["id"],
|
||||
"description": BESCHREIBUNG,
|
||||
"config_ordered": False, # Reihenfolge der access-list-Eintraege ist hier egal
|
||||
"config_type": "cli",
|
||||
"match_config": MATCH,
|
||||
"custom_compliance": False,
|
||||
"config_remediation": False,
|
||||
}
|
||||
print("Rule : anlegen -> %s auf %s, match_config=%r" % (FEATURE, PLATTFORM, MATCH))
|
||||
if APPLY:
|
||||
r = req("POST", "/plugins/golden-config/compliance-rule/", nutzlast)
|
||||
print(" angelegt (%s)" % r["id"])
|
||||
else:
|
||||
print(" Nutzlast: %s" % json.dumps(nutzlast))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user