fog-hyperv 0.0.9 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +12 -2
  3. data/lib/fog/bin/hyperv.rb +6 -4
  4. data/lib/fog/hyperv/collection.rb +89 -0
  5. data/lib/fog/hyperv/compute/models/bios.rb +70 -0
  6. data/lib/fog/hyperv/compute/models/cluster.rb +50 -0
  7. data/lib/fog/hyperv/compute/models/clusters.rb +21 -0
  8. data/lib/fog/hyperv/compute/models/com_port.rb +78 -0
  9. data/lib/fog/hyperv/compute/models/com_ports.rb +29 -0
  10. data/lib/fog/hyperv/compute/models/dvd_drive.rb +126 -0
  11. data/lib/fog/hyperv/compute/models/dvd_drives.rb +29 -0
  12. data/lib/fog/hyperv/compute/models/firmware.rb +78 -0
  13. data/lib/fog/hyperv/compute/models/floppy_drive.rb +64 -0
  14. data/lib/fog/hyperv/compute/models/floppy_drives.rb +18 -0
  15. data/lib/fog/hyperv/compute/models/hard_drive.rb +183 -0
  16. data/lib/fog/hyperv/compute/models/hard_drives.rb +28 -0
  17. data/lib/fog/hyperv/compute/models/host.rb +63 -0
  18. data/lib/fog/hyperv/compute/models/hosts.rb +13 -0
  19. data/lib/fog/hyperv/compute/models/network_adapter.rb +368 -0
  20. data/lib/fog/hyperv/compute/models/network_adapter_vlan.rb +172 -0
  21. data/lib/fog/hyperv/compute/models/network_adapters.rb +32 -0
  22. data/lib/fog/hyperv/compute/models/security.rb +121 -0
  23. data/lib/fog/hyperv/compute/models/server.rb +466 -0
  24. data/lib/fog/hyperv/compute/models/servers.rb +18 -0
  25. data/lib/fog/hyperv/compute/models/switch.rb +117 -0
  26. data/lib/fog/hyperv/compute/models/switches.rb +20 -0
  27. data/lib/fog/hyperv/compute/models/vhd.rb +210 -0
  28. data/lib/fog/hyperv/compute/models/vhds.rb +28 -0
  29. data/lib/fog/hyperv/compute/requests/add_vm_dvd_drive.rb +15 -0
  30. data/lib/fog/hyperv/compute/requests/add_vm_hard_disk_drive.rb +15 -0
  31. data/lib/fog/hyperv/compute/requests/add_vm_network_adapter.rb +24 -0
  32. data/lib/fog/hyperv/compute/requests/connect_vm_network_adapter.rb +41 -0
  33. data/lib/fog/hyperv/compute/requests/disable_vm_tpm.rb +16 -0
  34. data/lib/fog/hyperv/compute/requests/disconnect_vm_network_adapter.rb +29 -0
  35. data/lib/fog/hyperv/compute/requests/enable_vm_tpm.rb +16 -0
  36. data/lib/fog/hyperv/compute/requests/get_cluster.rb +11 -0
  37. data/lib/fog/hyperv/compute/requests/get_cluster_node.rb +22 -0
  38. data/lib/fog/hyperv/compute/requests/get_vhd.rb +32 -0
  39. data/lib/fog/hyperv/compute/requests/get_vm.rb +18 -0
  40. data/lib/fog/hyperv/compute/requests/get_vm_bios.rb +21 -0
  41. data/lib/fog/hyperv/compute/requests/get_vm_com_port.rb +17 -0
  42. data/lib/fog/hyperv/compute/requests/get_vm_dvd_drive.rb +25 -0
  43. data/lib/fog/hyperv/compute/requests/get_vm_firmware.rb +21 -0
  44. data/lib/fog/hyperv/compute/requests/get_vm_floppy_disk_drive.rb +16 -0
  45. data/lib/fog/hyperv/compute/requests/get_vm_group.rb +20 -0
  46. data/lib/fog/hyperv/compute/requests/get_vm_hard_disk_drive.rb +24 -0
  47. data/lib/fog/hyperv/compute/requests/get_vm_host.rb +9 -0
  48. data/lib/fog/hyperv/compute/requests/get_vm_host_cluster.rb +21 -0
  49. data/lib/fog/hyperv/compute/requests/get_vm_host_sbt.rb +10 -0
  50. data/lib/fog/hyperv/compute/requests/get_vm_key_protector.rb +16 -0
  51. data/lib/fog/hyperv/compute/requests/get_vm_network_adapter.rb +41 -0
  52. data/lib/fog/hyperv/compute/requests/get_vm_network_adapter_vlan.rb +41 -0
  53. data/lib/fog/hyperv/compute/requests/get_vm_security.rb +15 -0
  54. data/lib/fog/hyperv/compute/requests/get_vm_switch.rb +10 -0
  55. data/lib/fog/hyperv/compute/requests/mock_files/get_vm.json +4 -0
  56. data/lib/fog/hyperv/compute/requests/new_vhd.rb +9 -0
  57. data/lib/fog/hyperv/compute/requests/new_vm.rb +12 -0
  58. data/lib/fog/hyperv/compute/requests/new_vm_switch.rb +11 -0
  59. data/lib/fog/hyperv/compute/requests/optimize_vhd.rb +9 -0
  60. data/lib/fog/hyperv/compute/requests/remove_item.rb +10 -0
  61. data/lib/fog/hyperv/compute/requests/remove_vm.rb +16 -0
  62. data/lib/fog/hyperv/compute/requests/remove_vm_dvd_drive.rb +17 -0
  63. data/lib/fog/hyperv/compute/requests/remove_vm_hard_disk_drive.rb +17 -0
  64. data/lib/fog/hyperv/compute/requests/remove_vm_network_adapter.rb +29 -0
  65. data/lib/fog/hyperv/compute/requests/remove_vm_switch.rb +9 -0
  66. data/lib/fog/hyperv/compute/requests/rename_vm.rb +16 -0
  67. data/lib/fog/hyperv/compute/requests/rename_vm_network_adapter.rb +25 -0
  68. data/lib/fog/hyperv/compute/requests/rename_vm_switch.rb +16 -0
  69. data/lib/fog/hyperv/compute/requests/resize_vhd.rb +16 -0
  70. data/lib/fog/hyperv/compute/requests/restart_vm.rb +22 -0
  71. data/lib/fog/hyperv/compute/requests/resume_vm.rb +22 -0
  72. data/lib/fog/hyperv/compute/requests/save_vm.rb +22 -0
  73. data/lib/fog/hyperv/compute/requests/set_vm.rb +15 -0
  74. data/lib/fog/hyperv/compute/requests/set_vm_bios.rb +15 -0
  75. data/lib/fog/hyperv/compute/requests/set_vm_com_port.rb +16 -0
  76. data/lib/fog/hyperv/compute/requests/set_vm_dvd_drive.rb +16 -0
  77. data/lib/fog/hyperv/compute/requests/set_vm_firmware.rb +15 -0
  78. data/lib/fog/hyperv/compute/requests/set_vm_floppy_disk_drive.rb +16 -0
  79. data/lib/fog/hyperv/compute/requests/set_vm_hard_disk_drive.rb +16 -0
  80. data/lib/fog/hyperv/compute/requests/set_vm_key_protector.rb +15 -0
  81. data/lib/fog/hyperv/compute/requests/set_vm_network_adapter.rb +25 -0
  82. data/lib/fog/hyperv/compute/requests/set_vm_network_adapter_vlan.rb +25 -0
  83. data/lib/fog/hyperv/compute/requests/set_vm_security.rb +17 -0
  84. data/lib/fog/hyperv/compute/requests/set_vm_switch.rb +9 -0
  85. data/lib/fog/hyperv/compute/requests/start_vm.rb +22 -0
  86. data/lib/fog/hyperv/compute/requests/stop_vm.rb +22 -0
  87. data/lib/fog/hyperv/compute/requests/suspend_vm.rb +22 -0
  88. data/lib/fog/hyperv/compute/requests/update_vm.rb +22 -0
  89. data/lib/fog/hyperv/compute.rb +206 -387
  90. data/lib/fog/hyperv/constants.rb +24 -0
  91. data/lib/fog/hyperv/fog_extensions/associations/collection.rb +11 -0
  92. data/lib/fog/hyperv/fog_extensions/attributes/datetime.rb +28 -0
  93. data/lib/fog/hyperv/fog_extensions/attributes/enum.rb +139 -0
  94. data/lib/fog/hyperv/fog_extensions/attributes/enumarray.rb +149 -0
  95. data/lib/fog/hyperv/fog_extensions/attributes/timespan.rb +27 -0
  96. data/lib/fog/hyperv/model.rb +142 -0
  97. data/lib/fog/hyperv/utils/powershell.rb +88 -0
  98. data/lib/fog/hyperv/utils/winrm.rb +233 -0
  99. data/lib/fog/hyperv/version.rb +4 -1
  100. data/lib/fog/hyperv.rb +53 -44
  101. metadata +187 -105
  102. data/.gitignore +0 -10
  103. data/.travis.yml +0 -11
  104. data/CHANGELOG.md +0 -52
  105. data/Gemfile +0 -4
  106. data/Rakefile +0 -10
  107. data/fog-hyperv.gemspec +0 -25
  108. data/lib/fog/collection.rb +0 -152
  109. data/lib/fog/hyperv/fog_extensions/enum.rb +0 -85
  110. data/lib/fog/hyperv/models/compute/bios.rb +0 -61
  111. data/lib/fog/hyperv/models/compute/cluster.rb +0 -64
  112. data/lib/fog/hyperv/models/compute/clusters.rb +0 -15
  113. data/lib/fog/hyperv/models/compute/com_port.rb +0 -22
  114. data/lib/fog/hyperv/models/compute/dvd_drive.rb +0 -92
  115. data/lib/fog/hyperv/models/compute/dvd_drives.rb +0 -12
  116. data/lib/fog/hyperv/models/compute/firmware.rb +0 -53
  117. data/lib/fog/hyperv/models/compute/floppy_drive.rb +0 -53
  118. data/lib/fog/hyperv/models/compute/floppy_drives.rb +0 -12
  119. data/lib/fog/hyperv/models/compute/hard_drive.rb +0 -110
  120. data/lib/fog/hyperv/models/compute/hard_drives.rb +0 -11
  121. data/lib/fog/hyperv/models/compute/host.rb +0 -45
  122. data/lib/fog/hyperv/models/compute/hosts.rb +0 -15
  123. data/lib/fog/hyperv/models/compute/network_adapter.rb +0 -145
  124. data/lib/fog/hyperv/models/compute/network_adapters.rb +0 -19
  125. data/lib/fog/hyperv/models/compute/server.rb +0 -220
  126. data/lib/fog/hyperv/models/compute/servers.rb +0 -21
  127. data/lib/fog/hyperv/models/compute/switch.rb +0 -65
  128. data/lib/fog/hyperv/models/compute/switches.rb +0 -15
  129. data/lib/fog/hyperv/models/compute/vhd.rb +0 -101
  130. data/lib/fog/hyperv/models/compute/vhds.rb +0 -16
  131. data/lib/fog/hyperv/requests/compute/add_vm_dvd_drive.rb +0 -12
  132. data/lib/fog/hyperv/requests/compute/add_vm_hard_disk_drive.rb +0 -12
  133. data/lib/fog/hyperv/requests/compute/add_vm_network_adapter.rb +0 -12
  134. data/lib/fog/hyperv/requests/compute/connect_vm_network_adapter.rb +0 -12
  135. data/lib/fog/hyperv/requests/compute/disconnect_vm_network_adapter.rb +0 -12
  136. data/lib/fog/hyperv/requests/compute/get_cluster.rb +0 -11
  137. data/lib/fog/hyperv/requests/compute/get_cluster_node.rb +0 -19
  138. data/lib/fog/hyperv/requests/compute/get_vhd.rb +0 -34
  139. data/lib/fog/hyperv/requests/compute/get_vm.rb +0 -20
  140. data/lib/fog/hyperv/requests/compute/get_vm_bios.rb +0 -21
  141. data/lib/fog/hyperv/requests/compute/get_vm_dvd_drive.rb +0 -20
  142. data/lib/fog/hyperv/requests/compute/get_vm_firmware.rb +0 -19
  143. data/lib/fog/hyperv/requests/compute/get_vm_floppy_disk_drive.rb +0 -20
  144. data/lib/fog/hyperv/requests/compute/get_vm_group.rb +0 -23
  145. data/lib/fog/hyperv/requests/compute/get_vm_hard_disk_drive.rb +0 -20
  146. data/lib/fog/hyperv/requests/compute/get_vm_host.rb +0 -12
  147. data/lib/fog/hyperv/requests/compute/get_vm_host_cluster.rb +0 -25
  148. data/lib/fog/hyperv/requests/compute/get_vm_network_adapter.rb +0 -27
  149. data/lib/fog/hyperv/requests/compute/get_vm_switch.rb +0 -27
  150. data/lib/fog/hyperv/requests/compute/mock_files/get_vm.json +0 -1
  151. data/lib/fog/hyperv/requests/compute/new_vhd.rb +0 -12
  152. data/lib/fog/hyperv/requests/compute/new_vm.rb +0 -15
  153. data/lib/fog/hyperv/requests/compute/new_vm_switch.rb +0 -13
  154. data/lib/fog/hyperv/requests/compute/remove_item.rb +0 -13
  155. data/lib/fog/hyperv/requests/compute/remove_vm.rb +0 -15
  156. data/lib/fog/hyperv/requests/compute/remove_vm_dvd_drive.rb +0 -12
  157. data/lib/fog/hyperv/requests/compute/remove_vm_hard_disk_drive.rb +0 -12
  158. data/lib/fog/hyperv/requests/compute/remove_vm_network_adapter.rb +0 -12
  159. data/lib/fog/hyperv/requests/compute/restart_vm.rb +0 -15
  160. data/lib/fog/hyperv/requests/compute/set_vm.rb +0 -12
  161. data/lib/fog/hyperv/requests/compute/set_vm_bios.rb +0 -13
  162. data/lib/fog/hyperv/requests/compute/set_vm_dvd_drive.rb +0 -12
  163. data/lib/fog/hyperv/requests/compute/set_vm_firmware.rb +0 -13
  164. data/lib/fog/hyperv/requests/compute/set_vm_hard_disk_drive.rb +0 -12
  165. data/lib/fog/hyperv/requests/compute/set_vm_network_adapter.rb +0 -12
  166. data/lib/fog/hyperv/requests/compute/set_vm_network_adapter_vlan.rb +0 -12
  167. data/lib/fog/hyperv/requests/compute/set_vm_switch.rb +0 -13
  168. data/lib/fog/hyperv/requests/compute/start_vm.rb +0 -15
  169. data/lib/fog/hyperv/requests/compute/stop_vm.rb +0 -15
  170. data/lib/fog/model.rb +0 -91
  171. data/test/fog/hyperv_test.rb +0 -7
  172. data/test/test_helper.rb +0 -4
  173. /data/lib/fog/hyperv/{requests/compute → compute/requests}/mock_files/get_cluster.json +0 -0
  174. /data/lib/fog/hyperv/{requests/compute → compute/requests}/mock_files/get_cluster_node.json +0 -0
  175. /data/lib/fog/hyperv/{requests/compute → compute/requests}/mock_files/get_vhd.json +0 -0
  176. /data/lib/fog/hyperv/{requests/compute → compute/requests}/mock_files/get_vm_bios.json +0 -0
  177. /data/lib/fog/hyperv/{requests/compute → compute/requests}/mock_files/get_vm_dvd_drive.json +0 -0
  178. /data/lib/fog/hyperv/{requests/compute → compute/requests}/mock_files/get_vm_firmware.json +0 -0
  179. /data/lib/fog/hyperv/{requests/compute → compute/requests}/mock_files/get_vm_floppy_disk_drive.json +0 -0
  180. /data/lib/fog/hyperv/{requests/compute → compute/requests}/mock_files/get_vm_hard_disk_drive.json +0 -0
  181. /data/lib/fog/hyperv/{requests/compute → compute/requests}/mock_files/get_vm_host.json +0 -0
  182. /data/lib/fog/hyperv/{requests/compute → compute/requests}/mock_files/get_vm_network_adapter.json +0 -0
  183. /data/lib/fog/hyperv/{requests/compute → compute/requests}/mock_files/get_vm_switch.json +0 -0
