Class: Fog::Hyperv::Compute::Real

Inherits:
Shared
  • Object
show all
Includes:
Utils::Winrm
Defined in:
lib/fog/hyperv/compute.rb,
lib/fog/hyperv/compute/requests/get_vm.rb,
lib/fog/hyperv/compute/requests/new_vm.rb,
lib/fog/hyperv/compute/requests/set_vm.rb,
lib/fog/hyperv/compute/requests/get_vhd.rb,
lib/fog/hyperv/compute/requests/new_vhd.rb,
lib/fog/hyperv/compute/requests/save_vm.rb,
lib/fog/hyperv/compute/requests/stop_vm.rb,
lib/fog/hyperv/compute/requests/start_vm.rb,
lib/fog/hyperv/compute/requests/remove_vm.rb,
lib/fog/hyperv/compute/requests/rename_vm.rb,
lib/fog/hyperv/compute/requests/resume_vm.rb,
lib/fog/hyperv/compute/requests/update_vm.rb,
lib/fog/hyperv/compute/requests/resize_vhd.rb,
lib/fog/hyperv/compute/requests/restart_vm.rb,
lib/fog/hyperv/compute/requests/suspend_vm.rb,
lib/fog/hyperv/compute/requests/get_cluster.rb,
lib/fog/hyperv/compute/requests/get_vm_bios.rb,
lib/fog/hyperv/compute/requests/get_vm_host.rb,
lib/fog/hyperv/compute/requests/remove_item.rb,
lib/fog/hyperv/compute/requests/set_vm_bios.rb,
lib/fog/hyperv/compute/requests/get_vm_group.rb,
lib/fog/hyperv/compute/requests/optimize_vhd.rb,
lib/fog/hyperv/compute/requests/enable_vm_tpm.rb,
lib/fog/hyperv/compute/requests/get_vm_switch.rb,
lib/fog/hyperv/compute/requests/new_vm_switch.rb,
lib/fog/hyperv/compute/requests/set_vm_switch.rb,
lib/fog/hyperv/compute/requests/disable_vm_tpm.rb,
lib/fog/hyperv/compute/requests/get_vm_com_port.rb,
lib/fog/hyperv/compute/requests/get_vm_firmware.rb,
lib/fog/hyperv/compute/requests/get_vm_host_sbt.rb,
lib/fog/hyperv/compute/requests/get_vm_security.rb,
lib/fog/hyperv/compute/requests/set_vm_com_port.rb,
lib/fog/hyperv/compute/requests/set_vm_firmware.rb,
lib/fog/hyperv/compute/requests/set_vm_security.rb,
lib/fog/hyperv/compute/requests/add_vm_dvd_drive.rb,
lib/fog/hyperv/compute/requests/get_cluster_node.rb,
lib/fog/hyperv/compute/requests/get_vm_dvd_drive.rb,
lib/fog/hyperv/compute/requests/remove_vm_switch.rb,
lib/fog/hyperv/compute/requests/rename_vm_switch.rb,
lib/fog/hyperv/compute/requests/set_vm_dvd_drive.rb,
lib/fog/hyperv/compute/requests/get_vm_host_cluster.rb,
lib/fog/hyperv/compute/requests/remove_vm_dvd_drive.rb,
lib/fog/hyperv/compute/requests/get_vm_key_protector.rb,
lib/fog/hyperv/compute/requests/set_vm_key_protector.rb,
lib/fog/hyperv/compute/requests/add_vm_hard_disk_drive.rb,
lib/fog/hyperv/compute/requests/add_vm_network_adapter.rb,
lib/fog/hyperv/compute/requests/get_vm_hard_disk_drive.rb,
lib/fog/hyperv/compute/requests/get_vm_network_adapter.rb,
lib/fog/hyperv/compute/requests/set_vm_hard_disk_drive.rb,
lib/fog/hyperv/compute/requests/set_vm_network_adapter.rb,
lib/fog/hyperv/compute/requests/get_vm_floppy_disk_drive.rb,
lib/fog/hyperv/compute/requests/set_vm_floppy_disk_drive.rb,
lib/fog/hyperv/compute/requests/remove_vm_hard_disk_drive.rb,
lib/fog/hyperv/compute/requests/remove_vm_network_adapter.rb,
lib/fog/hyperv/compute/requests/rename_vm_network_adapter.rb,
lib/fog/hyperv/compute/requests/connect_vm_network_adapter.rb,
lib/fog/hyperv/compute/requests/get_vm_network_adapter_vlan.rb,
lib/fog/hyperv/compute/requests/set_vm_network_adapter_vlan.rb,
lib/fog/hyperv/compute/requests/disconnect_vm_network_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Winrm

