Skip to content

Commit

Permalink
Refs fog#197 - Encapsulate create_vm command
Browse files Browse the repository at this point in the history
  • Loading branch information
ezr-ondrej committed Mar 24, 2019
1 parent 86e938d commit 8804b70
Show file tree
Hide file tree
Showing 14 changed files with 492 additions and 396 deletions.
24 changes: 4 additions & 20 deletions lib/fog/vsphere/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -702,27 +702,11 @@ def validate_ssl_connection
end

def list_container_view(datacenter_obj_or_name, type, container_object = nil)
dc = if datacenter_obj_or_name.is_a?(String)
find_raw_datacenter(datacenter_obj_or_name)
else
datacenter_obj_or_name
end

container = if container_object
dc.public_send(container_object)
else
dc
end

container_view = connection.serviceContent.viewManager.CreateContainerView(
container: dc,
type: [type],
recursive: true
)
shared_request.send('list_container_view', datacenter_obj_or_name, type, container_object)
end

result = container_view.view
container_view.DestroyView
result
def shared_request
@shared_request ||= Fog::Vsphere::Requests::Compute::Request.new(connection)
end
end
end
Expand Down
562 changes: 286 additions & 276 deletions lib/fog/vsphere/requests/compute/create_vm.rb

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions lib/fog/vsphere/requests/compute/get_cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ def get_cluster(name, datacenter_name)
protected

def get_raw_cluster(name, datacenter_name_or_obj)
dc = if datacenter_name_or_obj.is_a?(String)
find_raw_datacenter(datacenter_name_or_obj)
else
datacenter_name_or_obj
end
dc.find_compute_resource(name)
shared_request.get_raw_cluster(name, datacenter_name_or_obj)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/fog/vsphere/requests/compute/get_datacenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def get_datacenter(name)
protected

def find_raw_datacenter(name)
raw_datacenters.find { |d| d.name == name } || get_raw_datacenter(name)
shared_request.find_raw_datacenter(name)
end

# @note RbVmomi takes path instead of name as argument to find datacenter
Expand Down
6 changes: 1 addition & 5 deletions lib/fog/vsphere/requests/compute/get_datastore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ def get_datastore(name, datacenter_name)
protected

def get_raw_datastore(name, datacenter_name)
get_raw_datastores(datacenter_name).detect { |ds| ds.name == name }
end

def get_raw_datastores(datacenter_name)
list_container_view(datacenter_name, 'Datastore', :datastoreFolder)
shared_request.get_raw_datastore(name, datacenter_name)
end
end

Expand Down
33 changes: 2 additions & 31 deletions lib/fog/vsphere/requests/compute/get_folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,11 @@ def get_folder(path, datacenter_name, type = nil)
protected

def get_raw_folder(path, datacenter_name_or_obj, type)
# The required path syntax - 'topfolder/subfolder

# Clean up path to be relative since we're providing datacenter name
dc = if datacenter_name_or_obj.is_a?(String)
find_raw_datacenter(datacenter_name_or_obj)
else
datacenter_name_or_obj
end

valid_types = %w[vm network datastore host]
raise ArgumentError, "#{type} is unknown" if type.nil? || type.empty?
raise "Invalid type (#{type}). Must be one of #{valid_types.join(', ')} " unless valid_types.include?(type.to_s)
meth = "#{type}Folder"
dc_root_folder = dc.send(meth)

