fog-hyperv 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +11 -0
  4. data/Gemfile +4 -0
  5. data/README.md +53 -0
  6. data/Rakefile +10 -0
  7. data/fog-hyperv.gemspec +28 -0
  8. data/lib/fog/bin/hyperv.rb +28 -0
  9. data/lib/fog/collection.rb +114 -0
  10. data/lib/fog/hyperv.rb +123 -0
  11. data/lib/fog/hyperv/compute.rb +327 -0
  12. data/lib/fog/hyperv/fog_extensions/enum.rb +51 -0
  13. data/lib/fog/hyperv/models/compute/bios.rb +52 -0
  14. data/lib/fog/hyperv/models/compute/cluster.rb +37 -0
  15. data/lib/fog/hyperv/models/compute/clusters.rb +11 -0
  16. data/lib/fog/hyperv/models/compute/com_port.rb +22 -0
  17. data/lib/fog/hyperv/models/compute/dvd_drive.rb +62 -0
  18. data/lib/fog/hyperv/models/compute/dvd_drives.rb +11 -0
  19. data/lib/fog/hyperv/models/compute/firmware.rb +52 -0
  20. data/lib/fog/hyperv/models/compute/floppy_drive.rb +48 -0
  21. data/lib/fog/hyperv/models/compute/floppy_drives.rb +11 -0
  22. data/lib/fog/hyperv/models/compute/hard_drive.rb +110 -0
  23. data/lib/fog/hyperv/models/compute/hard_drives.rb +11 -0
  24. data/lib/fog/hyperv/models/compute/host.rb +20 -0
  25. data/lib/fog/hyperv/models/compute/hosts.rb +15 -0
  26. data/lib/fog/hyperv/models/compute/network_adapter.rb +142 -0
  27. data/lib/fog/hyperv/models/compute/network_adapters.rb +19 -0
  28. data/lib/fog/hyperv/models/compute/server.rb +192 -0
  29. data/lib/fog/hyperv/models/compute/servers.rb +30 -0
  30. data/lib/fog/hyperv/models/compute/switch.rb +58 -0
  31. data/lib/fog/hyperv/models/compute/switches.rb +15 -0
  32. data/lib/fog/hyperv/models/compute/vhd.rb +100 -0
  33. data/lib/fog/hyperv/models/compute/vhds.rb +16 -0
  34. data/lib/fog/hyperv/requests/compute/add_vm_hard_disk_drive.rb +12 -0
  35. data/lib/fog/hyperv/requests/compute/add_vm_network_adapter.rb +12 -0
  36. data/lib/fog/hyperv/requests/compute/connect_vm_network_adapter.rb +12 -0
  37. data/lib/fog/hyperv/requests/compute/disconnect_vm_network_adapter.rb +12 -0
  38. data/lib/fog/hyperv/requests/compute/get_cluster.rb +11 -0
  39. data/lib/fog/hyperv/requests/compute/get_cluster_node.rb +19 -0
  40. data/lib/fog/hyperv/requests/compute/get_vhd.rb +34 -0
  41. data/lib/fog/hyperv/requests/compute/get_vm.rb +20 -0
  42. data/lib/fog/hyperv/requests/compute/get_vm_bios.rb +21 -0
  43. data/lib/fog/hyperv/requests/compute/get_vm_dvd_drive.rb +20 -0
  44. data/lib/fog/hyperv/requests/compute/get_vm_firmware.rb +21 -0
  45. data/lib/fog/hyperv/requests/compute/get_vm_floppy_disk_drive.rb +20 -0
  46. data/lib/fog/hyperv/requests/compute/get_vm_group.rb +23 -0
  47. data/lib/fog/hyperv/requests/compute/get_vm_hard_disk_drive.rb +20 -0
  48. data/lib/fog/hyperv/requests/compute/get_vm_host.rb +12 -0
  49. data/lib/fog/hyperv/requests/compute/get_vm_host_cluster.rb +25 -0
  50. data/lib/fog/hyperv/requests/compute/get_vm_network_adapter.rb +27 -0
  51. data/lib/fog/hyperv/requests/compute/get_vm_switch.rb +27 -0
  52. data/lib/fog/hyperv/requests/compute/mock_files/get_cluster.json +1 -0
  53. data/lib/fog/hyperv/requests/compute/mock_files/get_cluster_node.json +1 -0
  54. data/lib/fog/hyperv/requests/compute/mock_files/get_vhd.json +1 -0
  55. data/lib/fog/hyperv/requests/compute/mock_files/get_vm.json +1 -0
  56. data/lib/fog/hyperv/requests/compute/mock_files/get_vm_bios.json +1 -0
  57. data/lib/fog/hyperv/requests/compute/mock_files/get_vm_dvd_drive.json +1 -0
  58. data/lib/fog/hyperv/requests/compute/mock_files/get_vm_firmware.json +1 -0
  59. data/lib/fog/hyperv/requests/compute/mock_files/get_vm_floppy_disk_drive.json +1 -0
  60. data/lib/fog/hyperv/requests/compute/mock_files/get_vm_hard_disk_drive.json +1 -0
  61. data/lib/fog/hyperv/requests/compute/mock_files/get_vm_host.json +1 -0
  62. data/lib/fog/hyperv/requests/compute/mock_files/get_vm_network_adapter.json +1 -0
  63. data/lib/fog/hyperv/requests/compute/mock_files/get_vm_switch.json +1 -0
  64. data/lib/fog/hyperv/requests/compute/new_vhd.rb +12 -0
  65. data/lib/fog/hyperv/requests/compute/new_vm.rb +15 -0
  66. data/lib/fog/hyperv/requests/compute/new_vm_switch.rb +13 -0
  67. data/lib/fog/hyperv/requests/compute/remove_item.rb +13 -0
  68. data/lib/fog/hyperv/requests/compute/remove_vm.rb +15 -0
  69. data/lib/fog/hyperv/requests/compute/remove_vm_hard_disk_drive.rb +12 -0
  70. data/lib/fog/hyperv/requests/compute/remove_vm_network_adapter.rb +12 -0
  71. data/lib/fog/hyperv/requests/compute/restart_vm.rb +15 -0
  72. data/lib/fog/hyperv/requests/compute/set_vm.rb +12 -0
  73. data/lib/fog/hyperv/requests/compute/set_vm_bios.rb +13 -0
  74. data/lib/fog/hyperv/requests/compute/set_vm_dvd_drive.rb +12 -0
  75. data/lib/fog/hyperv/requests/compute/set_vm_firmware.rb +13 -0
  76. data/lib/fog/hyperv/requests/compute/set_vm_hard_disk_drive.rb +12 -0
  77. data/lib/fog/hyperv/requests/compute/set_vm_network_adapter.rb +12 -0
  78. data/lib/fog/hyperv/requests/compute/set_vm_switch.rb +13 -0
  79. data/lib/fog/hyperv/requests/compute/start_vm.rb +15 -0
  80. data/lib/fog/hyperv/requests/compute/stop_vm.rb +15 -0
  81. data/lib/fog/hyperv/version.rb +5 -0
  82. data/lib/fog/model.rb +64 -0
  83. data/test/fog/hyperv_test.rb +7 -0
  84. data/test/test_helper.rb +4 -0
  85. metadata +199 -0
