Trying to send a basic json array in a POST url API for the use of vmware vcenter.
I'm writing this in python which i'm new to.
Had a look at a few examples before posting this but they did'nt seem suited for this application.
I've Been googling similar examples and trying them, none of them have worked.
I have a large JSON array to post in this URL. Authentication is required to do so aswell hence a cookie session is requested first before executing the second API.
This API is used to clone a VM from a template in a library.
Just wondering if someone could help me. An understanding of vcenter is not required, just need help with a basic URL POST after session authentication and to be able to properly send a JSON array in the post.
The JSON array was developed using postman which appeared to have added a tonne of slashes, still no luck with or without them.
Could be just a simple typo that I can't see...
Thanks, heaps
I've tried altering the end header option in this line of the code where "data=json". Tried using params, data, JSON aswell.
item_detail_json_array=s.post('https://'+vcip+'/rest/vcenter/vm-template/library-items/37a0484b-bd8c-486f-b110-36c79c2295f7?action=deploy', data=json)
Also tried rearranging the JSON array with or without indents and newlines, with " and '.
import requests
import json
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
s=requests.Session()
s.verify=False
import json
vcip="xxx.xxx.xxx.xxx"
json = "{\r\n \"spec\": {\r\n \"description\": \"\",\r\n \"disk_storage\": {\r\n \"datastore\": \"datastore-25\",\r\n \"storage_policy\": {\r\n \"policy\": \"aa6d5a82-1c88-45da-85d3-3d74b91a5bad\",\r\n \"type\": \"USE_SPECIFIED_POLICY\"\r\n }\r\n },\r\n \"disk_storage_overrides\": [\r\n {\r\n \"key\": \"2000\",\r\n \"value\": {\r\n \"datastore\": \"datastore-25\",\r\n \"storage_policy\": {\r\n \"policy\": \"aa6d5a82-1c88-45da-85d3-3d74b91a5bad\",\r\n \"type\": \"USE_SPECIFIED_POLICY\"\r\n }\r\n }\r\n }\r\n ],\r\n \"guest_customization\": {\r\n \"name\": null\r\n },\r\n \"hardware_customization\": {\r\n \"cpu_update\": {\r\n \"num_cores_per_socket\": null,\r\n \"num_cpus\": null\r\n },\r\n \"disks_to_update\": [\r\n {\r\n \"key\": \"2000\",\r\n \"value\": {\r\n \"capacity\": 32212254721\r\n }\r\n }\r\n ],\r\n \"memory_update\": {\r\n \"memory\": null\r\n },\r\n \"nics\": [\r\n {\r\n \"key\": \"4000\",\r\n \"value\": {\r\n \"network\": \"network-26\"\r\n }\r\n }\r\n ]\r\n },\r\n \"name\": \"cloned via api6\",\r\n \"placement\": {\r\n \"folder\": \"group-v18\",\r\n \"resource_pool\": \"resgroup-23\"\r\n },\r\n \"powered_on\": true,\r\n \"vm_home_storage\": {\r\n \"datastore\": \"datastore-25\",\r\n \"storage_policy\": {\r\n \"policy\": \"aa6d5a82-1c88-45da-85d3-3d74b91a5bad\",\r\n \"type\": \"USE_SPECIFIED_POLICY\"\r\n }\r\n }\r\n }\r\n}"
def get_vc_session(vcip,username,password):
s.post('https://'+vcip+'/rest/com/vmware/cis/session', auth=(username,password))
return s
def get_vms(vcip):
item_detail_json_array=s.post('https://'+vcip+'/rest/vcenter/vm-template/library-items/37a0484b-bd8c-486f-b110-36c79c2295f7?action=deploy', data=json)
return item_detail_json_array
vcsession = get_vc_session(vcip,"administrator@vcenterserver","password1234")
itemid_json_array = get_vms(vcip) #executes the api enquiry stores as array
for x in itemid_json_array:
print(x) # just to get the response working for now
UPDATE--------------------------------
This code is working instead, seems perhaps vcenter only wants the session ID posted in the header in this method. Ugly and not very simple but it works to say the least. I tried just performing the one URL request, having all the parameters in there and using the AUTH parameter aswell, but vcenter kept spitting the dummy. So I believe i've narrowed it down to an authentication error. Seems to work okay with my original solution, however in this case, we're posting a large json array. In this case, Vcenter does not seem to like what is being spat out by the previous solution after the session request attempt is done, and seems to prefer the session id presented in the header when requesting the second API to clone the VM in vcenter. Most of this idea came form the example that POSTMAN spat out using an SDK library from vcenter.
import requests
import json
s=requests.Session()
s.verify=False
vcip = "xxx.xxx.xxx.xxx"
vmname = "testclonepython2"
username = 'administrator@vcenterdomain'
password = 'vcenterpass'
def get_vc_session(username,password):
s.post('https://'+vcip+'/rest/com/vmware/cis/session', auth=(username,password))
return s
get_vc_session(username,password)
cookie = s.cookies['vmware-api-session-id']
url = "https://"+vcip+"/rest/vcenter/vm-template/library-items/37a0484b-bd8c-486f-b110-36c79c2295f7"
querystring = {"action":"deploy"}
payload = "{\n \"spec\": {\n \"description\": \"\",\n \"disk_storage\": {\n \"datastore\": \"datastore-25\",\n \"storage_policy\": {\n \"policy\": \"aa6d5a82-1c88-45da-85d3-3d74b91a5bad\",\n \"type\": \"USE_SPECIFIED_POLICY\"\n }\n },\n \"disk_storage_overrides\": [\n {\n \"key\": \"2000\",\n \"value\": {\n \"datastore\": \"datastore-25\",\n \"storage_policy\": {\n \"policy\": \"aa6d5a82-1c88-45da-85d3-3d74b91a5bad\",\n \"type\": \"USE_SPECIFIED_POLICY\"\n }\n }\n }\n ],\n \"guest_customization\": {\n \"name\": null\n },\n \"hardware_customization\": {\n \"cpu_update\": {\n \"num_cores_per_socket\": null,\n \"num_cpus\": null\n },\n \"disks_to_update\": [\n {\n \"key\": \"2000\",\n \"value\": {\n \"capacity\": 32212254721\n }\n }\n ],\n \"memory_update\": {\n \"memory\": null\n },\n \"nics\": [\n {\n \"key\": \"4000\",\n \"value\": {\n \"network\": \"network-26\"\n }\n }\n ]\n },\n \"name\": \""+vmname+"\",\n \"placement\": {\n \"folder\": \"group-v18\",\n \"resource_pool\": \"resgroup-23\"\n },\n \"powered_on\": true,\n \"vm_home_storage\": {\n \"datastore\": \"datastore-25\",\n \"storage_policy\": {\n \"policy\": \"aa6d5a82-1c88-45da-85d3-3d74b91a5bad\",\n \"type\": \"USE_SPECIFIED_POLICY\"\n }\n }\n }\n}"
headers = {
'Content-Type': "application/json",
'Accept': "*/*",
'Cache-Control': "no-cache",
'Host': vcip,
'vmware-api-session-id': cookie,
'Accept-Encoding': "gzip, deflate",
'Connection': "keep-alive",
'cache-control': "no-cache"
}
response = requests.request("POST", url, data=payload, headers=headers, params=querystring, verify=False)
print(response.text)