#ps_version?, #run_cmd, #run_cmdlist, #run_wql

Methods inherited from Shared

#version

Constructor Details

#initialize(options = {}) ⇒ Real

rubocop:disable Style/OptionHash, Lint/MissingSuper – No super method



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/fog/hyperv/compute.rb', line 139

def initialize(options = {}) # rubocop:disable Style/OptionHash, Lint/MissingSuper -- No super method
  require 'fog/json'
  require 'logging'

  # Transfer cmdlet parameters as JSON object
  @bake_json = true
  @bake_optmap = true

  @connections = {}
  @hyperv_endpoint = options[:hyperv_endpoint]
  @hyperv_endpoint ||= "http://#{options[:hyperv_host]}:5985/wsman" if options[:hyperv_host]
  @hyperv_username = options[:hyperv_username]
  @hyperv_password = options[:hyperv_password]
  @hyperv_realm = options[:hyperv_realm]
  @hyperv_transport = options[:hyperv_transport] || (@hyperv_realm ? :kerberos : :negotiate)

  # Hide NEGOTIATE logging from WinRM to reduce log spam when debugging
  Logging.logger['WinRM::HTTP::HttpNegotiate'].level = :error

  @logger = Logging.logger[Fog::Hyperv]
  if options[:hyperv_debug]
    logger.level = :debug
    logger.add_appenders Logging.appenders.stdout
  end

  connect
end

Instance Attribute Details

#bake_jsonObject

Returns the value of attribute bake_json.



137
138
139
# File 'lib/fog/hyperv/compute.rb', line 137

def bake_json
  @bake_json
end

#bake_optmapObject

Returns the value of attribute bake_optmap.



137
138
139
# File 'lib/fog/hyperv/compute.rb', line 137

def bake_optmap
  @bake_optmap
end

#loggerObject (readonly)

Returns the value of attribute logger.



135
136
137
# File 'lib/fog/hyperv/compute.rb', line 135

def logger
  @logger
end

Instance Method Details

#add_vm_dvd_drive(vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/fog/hyperv/compute/requests/add_vm_dvd_drive.rb', line 5

def add_vm_dvd_drive(vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Add-VMDvdDrive', { passthru: true, **options }]
    ],
    target_computer: computer_name
  )
end

#add_vm_hard_disk_drive(vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/fog/hyperv/compute/requests/add_vm_hard_disk_drive.rb', line 5

def add_vm_hard_disk_drive(vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Add-VMHardDiskDrive', { passthru: true, **options }]
    ],
    target_computer: computer_name
  )
end

#add_vm_network_adapter(computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fog/hyperv/compute/requests/add_vm_network_adapter.rb', line 5

def add_vm_network_adapter(computer_name: nil, **options)
  requires_one options, :vm_id, :management_os
  options.delete :management_os

  if options[:vm_id]
    vm_id = options.delete :vm_id
    run_cmdlist(
      [
        ['$VM = Get-VM', { id: vm_id }],
        ['$VM | Add-VMNetworkAdapter', { _always_include: %i[is_legacy], passthru: true, **options }]
      ],
      target_computer: computer_name
    )
  else
    options.delete :vm_id
    run_cmd('Add-VMNetworkAdapter', _target_computer: computer_name, management_os: true, passthru: true, **options)
  end
