Python script to proxy iControl REST calls
Problem this snippet solves:
I have a customer who has an automation platform written in Perl. This tool has a web front end that serves as a self-service portal for operations folks to perform some tasks on back end systems in a controlled, automated manner. The customer wanted a way to make iControl REST calls without a tremendous amount of effort, so we came up with the idea of having him call a Python script, along with command line arguments, that would be passed to the BIG-IP and responses handed back. This is the script I gave him to do so.
How to use this snippet:
Run the command with no arguments from the command line to access the help section of the code.
Code :
#!/usr/bin/env python3 import argparse,json,requests from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) def icontrol_get(host,username,password,path): apiCall = requests.session() apiCall.headers.update({'Content-type':'application/json'}) apiCall.auth = (username,password) apiUri = 'https://' + host + path try: apiResponse = apiCall.get(apiUri,verify=False) except requests.exceptions.RequestException as e: print('{"responseStatus":"error","action":"get","host":"' + host + '","username":"' + username + '","path":"' + path + '","errorMsg":"' + str(e) + '"}') return(apiResponse.text) def icontrol_post(host,username,password,path,api_payload): apiCall = requests.session() apiCall.headers.update({'Content-type':'application/json'}) apiCall.auth = (username,password) apiUri = 'https://' + host + path try: apiResponse = apiCall.post(apiUri,verify=False,data=json.dumps(api_payload)) except requests.exceptions.RequestException as e: print('{"responseStatus":"error","action":"post","host":"' + host + '","username":"' + username + '","path":"' + path + '"},"payload":"' + api_payload + '","errorMsg":"' + str(e) + '"}') return(apiResponse.text) def icontrol_put(host,username,password,path,api_payload): apiCall = requests.session() apiCall.headers.update({'Content-type':'application/json'}) apiCall.auth = (username,password) apiUri = 'https://' + host + path try: apiResponse = apiCall.put(apiUri,verify=False,data=json.dumps(api_payload)) except requests.exceptions.RequestException as e: print('{"responseStatus":"error","action":"put","host":"' + host + '","username":"' + username + '","path":"' + path + '"},"payload":"' + api_payload + '","errorMsg":"' + str(e) + '"}') return(apiResponse.text) def icontrol_patch(host,username,password,path,api_payload): apiCall = requests.session() apiCall.headers.update({'Content-type':'application/json'}) apiCall.auth = (username,password) apiUri = 'https://' + host + path try: apiResponse = apiCall.patch(apiUri,verify=False,data=json.dumps(api_payload)) except requests.exceptions.RequestException as e: print('{"responseStatus":"error","action":"patch","host":"' + host + '","username":"' + username + '","path":"' + path + '"},"payload":"' + api_payload + '","errorMsg":"' + str(e) + '"}') return(apiResponse.text) def icontrol_delete(host,username,password,path): apiCall = requests.session() apiCall.headers.update({'Content-type':'application/json'}) apiCall.auth = (username,password) apiUri = 'https://' + host + path try: apiResponse = apiCall.delete(apiUri,verify=False) except requests.exceptions.RequestException as e: print('{"responseStatus":"error","action":"delete","host":"' + host + '","username":"' + username + '","path":"' + path + '"},"errorMsg":"' + str(e) + '"}') return(apiResponse.text) # # Parse the command line arguments # cmdargs = argparse.ArgumentParser() cmdargs.add_argument('--host',action='store',required=True,type=str,help='ip of BIG-IP REST interface, typically the mgmt ip') cmdargs.add_argument('--username',action='store',required=True,type=str,help='username for REST authentication') cmdargs.add_argument('--password',action='store',required=True,type=str,help='password for REST authentication') cmdargs.add_argument('--uri',action='store',required=True,type=str,help='the uri of the resource') cmdargs.add_argument('--action',action='store',required=True,type=str,help='the HTTP action (GET, PUT, POST, PATCH, DELETE)') cmdargs.add_argument('--payload',action='store',required=False,type=json.loads,help='the payload for PUT, POST and PATCH operations') parsed_args = cmdargs.parse_args() host = parsed_args.host username = parsed_args.username password = parsed_args.password uri = parsed_args.uri action = str(parsed_args.action).lower() payload = parsed_args.payload # # Proxy the REST call # if action == 'get': response = icontrol_get(host,username,password,uri) if action == 'put': response = icontrol_put(host,username,password,uri,payload) if action == 'post': response = icontrol_post(host,username,password,uri,payload) if action == 'delete': response = icontrol_delete(host,username,password,uri) if action == 'patch': response = icontrol_patch(host,username,password,uri,payload) print(response)
Tested this on version:
12.1Published Aug 13, 2019
Version 1.0G-Rob
Employee
Joined May 16, 2019
G-Rob
Employee
Joined May 16, 2019
No CommentsBe the first to comment