@@ -0,0 +1,11 @@
1
+ module Fog
2
+ module Compute
3
+ class Hyperv
4
+ class HardDrives < Fog::Hyperv::VMCollection
5
+ model Fog::Compute::Hyperv::HardDrive
6
+
7
+ get_method :get_vm_hard_disk_drive
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ module Fog
2
+ module Compute
3
+ class Hyperv
4
+ class Host < Fog::Hyperv::Model
5
+ identity :name
6
+
7
+ attribute :computer_name
8
+ attribute :fully_qualified_domain_name
9
+ attribute :logical_processor_count
10
+ attribute :memory_capacity
11
+ attribute :mac_address_minimum
12
+ attribute :mac_address_maximum
13
+ attribute :maximum_storage_migrations
14
+ attribute :maximum_virtual_machine_migrations
15
+ attribute :virtual_hard_disk_path
16
+ attribute :virtual_machine_path
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module Fog
2
+ module Compute
3
+ class Hyperv
4
+ class Hosts < Fog::Hyperv::Collection
5
+ model Fog::Compute::Hyperv::Host
6
+
7
+ get_method :get_vm_host
8
+
9
+ def get(name, filters = {})
10
+ super filters.merge(computer_name: name)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,142 @@
1
+ module Fog
2
+ module Compute
3
+ class Hyperv
4
+ class NetworkAdapter < Fog::Hyperv::Model
5
+ identity :id
6
+
7
+ # attribute :acl_list
8
+ attribute :computer_name
9
+ attribute :connected
10
+ attribute :dynamic_mac_address_enabled, type: :boolean, default: true
11
+ attribute :ip_addresses
12
+ # attribute :is_deleted
13
+ attribute :is_external_adapter
14
+ attribute :is_legacy
15
+ attribute :is_management_os
16
+ # attribute :isolation_setting # Might need lazy loading
17
+ attribute :mac_address
18
+ attribute :name, type: :string, default: 'Network Adapter'
19
+ # attribute :router_guard, type: :enum, values: [ :On, :Off ]
20
+ attribute :status, type: :enum, values: STATUS_ENUM_VALUES
21
+ attribute :switch_id
22
+ attribute :switch_name, type: :string
23
+ attribute :vm_id
24
+ attribute :vm_name
25
+ # attribute :vlan_setting # Might need lazy loading
26
+
27
+ def connect(switch, options = {})
28
+ requires :name, :computer_name, :vm_name
29
+
30
+ switch = switch.name if switch.is_a? Fog::Compute::Hyperv::Switch
31
+
32
+ service.connect_vm_network_adapter options.merge(
33
+ computer_name: computer_name,
34
+ name: name,
35
+ switch_name: switch,
36
+ vm_name: vm_name
37
+ )
38
+ end
39
+
40
+ def disconnect(options = {})
41
+ requires :name, :computer_name, :vm_name
42
+
43
+ service.disconnect_vm_network_adapter options.merge(
44
+ computer_name: computer_name,
45
+ name: name,
46
+ vm_name: vm_name
47
+ )
48
+ end
49
+
50
+ def switch
51
+ service.switches.get switch_name, computer_name: computer_name if switch_name
52
+ end
53
+
54
+ def ip_addresses
55
+ attributes[:ip_addresses] = [] \
56
+ if attributes[:ip_addresses] == ''
57
+ attributes[:ip_addresses]
58
+ end
59
+
60
+ def save
61
+ requires :name, :computer_name, :vm_name
62
+
63
+ data = \
64
+ if !persisted?
65
+ service.add_vm_network_adapter(
66
+ computer_name: computer_name,
67
+ name: name,
68
+ vm_name: vm_name,
69
+ passthru: true,
70
+
71
+ dynamic_mac_address: dynamic_mac_address_enabled,
72
+ static_mac_address: !dynamic_mac_address_enabled && mac_address,
73
+ switch_name: switch_name,
74
+
75
+ _return_fields: self.class.attributes,
76
+ _json_depth: 1
77
+ )
78
+ else
79
+ ret = service.set_vm_network_adapter(
80
+ computer_name: old.computer_name,
81
+ name: old.name,
82
+ vm_name: old.vm_name,
83
+ passthru: true,
84
+
85
+ dynamic_mac_address: changed?(:dynamic_mac_address_enabled) && dynamic_mac_address_enabled,
86
+ static_mac_address: (changed?(:mac_address) || changed?(:dynamic_mac_address_enabled)) && !dynamic_mac_address_enabled && mac_address,
87
+
88
+ _return_fields: self.class.attributes,
89
+ _json_depth: 1
90
+ )
91
+
92
+ if changed?(:switch_name)
93
+ service.disconnect_vm_network_adapter(
94
+ computer_name: ret.computer_name,
95
+ name: ret.name,
96
+ vm_name: ret.vm_name
97
+ ) unless switch_name
98
+
99
+ service.connect_vm_network_adapter(
100
+ computer_name: ret.computer_name,
101
+ name: ret.name,
102
+ vm_name: ret.vm_name,
103
+ switch_name: switch_name
104
+ ) if switch_name
105
+ end
106
+ ret[:switch_name] = switch_name
107
+ ret
108
+ end
109
+
110
+ if data.is_a? Array
111
+ data = data.find { |e| e[:id] == id } if id
112
+ data = data.last unless id
113
+ end
114
+
115
+ merge_attributes(data)
116
+ @old = dup
117
+ self
118
+ end
119
+
120
+ def destroy
121
+ requires :vm_name, :name, :computer_name, :id
122
+
123
+ service.remove_vm_network_adapter(
124
+ name: name,
125
+ computer_name: computer_name,
126
+ vm_name: vm_name
127
+ )
128
+ end
129
+
130
+ def reload
131
+ data = collection.get(
132
+ name,
133
+ computer_name: computer_name,
134
+ vm_name: vm_name
135
+ )
136
+ merge_attributes(data.attributes)
137
+ self
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,19 @@
1
+ module Fog
2
+ module Compute
3
+ class Hyperv
4
+ class NetworkAdapters < Fog::Hyperv::VMCollection
5
+ model Fog::Compute::Hyperv::NetworkAdapter
6
+
7
+ get_method :get_vm_network_adapter
8
+
9
+ def all(filters = {})
10
+ super filters.merge(all: !vm)
11
+ end
12
+
13
+ def get(name, filters = {})
14
+ super filters.merge(name: name, all: !vm)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,192 @@
1
+ require 'fog/compute/models/server'
2
+
3
+ module Fog
4
+ module Compute
5
+ class Hyperv
6
+ class Server < Fog::Compute::Server
7
+ extend Fog::Hyperv::ModelExtends
8
+ include Fog::Hyperv::ModelIncludes
9
+
10
+ VM_STATUS_ENUM_VALUES = {
11
+ 1 => :Unknown,
12
+ 2 => :Running,
13
+ 3 => :Off,
14
+ 4 => :Stopping,
15
+ 6 => :Saved,
16
+ 9 => :Paused,
17
+ 10 => :Starting,
18
+ 11 => :Reset,
19
+ 32773 => :Saving,
20
+ 32776 => :Pausing,
21
+ 32777 => :Resuming,
22
+ 32779 => :FastSaved,
23
+ 32780 => :FastSaving,
24
+ 32781 => :ForceShutdown,
25
+ 32782 => :ForceReboot,
26
+ 32783 => :RunningCritical,
27
+ 32784 => :OffCritical,
28
+ 32785 => :StoppingCritical,
29
+ 32786 => :SavedCritical,
30
+ 32787 => :PausedCritical,
31
+ 32788 => :StartingCritical,
32
+ 32789 => :ResetCritical,
33
+ 32790 => :SavingCritical,
34
+ 32791 => :PausingCritical,
35
+ 32792 => :ResumingCritical,
36
+ 32793 => :FastSavedCritical,
37
+ 32794 => :FastSavingCritical
38
+ }.freeze
39
+
40
+ identity :id, type: :string
41
+
42
+ attribute :name, type: :string
43
+ attribute :computer_name, type: :string
44
+ attribute :com_port1
45
+ attribute :com_port2
46
+ attribute :dynamic_memory_enabled, type: :boolean, default: false
47
+ attribute :generation, type: :integer, default: 1 # 1 => bios, 2 => uefi
48
+ attribute :state, type: :enum, values: VM_STATUS_ENUM_VALUES
49
+ attribute :status, type: :string
50
+ attribute :memory_assigned, type: :integer
51
+ attribute :memory_maximum, type: :integer, default: 171_798_691_84
52
+ attribute :memory_minimum, type: :integer, default: 536_870_912
53
+ attribute :memory_startup, type: :integer, default: 536_870_912
54
+ attribute :notes, type: :string
55
+ attribute :processor_count, type: :integer, default: 1
56
+
57
+ lazy_attributes :network_adapters,
58
+ :dvd_drives,
59
+ :hard_drives,
60
+ :floppy_drive
61
+
62
+ %i(network_adapters dvd_drives floppy_drives hard_drives vhds).each do |attr|
63
+ define_method attr do
64
+ attributes[attr] ||= [] unless persisted?
65
+ attributes[attr] ||= service.send(attr, vm: self)
66
+ end
67
+ end
68
+
69
+ %i(com_port1 com_port2).each do |attr|
70
+ define_method "#{attr}=".to_sym do |data|
71
+ attributes[attr] = Fog::Compute::Hyperv::ComPort.new(data) if data.is_a?(Hash)
72
+ end
73
+ end
74
+
75
+ def bios
76
+ @bios ||= begin
77
+ if generation == 1
78
+ Fog::Compute::Hyperv::Bios.new(service.get_vm_bios(computer_name: computer_name, vm_name: name).merge service: service)
79
+ elsif generation == 2
80
+ Fog::Compute::Hyperv::Firmware.new(service.get_vm_firmware(computer_name: computer_name, vm_name: name).merge service: service)
81
+ end
82
+ end
83
+ end
84
+ alias firmware :bios
85
+
86
+ alias vm_id :id
87
+ alias vm_name :name
88
+
89
+ def start(options = {})
90
+ requires :name, :computer_name
91
+ service.start_vm options.merge(
92
+ name: name,
93
+ computer_name: computer_name
94
+ )
95
+ end
96
+
97
+ def stop(options = {})
98
+ requires :name, :computer_name
99
+ service.stop_vm options.merge(
100
+ name: name,
101
+ computer_name: computer_name
102
+ )
103
+ end
104
+
105
+ def restart(options = {})
106
+ requires :name, :computer_name
107
+ service.restart_vm options.merge(
108
+ name: name,
109
+ computer_name: computer_name
110
+ )
111
+ end
112
+ alias reboot :restart
113
+
114
+ def destroy(options = {})
115
+ requires :name, :computer_name
116
+ stop turn_off: true if ready?
117
+ service.remove_vm options.merge(
118
+ name: name,
119
+ computer_name: computer_name
120
+ )
121
+ end
122
+
123
+ def add_interface(options = {})
124
+ network_adapters.create options
125
+ end
126
+
127
+ def save(options = {})
128
+ requires :name
129
+ logger.debug "Saving server with; #{attributes}, #{options}"
130
+
131
+ data = \
132
+ if !persisted?
133
+ usable = %i(name memory_startup generation boot_device switch_name no_vhd new_vhd_path new_vhd_size_bytes).freeze
134
+ service.new_vm \
135
+ attributes.select { |k, _v| usable.include? k }
136
+ .merge(options)
137
+ .merge(_return_fields: self.class.attributes, _json_depth: 1)
138
+ else
139
+ service.set_vm options.merge(
140
+ computer_name: old.computer_name,
141
+ name: old.name,
142
+ passthru: true,
143
+
144
+ processor_count: changed!(:processor_count),
145
+ dynamic_memory: changed?(:dynamic_memory_enabled) && dynamic_memory_enabled,
146
+ static_memory: changed?(:dynamic_memory_enabled) && !dynamic_memory_enabled,
147
+ memory_minimum_bytes: changed?(:memory_minimum) && dynamic_memory_enabled && memory_minimum,
148
+ memory_maximum_bytes: changed?(:memory_maximum) && dynamic_memory_enabled && memory_maximum,
149
+ memory_startup_bytes: changed!(:memory_startup),
150
+ notes: changed!(:notes),
151
+ new_name: changed!(:name),
152
+
153
+ _return_fields: self.class.attributes,
154
+ _json_depth: 1
155
+ )
156
+ end
157
+
158
+ merge_attributes(data)
159
+ @old = dup
160
+ self
161
+ end
162
+
163
+ def reload
164
+ data = collection.get id
165
+
166
+ clear_lazy
167
+ merge_attributes(data.attributes)
168
+ @old = data
169
+ self
170
+ end
171
+
172
+ def ready?
173
+ state_num == 2
174
+ end
175
+
176
+ def mac_addresses
177
+ network_adapters.map(&:mac_address)
178
+ end
179
+
180
+ def ip_addresses
181
+ network_adapters.map(&:ip_addresses).flatten
182
+ end
183
+
184
+ def public_ip_address
185
+ ip_addresses
186
+ .reject { |a| a =~ /^(169\.254|fe80)/ }
187
+ .first
188
+ end
189
+ end
190
+ end
191
+ end
192
+ end
@@ -0,0 +1,30 @@
1
+ module Fog
2
+ module Compute
3
+ class Hyperv
4
+ class Servers < Fog::Hyperv::Collection
5
+ attribute :cluster
6
+
7
+ model Fog::Compute::Hyperv::Server
8
+
9
+ get_method :get_vm
10
+
11
+ def search_attributes
12
+ attrs = super
13
+ attrs[:computer_name] = cluster.nodes.map { |n| n[:name] } if cluster
14
+ attrs.delete :cluster
15
+ attrs
16
+ end
17
+
18
+ def get(identity, filters = {})
19
+ guid = identity =~ /\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/
20
+
21
+ search = {}
22
+ search[:id] = identity if guid
23
+ search[:name] = identity unless guid
24
+
25
+ super search.merge(filters)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,58 @@
1
+ module Fog
2
+ module Compute
3
+ class Hyperv
4
+ class Switch < Fog::Hyperv::Model
5
+ identity :id
6
+
7
+ attribute :computer_name
8
+ # attribute :default_flow_minimum_bandwidth_absolute
9
+ # attribute :default_flow_minimum_bandwidth_weight
10
+ # attribute :is_deleted
11
+ attribute :name
12
+ attribute :net_adapter_interface_description
13
+ attribute :notes
14
+ attribute :switch_type, type: :enum, values: [ :Private, :Internal, :External ]
15
+
16
+ def save
17
+ requires :name
18
+
19
+ data = if persisted?
20
+ service.set_vm_switch(
21
+ computer_name: computer_name,
22
+ default_flow_minimum_bandwidth_absolute: default_flow_minimum_bandwidth_absolute,
23
+ default_flow_minimum_bandwidth_weight: default_flow_minimum_bandwidth_weight,
24
+ name: name,
25
+ net_adapter_interface_description: net_adapter_interface_description,
26
+ notes: notes,
27
+ switch_type: !net_adapter_interface_description && switch_type,
28
+ passthru: true,
29
+ _return_fields: self.class.attributes,
30
+ _json_depth: 1
31
+ )
32
+ else
33
+ service.new_vm_switch(
34
+ computer_name: computer_name,
35
+ name: name,
36
+ net_adapter_interface_description: net_adapter_interface_description,
37
+ notes: notes,
38
+ switch_type: !net_adapter_interface_description && switch_type,
39
+ _return_fields: self.class.attributes,
40
+ _json_depth: 1
41
+ )
42
+ end
43
+ merge_attributes(data)
44
+ self
45
+ end
46
+
47
+ def reload
48
+ data = collection.get(
49
+ name,
50
+ computer_name: computer_name
51
+ )
52
+ merge_attributes(data.attributes)
53
+ self
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end