diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2025-06-12 20:40:01 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2025-06-12 20:40:01 +0200 |
commit | efd2ee9e55f631f97e2f1ebe2020d309449a8e0a (patch) | |
tree | eace5162c5df745ae2c9abe92a21d534a7ae4a0e | |
parent | caf1626e21ead3f23b54d37d254a23b452498659 (diff) | |
download | infra-efd2ee9e55f631f97e2f1ebe2020d309449a8e0a.tar.gz infra-efd2ee9e55f631f97e2f1ebe2020d309449a8e0a.tar.bz2 infra-efd2ee9e55f631f97e2f1ebe2020d309449a8e0a.tar.xz infra-efd2ee9e55f631f97e2f1ebe2020d309449a8e0a.zip |
netbox
-rw-r--r-- | ansible/netbox/sync-unifi.py | 79 |
1 files changed, 62 insertions, 17 deletions
diff --git a/ansible/netbox/sync-unifi.py b/ansible/netbox/sync-unifi.py index 27d5a5b..4427b20 100644 --- a/ansible/netbox/sync-unifi.py +++ b/ansible/netbox/sync-unifi.py @@ -15,23 +15,45 @@ class Db(): self.macs = [] self.cables = [] +class NotFoundException(Exception): + def __init__(self, msg): + super().__init__(msg) + class Query(): - def __init__(self, args, query): + def __init__(self, args, query, projection=lambda x: x.id): self.args = args self.query = query + self.projection = projection + + def run(self, nb): + try: + ret = self.query() + except Exception as e: + print("Query failed: ") + print(f"Arguments: {value.args}") + print(e) + raise e + + if ret is None: + raise NotFoundException(f"resource not found, args={self.args}") + +# if len(ret) != 1: +# raise NotFoundException(f"multiple resources found, args={self.args}, result={ret}") + + return self.projection(ret) def find_device(nb, name: str): - return Query({}, lambda: nb.dcim.devices.get(name=name).id) + return Query({}, lambda: nb.dcim.devices.get(name=name)) def find_interface_by_mac(nb, mac_address: str): args = locals() del args["nb"] - return Query(args, lambda: nb.dcim.interfaces.get(mac_address=mac_address).id) + return Query(args, lambda: nb.dcim.interfaces.get(mac_address=mac_address)) def find_interface(nb, device: str, name: str): args = locals() del args["nb"] - return Query(args, lambda: nb.dcim.interfaces.get(device=device, name=name).id) + return Query(args, lambda: nb.dcim.interfaces.get(device=device, name=name)) class NetboxCache(): def __init__(self, nb): @@ -134,6 +156,33 @@ def create_or_update_ip_address(nb, data): ip.update(data) return nb.ipam.ip_addresses.get(id=ip.id) +def create_or_update_cable(nb, data): + if len(data["a_terminations"]) == 1: + a = data["a_terminations"][0] + else: + raise Exception("only single termination is supported") + + if len(data["b_terminations"]) == 1: + b = data["b_terminations"][0] + else: + raise Exception("only single termination is supported") + + cable = nb.dcim.cables.get( + termination_a_type=a["object_type"], + termination_a_id=a["object_id"], + termination_b_type=b["object_type"], + termination_b_id=b["object_id"], + ) + if cable is None: + cable = nb.dcim.cables.create(data) + print(f"Created Cable address id={ip.id}") + + return cable + + print(f"Updating cable id={ip.id}") + cable.update(data) + return nb.dcim.cables.get(id=cable.id) + def process_switch(d: UnifiDevice, db: Db, nb: NetboxCache, site, vrf): # db.devices.append({ # "name": d.name, @@ -182,8 +231,7 @@ def process_switch(d: UnifiDevice, db: Db, nb: NetboxCache, site, vrf): ] if e.chassis_id > d.mac: - a = b - b = a + a, b = b, a db.cables.append({ "a_terminations": a, @@ -196,13 +244,7 @@ def sync_db(db: Db, nb): if value is None or isinstance(value, str) or isinstance(value, int): return value elif isinstance(value, Query): - try: - return value.query() - except Exception as e: - print("Query failed: ") - print(f"Arguments: {value.args}") - print(e) - raise e + return value.run(nb) elif isinstance(value, dict): for k, v in value.items(): value[k] = resolve_query(v) @@ -230,10 +272,13 @@ def sync_db(db: Db, nb): create_or_update_ip_address(nb, ip) for cable in db.cables: - pprint(cable) - cable = resolve_query(cable) - pprint(cable) -# create_or_update_cable(nb, cable) + try: + cable = resolve_query(cable) + pprint(cable) + create_or_update_cable(nb, cable) + except NotFoundException: + print("Cable failed, could not find endpoint") + continue def main(): unifi_url=os.getenv("UNIFI_URL") |