end

#connect_vm_network_adapter(id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/hyperv/compute/requests/connect_vm_network_adapter.rb', line 5

def connect_vm_network_adapter(id:, computer_name: nil, **options)
  requires_one options, :vm_id, :management_os
  requires_one options, :switch_name, :switch_id
  options.delete :management_os

  cmdlist = []
  if options[:vm_id]
    vm_id = options.delete :vm_id
    cmdlist += [
      ['$VM = Get-VM', { id: vm_id }],
      ['$NIC = $VM | Get-VMNetworkAdapter', { _by_id: id }]
    ]
  else
    options.delete :vm_id
    cmdlist << ['$NIC = Get-VMNetworkAdapter', { _by_id: id, management_os: true }]
  end
  if options[:switch_id]
    switch_id = options.delete :switch_id
    options.delete :switch_name
    cmdlist += [
      ['$Switch = Get-VMSwitch', { id: switch_id }],
      ['$NIC | Connect-VMNetworkAdapter -VMSwitch @Switch', options]
    ]
  else
    switch_name = options.delete :switch_name
    options.delete :switch_id
    cmdlist << ['$NIC | Connect-VMNetworkAdapter', { switch_name:, **options }]
  end

  run_cmdlist(cmdlist, skip_json: true, target_computer: computer_name)
end

#disable_vm_tpm(vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/disable_vm_tpm.rb', line 5

def disable_vm_tpm(vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Disable-VMTPM', options]
    ],
    skip_json: true,
    target_computer: computer_name
  )
end

#disconnect_vm_network_adapter(id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fog/hyperv/compute/requests/disconnect_vm_network_adapter.rb', line 5

def disconnect_vm_network_adapter(id:, computer_name: nil, **options)
  requires_one options, :vm_id, :management_os
  options.delete :management_os

  cmdlist = []
  if options[:vm_id]
    vm_id = options.delete :vm_id
    cmdlist += [
      ['$VM = Get-VM', { id: vm_id }],
      ['$NIC = $VM | Get-VMNetworkAdapter', { _by_id: id }]
    ]
  else
    options.delete :vm_id
    cmdlist << ['$NIC = Get-VMNetworkAdapter', { _by_id: id, management_os: true }]
  end
  cmdlist << ['$NIC | Disconnect-VMNetworkAdapter', options]

  run_cmdlist(commands, skip_json: true, target_computer: computer_name)
end

#enable_vm_tpm(vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/enable_vm_tpm.rb', line 5

def enable_vm_tpm(vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Enable-VMTPM', options]
    ],
    skip_json: true,
    target_computer: computer_name
  )
end

#get_cluster(**options) ⇒ Object



5
6
7
8
9
# File 'lib/fog/hyperv/compute/requests/get_cluster.rb', line 5

def get_cluster(**options)
  _by_id = options.delete(:id)

  run_cmd 'Get-Cluster', _by_id:, **options
end

#get_cluster_node(id:, **options) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/fog/hyperv/compute/requests/get_cluster_node.rb', line 5

def get_cluster_node(id:, **options)
  run_cmdlist(
    [
      ['$Cluster = Get-Cluster', { _by_id: id }],
      ['$Cluster | Get-ClusterNode', options]
    ]
  )
end

#get_vhd(computer_name: nil, **options) ⇒ Object



5
6
7
8
# File 'lib/fog/hyperv/compute/requests/get_vhd.rb', line 5

def get_vhd(computer_name: nil, **options)
  requires_one options, :path, :disk_number unless options[:vm_id]
  run_cmd 'Get-VHD', _target_computer: computer_name, **options
end

#get_vm(**options) ⇒ Object



5
6
7
# File 'lib/fog/hyperv/compute/requests/get_vm.rb', line 5

def get_vm(**options)
  run_cmd('Get-VM', **options)
end

#get_vm_bios(vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/fog/hyperv/compute/requests/get_vm_bios.rb', line 5