@@ -0,0 +1,466 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fog/compute/models/server'
4
+
5
+ class Fog::Hyperv::Compute
6
+ # rubocop:disable Metrics/ClassLength
7
+
8
+ # A Hyper-V VM
9
+ class Server < Fog::Compute::Server
10
+ extend Fog::Hyperv::ModelExtends
11
+ include Fog::Hyperv::ModelIncludes
12
+
13
+ # rubocop:disable Layout/HashAlignment
14
+
15
+ # VM runtime state
16
+ #
17
+ # @note Defined by Microsoft.HyperV.PowerShell.VMState
18
+ VM_STATE_ENUM_VALUES = {
19
+ Other: 1,
20
+ Running: 2,
21
+ Off: 3,
22
+ Stopping: 4,
23
+ Saved: 6,
24
+ Paused: 9,
25
+ Starting: 10,
26
+ Reset: 11,
27
+ Saving: 32_773,
28
+ Pausing: 32_776,
29
+ Resuming: 32_777,
30
+ FastSaved: 32_779,
31
+ FastSaving: 32_780,
32
+ ForceShutdown: 32_781,
33
+ ForceReboot: 32_782,
34
+ Hibernated: 32_783,
35
+ ComponentServicing: 32_784,
36
+ RunningCritical: 32_785,
37
+ OffCritical: 32_786,
38
+ StoppingCritical: 32_787,
39
+ SavedCritical: 32_788,
40
+ PausedCritical: 32_789,
41
+ StartingCritical: 32_790,
42
+ ResetCritical: 32_791,
43
+ SavingCritical: 32_792,
44
+ PausingCritical: 32_793,
45
+ ResumingCritical: 32_794,
46
+ FastSavedCritical: 32_795,
47
+ FastSavingCritical: 32_796
48
+ }.freeze
49
+
50
+ # VM object status
51
+ #
52
+ # @note Defined by Microsoft.HyperV.PowerShell.VMOperationalStatus
53
+ VM_STATUS_ENUM_VALUES = {
54
+ Ok: 2,
55
+ Degraded: 3,
56
+ PredictiveFailure: 5,
57
+ InService: 11,
58
+ Dormant: 15,
59
+ SupportingEntityInError: 16,
60
+ CreatingSnapshot: 32_768,
61
+ ApplyingSnapshot: 23_769,
62
+ DeletingSnapshot: 32_770,
63
+ WaitingToStart: 32_771,
64
+ MergingDisks: 32_772,
65
+ ExportingVirtualMachine: 32_773,
66
+ MigratingVirtualMachine: 32_774,
67
+ BackingUpVirtualMachine: 32_776,
68
+ ModifyingUpVirtualMachine: 32_777,
69
+ StorageMigrationPhaseOne: 32_778,
70
+ StorageMigrationPhaseTwo: 32_779,
71
+ MigratingPlannedVm: 32_780,
72
+ CheckingCompatibility: 32_781,
73
+ ApplicationCriticalState: 32_782,
74
+ CommunicationTimedOut: 32_783,
75
+ CommunicationFailed: 32_784,
76
+ NoIommu: 32_785,
77
+ NoIovSupportInNic: 32_786,
78
+ SwitchNotInIovMode: 32_787,
79
+ IovBlockedByPolicy: 32_788,
80
+ IovNoAvailResources: 32_789,
81
+ IovGuestDriversNeeded: 32_790,
82
+ CriticalIoError: 32_795
83
+ }.freeze
84
+
85
+ # VM firmware generation - i.e. BIOS/UEFI
86
+ VM_GENERATION_VALUES = {
87
+ BIOS: 1,
88
+ UEFI: 2
89
+ }.freeze
90
+ # rubocop:enable Layout/HashAlignment
91
+
92
+ # @!attribute [r] id
93
+ # @return [String] the GUID of the VM
94
+ identity :id
95
+ alias vm_id :id
96
+
97
+ # @!attribute [r] computer_name
98
+ # @return [String] the name of the host running the VM
99
+ attribute :computer_name
100
+
101
+ # @!attribute name
102
+ # @return [String] the name of the VM
103
+ attribute :name
104
+ alias vm_name :name
105
+ # @!attribute [r] creation_time
106
+ # @return [Time] the time the VM was created
107
+ attribute :creation_time, type: :hypervdatetime
108
+ # @!attribute dynamic_memory_enabled
109
+ # @return [Boolean] if memory is dynamically handled - sliding between #memory_minimum and #memory_maximum
110
+ attribute :dynamic_memory_enabled, type: :boolean, default: false
111
+ # @!attribute [r] generation
112
+ # @return [Symbol] the VM firmware generation
113
+ attribute :generation, type: :hypervenum, values: VM_GENERATION_VALUES, default: :UEFI
114
+ # @!attribute [r] is_clustered
115
+ # @return [Boolean] iv the VM is clustered
116
+ attribute :is_clustered, type: :boolean, default: false
117
+ # @!attribute [r] state
118
+ # @return [Symbol] the state of the VM
119
+ # @see VM_STATE_ENUM_VALUES
120
+ attribute :state, type: :hypervenum, values: VM_STATE_ENUM_VALUES
121
+ # @!attribute [r] primary_operational_status
122
+ # @return [Symbol] the primary status of the VM
123
+ # @see VM_STATUS_ENUM_VALUES
124
+ attribute :primary_operational_status, type: :hypervenum, values: VM_STATUS_ENUM_VALUES
125
+ # @!attribute [r] secondary_operational_status
126
+ # @return [Symbol] the secondary status of the VM
127
+ # @see VM_STATUS_ENUM_VALUES
128
+ attribute :secondary_operational_status, type: :hypervenum, values: VM_STATUS_ENUM_VALUES
129
+ # @!attribute [r] memory_assigned
130
+ # @return [Integer] the memory assigned to the VM on startup
131
+ attribute :memory_assigned, type: :integer
132
+ # @!attribute memory_maximum
133
+ # @return [Integer] the maximum amount of memory the VM can assign when #dynamic_memory_enabled
134
+ attribute :memory_maximum, type: :integer, default: 17_179_869_184
135
+ # @!attribute memory_minimum
136
+ # @return [Integer] the minimum amount of memory the VM can assign when #dynamic_memory_enabled
137
+ attribute :memory_minimum, type: :integer, default: 536_870_912
138
+ # @!attribute memory_startup
139
+ # @return [Integer] the starting amount of memory the VM will be assigned
140
+ attribute :memory_startup, type: :integer, default: 536_870_912
141
+ # @!attribute notes
142
+ # @return [String] user-specified notes for the VM
143
+ attribute :notes, type: :string
144
+ # @!attribute processor_count
145
+ # @return [Integer] the number of processors in the VM
146
+ attribute :processor_count, type: :integer, default: 1
147
+ # @!attribute [r] uptime
148
+ # @return [Time] the amount of time the VM has been online
149
+ attribute :uptime, type: :hypervtimespan
150
+
151
+ # @!attribute com_ports
152
+ # @return [Array<ComPort>] the COM ports on the VM
153
+ collection :com_ports
154
+ # @!attribute dvd_drives
155
+ # @return [Array<DvdDrive>] the DVD drives on the VM
156
+ collection :dvd_drives
157
+ # @!attribute floppy_drives
158
+ # @note only supported on #generation +:BIOS+
159
+ # @return [Array<FloppyDrive>] the floppy drives on the VM
160
+ collection :floppy_drives
161
+ # @!attribute hard_drives
162
+ # @return [Array<HardDrive>] the hard drives on the VM
163
+ collection :hard_drives
164
+ # @!attribute network_adapters
165
+ # @return [Array<NetworkAdapter>] the network adapters on the VM
166
+ collection :network_adapters
167
+ # @!attribute vhds
168
+ # @return [Array<Vhd>] the VHD images in use by the VM
169
+ collection :vhds
170
+
171
+ has_one :bios, :bios
172
+ has_one :security, :security
173
+
174
+ def initialize(attrs = {})
175
+ @cluster = attrs.delete :cluster
176
+ @computer = attrs.delete :computer
177
+
178
+ super
179
+ end
180
+
181
+ # @!attribute [r] bios
182
+ # @return [Bios,Firmware] BIOS/UEFI configuration depending on generation
183
+ def bios
184
+ associations[:bios] ||= begin
185
+ requires :generation, :id
186
+ if generation == :BIOS
187
+ klass = Fog::Hyperv::Compute::Bios
188
+ method = :get_vm_bios
189
+ else
190
+ klass = Fog::Hyperv::Compute::Firmware
191
+ method = :get_vm_firmware
192
+ end
193
+
194
+ klass.new(
195
+ **service.public_send(
196
+ method,
197
+ computer_name: computer_name,
198
+ vm_id: vm_id,
199
+
200
+ _return_fields: klass.attributes
201
+ ),
202
+
203
+ vm: self,
204
+ service: @service,
205
+ computer: @computer,
206
+ cluster: @cluster
207
+ )
208
+ end
209
+ end
210
+ alias firmware :bios
211
+
212
+ # @!attribute [r] security
213
+ # @return [Security] UEFI security configuration, if #generation is +:UEFI+
214
+ def security
215
+ requires :generation, :id
216
+ return nil unless generation == :UEFI
217
+
218
+ associations[:security] ||= Fog::Hyperv::Compute::Security.new(
219
+ **service.get_vm_security(
220
+ computer_name: computer_name,
221
+ vm_id: vm_id,
222
+
223
+ _return_fields: Fog::Hyperv::Compute::Security.attributes
224
+ ),
225
+
226
+ vm: self,
227
+ service: @service,
228
+ computer: @computer,
229
+ cluster: @cluster
230
+ )
231
+ end
232
+
233
+ # @!attribute tpm_enabled
234
+ # @return [Boolean] if a vTPM is enabled on the VM, only available if #generation is +:UEFI+
235
+ def tpm_enabled
236
+ security&.tpm_enabled
237
+ end
238
+
239
+ def tpm_enabled=(enabled)
240
+ return unless security
241
+ return if enabled == tpm_enabled
242
+
243
+ security.tpm_enabled = enabled
244
+ security.save
245
+ enabled # rubocop:disable Lint/Void -- Needs to shadow the result of security.save
246
+ end
247
+
248
+ # Start the VM
249
+ def start
250
+ requires :id
251
+
252
+ service.start_vm(computer_name: computer_name, id: id)
253
+ true
254
+ end
255
+
256
+ # Stop the VM
257
+ # @param [Boolean] turn_off perform an instant power off instead of an ACPI shutdown
258
+ def stop(turn_off: false)
259
+ requires :id
260
+
261
+ service.stop_vm(computer_name: computer_name, id: id, turn_off: turn_off)
262
+ true
263
+ end
264
+
265
+ # Suspend VM execution
266
+ def suspend
267
+ requires :id
268
+
269
+ service.suspend_vm(computer_name: computer_name, id: id)
270
+ true
271
+ end
272
+
273
+ # Resume suspended VM execution
274
+ def resume
275
+ requires :id
276
+
277
+ service.resume_vm(computer_name: computer_name, id: id)
278
+ true
279
+ end
280
+
281
+ # Hibernate the VM, i.e. save VM state to disk and turn it off
282
+ def hibernate
283
+ requires :id
284
+
285
+ service.save_vm(computer_name: computer_name, id: id)
286
+ true
287
+ end
288
+
289
+ # Restart the VM
290
+ # @note This method will always cause a hard reset, i.e. power off and on without waiting for OS shutdown
291
+ def restart
292
+ requires :id
293
+
294
+ service.restart_vm(computer_name: computer_name, id: id)
295
+ true
296
+ end
297
+ alias reboot :restart
298
+
299
+ # Update the VM object to the latest version
300
+ def update_version
301
+ requires :id
302
+
303
+ service.update_vm(computer_name: computer_name, id: id)
304
+ true
305
+ end
306
+
307
+ # Remove the VM object from Hyper-V
308
+ #
309
+ # @note if the VM has VHDs, make sure to remove them first to not leave the VM data remaining on disk
310
+ def destroy
311
+ requires :id
312
+ stop turn_off: true if ready?
313
+
314
+ service.remove_vm(computer_name: computer_name, id: id)
315
+ true
316
+ end
317
+
318
+ # Create the VM object if it doesn't exist
319
+ # @param [Symbol] boot_device the default boot device to configure the VM with, one of BOOT_DEVICE
320
+ # @param [String] switch_name the name of a Switch to connect the VM to on creation
321
+ def create(boot_device: nil, switch_name: nil, **attrs)
322
+ attrs[:no_vhd] = true unless attrs[:new_vhd_path]
323
+
324
+ # Attributes that can't be set as part of the New-VM call
325
+ post_create_attributes = {
326
+ processor_count: processor_count > 1 ? processor_count : nil,
327
+ notes: notes.nil? || notes == '' ? nil : notes
328
+ }
329
+ if dynamic_memory_enabled
330
+ post_create_attributes.merge!(
331
+ dynamic_memory_enabled: true,
332
+ memory_minimum_bytes: memory_minimum,
333
+ memory_maximum_bytes: memory_maximum
334
+ )
335
+ end
336
+ post_create_attributes.compact!
337
+
338
+ merge_attributes(
339
+ service.new_vm(
340
+ computer_name: computer_name,
341
+ name: name,
342
+
343
+ generation: generation_num,
344
+ memory_startup_bytes: memory_startup,
345
+ boot_device: boot_device,
346
+ switch_name: switch_name,
347
+
348
+ **attrs,
349
+
350
+ _return_fields: self.class.attributes
351
+ )
352
+ )
353
+ attributes.merge! post_create_attributes if post_create_attributes.any?
354
+ save if dirty?
355
+
356
+ # Save any associations that have been manually assigned before VM creation
357
+ self.class.associations.each_key do |assoc|
358
+ next unless associations.key? assoc
359
+
360
+ associations[assoc].select do |obj|
361
+ obj.computer_name = computer_name if obj.respond_to?(:computer_name=)
362
+ obj.vm_id = id if obj.respond_to?(:vm_id=)
363
+
364
+ obj.save if obj.dirty? || !obj.persisted?
365
+ next if assoc != :vhd || hard_drives.none? { |hdd| hdd.path == vhd.path }
366
+
367
+ hard_drives.create(path: vhd.path)
368
+ end
369
+ associations[assoc].clear
370
+ end
371
+
372
+ self
373
+ end
374
+
375
+ def update
376
+ requires :id
377
+
378
+ changes = {
379
+ processor_count: changed!(:processor_count),
380
+ memory_startup_bytes: changed!(:memory_startup),
381
+ new_vm_name: changed!(:name)
382
+ }.compact
383
+ if dynamic_memory_enabled
384
+ changes[:dynamic_memory] = true if changed?(:dynamic_memory_enabled)
385
+ changes[:memory_minimum_bytes] = memory_minimum if changed?(:dynamic_memory_enabled, :memory_minimum)
386
+ changes[:memory_maximum_bytes] = memory_maximum if changed?(:dynamic_memory_enabled, :memory_maximum)
387
+ elsif changed?(:dynamic_memory_enabled)
388
+ changes[:static_memory] = true
389
+ end
390
+ changes.compact!
391
+
392
+ changes[:notes] = notes || '' if changed? :notes
393
+ return self unless changes.any?
394
+
395
+ merge_attributes(
396
+ service.set_vm(
397
+ computer_name: old.computer_name,
398
+ id: old.id,
399
+
400
+ **changes,
401
+
402
+ _always_include: changes.keys,
403
+ _return_fields: self.class.attributes
404
+ )
405
+ )
406
+ end
407
+
408
+ # Reload the VM attributes from the Hyper-V server
409
+ # @return [self] if model successfully reloaded
410
+ # @return [nil] if something went wrong or the model was not found
411
+ def reload
412
+ requires :id
413
+
414
+ data = service.get_vm computer_name: computer_name, id: id, _return_fields: self.class.attributes
415
+ return unless data
416
+
417
+ merge_attributes(data)
418
+ @old = data
419
+
420
+ self
421
+ end
422
+
423
+ # Build a path for where to store a VHD of a given name
424
+ # @return [String] the absolute path for the VHD
425
+ def build_vhd_path(filename)
426
+ requires :name
427
+
428
+ [computer.virtual_hard_disk_path, '\\', name, '\\', filename].join
429
+ end
430
+
431
+ # Get the username of the main system account
432
+ # @return [String] the system account username - usually "Administrator" or "root"
433
+ def username
434
+ @username || 'Administrator'
435
+ end
436
+
437
+ # @return [Boolean] if the VM is ready? (i.e. #state is +:Running+)
438
+ def ready?
439
+ state_num == 2
440
+ end
441
+
442
+ # @return [Array<String>] the MAC addresses of all attached network adapters
443
+ def mac_addresses
444
+ network_adapters.map(&:mac_address)
445
+ end
446
+
447
+ # @return [Array<String>] the IP addresses of all attached network adapters
448
+ def ip_addresses
449
+ network_adapters.map(&:ip_addresses).flatten
450
+ end
451
+
452
+ # @return [Array<String>] the public (not link-local) IP addresses of all attached network adapters
453
+ def public_ip_addresses
454
+ ip_addresses
455
+ .map { |a| IPAddr.new a }
456
+ .reject(&:link_local?)
457
+ .map(&:to_s)
458
+ end
459
+
460
+ # @return [String,nil] the first public IP address of the VM - if any
461
+ def public_ip_address
462
+ public_ip_addresses.first
463
+ end
464
+ end
465
+ # rubocop:enable Metrics/ClassLength
466
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Fog::Hyperv::Compute
4
+ class Servers < Fog::Hyperv::Collection
5
+ model Fog::Hyperv::Compute::Server
6
+
7
+ get_method :get_vm
8
+
9
+ def get(identifier, **filters)
10
+ id = identifier if identifier =~ /\A#{Fog::Hyperv::GUID}\z/i
11
+ name = identifier unless id
12
+
13
+ raise ArgumentError, 'Must provide a name or GUID' if (id.nil? || id.empty?) && (name.nil? || name.empty?)
14
+
15
+ super(name: name, id: id, **filters)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Fog::Hyperv::Compute
4
+ class Switch < Fog::Hyperv::Model
5
+ # @!attribute [r] id
6
+ # @return [String] the GUID of the network switch
7
+ identity :id
8
+
9
+ # @!attribute [r] computer_name
10
+ # @return [String] the name of the computer hosting this network switch
11
+ attribute :computer_name
12
+
13
+ # @!attribute allow_management_os
14
+ # @return [Boolean] is the management OS allowed to use this switch
15
+ attribute :allow_management_os, type: :boolean
16
+ # @!attribute default_flow_minimum_bandwidth_absolute
17
+ # @return [Integer] the default minimum bandwidth allocation for VMs without specific allocations
18
+ attribute :default_flow_minimum_bandwidth_absolute, type: :integer
19
+ # @!attribute default_flow_minimum_bandwidth_weight
20
+ # @return [Integer] the default minimum bandwidth allocation for VMs without specific allocations, as a relative weight
21
+ attribute :default_flow_minimum_bandwidth_weight, type: :integer
22
+ # @!attribute name
23
+ # @return [String] the name of the network switch
24
+ attribute :name
25
+ # @!attribute [r] net_adapter_interface_description
26
+ # @return [String] the network interface description this switch is attached to
27
+ attribute :net_adapter_interface_description
28
+ # @!attribute [r] net_adapter_name
29
+ # @return [String] the network interface name this switch is attached to
30
+ attribute :net_adapter_name
31
+ # @!attribute notes
32
+ # @return [String] the user-specified notes for the network switch
33
+ attribute :notes
34
+ # @!attribute resource_pool_name
35
+ # @return [String] the resource pool the switch is part of
36
+ attribute :resource_pool_name
37
+ # @!attribute [r] switch_type
38
+ # @return [:Private, :Internal, :External] the type of network switch
39
+ attribute :switch_type, type: :hypervenum, values: %i[Private Internal External]
40
+
41
+ def create
42
+ requires :name
43
+ requires_one :net_adapter_name, :net_adapter_interface_description, :switch_type
44
+
45
+ merge_attributes(
46
+ service.new_vm_switch(
47
+ computer_name: computer_name,
48
+ name: name,
49
+
50
+ allow_management_os: allow_management_os,
51
+ net_adapter_interface_description: net_adapter_interface_description,
52
+ net_adapter_name: net_adapter_name,
53
+ notes: notes,
54
+ switch_type: !net_adapter_interface_description && switch_type,
55
+
56
+ _return_fields: self.class.attributes
57
+ )
58
+ )
59
+ end
60
+
61
+ def update
62
+ requires :id
63
+
64
+ if changed?(:name)
65
+ service.rename_vm_switch(
66
+ computer_name: old.computer_name,
67
+ id: old.id,
68
+
69
+ new_name: name
70
+ )
71
+ @old.name = name
72
+ end
73
+
74
+ changes = {
75
+ allow_management_os: changed!(:allow_management_os),
76
+ default_flow_minimum_bandwidth_absolute: changed!(:default_flow_minimum_bandwidth_absolute),
77
+ default_flow_minimum_bandwidth_weight: changed!(:default_flow_minimum_bandwidth_weight)
78
+ }.compact
79
+ changes[:notes] = notes || '' if changed? :notes
80
+ return self unless changes.any?
81
+
82
+ merge_attributes(
83
+ service.set_vm_switch(
84
+ computer_name: old.computer_name,
85
+ id: old.id,
86
+
87
+ **changes,
88
+
89
+ _always_include: changes.keys,
90
+ _return_fields: self.class.attributes
91
+ )
92
+ )
93
+ end
94
+
95
+ def destroy
96
+ requires :id
97
+
98
+ service.remove_vm_switch(
99
+ computer_name: computer_name,
100
+ id: id
101
+ )
102
+ true
103
+ end
104
+
105
+ def reload
106
+ requires :id
107
+
108
+ data = service.get_vm_switch(
109
+ computer_name: computer_name,
110
+ id: id
111
+ )
112
+ return unless data
113
+
114
+ merge_attributes(data)
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Fog::Hyperv::Compute
4
+ class Switches < Fog::Hyperv::Collection
5
+ model Fog::Hyperv::Compute::Switch
6
+
7
+ get_method :get_vm_switch
8
+
9
+ attribute :computer_name
10
+
11
+ def get(identifier, **filter)
12
+ id = identifier if identifier =~ /\A#{Fog::Hyperv::GUID}\z/i
13
+ name = identifier unless id
14
+
15
+ raise ArgumentError, 'Must provide a name or GUID' if (id.nil? || id.empty?) && (name.nil? || name.empty?)
16
+
17
+ super(name: name, id: id, **filter)
18
+ end
19
+ end
20
+ end