diff --git a/main.py b/main.py index 144fd5e..3b6b351 100644 --- a/main.py +++ b/main.py @@ -62,6 +62,18 @@ def get_ips(server_id:int): elif value["ipAttributes"]["isIpv6"] == 1: INET6.append(f'{value["ip"]}/112') return {"INET": INET, "INET6": INET6} + +def get_af(subnet_id:int): + type:int = 4 + af_url = f'https://{TENANTOS}/api/subnets/{subnet_id}' + af_response = requests.get(af_url, headers=header) + af_data = json.load(af_response.json()) + if af_data["result"]["type"] == "v4": + return type + else: + type = 6 + return type + # JunOS def junos_add_config_builder(vrf:str, inet:list, inet6:list, port:str): @@ -88,14 +100,16 @@ if __name__ == "__main__": ) parser.add_argument("--action", type=str, required=True) parser.add_argument("--server", type=int, required=True) - parser.add_argument("--ips", type=str, required=True) + parser.add_argument("--subnet", type=int, required=True) + parser.add_argument("--ip", type=str, required=True) args = parser.parse_args() # Process process = check_tag(args.server) if process: switch = get_switch_info(args.server) - ips = get_ips(args.server) + ip = args.ip + af = get_af(args.subnet) if args.action == "add": config = junos_add_config_builder(VRF, ips["INET"], ips["INET6"], switch["port_name"]) switch_connect = ConnectHandler( @@ -104,9 +118,9 @@ if __name__ == "__main__": username = switch["switch_user"], password = switch["switch_pass"], ) - if "inet6" in config: - switch_connect.send_config_set(config["inet"]) - switch_connect.send_config_set(config["inet6"]) - else: - switch_connect.send_config_set(config["inet"]) + if af == 4: + config_command = [ f'set routing-instances {VRF} routing-options static route {ip}/32 next-hop {switch["port_name"]}' ] + elif af == 6: + config_command = [ f'set routing-instances {VRF} routing-options rib {VRF}.inet6.0 static route {ip}/64 next-hop {switch["port_name"]}' ] + switch_connect.send_config_set(config_command) switch_connect.disconnect()