Python based API to NSX Controller
Lets look at how to develop Python based API to connect to NSX controller and do some basic provisioning/configuring on the NSX Controllers.
NSX Controllers provide REST based interface and we would utilize this for our communication purpose.
Request Method
Here,we have Python helper API to send HTTP requests(GET,PUT,POST) to the NSX controller. def request(self, method, url, body, content_type="application/json"):
headers = { "Content-Type" : content_type, "Cookie" : self.cookie }
self.httpcon.request(method, url, body, headers)
response = self.httpcon.getresponse()
self.cookie = response.getheader("Set-Cookie", self.cookie)
status = response.status
if status != httplib.OK and status != httplib.CREATED and status != httplib.NO_CONTENT:
print "%s to %s got unexpected response code: %d (content = '%s')" \
% (method,url, response.status, response.read())
raise Exception("Request Failed")
return None
return response.read()
Login Method
Login method takes the user-id and password and does a login on the controller. The cookie received on successful login is saved for use in future requests.
def login(self, user,password):
headers = {}
headers["Content-Type"] = "application/x-www-form-urlencoded"
body = urllib.urlencode({ "username": user, "password": password })
self.httpcon.request("POST", "/ws.v1/login", body, headers)
response = self.httpcon.getresponse()
self.cookie = response.getheader("Set-Cookie", "")
if response.status != httplib.OK:
raise Exception("login failed")
self.httpcon.close()
Auxiliary methods in helper API
Constructor takes the host(ip address) and port(default value 443) for establishing the htpps connection to the controller.
def __init__(self, host, port):
self.httpcon = httplib.HTTPSConnection(host,port)
def formatSelectFields(self,**kwargs):
additionalSelectFields =""
for i in kwargs:
if(kwargs[i]):
additionalSelectFields="%s&%s=%s"%(additionalSelectFields,i,kwargs[i])
return additionalSelectFields
List Logical Switches
This method list all the logical switches on the controller cluster. Individual logical switch can be retrieved using display_name or uuid
def listLogicalSwitches(self,display_name="",uuid=""):
kwargs={"display_name":display_name,"uuid":uuid}
selectFlds = self.formatSelectFields(**kwargs)
res_str = self.request("GET","/ws.v1/lswitch?_page_length=5000&fields=*"+selectFlds, "")
return res_str
Delete a Logical Switch
This method deletes a logical switch give its uuid.
def deleteLogicalSwitch(self,ls_uuid):
res_str = self.request("DELETE","/ws.v1/lswitch/"+ls_uuid, "")
return res_str
Create a Logical Switch
This method creates a logical Switch with display_name and VNI provided as arguments.
def createLogicalSwitch(self,display_name,vni):
tn_obj = { "display_name" : display_name,
"transport_zones" : [
{
"zone_uuid" : self.transportZoneUUID,
"binding_config" : {
"vxlan_transport": [
{
"transport": vni,
}
]
},
"transport_type": "vxlan"
}
],
"replication_mode": "service",
"type": "LogicalSwitchConfig"
}
res_str = self.request("POST", "/ws.v1/lswitch", simplejson.dumps(tn_obj))
print "Created Logical Switch: '%s' " % (display_name)
return simplejson.loads(res_str)['uuid']
No comments:
Post a Comment