/*
         * Creates a basic VM on a cluster with the following configuration:
         * - Create 2 disks and specify one of them on scsi0:0 since
         * it's the boot disk.
         * - Specify 1 ethernet adapter using a Standard Portgroup backing.
         * - Setup for PXE install by selecting network as first boot device.
         * - Use guest and system provided defaults for most configuration settings.
         */
        private void CreateVm(VMTypes.PlacementSpec vmPlacementSpec, string standardNetworkBacking)
        {
            // Create the scsi disk as a boot disk
            DiskTypes.CreateSpec bootDiskCreateSpec =
                new DiskTypes.CreateSpec();
            bootDiskCreateSpec.SetType(DiskTypes.HostBusAdapterType.SCSI);
            ScsiAddressSpec scsiAddressSpec = new ScsiAddressSpec();

            scsiAddressSpec.SetBus(0L);
            scsiAddressSpec.SetUnit(0L);
            bootDiskCreateSpec.SetScsi(scsiAddressSpec);
            bootDiskCreateSpec.SetNewVmdk(new DiskTypes.VmdkCreateSpec());

            // Create a data disk
            DiskTypes.CreateSpec dataDiskCreateSpec =
                new DiskTypes.CreateSpec();
            dataDiskCreateSpec.SetNewVmdk(new DiskTypes.VmdkCreateSpec());

            // Create a Ethernet NIC with standard network backing
            EthernetTypes.BackingSpec nicBackingSpec =
                new EthernetTypes.BackingSpec();
            nicBackingSpec.SetType(EthernetTypes.BackingType.STANDARD_PORTGROUP);
            nicBackingSpec.SetNetwork(standardNetworkBacking);
            EthernetTypes.CreateSpec nicCreateSpec =
                new EthernetTypes.CreateSpec();
            nicCreateSpec.SetStartConnected(true);
            nicCreateSpec.SetBacking(nicBackingSpec);

            // Specify the boot order
            List <DeviceTypes.EntryCreateSpec> bootDevices =
                new List <DeviceTypes.EntryCreateSpec>();

            bootDevices.Add(new DeviceTypes.EntryCreateSpec());
            bootDevices.Add(new DeviceTypes.EntryCreateSpec());
            bootDevices[0].SetType(DeviceTypes.Type.ETHERNET);
            bootDevices[1].SetType(DeviceTypes.Type.DISK);

            // Create the VM
            VMTypes.CreateSpec vmCreateSpec = new VMTypes.CreateSpec();
            vmCreateSpec.SetGuestOS(vmGuestOS);
            vmCreateSpec.SetName(BasicVmName);
            vmCreateSpec.SetBootDevices(bootDevices);
            vmCreateSpec.SetPlacement(vmPlacementSpec);
            vmCreateSpec.SetNics(new List <EthernetTypes.CreateSpec>
            {
                nicCreateSpec
            });
            vmCreateSpec.SetDisks(new List <DiskTypes.CreateSpec>
            {
                bootDiskCreateSpec,
                dataDiskCreateSpec
            });

            Console.WriteLine(dataDiskCreateSpec.ToString());
            Console.WriteLine("\n\n#### Example: Creating Basic VM with spec:\n"
                              + vmCreateSpec);
            this.vmService = VapiAuthHelper.StubFactory.CreateStub <VM>(
                SessionStubConfiguration);
            this.basicVmId = this.vmService.Create(vmCreateSpec);
            VMTypes.Info vmInfo = this.vmService.Get(basicVmId);
            Console.WriteLine("\nBasic VM Info:\n" + vmInfo);
        }
        /// <summary>
        /// Create an exhaustive VM with the following configuration:
        /// - Hardware Version = VMX_11 (for 6.0)
        /// - CPU (count = 2, coresPerSocket = 2, hotAddEnabled = false,
        ///   hotRemoveEnabled = false)
        /// - Memory (size_mib = 2 GB, hotAddEnabled = false)
        /// - 3 Disks and specify each of the HBAs and the unit numbers
        ///   (capacity=40 GB, name=<some value>, spaceEfficient=true)
        /// - Specify 2 ethernet adapters, one using a Standard Portgroup backing
        ///   and the other using a DISTRIBUTED_PORTGROUP networking backing.
        ///        # nic1: Specify Ethernet (macType=MANUAL,
        ///        macAddress=<some value>)
        ///        # nic2: Specify Ethernet (macType=GENERATED)
        /// - 1 CDROM (type=ISO_FILE, file="small.iso", startConnected=true)
        /// - 1 Serial Port (type=NETWORK_SERVER, file="tcp://localhost/16000",
        ///   startConnected=true)
        /// - 1 Parallel Port  (type=HOST_DEVICE, startConnected=false)
        /// - 1 Floppy Drive (type=CLIENT_DEVICE)
        /// - Boot, type=BIOS
        /// - BootDevice order: CDROM, DISK, ETHERNET
        /// </summary>
        private void CreateVm(VMTypes.PlacementSpec vmPlacementSpec,
                              string standardNetworkBacking, string distributedNetworkBacking)
        {
            // CPU UpdateSpec
            CpuTypes.UpdateSpec cpuUpdateSpec = new CpuTypes.UpdateSpec();
            cpuUpdateSpec.SetCoresPerSocket(1L);
            cpuUpdateSpec.SetHotAddEnabled(false);
            cpuUpdateSpec.SetHotRemoveEnabled(false);

            // Memory UpdateSpec
            MemoryTypes.UpdateSpec memoryUpdateSpec =
                new MemoryTypes.UpdateSpec();
            memoryUpdateSpec.SetSizeMiB(2 * 1024L);
            memoryUpdateSpec.SetHotAddEnabled(false);

            // Boot disk
            ScsiAddressSpec scsiAddressSpec = new ScsiAddressSpec();

            scsiAddressSpec.SetBus(0L);
            scsiAddressSpec.SetUnit(0L);
            DiskTypes.VmdkCreateSpec vmdkCreateSpec1 =
                new DiskTypes.VmdkCreateSpec();
            vmdkCreateSpec1.SetCapacity(40 * GB);
            vmdkCreateSpec1.SetName("boot");
            DiskTypes.CreateSpec diskCreateSpec1 = new DiskTypes.CreateSpec();
            diskCreateSpec1.SetType(DiskTypes.HostBusAdapterType.SCSI);
            diskCreateSpec1.SetScsi(scsiAddressSpec);
            diskCreateSpec1.SetNewVmdk(vmdkCreateSpec1);

            // Data disk 1
            DiskTypes.VmdkCreateSpec vmdkCreateSpec2 =
                new DiskTypes.VmdkCreateSpec();
            vmdkCreateSpec2.SetCapacity(10 * GB);
            vmdkCreateSpec2.SetName("data1");
            DiskTypes.CreateSpec diskCreateSpec2 = new DiskTypes.CreateSpec();
            diskCreateSpec2.SetNewVmdk(vmdkCreateSpec2);

            // Data disk 2
            DiskTypes.VmdkCreateSpec vmdkCreateSpec3 =
                new DiskTypes.VmdkCreateSpec();
            vmdkCreateSpec3.SetCapacity(10 * GB);
            vmdkCreateSpec3.SetName("data2");
            DiskTypes.CreateSpec diskCreateSpec3 = new DiskTypes.CreateSpec();
            diskCreateSpec3.SetNewVmdk(vmdkCreateSpec3);

            // Ethernet CreateSpec (manual with standard portgroup)
            EthernetTypes.BackingSpec nicStandardNetworkBacking =
                new EthernetTypes.BackingSpec();
            nicStandardNetworkBacking.SetNetwork(standardNetworkBacking);
            nicStandardNetworkBacking.SetType(
                EthernetTypes.BackingType.STANDARD_PORTGROUP);
            EthernetTypes.CreateSpec manualEthernetSpec =
                new EthernetTypes.CreateSpec();
            manualEthernetSpec.SetStartConnected(true);
            manualEthernetSpec.SetMacType(EthernetTypes.MacAddressType.MANUAL);
            manualEthernetSpec.SetMacAddress("11:23:58:13:21:34");
            manualEthernetSpec.SetBacking(nicStandardNetworkBacking);


            EthernetTypes.BackingSpec nicDistributedNetworkBacking =
                new EthernetTypes.BackingSpec();
            nicDistributedNetworkBacking.SetNetwork(distributedNetworkBacking);
            nicDistributedNetworkBacking.SetType(
                EthernetTypes.BackingType.DISTRIBUTED_PORTGROUP);
            EthernetTypes.CreateSpec generatedEthernetSpec =
                new EthernetTypes.CreateSpec();
            generatedEthernetSpec.SetStartConnected(true);
            generatedEthernetSpec.SetMacType(
                EthernetTypes.MacAddressType.GENERATED);
            generatedEthernetSpec.SetBacking(nicDistributedNetworkBacking);

            // Cdrom CreateSpec
            CdromTypes.BackingSpec cdromBackingSpec =
                new CdromTypes.BackingSpec();
            cdromBackingSpec.SetType(CdromTypes.BackingType.ISO_FILE);
            cdromBackingSpec.SetIsoFile(IsoDatastorePath);
            CdromTypes.CreateSpec cdromCreateSpec =
                new CdromTypes.CreateSpec();
            cdromCreateSpec.SetBacking(cdromBackingSpec);

            // Serial Port CreateSpec
            SerialTypes.BackingSpec serialBackingSpec =
                new SerialTypes.BackingSpec();
            serialBackingSpec.SetType(SerialTypes.BackingType.NETWORK_SERVER);
            serialBackingSpec.SetNetworkLocation(
                new Uri(SerialPortNetworkServiceLocation));
            SerialTypes.CreateSpec serialCreateSpec =
                new SerialTypes.CreateSpec();
            serialCreateSpec.SetStartConnected(false);
            serialCreateSpec.SetBacking(serialBackingSpec);

            // Parallel port CreateSpec
            ParallelTypes.BackingSpec parallelBackingSpec =
                new ParallelTypes.BackingSpec();
            parallelBackingSpec.SetType(ParallelTypes.BackingType.HOST_DEVICE);
            ParallelTypes.CreateSpec parallelCreateSpec =
                new ParallelTypes.CreateSpec();
            parallelCreateSpec.SetBacking(parallelBackingSpec);
            parallelCreateSpec.SetStartConnected(false);

            // Floppy CreateSpec
            FloppyTypes.BackingSpec floppyBackingSpec =
                new FloppyTypes.BackingSpec();
            floppyBackingSpec.SetType(FloppyTypes.BackingType.CLIENT_DEVICE);
            FloppyTypes.CreateSpec floppyCreateSpec =
                new FloppyTypes.CreateSpec();
            floppyCreateSpec.SetBacking(floppyBackingSpec);

            // Specify the boot order
            DeviceTypes.EntryCreateSpec cdromEntry =
                new DeviceTypes.EntryCreateSpec();
            cdromEntry.SetType(DeviceTypes.Type.CDROM);

            DeviceTypes.EntryCreateSpec diskEntry =
                new DeviceTypes.EntryCreateSpec();
            diskEntry.SetType(DeviceTypes.Type.DISK);

            DeviceTypes.EntryCreateSpec ethernetEntry =
                new DeviceTypes.EntryCreateSpec();
            ethernetEntry.SetType(DeviceTypes.Type.ETHERNET);

            List <DeviceTypes.EntryCreateSpec> bootDevices =
                new List <DeviceTypes.EntryCreateSpec> {
                cdromEntry, diskEntry,
                ethernetEntry
            };

            // Create a VM with above configuration
            VMTypes.CreateSpec vmCreateSpec = new VMTypes.CreateSpec();
            vmCreateSpec.SetBootDevices(bootDevices);
            vmCreateSpec.SetCdroms(
                new List <CdromTypes.CreateSpec> {
                cdromCreateSpec
            });
            vmCreateSpec.SetCpu(cpuUpdateSpec);
            vmCreateSpec.SetDisks(new List <DiskTypes.CreateSpec> {
                diskCreateSpec1, diskCreateSpec2, diskCreateSpec3
            });
            vmCreateSpec.SetFloppies(
                new List <FloppyTypes.CreateSpec> {
                floppyCreateSpec
            });
            vmCreateSpec.SetHardwareVersion(HARDWARE_VERSION);
            vmCreateSpec.SetMemory(memoryUpdateSpec);
            vmCreateSpec.SetGuestOS(vmGuestOS);
            vmCreateSpec.SetName(ExhaustiveVmName);
            vmCreateSpec.SetNics(
                new List <EthernetTypes.CreateSpec> {
                manualEthernetSpec,
                generatedEthernetSpec
            });
            vmCreateSpec.SetParallelPorts(
                new List <ParallelTypes.CreateSpec> {
                parallelCreateSpec
            });
            vmCreateSpec.SetPlacement(vmPlacementSpec);
            vmCreateSpec.SetSerialPorts(
                new List <SerialTypes.CreateSpec> {
                serialCreateSpec
            });

            Console.WriteLine("\n\n#### Example: Creating exhaustive VM with "
                              + "spec:\n" + vmCreateSpec);
            this.vmService = VapiAuthHelper.StubFactory.CreateStub <VM>(
                SessionStubConfiguration);
            this.exhaustiveVMId = vmService.Create(vmCreateSpec);
            Console.WriteLine("\nCreated exhaustive VM : " + ExhaustiveVmName
                              + " with id: " + this.exhaustiveVMId);
            VMTypes.Info vmInfo = vmService.Get(this.exhaustiveVMId);
            Console.WriteLine("\nExhaustive VM Info:\n" + vmInfo);
        }
        public override void Run()
        {
            // Login
            VapiAuthHelper           = new VapiAuthenticationHelper();
            SessionStubConfiguration =
                VapiAuthHelper.LoginByUsernameAndPassword(
                    Server, UserName, Password);

            this.powerService = VapiAuthHelper.StubFactory.CreateStub <Power>(
                SessionStubConfiguration);
            this.ethernetService =
                VapiAuthHelper.StubFactory.CreateStub <Ethernet>(
                    SessionStubConfiguration);

            Console.WriteLine("\n\n#### Setup: Get the virtual machine id");
            this.vmId = VmHelper.GetVm(VapiAuthHelper.StubFactory,
                                       SessionStubConfiguration, VmName);
            Console.WriteLine("Using VM: " + VmName + " (vmId=" + this.vmId +
                              ") for ethernet adapter configuration sample");

            // List all ethernet adapters of the virtual machine
            List <EthernetTypes.Summary> nicSummaries =
                this.ethernetService.List(this.vmId);

            nicSummaries.ForEach(i => Console.WriteLine(i));

            Console.WriteLine("\n\n#### Print info for each Ethernet NIC on "
                              + "the vm.");
            foreach (EthernetTypes.Summary nicSummary in nicSummaries)
            {
                EthernetTypes.Info info =
                    this.ethernetService.Get(this.vmId, nicSummary.GetNic());
                Console.WriteLine(info);
            }

            Console.WriteLine("\n\n#### Example: Create ethernet NIC using "
                              + "standard portgroup and default settings");
            string stdNetworkId = NetworkHelper.GetStandardNetworkBacking(
                VapiAuthHelper.StubFactory, SessionStubConfiguration,
                Datacenter, StandardPortGroup);

            EthernetTypes.CreateSpec nicCreateSpec =
                new EthernetTypes.CreateSpec();
            EthernetTypes.BackingSpec nicBackingSpec =
                new EthernetTypes.BackingSpec();
            nicBackingSpec.SetNetwork(stdNetworkId);
            nicBackingSpec.SetType(
                EthernetTypes.BackingType.STANDARD_PORTGROUP);
            nicCreateSpec.SetBacking(nicBackingSpec);
            string nicId =
                this.ethernetService.Create(this.vmId, nicCreateSpec);

            this.createdNics.Add(nicId);
            Console.WriteLine(nicCreateSpec);
            EthernetTypes.Info nicInfo =
                this.ethernetService.Get(this.vmId, nicId);
            Console.WriteLine("VM ID=" + this.vmId);
            Console.WriteLine("Ethernet NIC ID=" + nicId);
            Console.WriteLine(nicInfo);

            Console.WriteLine("\n\n#### Example: Create Ethernet NIC using"
                              + " standard portgroup and specifying start_connected=true,"
                              + " allow_guest_control=true, mac_type, mac_address,"
                              + " wake_on_lan=enabled.");
            nicBackingSpec = new EthernetTypes.BackingSpec();
            nicBackingSpec.SetNetwork(stdNetworkId);
            nicBackingSpec.SetType(
                EthernetTypes.BackingType.STANDARD_PORTGROUP);
            nicCreateSpec = new EthernetTypes.CreateSpec();
            nicCreateSpec.SetAllowGuestControl(true);
            nicCreateSpec.SetMacType(EthernetTypes.MacAddressType.MANUAL);
            nicCreateSpec.SetMacAddress("01:23:45:67:89:10");
            nicCreateSpec.SetBacking(nicBackingSpec);
            nicId = this.ethernetService.Create(this.vmId, nicCreateSpec);
            this.createdNics.Add(nicId);
            Console.WriteLine(nicCreateSpec);
            nicInfo = this.ethernetService.Get(this.vmId, nicId);
            Console.WriteLine("VM ID=" + this.vmId);
            Console.WriteLine("Ethernet NIC ID=" + nicId);
            Console.WriteLine(nicInfo);

            Console.WriteLine("\n\n#### Example: Create Ethernet NIC using"
                              + " distributed portgroup and specifying start_connected=true,"
                              + " allow_guest_control=true, mac_type, mac_address,"
                              + " wake_on_lan=enabled.");
            nicBackingSpec = new EthernetTypes.BackingSpec();
            nicBackingSpec.SetNetwork(stdNetworkId);
            nicBackingSpec.SetType(
                EthernetTypes.BackingType.STANDARD_PORTGROUP);
            nicCreateSpec = new EthernetTypes.CreateSpec();
            nicCreateSpec.SetAllowGuestControl(true);
            nicCreateSpec.SetMacType(EthernetTypes.MacAddressType.MANUAL);
            nicCreateSpec.SetMacAddress("24:68:10:12:14:16");
            nicCreateSpec.SetBacking(nicBackingSpec);
            nicId = this.ethernetService.Create(this.vmId, nicCreateSpec);
            this.createdNics.Add(nicId);
            Console.WriteLine(nicCreateSpec);
            nicInfo = this.ethernetService.Get(this.vmId, nicId);
            Console.WriteLine("VM ID=" + this.vmId);
            Console.WriteLine("Ethernet NIC ID=" + nicId);
            Console.WriteLine(nicInfo);

            String lastNicId = nicId;

            Console.WriteLine(
                "\n\n#### Example: Update Ethernet NIC with different"
                + " backing.");
            nicBackingSpec = new EthernetTypes.BackingSpec();
            nicBackingSpec.SetType(
                EthernetTypes.BackingType.STANDARD_PORTGROUP);
            nicBackingSpec.SetNetwork(stdNetworkId);
            EthernetTypes.UpdateSpec nicUpdateSpec =
                new EthernetTypes.UpdateSpec();
            nicUpdateSpec.SetBacking(nicBackingSpec);
            this.ethernetService.Update(this.vmId, lastNicId, nicUpdateSpec);
            Console.WriteLine(nicUpdateSpec);
            nicInfo = this.ethernetService.Get(this.vmId, lastNicId);
            Console.WriteLine("VM ID=" + this.vmId);
            Console.WriteLine("Ethernet NIC ID=" + lastNicId);
            Console.WriteLine(nicInfo);

            Console.WriteLine("\n\n#### Example: Update the Ethernet NIC,"
                              + " wake_on_lan = false, mac_type=GENERATED,"
                              + " startConnected = false, allowGuestControl = false.");
            nicUpdateSpec = new EthernetTypes.UpdateSpec();
            nicUpdateSpec.SetAllowGuestControl(false);
            nicUpdateSpec.SetStartConnected(false);
            nicUpdateSpec.SetWakeOnLanEnabled(false);
            this.ethernetService.Update(this.vmId, lastNicId, nicUpdateSpec);
            Console.WriteLine(nicUpdateSpec);
            nicInfo = this.ethernetService.Get(this.vmId, lastNicId);
            Console.WriteLine("VM ID=" + this.vmId);
            Console.WriteLine("Ethernet NIC ID=" + lastNicId);
            Console.WriteLine(nicInfo);

            Console.WriteLine("\n\n#### Powering on VM to run "
                              + "connect/disconnect example.");
            this.powerService.Start(this.vmId);
            nicInfo = this.ethernetService.Get(this.vmId, lastNicId);
            Console.WriteLine("VM ID=" + this.vmId);
            Console.WriteLine("Ethernet NIC ID=" + lastNicId);
            Console.WriteLine(nicInfo);

            Console.WriteLine("\n\n#### Example: Connect Ethernet NIC after"
                              + " powering on VM.");
            this.ethernetService.Connect(this.vmId, lastNicId);
            nicInfo = this.ethernetService.Get(this.vmId, lastNicId);
            Console.WriteLine("VM ID=" + this.vmId);
            Console.WriteLine("Ethernet NIC ID=" + lastNicId);
            Console.WriteLine(nicInfo);

            Console.WriteLine("\n\n#### Example: Disconnect Ethernet NIC after"
                              + " powering on VM.");
            this.ethernetService.Disconnect(this.vmId, lastNicId);
            nicInfo = this.ethernetService.Get(this.vmId, lastNicId);
            Console.WriteLine("VM ID=" + this.vmId);
            Console.WriteLine("Ethernet NIC ID=" + lastNicId);
            Console.WriteLine(nicInfo);

            // Power off the VM
            Console.WriteLine("\n\n#### Powering off the VM after"
                              + " connect/disconnect example.");
            this.powerService.Stop(this.vmId);
            nicInfo = this.ethernetService.Get(this.vmId, lastNicId);
            Console.WriteLine("VM ID=" + this.vmId);
            Console.WriteLine("Ethernet NIC ID=" + lastNicId);
            Console.WriteLine(nicInfo);
        }