[ovirt-users] Error while using REST API with Ovirt

Juan Hernandez jhernand at redhat.com
Thu Sep 18 10:05:20 UTC 2014


On 09/18/2014 08:18 AM, Chandrahasa S wrote:
> Dear Experts.
> 
> We are Integrating our internal cloud portal with  Ovirt / RHEVM version
> 3.4.
> 
> We are integrating our internal cloud with Ovirt / RHEVM. VM template
> created using cloud init.
> 
> Through REST API Nippet while codes passes command to template ( with
> cloud init) IP, HOSTNAME, We are able to set IP and Hostname to VM
> through code.
> 
> But post this Manager getting handed. Error code is attached.
> 
> Need your help please.
> 

If I understand correctly you already created a template with the cloud
init configuration, then you created a VM from that template, and you
want to modify it and start it. In order to update the VM you need to
issue a PUT request, and the tag name for the host is "host_name":

    String updateXml =
          "<?xml version=\"1.0\"?>"
        + "<vm>"
        +   "<initialization>"
        +     "<host_name>meghaasadmin.ultimatixuat.net</host_name>"
        +     "<regenerate_ssh_keys>false</regenerate_ssh_keys>"
        +     "<nic_configurations>"
        +       "<nic_configuration>"
        +         "<name>eth0</name>"
        +         "<ip address=\"1.1.1.1\" netmask=\"255.255.255.0\"
gateway=\"13.3.3.3\"/>"
        +         "<boot_protocol>static</boot_protocol>"
        +         "<on_boot>true</on_boot>"
        +       "</nic_configuration>"
        +     "</nic_configurations>"
        +   "</initialization>"
        + "</vm>";
    StringEntity updateEntity = new StringEntity(updateXml);
    HttpPut updateRequest = new HttpPut(apiUrl + "/vms/" + vmId);
    updateRequest.setHeader("Content-Type", "application/xml");
    updateRequest.setHeader("Accept", "application/xml");
    updateRequest.setEntity(updateEntity);
    HttpResponse updateResponse = client.execute(updateRequest);

Then, after the update, you need to start the VM and it will use that
configuration:

    String startXml =
          "<?xml version=\"1.0\"?>"
        + "<action/>";
    StringEntity startEntity = new StringEntity(startXml);
    HttpPost startRequest = new HttpPost(apiUrl + "/vms/" + vmId +
"/start");
    startRequest.setEntity(startEntity);
    startRequest.setHeader("Content-Type", "application/xml");
    startRequest.setHeader("Accept", "application/xml");
    HttpResponse startResponse = client.execute(startRequest);

Some advices:

* Check the XML schema for the structure of the XML documents:

  https://rhevmanager/api?schema

* When sending requests to the RESTAPI makes sure to always explicitly
add the Content-Type and Accept headers:

  yourRequest.setHeader("Content-Type", "application/xml");
  yourRequest.setHeader("Accept", "application/xml");

* Take into account that the initialization with cloud-init will only be
executed the first time you start the VM, so in your tests you will need
to remove the VM and create it again.

* If you are planning to do complex things you may find it helpful the
Java SDK:

  http://www.ovirt.org/Java-sdk

-- 
Dirección Comercial: C/Jose Bardasano Baos, 9, Edif. Gorbea 3, planta
3ºD, 28016 Madrid, Spain
Inscrita en el Reg. Mercantil de Madrid – C.I.F. B82657941 - Red Hat S.L.



More information about the Users mailing list