first commit
This commit is contained in:
112
main.py
Normal file
112
main.py
Normal file
@ -0,0 +1,112 @@
|
||||
import argparse, requests, json
|
||||
from netmiko import ConnectHandler
|
||||
|
||||
### Define API
|
||||
|
||||
TENANTOS:str = ""
|
||||
AUTH_KEY:str = ""
|
||||
ACTION_TAG:str = ""
|
||||
VRF:str = ""
|
||||
|
||||
###
|
||||
|
||||
# Header
|
||||
|
||||
header = {
|
||||
"Authorization": AUTH_KEY,
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
}
|
||||
|
||||
# Tenantos API
|
||||
|
||||
def get_switch_info(server_id:int):
|
||||
server_url = f'https://{TENANTOS}/api/servers/{server_id}/connections'
|
||||
server_response = requests.get(server_url, headers=header)
|
||||
data = json.load(server_response.json())
|
||||
for value in data["result"]:
|
||||
if "switchId" in value["related_data"]["meta"]:
|
||||
switch:int = value["related_data"]["meta"]["switchId"]
|
||||
port_id:int = value["related_data"]["meta"]["portId"]
|
||||
switch_url = f"https://{TENANTOS}/api/networkDevices/{switch}"
|
||||
switch_response = requests.get(switch_url, headers=header)
|
||||
switch_data = json.load(switch_response.json())
|
||||
switch_address:str = switch_data["result"]["host"]
|
||||
switch_user:str = switch_data["result"]["managementUser"]
|
||||
switch_pass:str = switch_data["result"]["managementPassword"]
|
||||
for port in switch_data["result"]["ports"]:
|
||||
if port["id"] == port_id:
|
||||
port_name:str = port["name"]
|
||||
return {"switch_address": switch_address, "switch_user": switch_user, "switch_pass": switch_pass, "port_name": port_name}
|
||||
|
||||
def check_tag(server_id:int):
|
||||
exist:bool = False
|
||||
tag_url = f'https://{TENANTOS}/api/servers/{server_id}'
|
||||
tag_response = requests.get(tag_url, headers=header)
|
||||
tag_data = json.load(tag_response.json())
|
||||
tag_lists:list = tag_data["result"]["tags"]
|
||||
for tag in tag_lists:
|
||||
if tag["name"] == ACTION_TAG:
|
||||
exist = True
|
||||
return exist
|
||||
|
||||
def get_ips(server_id:int):
|
||||
INET:list = []
|
||||
INET6:list = []
|
||||
ips_url = f'https://{TENANTOS}/api/servers/{server_id}'
|
||||
ips_response = requests.get(ips_url, headers=header)
|
||||
ips_data = json.load(ips_response.json())
|
||||
for value in ips_data["result"]["ipassignments"]:
|
||||
if value["ipAttributes"]["isIpv4"] == 1:
|
||||
INET.append(f'{value["ip"]}/32')
|
||||
elif value["ipAttributes"]["isIpv6"] == 1:
|
||||
INET6.append(f'{value["ip"]}/112')
|
||||
return {"INET": INET, "INET6": INET6}
|
||||
# JunOS
|
||||
|
||||
def junos_add_config_builder(vrf:str, inet:list, inet6:list, port:str):
|
||||
if inet:
|
||||
config_inet = []
|
||||
for ip in inet:
|
||||
config_inet.append(f'set routing-instances {vrf} routing-options static route {ip} next-hop {port}')
|
||||
if inet6:
|
||||
config_inet6 = []
|
||||
for ip in inet6:
|
||||
config_inet6.append(f'set routing-instances {vrf} routing-options rib {vrf}.inet6.0 static route {ip} next-hop {port}')
|
||||
|
||||
if inet and inet6:
|
||||
return {"inet": config_inet, "inet6": config_inet6}
|
||||
else:
|
||||
return {"inet": config_inet}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
# Define arguments
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="main.py"
|
||||
)
|
||||
parser.add_argument("--action", type=str, required=True)
|
||||
parser.add_argument("--server", type=int, required=True)
|
||||
parser.add_argument("--ips", 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)
|
||||
if args.action == "add":
|
||||
config = junos_add_config_builder(VRF, ips["INET"], ips["INET6"], switch["port_name"])
|
||||
switch_connect = ConnectHandler(
|
||||
device_type = "juniper_junos",
|
||||
host = switch["switch_address"],
|
||||
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"])
|
||||
switch_connect.disconnect()
|
||||
Reference in New Issue
Block a user