Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 65 additions & 61 deletions sdwan.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#! /usr/bin/env python
"""
Class with REST Api GET and POST libraries

Example: python rest_api_lib.py vmanage_hostname username password

PARAMETERS:
vmanage_hostname : Ip address of the vmanage or the dns name of the vmanage
username : Username to login the vmanage
password : Password to login the vmanage

Note: All the three arguments are manadatory
"""
import time
import requests
import sys
import json
Expand Down Expand Up @@ -60,6 +58,16 @@ def login(self, vmanage_ip, username, password):
print ("Login Failed")
sys.exit(0)

#ABG update token to session headers
token_url = base_url_str + 'dataservice/client/token'
login_token = sess.get(url=token_url, verify=False)

if login_token.status_code == 200:
if b'<html>' in login_token.content:
print ("Login Token Failed")
exit(0)

sess.headers['X-XSRF-TOKEN'] = login_token.content
self.session[vmanage_ip] = sess

def get_request(self, mount_point):
Expand Down Expand Up @@ -89,15 +97,12 @@ def cli():
pass

@click.command()
def device_list():
@click.option("--search", help="search for device name") #ABG
def device_list(search):
"""Retrieve and return network devices list.

Returns information about each device that is part of the fabric.

Example command:

./sdwan.py device_list

./sdwan.py device_list --search abcd1234567890
"""
click.secho("Retrieving the devices.")

Expand All @@ -106,25 +111,24 @@ def device_list():

headers = ["Host-Name", "Device Type", "Device ID", "System IP", "Site ID", "Version", "Device Model"]
table = list()

for item in items:
tr = [item['host-name'], item['device-type'], item['uuid'], item['system-ip'], item['site-id'], item['version'], item['device-model']]
table.append(tr)
if search==None:search=''
if search in item['host-name']: #ABG
tr = [item['host-name'], item['device-type'], item['uuid'], item['system-ip'], item['site-id'], item['version'], item['device-model']]
table.append(tr)
try:
click.echo(tabulate.tabulate(table, headers, tablefmt="fancy_grid"))
except UnicodeEncodeError:
click.echo(tabulate.tabulate(table, headers, tablefmt="grid"))

@click.command()
def template_list():
@click.option("--search", help="search for template name") #ABG
def template_list(search):
"""Retrieve and return templates list.

Returns the templates available on the vManage instance.

Example command:

./sdwan.py template_list

./sdwan.py template_list --search abcd1234567890
"""
click.secho("Retrieving the templates available.")

Expand All @@ -135,8 +139,10 @@ def template_list():
table = list()

for item in items:
tr = [item['templateName'], item['deviceType'], item['templateId'], item['devicesAttached'], item['templateAttached']]
table.append(tr)
if search==None:search=''
if search in item['templateName']: #ABG
tr = [item['templateName'], item['deviceType'], item['templateId'], item['devicesAttached'], item['templateAttached']]
table.append(tr)
try:
click.echo(tabulate.tabulate(table, headers, tablefmt="fancy_grid"))
except UnicodeEncodeError:
Expand All @@ -146,11 +152,8 @@ def template_list():
@click.option("--template", help="Name of the template you wish to retrieve information for")
def attached_devices(template):
"""Retrieve and return devices associated to a template.

Example command:

./sdwan.py attached_devices --template abcd1234567890

"""

url = "template/device/config/attached/{0}".format(template)
Expand All @@ -170,64 +173,66 @@ def attached_devices(template):
click.echo(tabulate.tabulate(table, headers, tablefmt="grid"))

@click.command()
@click.option("--template", help="Name of the template to deploy")
@click.option("--target", help="Hostname of target network device.")
@click.option("--hostname", help="Hostname you wish the target has")
@click.option("--sysip", help="System IP you wish the target has")
@click.option("--loopip", help="Loopback interface IP address")
@click.option("--geip", help="Gigabit0/0 interface IP address")
@click.option("--siteid", help="Site ID")
#@click.argument("parameters", nargs=-1)
def attach(template, target, hostname, sysip, loopip, geip, siteid):
@click.option("--template", help="Template ID",prompt=True, required=True)#ABG
@click.option("--deviceid", help="Device UUID",prompt=True, required=True)#ABG
def attach(template, deviceid):
"""Attach a template with Cisco SDWAN.

Provide all template parameters and their values as arguments.

Provide template id and device id as arguments.
Example command:

./sdwan.py attach --template TemplateID --target TargetID --hostname devnet01.cisco.com
--sysip 1.1.1.1 --loopip 2.2.2.2/24 --geip 3.3.3.3/24 --siteid 999
./sdwan.py attach --template TemplateID --deviceid deviceID
"""
click.secho("Attempting to attach template.")

#Step1: Genrating device confugration input
#==========================================
click.secho(">> Attempting to genrate input for device template attach.")

payload = {"templateId":template,"deviceIds":[deviceid],"isEdited":"False","isMasterEdited":"False"}

response = sdwanp.post_request('template/device/config/input', payload)

if "data" in response.keys(): #if reqest was succful we will have data
click.secho(">> Input for device template attach genrated! Attempting to attche template")
else:
click.secho("error")
#TODO break here


#Step2: Attaching template to device
#===================================
payload = {
"deviceTemplateList":[
{
"templateId":str(template),
"device":[
{
"csv-status":"complete",
"csv-deviceId":str(target),
"csv-deviceIP":str(sysip),
"csv-host-name":str(hostname),
"/1/loopback1/interface/ip/address":str(loopip),
"/0/ge0/0/interface/ip/address":str(geip),
"//system/host-name":str(hostname),
"//system/system-ip":str(sysip),
"//system/site-id":str(siteid),
"csv-templateId":str(template),
"selected":"true"
}
],
"templateId":template,
"device":[],
"isEdited":"false",
"isMasterEdited":"false"
}
]
}

payload['deviceTemplateList'][0]['device'].append(response['data'][0])
response = sdwanp.post_request('template/device/config/attachfeature', payload)
print (response)
print(response)

if "id" in response.keys():
status="0"
while status!="done":
time.sleep(5)
resp = json.loads( sdwanp.get_request("device/action/status/"+str(response["id"])) )
status=resp["summary"]["status"]
click.secho("-", nl=False)
click.secho(">> done")
else:
click.secho("error!")
#TODO break here

@click.command()
@click.option("--target", help="ID of the to detach")
@click.option("--sysip", help="System IP of the system to detach")
def detach(target, sysip):
"""Detach a template with Cisco SDWAN.

Provide all template parameters and their values as arguments.

Example command:

./sdwan.py detach --target TargetID --sysip 1.1.1.1
"""
click.secho("Attempting to detach template.")
Expand All @@ -252,5 +257,4 @@ def detach(target, sysip):
cli.add_command(template_list)

if __name__ == "__main__":
cli()

cli()