def get_vm_bios(vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Get-VMBios', options]
    ],
    target_computer: computer_name
  )
end

#get_vm_com_port(vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/fog/hyperv/compute/requests/get_vm_com_port.rb', line 5

def get_vm_com_port(vm_id:, computer_name: nil, **options)
  _by_id = options.delete :id

  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Get-VMComPort', { _by_id:, **options }]
    ],
    target_computer: computer_name
  )
end

#get_vm_dvd_drive(vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/fog/hyperv/compute/requests/get_vm_dvd_drive.rb', line 5

def get_vm_dvd_drive(vm_id:, computer_name: nil, **options)
  _by_id = options.delete :id

  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Get-VMDvdDrive', { _by_id:, **options }]
    ],
    target_computer: computer_name
  )
end

#get_vm_firmware(vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/fog/hyperv/compute/requests/get_vm_firmware.rb', line 5

def get_vm_firmware(vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Get-VMFirmware', options]
    ],
    target_computer: computer_name
  )
end

#get_vm_floppy_disk_drive(vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/get_vm_floppy_disk_drive.rb', line 5

def get_vm_floppy_disk_drive(vm_id:, computer_name: nil, **options)
  _by_id = options.delete :id
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Get-VMFloppyDiskDrive', { _by_id:, **options }]
    ],
    target_computer: computer_name
  )
end

#get_vm_group(computer_name: nil, **options) ⇒ Object



5
6
7
8
9
# File 'lib/fog/hyperv/compute/requests/get_vm_group.rb', line 5

def get_vm_group(computer_name: nil, **options)
  requires_version '10.0'

  run_cmd('Get-VMGroup', _target_computer: computer_name, **options)
end

#get_vm_hard_disk_drive(vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/get_vm_hard_disk_drive.rb', line 5

def get_vm_hard_disk_drive(vm_id:, computer_name: nil, **options)
  _by_id = options.delete :id
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Get-VMHardDiskDrive', { _by_id:, **options }]
    ],
    target_computer: computer_name
  )
end

#get_vm_host(computer_name: nil, **options) ⇒ Object



5
6
7
# File 'lib/fog/hyperv/compute/requests/get_vm_host.rb', line 5

def get_vm_host(computer_name: nil, **options)
  run_cmd 'Get-VMHost', _target_computer: computer_name, **options
end

#get_vm_host_cluster(cluster_name:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
# File 'lib/fog/hyperv/compute/requests/get_vm_host_cluster.rb', line 5

def get_vm_host_cluster(cluster_name:, computer_name: nil, **options)
  requires_version '10.0'

  run_cmd 'Get-VMHostCluster', _target_computer: computer_name, cluster_name:, **options
end

#get_vm_host_sbt(computer_name:, **options) ⇒ Object

Retrieve SecureBoot Templates for a VM Host



6
7
8
# File 'lib/fog/hyperv/compute/requests/get_vm_host_sbt.rb', line 6

def get_vm_host_sbt(computer_name:, **options)
  run_cmd '(Get-VMHost @Args).SecureBootTemplates', _target_computer: computer_name, **options
end

#get_vm_key_protector(vm_id:, computer_name: nil, **options) ⇒ Object

Retrieve SecureBoot Templates for a VM Host



6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/get_vm_key_protector.rb', line 6

def get_vm_key_protector(vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Get-VMKeyProtector', options]
    ],
    target_computer: computer_name
  )
end

#get_vm_network_adapter(computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/hyperv/compute/requests/get_vm_network_adapter.rb', line 5

def get_vm_network_adapter(computer_name: nil, **options)
  requires_one options, :vm_id, :all, :management_os

  id = options.delete :id
  if options[:vm_id]
    vm_id = options.delete :vm_id
    options.delete :management_os
    options.delete :all
    run_cmdlist(
      [
        ['$VM = Get-VM', { id: vm_id }],
        ['$VM | Get-VMNetworkAdapter', { _by_id: id, **options }]
      ],
      target_computer: computer_name
    )
  else
    options.delete :management_os if options[:all]
    run_cmd 'Get-VMNetworkAdapter', _by_id: id, _target_computer: computer_name, **options
  end
