----- Original Message -----
> From: "Tejesh M" <tejes...@gmail.com>
> To: "users@oVirt.org" <users@ovirt.org>
> Sent: Wednesday, February 19, 2014 3:24:40 PM
> Subject: [Users] Fwd: Sample code for setting NIC - CloudInit
> 
> Hi,
> 
> Can someone share me sample java code for assigning IP address for VM on eth0
> through Java SDK via CloudInit ?
> 

Hi Tejesh,

I've attached a sample code that sends the required request (as the output is 
demonstrated in debug mode).
Note that the code is jdk-7 compliant.
I haven't configured cloud-init and haven't tested it end-to-end.
Please try to test it on your environment and provide a feedback for it.

Thanks,
Moti

> Something Like this but in Java:
> <network_configuration>
>          <nics>
>            <nic>
>              <name>eth0</name>
>              <boot_protocol>STATIC</boot_protocol>
>              <network>
>                <ip address="192.168.2.11" netmask="255.255.0.0"
>                gateway="192.168.2.1" />
>              </network>
>              <on_boot>true</on_boot>
>            </nic>
>            <nic>
>              <name>eth1</name>
>              <boot_protocol>DHCP</boot_protocol>
>            </nic>
>            <nic>
>              <name>eth2</name>
>              <boot_protocol>NONE</boot_protocol>
>              <on_boot>true</on_boot>
>            </nic>
>          </nics>
>          <dns>
>            <servers>
>              <host>
>                <address>1.1.2.2</address>
>              </host>
>              <host>
>                <address>1.2.3.4</address>
>              </host>
>            </servers>
>            <search_domains>
>              <host>
>                <address>qa.lab</address>
>              </host>
>              <host>
>                <address> google.com </address>
>              </host>
>            </search_domains>
>          </dns>
>        </network_configuration>
> 
> 
> 
> --
> Thanks & Regards
> Tejesh
> 
> _______________________________________________
> Users mailing list
> Users@ovirt.org
> http://lists.ovirt.org/mailman/listinfo/users
> 
package org.ovirt.testday;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.ovirt.engine.sdk.Api;
import org.ovirt.engine.sdk.decorators.VM;
import org.ovirt.engine.sdk.entities.Action;
import org.ovirt.engine.sdk.entities.CloudInit;
import org.ovirt.engine.sdk.entities.DNS;
import org.ovirt.engine.sdk.entities.Host;
import org.ovirt.engine.sdk.entities.Hosts;
import org.ovirt.engine.sdk.entities.IP;
import org.ovirt.engine.sdk.entities.Initialization;
import org.ovirt.engine.sdk.entities.NIC;
import org.ovirt.engine.sdk.entities.Network;
import org.ovirt.engine.sdk.entities.NetworkConfiguration;
import org.ovirt.engine.sdk.entities.Nics;
import org.ovirt.engine.sdk.exceptions.ServerException;
import org.ovirt.engine.sdk.exceptions.UnsecuredConnectionAttemptError;


/**
 * Sample non-secured code for sending the following request:
 *     <network_configuration>
         <nics>
           <nic>
             <name>eth0</name>
             <boot_protocol>STATIC</boot_protocol>
             <network>
               <ip address="192.168.2.11" netmask="255.255.0.0" gateway="192.168.2.1" />
             </network>
             <on_boot>true</on_boot>
           </nic>
           <nic>
             <name>eth1</name>
             <boot_protocol>DHCP</boot_protocol>
           </nic>
           <nic>
             <name>eth2</name>
             <boot_protocol>NONE</boot_protocol>
             <on_boot>true</on_boot>
           </nic>
         </nics>
         <dns>
           <servers>
             <host>
               <address>1.1.2.2</address>
             </host>
             <host>
               <address>1.2.3.4</address>
             </host>
           </servers>
           <search_domains>
             <host>
               <address>qa.lab</address>
             </host>
             <host>
               <address>google.com</address>
             </host>
           </search_domains>
         </dns>
       </network_configuration>
 *
 */
public class VmCloudInit {

    public static void main(String[] args) throws ClientProtocolException, ServerException,
            UnsecuredConnectionAttemptError, IOException {

        try (Api api = new Api("http://localhost:8080/api";,
                "admin@internal",
                "1",
                null, null, null, null, null, null, true)){

            VM vm = api.getVMs().get("vm1");
            NetworkConfiguration networkConfiguration = new NetworkConfiguration();
            DNS dns = new DNS();
            dns.setServers(createServersList("1.1.2.2", "1.2.3.4"));
            dns.setSearchDomains(createServersList("qa.lab", "google.com"));
            networkConfiguration.setDns(dns);
            networkConfiguration.setNics(new Nics());
            List<NIC> nics = networkConfiguration.getNics().getNics();
            nics.add(createNic("eth0", "STATIC", createNetwork("192.168.2.11", "255.255.0.0", "192.168.2.1"), true));
            nics.add(createNic("eth1", "DHCP", null, false));
            nics.add(createNic("eth1", "NONE", null, true));
            vm.setInitialization(new Initialization());
            vm.getInitialization().setCloudInit(new CloudInit());
            vm.getInitialization().getCloudInit().setNetworkConfiguration(networkConfiguration);
            Action action = new Action();
            action.setVm(vm);
            vm.start(action);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static NIC createNic(String name, String bootProtocol, Network network, boolean onBoot) {
        NIC nic = new NIC();
        nic.setName(name);
        nic.setBootProtocol(bootProtocol);
        nic.setNetwork(network);
        nic.setOnBoot(onBoot);
        return nic;
    }

    public static Hosts createServersList(String... addresses) {
        List<String> hostAddresses = Arrays.asList(addresses);
        Hosts servers = new Hosts();
        for (String address : hostAddresses) {
            Host host = new Host();
            host.setAddress(address);
            servers.getHosts().add(host);
        }

        return servers;
    }

    public static Network createNetwork(String address, String netmask, String gateway) {
        Network network = new Network();
        IP ip = new IP();
        ip.setAddress(address);
        ip.setNetmask(netmask);
        ip.setGateway(gateway);
        network.setIp(ip);
        return network;
    }

}
_______________________________________________
Users mailing list
Users@ovirt.org
http://lists.ovirt.org/mailman/listinfo/users

Reply via email to