the-packet-thrower

VEDGE Provisioning Script

May 4th, 2019
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. import requests
  2. import urllib3
  3. import json
  4.  
  5. urllib3.disable_warnings()
  6.  
  7. vManageUrl = 'https://vmanage01.testlab.com/'
  8. baseUrl = 'https://vmanage01.testlab.com/dataservice/'
  9.  
  10. class apiSess:
  11.     def __init__(self):
  12.         self.session = {}       # API Session Object
  13.         self.uuid = []          # List of available UUID
  14.         self.templateid = ''    # Device Template ID
  15.         self.aaa()              # Perform AAA Login
  16.  
  17.     def aaa(self):
  18.         print "***** Authenticating to vManage *****"
  19.         url = vManageUrl + 'j_security_check'
  20.         body = {
  21.             'j_username': 'admin',
  22.             'j_password': 'admin'
  23.         }
  24.         tempSess = requests.session()
  25.         resp = tempSess.post(url, data=body, verify=False)
  26.  
  27.         if resp.status_code == 200:
  28.             print 'Connection Successful'
  29.             self.session = tempSess
  30.         else:
  31.             print 'Connection Failed'
  32.             exit(1)
  33.  
  34.     def getAPI(self, path):
  35.         url = baseUrl + path
  36.         tempSess = self.session
  37.         resp = tempSess.get(url, verify=False)
  38.         if resp.status_code == 200:
  39.             print "GET Successful"
  40.             return resp
  41.         else:
  42.             print "GET Failed"
  43.  
  44.     def postAPI(self, path, body):
  45.         url = baseUrl + path
  46.         tempSess = self.session
  47.         resp = tempSess.post(url, json=body, verify=False)
  48.         if resp.status_code == 200:
  49.             print 'POST Successful'
  50.         else:
  51.             print 'POST Failed'
  52.             print resp.content
  53.             exit(1)
  54.  
  55. #************* MAIN ******************
  56.  
  57. # Get Session Cookie
  58. apiObj = apiSess()
  59.  
  60. # Get Available Device UUID
  61. print '***** Get Available Device UUID *****'
  62. resp = apiObj.getAPI('system/device/vedges')
  63. devList = resp.json()['data']
  64. for device in devList:
  65.     if 'system-ip' not in device:
  66.         print device['chasisNumber']
  67.         apiObj.uuid.append(device['chasisNumber'])
  68.  
  69. # Get Device Template ID
  70. print '***** Get Template ID *****'
  71. resp = apiObj.getAPI('template/device')
  72. templateList = resp.json()['data']
  73.  
  74. for k in templateList:
  75.     if k['templateName'] == 'BRANCH':
  76.         apiObj.templateid = k['templateId']
  77.  
  78. print 'Template ' + 'BRANCH' + ' = ' + apiObj.templateid
  79.  
  80. # Add device to template
  81. print '***** Add Device to Template *****'
  82. with open('vedge_para.csv', 'r') as f:
  83.     # Get Column Header
  84.     keyList = f.readline().rstrip('\n').split(',')
  85.     devNo = 0
  86.     for line in f:
  87.         # Read Device Config One Line at a time
  88.         paraList = line.rstrip('\n').split(',')
  89.         devConfig = dict(zip(keyList,paraList))
  90.         # Assign UUID to Device
  91.         devConfig['csv-deviceId'] = apiObj.uuid[devNo]
  92.         # Build Device Config Structure
  93.         print "---> Creating Config for Device #" + str(devNo + 1) + " " + str(devConfig)
  94.         print ' - Assigned UUID is ' + devConfig['csv-deviceId']
  95.         configDict = {
  96.             "deviceTemplateList": [
  97.                 {
  98.                     "templateId": apiObj.templateid,
  99.                     "device": [devConfig],
  100.                     "isEdited": 'false',
  101.                     "isMasterEdited": 'false'
  102.                 }
  103.             ]
  104.         }
  105.         # Send to vManage
  106.         resp = apiObj.postAPI('template/device/config/attachfeature', configDict)
  107.         print " - Device successfully attached to template"
  108.         devNo += 1
  109.     f.close()
Advertisement
Add Comment
Please, Sign In to add comment