end

#get_vm_network_adapter_vlan(id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fog/hyperv/compute/requests/get_vm_network_adapter_vlan.rb', line 5

def get_vm_network_adapter_vlan(id:, computer_name: nil, **options)
  requires_one options, :vm_id, :management_os

  commands = []
  if options.key? :vm_id
    vm_id = options.delete :vm_id
    options.delete :management_os
    commands += [
      ['$VM = Get-VM', { id: vm_id }],
      ['$NIC = $VM | Get-VMNetworkAdapter', { _by_id: id }]
    ]
  else
    options.delete :management_os
    commands << ['$NIC = Get-VMNetworkAdapter', { management_os: true, _by_id: id }]
  end
  commands << ['$NIC | Get-VMNetworkAdapterVlan', options]

  run_cmdlist(
    commands,
    target_computer: computer_name
  )
end

#get_vm_security(vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/fog/hyperv/compute/requests/get_vm_security.rb', line 5

def get_vm_security(vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Get-VMSecurity', options]
    ],
    target_computer: computer_name
  )
end

#get_vm_switch(computer_name: nil, **options) ⇒ Object



5
6
7
8
# File 'lib/fog/hyperv/compute/requests/get_vm_switch.rb', line 5

def get_vm_switch(computer_name: nil, **options)
  id = options.delete :id
  run_cmd 'Get-VMSwitch', _by_id: id, _target_computer: computer_name, **options
end

#local?Boolean

Is the provider communicating with a Hyper-V install on the local machine

Returns:

  • (Boolean)


168
169
170
# File 'lib/fog/hyperv/compute.rb', line 168

def local?
  false # @hyperv_endpoint.nil?
end

#new_vhd(path:, size_bytes:, computer_name: nil, **options) ⇒ Object



5
6
7
# File 'lib/fog/hyperv/compute/requests/new_vhd.rb', line 5

def new_vhd(path:, size_bytes:, computer_name: nil, **options)
  run_cmd 'New-VHD', _target_computer: computer_name, path:, size_bytes:, **options
end

#new_vm(computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
# File 'lib/fog/hyperv/compute/requests/new_vm.rb', line 5

def new_vm(computer_name: nil, **options)
  requires options, :new_vhd_path, :new_vhd_size_bytes \
    if options[:new_whd_path] || options[:new_vhd_size_bytes]

  run_cmd 'New-VM', _target_computer: computer_name, **options
end

#new_vm_switch(name:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
# File 'lib/fog/hyperv/compute/requests/new_vm_switch.rb', line 5

def new_vm_switch(name:, computer_name: nil, **options)
  requires_one options, :net_adapter_name, :net_adapter_interface_description

  run_cmd 'New-VMSwitch', _target_computer: computer_name, name:, **options
end

#optimize_vhd(path:, computer_name: nil, **options) ⇒ Object



5
6
7
# File 'lib/fog/hyperv/compute/requests/optimize_vhd.rb', line 5

def optimize_vhd(path:, computer_name: nil, **options)
  run_cmd 'Optimize-VHD', _target_computer: computer_name, _skip_json: true, path:, **options
end

#remove_item(path:, computer_name: nil, **options) ⇒ Object



5
6
7
8
# File 'lib/fog/hyperv/compute/requests/remove_item.rb', line 5

def remove_item(path:, computer_name: nil, **options)
  # TODO: Really lock this method down, validation is good.
  run_cmd 'Remove-Item', _target_computer: computer_name, _skip_json: true, path:, force: true, **options
end

#remove_vm(id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/remove_vm.rb', line 5

def remove_vm(id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: }],
      ['$VM | Remove-VM', { force: true, **options }]
    ],
    skip_json: true,
    target_computer: computer_name
  )
