def run():
    global vm
    vm = get_vm(stub_config, vm_name)
    if not vm:
        raise Exception('Sample requires an existing vm with name ({}). '
                        'Please create the vm first.'.format(vm_name))
    print("Using VM '{}' ({}) for BootDevice Sample".format(vm_name, vm))

    # Create BootDevice stub used for making requests
    global boot_device_svc
    boot_device_svc = BootDevice(stub_config)

    print('\n# Example: Get current BootDevice configuration')
    boot_device_entries = boot_device_svc.get(vm)
    print('vm.hardware.boot.Device.get({}) -> {}'.format(
        vm, pp(boot_device_entries)))

    # Save current BootDevice info to verify that we have cleaned up properly
    global orig_boot_device_entries
    orig_boot_device_entries = boot_device_entries

    # Get device identifiers for Disks
    disk_svc = Disk(stub_config)
    disk_summaries = disk_svc.list(vm)
    print('vm.hardware.Disk.list({}) -> {}'.format(vm, pp(disk_summaries)))
    disks = [disk_summary.disk for disk_summary in disk_summaries]

    # Get device identifiers for Ethernet nics
    ethernet_svc = Ethernet(stub_config)
    nic_summaries = ethernet_svc.list(vm)
    print('vm.hardware.Ethernet.list({}) -> {}'.format(vm, pp(nic_summaries)))
    nics = [nic_summary.nic for nic_summary in nic_summaries]

    print('\n# Example: Set Boot Order to be Floppy, '
          'Disk1, Disk2, Disk3, Cdrom,')
    print('#          Network (nic0), Network (nic1).')
    boot_device_entries = [
        BootDevice.Entry(BootDevice.Type.FLOPPY),
        BootDevice.Entry(BootDevice.Type.DISK, disks=disks),
        BootDevice.Entry(BootDevice.Type.CDROM)
    ]
    for nic in nics:
        boot_device_entries.append(
            BootDevice.Entry(BootDevice.Type.ETHERNET, nic=nic))
    print('vm.hardware.boot.Device.set({}, {})'.format(vm,
                                                       boot_device_entries))
    boot_device_svc.set(vm, boot_device_entries)
    boot_device_entries = boot_device_svc.get(vm)
    print('vm.hardware.boot.Device.get({}) -> {}'.format(
        vm, pp(boot_device_entries)))
