forked from fog/fog-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs fog#197 - Encapsulate create_vm command
- Loading branch information
1 parent
86e938d
commit 8804b70
Showing
14 changed files
with
492 additions
and
396 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.