improve ip process func
This commit is contained in:
28
main.py
28
main.py
@ -62,6 +62,18 @@ def get_ips(server_id:int):
|
|||||||
elif value["ipAttributes"]["isIpv6"] == 1:
|
elif value["ipAttributes"]["isIpv6"] == 1:
|
||||||
INET6.append(f'{value["ip"]}/112')
|
INET6.append(f'{value["ip"]}/112')
|
||||||
return {"INET": INET, "INET6": INET6}
|
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
|
# JunOS
|
||||||
|
|
||||||
def junos_add_config_builder(vrf:str, inet:list, inet6:list, port:str):
|
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("--action", type=str, required=True)
|
||||||
parser.add_argument("--server", type=int, 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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Process
|
# Process
|
||||||
process = check_tag(args.server)
|
process = check_tag(args.server)
|
||||||
if process:
|
if process:
|
||||||
switch = get_switch_info(args.server)
|
switch = get_switch_info(args.server)
|
||||||
ips = get_ips(args.server)
|
ip = args.ip
|
||||||
|
af = get_af(args.subnet)
|
||||||
if args.action == "add":
|
if args.action == "add":
|
||||||
config = junos_add_config_builder(VRF, ips["INET"], ips["INET6"], switch["port_name"])
|
config = junos_add_config_builder(VRF, ips["INET"], ips["INET6"], switch["port_name"])
|
||||||
switch_connect = ConnectHandler(
|
switch_connect = ConnectHandler(
|
||||||
@ -104,9 +118,9 @@ if __name__ == "__main__":
|
|||||||
username = switch["switch_user"],
|
username = switch["switch_user"],
|
||||||
password = switch["switch_pass"],
|
password = switch["switch_pass"],
|
||||||
)
|
)
|
||||||
if "inet6" in config:
|
if af == 4:
|
||||||
switch_connect.send_config_set(config["inet"])
|
config_command = [ f'set routing-instances {VRF} routing-options static route {ip}/32 next-hop {switch["port_name"]}' ]
|
||||||
switch_connect.send_config_set(config["inet6"])
|
elif af == 6:
|
||||||
else:
|
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["inet"])
|
switch_connect.send_config_set(config_command)
|
||||||
switch_connect.disconnect()
|
switch_connect.disconnect()
|
||||||
|
|||||||
Reference in New Issue
Block a user