# Filter the root path for this datacenter not to be used."
dc_root_folder_path = dc_root_folder.path.map { |_, name| name }.join('/')
paths = path.sub(/^\/?#{Regexp.quote(dc_root_folder_path)}\/?/, '').split('/')

return dc_root_folder if paths.empty?
# Walk the tree resetting the folder pointer as we go
paths.reduce(dc_root_folder) do |last_returned_folder, sub_folder|
# JJM VIM::Folder#find appears to be quite efficient as it uses the
# searchIndex It certainly appears to be faster than
# VIM::Folder#inventory since that returns _all_ managed objects of
# a certain type _and_ their properties.
sub = last_returned_folder.find(sub_folder, RbVmomi::VIM::Folder)
raise Fog::Vsphere::Compute::NotFound, "Could not descend into #{sub_folder}. Please check your path. #{path}" unless sub
sub
end
shared_request.get_raw_folder(path, datacenter_name_or_obj, type)
end

def get_raw_vmfolder(path, datacenter_name)
get_raw_folder(path, datacenter_name, 'vm')
shared_request.get_raw_folder(path, datacenter_name, 'vm')
end

def folder_attributes(folder, datacenter_name)
Expand Down
4 changes: 1 addition & 3 deletions lib/fog/vsphere/requests/compute/get_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ def get_host(name, cluster_name, datacenter_name)
protected

def get_raw_host(name, cluster_name, datacenter_name)
cluster = get_raw_cluster(cluster_name, datacenter_name)
cluster.host.find { |host| host.name == name } ||
raise(Fog::Vsphere::Compute::NotFound, "no such host #{name}")
shared_request.get_raw_host(name, cluster_name, datacenter_name)
end
end
end
Expand Down
28 changes: 1 addition & 27 deletions lib/fog/vsphere/requests/compute/get_network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,7 @@ def get_network(name, datacenter_name)
protected

def get_raw_network(name, datacenter_name, distributedswitch = nil)
finder = choose_finder(name, distributedswitch)
get_all_raw_networks(datacenter_name).find { |n| finder.call(n) }
end
end

module Shared
protected

def get_all_raw_networks(datacenter_name)
list_container_view(datacenter_name, 'Network', :networkFolder)
end

def choose_finder(name, distributedswitch)
case distributedswitch
when String
# only the one will do
proc do |n|
n.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) && (n.name == name || n.key == name) &&
(n.config.distributedVirtualSwitch.name == distributedswitch)
end
when :dvs
# the first distributed virtual switch will do - selected by network - gives control to vsphere
proc { |n| n.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) && (n.name == name || n.key == name) }
else
# the first matching network will do, seems like the non-distributed networks come first
proc { |n| (n.name == name || (n.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) && n.key == name)) }
end
shared_request.get_raw_network(name, datacenter_name, distributedswitch)
end
end

Expand Down
4 changes: 1 addition & 3 deletions lib/fog/vsphere/requests/compute/get_resource_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ def get_resource_pool(name, cluster_name, datacenter_name)
protected

def get_raw_resource_pool(name, cluster_name, datacenter_name)
dc = find_raw_datacenter(datacenter_name)
cluster = dc.find_compute_resource(cluster_name)
name.nil? ? cluster.resourcePool : cluster.resourcePool.traverse(name)
shared_request.get_raw_resource_pool(name, cluster_name, datacenter_name)
end
end

Expand Down
6 changes: 1 addition & 5 deletions lib/fog/vsphere/requests/compute/get_storage_pod.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ def get_storage_pod(name, datacenter_name)
protected

def get_raw_storage_pod(name, datacenter_name)
raw_storage_pods(datacenter_name).detect { |pod| pod.name == name }
end

def raw_storage_pods(datacenter_name)
list_container_view(datacenter_name, 'StoragePod')
shared_request.get_raw_storage_pod(name, datacenter_name)
end
end

Expand Down
13 changes: 1 addition & 12 deletions lib/fog/vsphere/requests/compute/list_datacenters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,7 @@ def raw_getpathmo(mo)
end

def raw_datacenters(folder = nil)
folder ||= connection.rootFolder
@raw_datacenters ||= get_raw_datacenters_from_folder folder
end

def get_raw_datacenters_from_folder(folder = nil)
folder.childEntity.map do |childE|
if childE.is_a? RbVmomi::VIM::Datacenter
childE
elsif childE.is_a? RbVmomi::VIM::Folder
get_raw_datacenters_from_folder childE
end
end.flatten
shared_request.raw_datacenters(folder)
end

def find_datacenters(name = nil)
Expand Down
166 changes: 166 additions & 0 deletions lib/fog/vsphere/requests/compute/request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
require 'fog/vsphere/requests/request'

module Fog
module Vsphere
module Requests
module Compute
class Request < Fog::Vsphere::Requests::Request

def raw_datacenters(folder = nil)
folder ||= connection.rootFolder
@raw_datacenters ||= get_raw_datacenters_from_folder folder
end

def find_raw_datacenter(name)
raw_datacenters.find { |d| d.name == name } || get_raw_datacenter(name)
end

def get_raw_cluster(name, datacenter_name_or_obj)
dc = if datacenter_name_or_obj.is_a?(String)
find_raw_datacenter(datacenter_name_or_obj)
else
datacenter_name_or_obj
end
dc.find_compute_resource(name)
end

def get_raw_host(name, cluster_name, datacenter_name)
cluster = get_raw_cluster(cluster_name, datacenter_name)
cluster.host.find { |host| host.name == name } ||
raise(Fog::Vsphere::Compute::NotFound, "no such host #{name}")
end

