diff --git a/sdwan.py b/sdwan.py index 6354bd3..c2b83c8 100755 --- a/sdwan.py +++ b/sdwan.py @@ -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 @@ -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'' 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): @@ -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.") @@ -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.") @@ -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: @@ -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) @@ -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.") @@ -252,5 +257,4 @@ def detach(target, sysip): cli.add_command(template_list) if __name__ == "__main__": - cli() - \ No newline at end of file + cli() \ No newline at end of file