end

#remove_vm_dvd_drive(id:, vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/fog/hyperv/compute/requests/remove_vm_dvd_drive.rb', line 5

def remove_vm_dvd_drive(id:, vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$DVD = $VM | Get-VMDvdDrive', { _by_id: id }],
      ['$DVD | Remove-VMDvdDrive', options]
    ],
    skip_json: true,
    target_computer: computer_name
  )
end

#remove_vm_hard_disk_drive(vm_id:, id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/fog/hyperv/compute/requests/remove_vm_hard_disk_drive.rb', line 5

def remove_vm_hard_disk_drive(vm_id:, id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$HDD = $VM | Get-VMHardDiskDrive', { _by_id: id }],
      ['$HDD | Remove-VMHardDiskDrive', options]
    ],
    skip_json: true,
    target_computer: computer_name
  )
end

#remove_vm_network_adapter(id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/hyperv/compute/requests/remove_vm_network_adapter.rb', line 5

def remove_vm_network_adapter(id:, computer_name: nil, **options)
  requires_one options, :vm_id, :management_os
  options.delete :management_os

  cmdlist = []
  if options[:vm_id]
    vm_id = options.delete :vm_id
    cmdlist += [
      ['$VM = Get-VM', { id: vm_id }],
      ['$NIC = $VM | Get-VMNetworkAdapter', { _by_id: id }]
    ]
  else
    options.delete :vm_id
    cmdlist << ['$NIC = Get-VMNetworkAdapter', { _by_id: id, management_os: true }]
  end
  cmdlist << ['$NIC | Remove-VMNetworkAdapter', options]

  run_cmdlist(
    cmdlist,
    skip_json: true,
    target_computer: computer_name
  )
end

#remove_vm_switch(id:, computer_name: nil, **options) ⇒ Object



5
6
7
# File 'lib/fog/hyperv/compute/requests/remove_vm_switch.rb', line 5

def remove_vm_switch(id:, computer_name: nil, **options)
  run_cmd 'Remove-VMSwitch', _target_computer: computer_name, _by_id: id, _skip_json: true, force: true, **options
end

#rename_vm(id:, new_name:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/rename_vm.rb', line 5

def rename_vm(id:, new_name:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: }],
      ['$VM | Rename-VM', { new_name:, **options }]
    ],
    target_computer: computer_name,
    skip_json: true
  )
end

#rename_vm_network_adapter(id:, new_name:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fog/hyperv/compute/requests/rename_vm_network_adapter.rb', line 5

def rename_vm_network_adapter(id:, new_name:, computer_name: nil, **options)
  requires_one options, :vm_id, :management_os
  options.delete :management_os

  cmdlist = []
  if options[:vm_id]
    vm_id = options.delete :vm_id
    cmdlist += [
      ['$VM = Get-VM', { id: vm_id }],
      ['$NIC = $VM | Get-VMNetworkAdapter', { _by_id: id }]
    ]
  else
    options.delete :vm_id
    cmdlist << ['$NIC = Get-VMNetworkAdapter', { _by_id: id, management_os: true }]
  end
  cmdlist << ['$NIC | Rename-VMNetworkAdapter', { new_name:, **options }]

  run_cmdlist(cmdlist, target_computer: computer_name, skip_json: true)
end

#rename_vm_switch(id:, new_name:, computer_name: nil, **options) ⇒ Object



5
6
7
# File 'lib/fog/hyperv/compute/requests/rename_vm_switch.rb', line 5

def rename_vm_switch(id:, new_name:, computer_name: nil, **options)
  run_cmd 'Rename-VMSwitch', new_name:, _target_computer: computer_name, _by_id: id, _skip_json: true, **options
end

#resize_vhd(path:, size_bytes:, computer_name: nil, **options) ⇒ Object



5
6
7
# File 'lib/fog/hyperv/compute/requests/resize_vhd.rb', line 5