def get_raw_resource_pool(name, cluster_name, datacenter_name)
dc = find_raw_datacenter(datacenter_name)
cluster = dc.find_compute_resource(cluster_name)
name.nil? ? cluster.resourcePool : cluster.resourcePool.traverse(name)
end

# Folders

def get_raw_folder(path, datacenter_name_or_obj, type)
# The required path syntax - 'topfolder/subfolder

# Clean up path to be relative since we're providing datacenter name
dc = if datacenter_name_or_obj.is_a?(String)
find_raw_datacenter(datacenter_name_or_obj)
else
datacenter_name_or_obj
end

valid_types = %w[vm network datastore host]
raise ArgumentError, "#{type} is unknown" if type.nil? || type.empty?
raise "Invalid type (#{type}). Must be one of #{valid_types.join(', ')} " unless valid_types.include?(type.to_s)
meth = "#{type}Folder"
dc_root_folder = dc.send(meth)

# Filter the root path for this datacenter not to be used."
dc_root_folder_path = dc_root_folder.path.map { |_, name| name }.join('/')
paths = path.sub(/^\/?#{Regexp.quote(dc_root_folder_path)}\/?/, '').split('/')

return dc_root_folder if paths.empty?
# Walk the tree resetting the folder pointer as we go
paths.reduce(dc_root_folder) do |last_returned_folder, sub_folder|
# JJM VIM::Folder#find appears to be quite efficient as it uses the
# searchIndex It certainly appears to be faster than
# VIM::Folder#inventory since that returns _all_ managed objects of
# a certain type _and_ their properties.
sub = last_returned_folder.find(sub_folder, RbVmomi::VIM::Folder)
raise Fog::Vsphere::Compute::NotFound, "Could not descend into #{sub_folder}. Please check your path. #{path}" unless sub
sub
end
end

def get_raw_vmfolder(path, datacenter_name)
get_raw_folder(path, datacenter_name, 'vm')
end

# Datastores

def get_raw_datastore(name, datacenter_name)
get_raw_datastores(datacenter_name).detect { |ds| ds.name == name }
end

def get_raw_datastores(datacenter_name)
list_container_view(datacenter_name, 'Datastore', :datastoreFolder)
end

# Storage pods

def raw_storage_pods(datacenter_name)
list_container_view(datacenter_name, 'StoragePod')
end

def get_raw_storage_pod(name, datacenter_name)
raw_storage_pods(datacenter_name).detect { |pod| pod.name == name }
end

# Networks

def get_raw_network(name, datacenter_name, distributedswitch = nil)
shared_request.get_raw_network(name, datacenter_name, distributedswitch)
finder = choose_finder(name, distributedswitch)
get_all_raw_networks(datacenter_name).find { |n| finder.call(n) }
end

def get_all_raw_networks(datacenter_name)
list_container_view(datacenter_name, 'Network', :networkFolder)
end

def choose_network_finder(name, distributedswitch)
case distributedswitch
when String
# only the one will do
proc do |n|
n.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) && (n.name == name || n.key == name) &&
(n.config.distributedVirtualSwitch.name == distributedswitch)
end
when :dvs
# the first distributed virtual switch will do - selected by network - gives control to vsphere
proc { |n| n.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) && (n.name == name || n.key == name) }
else
# the first matching network will do, seems like the non-distributed networks come first
proc { |n| (n.name == name || (n.is_a?(RbVmomi::VIM::DistributedVirtualPortgroup) && n.key == name)) }
end
end

private

def get_raw_datacenters_from_folder(folder = nil)
folder.childEntity.map do |childE|
if childE.is_a? RbVmomi::VIM::Datacenter
childE
elsif childE.is_a? RbVmomi::VIM::Folder
get_raw_datacenters_from_folder childE
end
end.flatten
end

def list_container_view(datacenter_obj_or_name, type, container_object = nil)
dc = if datacenter_obj_or_name.is_a?(String)
find_raw_datacenter(datacenter_obj_or_name)
else
datacenter_obj_or_name
end

container = if container_object
dc.public_send(container_object)
else
dc
end

container_view = connection.serviceContent.viewManager.CreateContainerView(
container: dc,
type: [type],
recursive: true
)

result = container_view.view
container_view.DestroyView
result
end
end
end
end
end
end
19 changes: 19 additions & 0 deletions lib/fog/vsphere/requests/request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Fog
module Vsphere
module Requests
class Request

attr_accessor :connection

def initialize(connection)
@connection = connection
end

def run
raise Fog::Errors::NotImplemented, 'The commands #{class.name}#run method is not implemented.'
end

end
end
end
end
Loading

0 comments on commit 8804b70

Please sign in to comment.