[ovirt-users] RHEVM3.3 java SDK on RHEV3.6

Juan Hernández jhernand at redhat.com
Tue Nov 8 08:57:38 UTC 2016


On 11/06/2016 09:39 AM, Tejesh M wrote:
> Hi All,
> 
> I'm facing an issue in invoking Cloudinit while customization in RHEV
> 3.6 using RHEV Java SDK 3.6.
> 
> Below link says we need to explicitly call *use_cloud_init=True*, but
> i'm not sure how to pass this value as parameter while creating VM.
> 
> http://users.ovirt.narkive.com/BGUkanAk/ovirt-users-cloud-init-not-apply-when-v-create-from-api
> 

You need to use version 3.6.0.0 of the SDK, or newer, as that is where
the support for this "use_cloud_init" parameter was added.

The new parameter is part of the "Action" class, so in your code you
need to set it:

  actionData.setUseCloudInit(true);

I take the opportunity to remind you that in version 4.2 of the engine
we will drop support for version 3 of the API and of version 3 of the
SDKs. So, once you migrate to version 4 of the engine consider using
version 4 of the SDK. There is an example of how to use cloud-init with
version 4 of the SDK here:


https://github.com/oVirt/ovirt-engine-sdk-java/blob/master/sdk/src/test/java/org/ovirt/engine/sdk4/examples/StartVmWithCloudInit.java

Note that this won't work with version 3.6 of the engine, for that you
need to keep using version 3 of the SDK.

> 
> _*My Code:*_
> String password = "password"; //"password"
> 
> String templateName = "centos6.7-final";
> String clusterName = "Default";
> String vmName = "testVM3";
> 
> //Prepare the data to create the VM from the template:
> org.ovirt.engine.sdk.entities.Template templateData = new Template();
> templateData.setName(templateName);
> org.ovirt.engine.sdk.entities.Cluster clusterData = new Cluster();
> clusterData.setName(clusterName);
> org.ovirt.engine.sdk.entities.VM vmDataForCreate = new VM();
> vmDataForCreate.setName(vmName);
> vmDataForCreate.setCluster(clusterData);
> vmDataForCreate.setTemplate(templateData);
> 
> api.getVMs().add(vmDataForCreate);
> String state;
> for (;;) {
>  state = api.getVMs().get(vmName).getStatus().getState();
> if ("down".equals(state)) {
>   break;
> }
> Thread.sleep(1000);
> }
> System.out.println("System Status:"+state); 
> org.ovirt.engine.sdk.entities.User userData = new User();
> userData.setUserName("root");
> userData.setPassword(password);
> Users usersData = new Users();
> usersData.getUsers().add(userData);
> CloudInit cloudData = new CloudInit();
> 
> 
> cloudData.setUsers(usersData);
> Host hostData = new Host();
> hostData.setAddress(vmName);
> cloudData.setHost(hostData);
> 
> org.ovirt.engine.sdk.entities.NetworkConfiguration
> networkConfiguration=new NetworkConfiguration();
> 
> networkConfiguration.setNics(new Nics());
> 
> Nics nics = networkConfiguration.getNics();
> nics.getNics().add(createNic("eth0", "STATIC",
> createNetwork("192.168.1.102", "255.255.0.0", "192.168.2.1"), true));
> 
> networkConfiguration.setNics(nics);
> 
> cloudData.setNetworkConfiguration(networkConfiguration);
> 
> Initialization initData = new Initialization();
> 
> initData.setCloudInit(cloudData);
> 
> VM vmDataForStart = new VM();
> vmDataForStart.setInitialization(initData);
> Action actionData = new Action();
> 
> actionData.setVm(vmDataForStart);
> 
> // Send the request to start the VM to the server:
> api.getVMs().get(vmName).start(actionData);
> 
> -- 
> Thanks & Regards
> Tejesh
> 
-- 
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