def run():
    GiB = 1024 * 1024 * 1024

    # Create Disk stub used for making requests
    global disk_svc
    disk_svc = Disk(stub_config)

    print('\n# Example: List all Disks for a VM')
    disk_summaries = disk_svc.list(vm=vm)
    print('vm.hardware.Disk.list({}) -> {}'.format(vm, disk_summaries))

    # Save current list of disks to verify that we have cleaned up properly
    global orig_disk_summaries
    orig_disk_summaries = disk_summaries

    # Get information for each Disk on the VM
    for disk_summary in disk_summaries:
        disk = disk_summary.disk
        disk_info = disk_svc.get(vm=vm, disk=disk)
        print('vm.hardware.Disk.get({}, {}) -> {}'.format(
            vm, disk, pp(disk_info)))

    print('\n# Example: Create a new Disk using default settings')
    disk_create_spec = Disk.CreateSpec(new_vmdk=Disk.VmdkCreateSpec())
    disk = disk_svc.create(vm=vm, spec=disk_create_spec)
    print('vm.hardware.Disk.create({}, {}) -> {}'.format(
        vm, disk_create_spec, disk))
    global disks_to_delete
    disks_to_delete.append(disk)
    disk_info = disk_svc.get(vm, disk)
    print('vm.hardware.Disk.get({}, {}) -> {}'.format(vm, disk, pp(disk_info)))

    print(
        '\n# Example: Create a new Disk specifying the capacity in bytes \n' +
        '# and that the flat format (ie. SeSparse format) should be used.')
    disk_create_spec = Disk.CreateSpec(new_vmdk=Disk.VmdkCreateSpec(
        capacity=10 * GiB))
    disk = disk_svc.create(vm=vm, spec=disk_create_spec)
    print('vm.hardware.Disk.create({}, {}) -> {}'.format(
        vm, disk_create_spec, disk))
    disks_to_delete.append(disk)
    disk_info = disk_svc.get(vm, disk)
    print('vm.hardware.Disk.get({}, {}) -> {}'.format(vm, disk, pp(disk_info)))

    print('\n# Example: Create a new SCSI Disk')
    disk_create_spec = Disk.CreateSpec(
        type=Disk.HostBusAdapterType.SCSI,
        new_vmdk=Disk.VmdkCreateSpec(capacity=10 * GiB))
    disk = disk_svc.create(vm=vm, spec=disk_create_spec)
    print('vm.hardware.Disk.create({}, {}) -> {}'.format(
        vm, disk_create_spec, disk))
    disks_to_delete.append(disk)
    disk_info = disk_svc.get(vm, disk)
    print('vm.hardware.Disk.get({}, {}) -> {}'.format(vm, disk, pp(disk_info)))

    print('\n# Example: Create a new SCSI Disk on a specific bus')
    disk_create_spec = Disk.CreateSpec(
        type=Disk.HostBusAdapterType.SCSI,
        scsi=ScsiAddressSpec(bus=0),
        new_vmdk=Disk.VmdkCreateSpec(capacity=10 * GiB))
    disk = disk_svc.create(vm=vm, spec=disk_create_spec)
    print('vm.hardware.Disk.create({}, {}) -> {}'.format(
        vm, disk_create_spec, disk))
    disks_to_delete.append(disk)
    disk_info = disk_svc.get(vm, disk)
    print('vm.hardware.Disk.get({}, {}) -> {}'.format(vm, disk, pp(disk_info)))

    print(
        '\n# Example: Create a new SCSI Disk on a specific bus and unit number'
    )
    disk_create_spec = Disk.CreateSpec(
        type=Disk.HostBusAdapterType.SCSI,
        scsi=ScsiAddressSpec(bus=0, unit=10),
        new_vmdk=Disk.VmdkCreateSpec(capacity=10 * GiB))
    disk = disk_svc.create(vm=vm, spec=disk_create_spec)
    print('vm.hardware.Disk.create({}, {}) -> {}'.format(
        vm, disk_create_spec, disk))
    disks_to_delete.append(disk)
    disk_info = disk_svc.get(vm, disk)
    print('vm.hardware.Disk.get({}, {}) -> {}'.format(vm, disk, pp(disk_info)))

    print('\n# Example: Create a SATA controller')
    global sata_svc
    sata_svc = Sata(stub_config)
    sata_create_spec = Sata.CreateSpec()
    print('# Adding SATA controller for SATA Disk')
    global sata
    sata = sata_svc.create(vm, sata_create_spec)
    print('vm.hardware.adapter.Sata.create({}, {}) -> {}'.format(
        vm, sata_create_spec, sata))

    print('\n# Example: Create a new SATA disk')
    disk_create_spec = Disk.CreateSpec(
        type=Disk.HostBusAdapterType.SATA,
        new_vmdk=Disk.VmdkCreateSpec(capacity=10 * GiB))
    disk = disk_svc.create(vm=vm, spec=disk_create_spec)
    print('vm.hardware.Disk.create({}, {}) -> {}'.format(
        vm, disk_create_spec, disk))
    disks_to_delete.append(disk)
    disk_info = disk_svc.get(vm, disk)
    print('vm.hardware.Disk.get({}, {}) -> {}'.format(vm, disk, pp(disk_info)))

    print('\n# Example: Create a new SATA disk on a specific bus')
    disk_create_spec = Disk.CreateSpec(
        type=Disk.HostBusAdapterType.SATA,
        sata=SataAddressSpec(bus=0),
        new_vmdk=Disk.VmdkCreateSpec(capacity=10 * GiB))
    disk = disk_svc.create(vm=vm, spec=disk_create_spec)
    print('vm.hardware.Disk.create({}, {}) -> {}'.format(
        vm, disk_create_spec, disk))
    disks_to_delete.append(disk)
    disk_info = disk_svc.get(vm, disk)
    print('vm.hardware.Disk.get({}, {}) -> {}'.format(vm, disk, pp(disk_info)))

    print('\n# Example: Create a new SATA disk on a specific bus and specific '
          'unit')
    disk_create_spec = Disk.CreateSpec(
        type=Disk.HostBusAdapterType.SATA,
        sata=SataAddressSpec(bus=0, unit=20),
        new_vmdk=Disk.VmdkCreateSpec(capacity=10 * GiB))
    disk = disk_svc.create(vm=vm, spec=disk_create_spec)
    print('vm.hardware.Disk.create({}, {}) -> {}'.format(
        vm, disk_create_spec, disk))
    disks_to_delete.append(disk)
    disk_info = disk_svc.get(vm, disk)
    print('vm.hardware.Disk.get({}, {}) -> {}'.format(vm, disk, pp(disk_info)))

    print('\n# Example: Create a new IDE disk')
    disk_create_spec = Disk.CreateSpec(
        type=Disk.HostBusAdapterType.IDE,
        new_vmdk=Disk.VmdkCreateSpec(capacity=10 * GiB))
    disk = disk_svc.create(vm=vm, spec=disk_create_spec)
    print('vm.hardware.Disk.create({}, {}) -> {}'.format(
        vm, disk_create_spec, disk))
    disks_to_delete.append(disk)
    disk_info = disk_svc.get(vm, disk)
    print('vm.hardware.Disk.get({}, {}) -> {}'.format(vm, disk, pp(disk_info)))

    print('\n# Example: Create a new IDE disk on a specific bus and '
          'specific unit')
    disk_create_spec = Disk.CreateSpec(
        type=Disk.HostBusAdapterType.IDE,
        ide=IdeAddressSpec(False, False),
        new_vmdk=Disk.VmdkCreateSpec(capacity=10 * GiB))
    disk = disk_svc.create(vm=vm, spec=disk_create_spec)
    print('vm.hardware.Disk.create({}, {}) -> {}'.format(
        vm, disk_create_spec, disk))
    disks_to_delete.append(disk)
    disk_info = disk_svc.get(vm, disk)
    print('vm.hardware.Disk.get({}, {}) -> {}'.format(vm, disk, pp(disk_info)))

    print(
        '\n# Example: Attach an existing VMDK using the default bus and unit')
    datastore_path = datastore_root_path + '/attach-defaults.vmdk'
    delete_vmdk_if_exist(stub_config, service_instance._stub, datacenter_name,
                         datastore_name, datastore_path)
    create_vmdk(service_instance, datacenter_mo, datastore_path)
    disk_create_spec = Disk.CreateSpec(backing=Disk.BackingSpec(
        type=Disk.BackingType.VMDK_FILE, vmdk_file=datastore_path))
    disk = disk_svc.create(vm=vm, spec=disk_create_spec)
    print('vm.hardware.Disk.create({}, {}) -> {}'.format(
        vm, disk_create_spec, disk))
    disks_to_delete.append(disk)
    disk_info = disk_svc.get(vm, disk)
    print('vm.hardware.Disk.get({}, {}) -> {}'.format(vm, disk, pp(disk_info)))

    print('\n# Example: Attach an existing VMDK as a SCSI disk')
    datastore_path = datastore_root_path + '/attach-scsi.vmdk'
    delete_vmdk_if_exist(stub_config, service_instance._stub, datacenter_name,
                         datastore_name, datastore_path)
    create_vmdk(service_instance, datacenter_mo, datastore_path)

    disk_create_spec = Disk.CreateSpec(type=Disk.HostBusAdapterType.SCSI,
                                       backing=Disk.BackingSpec(
                                           type=Disk.BackingType.VMDK_FILE,
                                           vmdk_file=datastore_path))
    disk = disk_svc.create(vm=vm, spec=disk_create_spec)
    print('vm.hardware.Disk.create({}, {}) -> {}'.format(
        vm, disk_create_spec, disk))
    disks_to_delete.append(disk)
    disk_info = disk_svc.get(vm, disk)
    print('vm.hardware.Disk.get({}, {}) -> {}'.format(vm, disk, pp(disk_info)))

    print('\n# Example: Attach an existing VMDK as a SCSI disk '
          'to a specific bus')
    datastore_path = datastore_root_path + '/attach-scsi0.vmdk'
    delete_vmdk_if_exist(stub_config, service_instance._stub, datacenter_name,
                         datastore_name, datastore_path)
    create_vmdk(service_instance, datacenter_mo, datastore_path)
    disk_create_spec = Disk.CreateSpec(type=Disk.HostBusAdapterType.SCSI,
                                       scsi=ScsiAddressSpec(bus=0),
                                       backing=Disk.BackingSpec(
                                           type=Disk.BackingType.VMDK_FILE,
                                           vmdk_file=datastore_path))
    disk = disk_svc.create(vm=vm, spec=disk_create_spec)
    print('vm.hardware.Disk.create({}, {}) -> {}'.format(
        vm, disk_create_spec, disk))
    disks_to_delete.append(disk)
    disk_info = disk_svc.get(vm, disk)
    print('vm.hardware.Disk.get({}, {}) -> {}'.format(vm, disk, pp(disk_info)))

    print('\n# Example: Attach an existing VMDK as a SCSI disk '
          'to a specific bus and specific unit')
    datastore_path = datastore_root_path + '/attach-scsi0:11.vmdk'
    delete_vmdk_if_exist(stub_config, service_instance._stub, datacenter_name,
                         datastore_name, datastore_path)
    create_vmdk(service_instance, datacenter_mo, datastore_path)
    disk_create_spec = Disk.CreateSpec(type=Disk.HostBusAdapterType.SCSI,
                                       scsi=ScsiAddressSpec(bus=0, unit=11),
                                       backing=Disk.BackingSpec(
                                           type=Disk.BackingType.VMDK_FILE,
                                           vmdk_file=datastore_path))
    disk = disk_svc.create(vm=vm, spec=disk_create_spec)
    print('vm.hardware.Disk.create({}, {}) -> {}'.format(
        vm, disk_create_spec, disk))
    disks_to_delete.append(disk)
    disk_info = disk_svc.get(vm, disk)
    print('vm.hardware.Disk.get({}, {}) -> {}'.format(vm, disk, pp(disk_info)))

    # Samples to update operation to change backing
    # Save the disk_info so we can delete the VMDK
    global saved_disk_info
    saved_disk_info = disk_info
    print(
        '\n# Example: Change the backing of the last disk to a new VMDK file.')
    datastore_path = datastore_root_path + '/update-scsi0:11.vmdk'
    delete_vmdk_if_exist(stub_config, service_instance._stub, datacenter_name,
                         datastore_name, datastore_path)
    create_vmdk(service_instance, datacenter_mo, datastore_path)
    disk_update_spec = Disk.UpdateSpec(backing=Disk.BackingSpec(
        type=Disk.BackingType.VMDK_FILE, vmdk_file=datastore_path))
    print('vm.hardware.Disk.update({}, {}, {})'.format(vm, disk,
                                                       disk_update_spec))
    disk_svc.update(vm=vm, disk=disk, spec=disk_update_spec)
    disk_info = disk_svc.get(vm, disk)
    print('vm.hardware.Disk.get({}, {}) -> {}'.format(vm, disk, pp(disk_info)))