def get_ip_netbox(pool_name): prefixes = netbox.Read().Prefixes().get_by_name(pool_name) if prefixes.get("count") is None: return { "status": "error", "message": f"Prefixes named {pool_name} does not exist" } try: prefix = prefixes.get("results")[0] except IndexError as prefix_index_error: return {"status": "error", "message": f"Prefix: {prefix_index_error}"} try: prefix_id = prefix.get("id") except KeyError as prefix_key_error: return {"status": "error", "message": f"Prefix: {prefix_key_error}"} addresses = netbox.Read().Addresses().get_free_ips_by_prefix_id(prefix_id) try: address = addresses[0] except IndexError as address_index_error: return { "status": "error", "message": f"Address: {address_index_error}" } try: ip_address = address.get("address") except KeyError as address_key_error: return {"status": "error", "message": f"Address: {address_key_error}"} return {"status": "good", "address": ip_address}
def get_free_ip_global_vrf(region, prefix_type): prefixes = netbox.Read().Prefixes().get_by_three_tags_v4( "erip", region, prefix_type) if prefixes.get("count") is None: return { "status": "error", "message": f"There are not Prefixes in GRT with such parameters: " f"erip, {region}, {prefix_type}" } else: for prefix in prefixes.get("results"): prefix_id = prefix.get("id") addresses = netbox.Read().Addresses().get_free_ips_by_prefix_id( prefix_id) if addresses: return { "status": "good", "message": { "address": addresses[0], "prefix": prefix } } else: continue else: return { "status": "error", "message": f"There are not Addresses in GRT with such parameters: " f"erip, {region}, {prefix_type}" }
def delete_address_vrf_global(address, region, prefix_type): """Delete ip address from Netbox""" address_params = netbox.Read().Addresses().get_by_address_and_three_tags( address, "erip", region, prefix_type) if address_params.get("count") is None: return { "status": "error", "message": f"Address in GRT with such parameters has not been deleted:" f"{address}, erip, {region}, {prefix_type}" } else: try: address_id = address_params.get("results")[0].get("id") except IndexError as index_error: return { "status": "error", "message": f"There are no Addresses with such parameters: " f"{address}, erip, {region}, {prefix_type}" } netbox.Delete().Addresses().delete_by_id(address_id) return { "status": "good", "message": f"Address in GRT with such parameters deleted:" f"{address}, erip, {region}, {prefix_type}" }
def check_vrf(vrf_name): vrfs = netbox.Read().VRFS().get_by_name(vrf_name) if vrfs.get("count") is None: return {"status": "error", "message": f"VRF named {vrf_name} does not exist"} try: vrf = vrfs.get("results")[0] except IndexError: return {"status": "error", "message": f"VRF named {vrf_name} does not exist"} return {"status": "good", "message": vrf}
def get_free_ip(region, prefix_type, in_vrf): vrf_rd = "25106:" + str(in_vrf.get("rd")) vrf = netbox.Read().VRFS().get_by_rd(vrf_rd) if vrf.get("count") is None: return {"status": "error", "message": f"No VRF with such RD {vrf_rd}"} else: try: vrf_id = vrf.get("results")[0].get("id") except IndexError as index_error: return {"status": "error", "message": index_error} prefixes = netbox.Read().Prefixes().get_by_vrf_id_and_three_tag_v4( vrf_id, "erip", region, prefix_type) if prefixes.get("count") is None: return { "status": "error", "message": f"There are not Prefixes in VRF {in_vrf.get('name')} with such parameters: " f"erip, {region}, {prefix_type}" } else: for prefix in prefixes.get("results"): prefix_id = prefix.get("id") addresses = netbox.Read().Addresses().get_free_ips_by_prefix_id( prefix_id) if addresses: return { "status": "good", "message": { "address": addresses[0], "prefix": prefix, "vrf": vrf.get("results")[0] } } else: continue else: return { "status": "error", "message": f"There are not Addresses in VRF {in_vrf.get('name')} with such parameters: " f"erip, {region}, {prefix_type}" }
def delete_address(address, region, prefix_type, in_vrf): """Delete ip address from Netbox""" vrf_rd = "25106:" + str(in_vrf.get("rd")) vrf = netbox.Read().VRFS().get_by_rd(vrf_rd) if vrf.get("count") is None: return {"status": "error", "message": f"No VRF with such RD {vrf_rd}"} else: try: vrf_id = vrf.get("results")[0].get("id") except IndexError as index_error: return {"status": "error", "message": index_error} address_params = netbox.Read().Addresses().get_by_vrf_id_and_address( vrf_id, address) if address_params.get("count") is None: return { "status": "error", "message": f"Address in VRF {in_vrf.get('name')} with such parameters " f"has not been deleted: {address}, erip, {region}, {prefix_type}" } else: try: address_id = address_params.get("results")[0].get("id") except IndexError as index_error: return { "status": "error", "message": f"There are no Addresses with such parameters: " f"{in_vrf.get('name')}, {address}, erip, {region}, {prefix_type}" } netbox.Delete().Addresses().delete_by_id(address_id) return { "status": "good", "message": f"Address in VRF {in_vrf.get('name')} with such parameters deleted:" f"{address}, erip, {region}, {prefix_type}" }
def get_gateway(prefix): addresses = netbox.Read().Addresses().get_by_prefix(prefix.get("prefix")) if addresses.get("count") is None: return None else: for address in addresses.get('results'): if "gateway" in address.get("tags"): return address else: continue else: return None
def delete_ip_netbox(vrf_name, ip_address): vrfs = netbox.Read().VRFS().get_by_name(vrf_name) if vrfs.get("count") is None: return {"status": "error", "message": f"VRF named {vrf_name} does not exist"} try: vrf = netbox.Read().VRFS().get_by_name(vrf_name).get("results")[0] except IndexError as vrf_index_error: return {"status": "error", "message": vrf_index_error} try: vrf_id = vrf.get("id") except KeyError as vrf_key_error: return {"status": "error", "message": vrf_key_error} ips = netbox.Read().Addresses().get_by_vrf_id_and_address(vrf_id, ip_address) try: ip = ips.get('results')[0] except IndexError as ip_index_error: return {"status": "error", "message": ip_index_error} try: ip_id = ip.get('id') except KeyError as ip_key_error: return {"status": "error", "message": ip_key_error} netbox.Delete().Addresses().delete_by_id(ip_id)
def create_address(netbox_message, pool_name): try: value = netbox_message.get("address").split("/")[0] except IndexError: return { "status": "error", "message": "Failed to get address from Netbox" } prefixes = netbox.Read().Prefixes().get_by_name(pool_name) if prefixes.get("count") is None: return { "status": "error", "message": f"Prefixes named {pool_name} does not exist" } try: prefix = prefixes.get('results')[0] except IndexError: return { "status": "error", "message": f"Failed to get Prefix named {pool_name} from Netbox" } try: vrf_id = prefix.get('vrf').get('id') except KeyError: return { "status": "error", "message": f"Failed to get VRF ID from Netbox" } try: tenant_id = prefix.get('tenant').get('id') except KeyError: return { "status": "error", "message": f"Failed to get Tenant ID from Netbox" } netbox.Create().Addresses().create(address=value, vrf_id=vrf_id, tenant_id=tenant_id, description='', custom_fields={}) return {"status": "good", "message": value}