def resize_vhd(path:, size_bytes:, computer_name: nil, **options)
  run_cmd 'Resize-VHD', _target_computer: computer_name, _skip_json: true, path:, size_bytes:, **options
end

#restart_vm(id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/restart_vm.rb', line 5

def restart_vm(id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: }],
      ['$VM | Restart-VM', { force: true, **options }]
    ],
    skip_json: true,
    target_computer: computer_name
  )
end

#resume_vm(id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/resume_vm.rb', line 5

def resume_vm(id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: }],
      ['$VM | Resume-VM', options]
    ],
    skip_json: true,
    target_computer: computer_name
  )
end

#save_vm(id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/save_vm.rb', line 5

def save_vm(id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: }],
      ['$VM | Save-VM', options]
    ],
    skip_json: true,
    target_computer: computer_name
  )
end

#set_vm(id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/fog/hyperv/compute/requests/set_vm.rb', line 5

def set_vm(id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: }],
      ['$VM | Set-VM', { passthru: true, **options }]
    ],
    target_computer: computer_name
  )
end

#set_vm_bios(vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/fog/hyperv/compute/requests/set_vm_bios.rb', line 5

def set_vm_bios(vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Set-VMBios', { passthru: true, **options }]
    ],
    target_computer: computer_name
  )
end

#set_vm_com_port(id:, vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/set_vm_com_port.rb', line 5

def set_vm_com_port(id:, vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$COM = $VM | Get-VMComPort', { _by_id: id }],
      ['$COM | Set-VMComPort', { passthru: true, **options }]
    ],
    target_computer: computer_name
  )
end

#set_vm_dvd_drive(id:, vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/set_vm_dvd_drive.rb', line 5

def set_vm_dvd_drive(id:, vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$DVD = $VM | Get-VMDvdDrive', { _by_id: id }],
      ['$DVD | Set-VMDvdDrive', { passthru: true, **options }]
    ],
    target_computer: computer_name
  )
end

#set_vm_firmware(vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/fog/hyperv/compute/requests/set_vm_firmware.rb', line 5

def set_vm_firmware(vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Set-VMFirmware', { passthru: true, **options }]
    ],
    target_computer: computer_name
  )
end

#set_vm_floppy_disk_drive(id:, vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/set_vm_floppy_disk_drive.rb', line 5

def set_vm_floppy_disk_drive(id:, vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$Floppy = $VM | Get-VMFloppyDiskDrive', { _by_id: id }],
      ['$Floppy | Set-VMFloppyDiskDrive', { passthru: true, **options }]
    ],
    target_computer: computer_name
  )
end

#set_vm_hard_disk_drive(id:, vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/set_vm_hard_disk_drive.rb', line 5

def set_vm_hard_disk_drive(id:, vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$HDD = $VM | Get-VMHardDiskDrive', { _by_id: id }],
      ['$HDD | Set-VMHardDiskDrive', { passthru: true, **options }]
    ],
    target_computer: computer_name
  )
end

#set_vm_key_protector(vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/fog/hyperv/compute/requests/set_vm_key_protector.rb', line 5

def set_vm_key_protector(vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Set-VMKeyProtector', options]
    ],
    target_computer: computer_name
  )
end

#set_vm_network_adapter(id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fog/hyperv/compute/requests/set_vm_network_adapter.rb', line 5

def set_vm_network_adapter(id:, computer_name: nil, **options)
  requires_one options, :vm_id, :management_os
  options.delete :management_os

  cmdlist = []
  if options[:vm_id]
    vm_id = options.delete :vm_id
    cmdlist += [
      ['$VM = Get-VM', { id: vm_id }],
      ['$NIC = $VM | Get-VMNetworkAdapter', { _by_id: id }]
    ]
  else
    options.delete :vm_id
    cmdlist << ['$NIC = Get-VMNetworkAdapter', { _by_id: id, management_os: true }]
  end
  cmdlist << ['$NIC | Set-VMNetworkAdapter', { passthru: true, **options }]

  run_cmdlist(cmdlist, target_computer: computer_name)
