How to deploy a virtual machine from OVA or OVF
Pre-reqs
- Access to the API
- Generate or use an existing Access Token.
Step-by-step guide
In order to deploy a new virtual machine, we need at least 5 parameters, however, in case of deploying a vm from image, there's a 6th parameter:
- client = DepartmentName
- built_from = Image
- description = Testing image deployment
- folder
- os
- source_image
If additional customization is needed, we can add:
- disks
- networks
Source Image
In order to get the source image, the user deploying the VM will need to upload the OVA or OVF to VSKEY-STOR and then look for the path using the API as follows:
Login to VSKEY-STOR with your VSS credentials and upload the OVA or OVF (including vmdk disks) to deploy.
Since we run vSphere 5.5, VMX 10 is supported. If you have the latest VMware Fusion or Workstation (VMX 12), update VM compatibility settings to version 10.
Using the /v2/image resource, query any available OVA/OVF files associated with your account as follows:
Requesthttp GET "https://vss-api.eis.utoronto.ca:8001/v2/image" "Authorization: Bearer $TK" curl -H "Authorization: Bearer $TK" -X GET "https://vss-api.eis.utoronto.ca:8001/v2/image"
For our new VM, we will use the JSON object attribute path Ubuntu_64-bit-vmx10.ova.
Target folder
Folders can be got from the /v2/folder with filters as shown below:
Make a GET request disabling summary and looking for the name in question, in this case our target folder will be API Demo
Requesthttp GET "https://vss-api.eis.utoronto.ca/v2/folder?filter=name,like,%APIDemo%" "Authorization: Bearer $TK" curl -H "Authorization: Bearer $TK" -X GET "https://vss-api.eis.utoronto.ca/v2/folder?filter=name,like,%APIDemo%"
- From the response body, save the managed object reference (moref) of the folder in question: group-6666. This will be the value of the parameter folder in the VM deployment request.
Target OS
Using the /v2/os resource, the guest identifier can be obtained filtering by name:
Make a GET request with the filter name equals to the operating system to use, for instance ubuntu:
http GET "https://vss-api.eis.utoronto.ca/v2/os?filter=guest_id,like,%ubuntu%" "Authorization: Bearer $TK" curl -H "Authorization: Bearer $TK" -X GET "https://vss-api.eis.utoronto.ca/v2/os?filter=guest_id,like,%ubuntu%"
- Copy the attribute id value ubuntu64Guest which will be used to populate the os parameter in the VM deployment request.
Target Network
The /v2/network resource provides all virtual networks available for you to use, and as we did with the target folder, we need the target network moref.
Make a GET request to the network resource filtering by name as follows:
http GET "https://vss-api.eis.utoronto.ca/v2/network?filter=name,like,%1072%" "Authorization: Bearer $TK" curl -H "Authorization: Bearer $TK" -X GET "https://vss-api.eis.utoronto.ca:8001/v2/network?filter=name,like,%1072%"
- The managed object reference dvportgroup-92 will be used to map any existent NIC. If it does not exist, a new NIC will be created.
Deployment
So far, we've got the following parameters ready to deploy a new vm:
- client = DepartmentName
- built_from = Image
- description = Testing image deployment
- folder = group-6666
- os = ubuntu64Guest
- source_image = Ubuntu_64-bit-vmx10.ova
- networks = dvportgroup-92
However, according to the API documentation, the payload sent via a POST request to the /v2/vm endpoint needs to be sent in JSON format.
{"client": "DepartmentName", "built_from": "image", "description": "Testing image deployment", "folder": "group-6666", "os": "ubuntu64Guest", "source_image": "Ubuntu_64-bit-vmx10.ova", "networks": [{"network": "dvportgroup-92"}], "disks": [40, 40]}
Disks was not considered originally but can be added. In this example it will expand disk 1 and add another disk of 40G.
curl -H "Authorization: Bearer $TK" -X POST -H "Content-Type: application/json" https://vss-api.eis.utoronto.ca:8001/v2/vm -d {"client": "DepartmentName", "built_from": "image", "description": "Testing image deployment", "folder": "group-6666", "os": "ubuntu64Guest", "source_image": "Ubuntu_64-bit-vmx10.ova", "networks": [{"network": "dvportgroup-92"}], "disks": [40, 40]}
http POST "https://vss-api.eis.utoronto.ca:8001/v2/vm client=DepartmentName" "Authorization: Bearer $TK" built_from=image description="Testing image deployment" folder="group-6666" os="ubuntu64Guest" source_image="Ubuntu_64-bit-vmx10.ova" networks:='[{"network": "dvportgroup-92"}]' disks:='[40, 40]'
Related articles