In this post I would like to share my experience with Rest APIs using python as a consumer, this will be very useful and innovative in term of operation and automation.
Most of the time system (Storage, UNIX, Windows, Network) admins complain about the amount of work, many repetitive tasks are done in a tedious way, critical mistakes can happen due to doing many tasks at the same time. Apart from all that, recently the self service approach started to rise as a way to offload the tasks to the end user, budget control and reducing interactions. Also not to forget to mention the agility of the environment which will involve the swift creation and removal of environments.
Probably, all of what mentioned above is doable in UNIX and Windows servers, if not most of them. Now, but what about appliances, such as switches, storage, and any embedded systems. Recently, all new well supported systems started to come with enabled Rest APIs, to facilitate integration with other systems, reporting and monitoring.
Using Rest APIs of specific system will have few requirements:
- Understanding HTTP requests
- Understanding JSON/XML data
- Programming language (Python) with request module installed
- Documentation of how to use the system Rest API
Briefly, the HTTP requests are revolving around GET, POST, PUT, DELETE, UPDATE and some other types which we don't require usually in our Rest API operations. GET is used for list/retrieving objects, POST for creation of objects, PUT for updating attributes, UPDATE for updating the objects and intuitively DELETE for deleting objects.
Object here represents element to be Listed, Created or Deleted..etc. as per the above type of requests.
Security is important element in the Rest APIs that is why there are couple of ways to secure Restful resources. Github for example is using Basic Authentication, OAuth2 token and OAuth2 key/secret check here for more information.
Basic authentication is the usual used Rest API security method, which is basically Username and Password concatenated with ":" in the middle, and prepended by "Basic " passed in a base64 encrypted format.
Here is how you can demonstrate the use of base64 encoding (you don't need to do this in your code as HTTP protocol will do this automatically in the background) and how it will look like in the header parameters:
I am using python version 3.x
I am using python version 3.x
#!/usr/bin/python3.6
import base64
import sys
print (base64.b64encode("user:pass".encode()).decode())
The HTTP will have the below parameter in the request header:
Key | Value |
---|---|
Authorization | Basic dXNlcjpwYXNz |
You can use google chrome plugin called Postman to troubleshoot or debug your requests.
Now, starting to make GET requests (without authentication) is easy by following few steps in github..
#!/usr/bin/python3.6
import json
import requests
requests.packages.urllib3.disable_warnings()
response = requests.get('https://api.github.com/users', verify=False)
json_data = json.loads(response.text)
print(json_data)
The above code will get you the list of users in github in json format, you can use online json formatter to view it in a readable format.
Similarly, we can use this to interact with EMC VPLEX:
#!/usr/bin/python3.6
import requests
requests.packages.urllib3.disable_warnings()
response = requests.get('https://api.github.com/users', verify=False)
json_data = json.loads(response.text)
print(json_data)
The above code will get you the list of users in github in json format, you can use online json formatter to view it in a readable format.
Similarly, we can use this to interact with EMC VPLEX:
#!/usr/bin/python3.6
import requests, json
requests.packages.urllib3.disable_warnings()
url = "https://Cluster_IP/vplex/clusters/Cluster_ID/virtual-volumes/*"
payload = (Username, Password)
r = requests.get(url, auth = payload, verify = False) #make get request passing the credentials
data = json.loads(r.text) #read response as json and data will be a dictionary variable
print(data) #printing data
print(json.dumps(data)) #printing will be in a nice readable format
Using Rest API I developed interface for deleting VPLEX volumes, which takes less than a minute. This task used to take 15 to 30 minutes using CLI(which was the only available way to delete the volume). Therefore, the productivity significantly increased, and this way can help in automating provisioning and destroying Demo environments, which is the current trend in demonstrating new technologies.