Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import urllib3
- import json
- urllib3.disable_warnings()
- vManageUrl = 'https://vmanage01.testlab.com/'
- baseUrl = 'https://vmanage01.testlab.com/dataservice/'
- class apiSess:
- def __init__(self):
- self.session = {} # API Session Object
- self.uuid = [] # List of available UUID
- self.templateid = '' # Device Template ID
- self.aaa() # Perform AAA Login
- def aaa(self):
- print "***** Authenticating to vManage *****"
- url = vManageUrl + 'j_security_check'
- body = {
- 'j_username': 'admin',
- 'j_password': 'admin'
- }
- tempSess = requests.session()
- resp = tempSess.post(url, data=body, verify=False)
- if resp.status_code == 200:
- print 'Connection Successful'
- self.session = tempSess
- else:
- print 'Connection Failed'
- exit(1)
- def getAPI(self, path):
- url = baseUrl + path
- tempSess = self.session
- resp = tempSess.get(url, verify=False)
- if resp.status_code == 200:
- print "GET Successful"
- return resp
- else:
- print "GET Failed"
- def postAPI(self, path, body):
- url = baseUrl + path
- tempSess = self.session
- resp = tempSess.post(url, json=body, verify=False)
- if resp.status_code == 200:
- print 'POST Successful'
- else:
- print 'POST Failed'
- print resp.content
- exit(1)
- #************* MAIN ******************
- # Get Session Cookie
- apiObj = apiSess()
- # Get Available Device UUID
- print '***** Get Available Device UUID *****'
- resp = apiObj.getAPI('system/device/vedges')
- devList = resp.json()['data']
- for device in devList:
- if 'system-ip' not in device:
- print device['chasisNumber']
- apiObj.uuid.append(device['chasisNumber'])
- # Get Device Template ID
- print '***** Get Template ID *****'
- resp = apiObj.getAPI('template/device')
- templateList = resp.json()['data']
- for k in templateList:
- if k['templateName'] == 'BRANCH':
- apiObj.templateid = k['templateId']
- print 'Template ' + 'BRANCH' + ' = ' + apiObj.templateid
- # Add device to template
- print '***** Add Device to Template *****'
- with open('vedge_para.csv', 'r') as f:
- # Get Column Header
- keyList = f.readline().rstrip('\n').split(',')
- devNo = 0
- for line in f:
- # Read Device Config One Line at a time
- paraList = line.rstrip('\n').split(',')
- devConfig = dict(zip(keyList,paraList))
- # Assign UUID to Device
- devConfig['csv-deviceId'] = apiObj.uuid[devNo]
- # Build Device Config Structure
- print "---> Creating Config for Device #" + str(devNo + 1) + " " + str(devConfig)
- print ' - Assigned UUID is ' + devConfig['csv-deviceId']
- configDict = {
- "deviceTemplateList": [
- {
- "templateId": apiObj.templateid,
- "device": [devConfig],
- "isEdited": 'false',
- "isMasterEdited": 'false'
- }
- ]
- }
- # Send to vManage
- resp = apiObj.postAPI('template/device/config/attachfeature', configDict)
- print " - Device successfully attached to template"
- devNo += 1
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment