The documentation does not include information about how to use RESTCONF clients. This example documents some common tasks using Python.
Configure the RESTCONF server on the switch.
#!/usr/bin/env python import sys import json import requests from requests import Request, Session from requests.auth import HTTPBasicAuth from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) ######################################################################## Host = '192.0.2.1' TcpPort = ‘8080' UserName = 'rwa' PassWord = 'rwa' LoginUrl = 'https://%s:%s/auth/token' % (Host, TcpPort)QueryUrl = 'https://%s:%s/rest/restconf/data/' % (Host, TcpPort) vlan = 123 ######################################################################## session = Session() session.verify = False session.timeout = 5 session.headers.update({ 'Accept': 'application/json', 'Accept-Encoding': 'gzip, deflate, br', 'Connection': 'keep-alive', 'Cache-Control': 'no-cache', 'Pragma': 'no-cache‘, })
Note
The token expires after 24 hours.
body = '{"username": "%s", "password" : "%s" }' % (UserName,PassWord) response = session.put( LoginUrl, body ) if response.status_code != 200: print "ERROR: Login failed" sys.exit(1) else: session.headers.update({ 'X-Auth-Token': response.headers['X-Auth-Token‘] }) print "INFO: login passed"
response = session.get( QueryUrl + 'openconfig-vlan:vlans' ) if response.status_code != 200: print 'ERROR: can‘t fetch VLANs'
response = session.get( QueryUrl + 'openconfig-vlan:vlans/vlan=%s' % vlan ) if response.status_code != 200: print 'INFO: VLAN %s doesn‘t exists' % vlan else: print 'INFO: VLAN %s exists' % vlan
inbound_data = json.loads( response.text ) for vlan in inbound_data['openconfig-vlan:vlans' ]['vlan']: print 'VLAN: %s [%s]' % ( vlan['state']['name'], vlan['vlan-id‘] )
inbound_data = json.loads( response.text ) for dataVlan in inbound_data[ dataObject ]['vlan']: print '' print 'VLAN: ' + dataVlan['state']['name'] + '[' + dataVlan['vlan-id'] + ']' interfaces = ' ' if 'members' in dataVlan : for interface in dataVlan['members']['member ']: interfaces = interfaces + interface['interface-ref']['state']['interface'] + ',' print interfaces
dataObject = 'openconfig-vlan:vlans‘ data = { "openconfig-vlan:vlan": [ { "config": { "name": "Test-VLAN", "extreme-mod-oc-vlan:stg-id": 1, "vlan-id": vlan } } ] } response = session.post( QueryUrl + dataObject, json=data ) if response.status_code != 201: print "ERROR: add VLAN %s failed" % vlan else: print "INFO: VLAN %s added " % vlan
dataObject = 'openconfig-vlan:vlans/vlan=%s/config' % vlan data = { "openconfig-vlan:config": { "name": "New-VLAN-Name" } } response = session.patch( QueryUrl + dataObject ) if response.status_code != 204: print "ERROR: update VLAN %s fails" % vlan else: print "INFO: VLAN %s updated" % vlan
dataObject = 'openconfig-vlan:vlans/vlan=%s' % vlan response = session.delete( QueryUrl + dataObject ) if response.status_code != 204: print "ERROR: delete VLAN %s fails" % vlan else: print "INFO: VLAN %s deleted" % vlan