end

#set_vm_network_adapter_vlan(id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fog/hyperv/compute/requests/set_vm_network_adapter_vlan.rb', line 5

def set_vm_network_adapter_vlan(id:, computer_name: nil, **options)
  requires_one options, :vm_id, :management_os
  options.delete :management_os

  cmdlist = []
  if options[:vm_id]
    vm_id = options.delete :vm_id
    cmdlist += [
      ['$VM = Get-VM', { id: vm_id }],
      ['$NIC = $VM | Get-VMNetworkAdapter', { _by_id: id }]
    ]
  else
    options.delete :vm_id
    cmdlist << ['$NIC = Get-VMNetworkAdapter', { _by_id: id, management_os: true }]
  end
  cmdlist << ['$NIC | Set-VMNetworkAdapterVlan', { passthru: true, **options }]

  run_cmdlist(cmdlist, target_computer: computer_name)
end

#set_vm_security(vm_id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/fog/hyperv/compute/requests/set_vm_security.rb', line 5

def set_vm_security(vm_id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: vm_id }],
      ['$VM | Set-VMSecurity', {
        _always_include: %i[encrypt_state_and_vm_migration_traffic virtualization_based_security_opt_out], passthru: true, **options
      }]
    ],
    target_computer: computer_name
  )
end

#set_vm_switch(id:, computer_name: nil, **options) ⇒ Object



5
6
7
# File 'lib/fog/hyperv/compute/requests/set_vm_switch.rb', line 5

def set_vm_switch(id:, computer_name: nil, **options)
  run_cmd 'Set-VMSwitch', _target_computer: computer_name, _by_id: id, passthru: true, **options
end

#start_vm(id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/start_vm.rb', line 5

def start_vm(id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: }],
      ['$VM | Start-VM', options]
    ],
    skip_json: true,
    target_computer: computer_name
  )
end

#stop_vm(id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/stop_vm.rb', line 5

def stop_vm(id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: }],
      ['$VM | Stop-VM', { force: true, **options }]
    ],
    skip_json: true,
    target_computer: computer_name
  )
end

#supports_clusters?Boolean

Does the Hyper-V install support clusters - i.e. is the machine joined to a failover cluster

Returns:

  • (Boolean)

    is the Hyper-V setup possible to cluster



187
188
189
190
191
192
# File 'lib/fog/hyperv/compute.rb', line 187

def supports_clusters?
  run_wql('SELECT Name FROM MSCluster_ResourceGroup', _namespace: 'root/mscluster/*')[:xml_fragment] && true
rescue StandardError => e
  logger.debug "Cluster support checking failed with #{e.class}: #{e.message}"
  false
end

#suspend_vm(id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/suspend_vm.rb', line 5

def suspend_vm(id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: }],
      ['$VM | Suspend-VM', options]
    ],
    skip_json: true,
    target_computer: computer_name
  )
end

#update_vm(id:, computer_name: nil, **options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/fog/hyperv/compute/requests/update_vm.rb', line 5

def update_vm(id:, computer_name: nil, **options)
  run_cmdlist(
    [
      ['$VM = Get-VM', { id: }],
      ['$VM | Update-VM', options]
    ],
    skip_json: true,
    target_computer: computer_name
  )
end

#valid?Boolean

Is the Hyper-V provider valid - i.e. can it communicate with a Hyper-V install

Returns:

  • (Boolean)

    is the endpoint running a working Hyper-V setup



174
175
176
177
178
179
180
181
182
183
# File 'lib/fog/hyperv/compute.rb', line 174

def valid?
  if local?
    run_cmd('Get-VMHost', _return_fields: :name) && true
  else
    run_wql('SELECT Name FROM Msvm_ComputerSystem WHERE Caption = "Hosting Computer System"')[:xml_fragment] && true
  end
rescue StandardError => e
  logger.debug "Validation failed with #{e.class}; #{e.message}"
  false
end