Last active
August 15, 2017 17:08
-
-
Save chrisruffalo/e5568bdf2c19e3249a9af2634f15d08e to your computer and use it in GitHub Desktop.
Add a vSphere folder in CFME 4.2+
This file contains hidden or 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
# require rbvmomi, should be installed already because it is used by CFME | |
require 'rbvmomi' | |
# todo: input options/dialog values/etc | |
data_center = "RHC-DC-VMware" | |
folder_path = "test_folder/test_folder/test_folder2" | |
# todo: possibly validate to make sure folder_path doesn't start with '/' and/or if it does remove it | |
# determine full path .. the "vm" is because it is a vm folder | |
data_center_folder_path = "#{data_center}/vm/#{folder_path}" | |
full_folder_path = "data_centers/#{data_center_folder_path}" | |
# get EMS object | |
ems = nil | |
# get ems, could add other sources (event, host, vm, etc) to get or try and get a list of providers if we wanted to | |
if $evm.root['ext_management_system'].present? | |
# get ems from root | |
ems = $evm.root['ext_management_system'] | |
else | |
# error out | |
return MIQ_STOP | |
end | |
# log found EMS | |
$evm.log("info", "Found EMS: #{ems.name}") | |
# create folder by default | |
create_folder = true | |
# get folders from ems, there may be an easier way to find/index the existing folders but this should be fast enough | |
ems.ems_folders.each do |folder| | |
# if a folder path is present then analyze against chosen path | |
existing_folder_path = folder.folder_path | |
if existing_folder_path.present? | |
$evm.log("info", "Found existing data_center/folder path: '#{existing_folder_path}'") | |
# if the folder path that was chosen | |
# equals the existing folder path | |
# then break and signal that we don't | |
# need to create the folder | |
if full_folder_path == existing_folder_path | |
create_folder = false | |
break | |
end # folder path is equal to existing path | |
end # folder path is not nil | |
end # for-each folder path | |
# create folder logic | |
if create_folder | |
# connect to vCenter | |
$evm.log("info", "Connecting to vCenter: '#{ems.hostname}' with user '#{ems.authentication_userid}'") | |
vim = RbVmomi::VIM.connect(host: ems.hostname, user: ems.authentication_userid, password: ems.authentication_password, insecure: true) | |
# traverse to/create folder path | |
$evm.log("info", "Creating path: '#{folder_path}' in DC '#{data_center}'") | |
# using this we traverse into the DC's and get one from the data center option | |
dc = vim.serviceInstance.content.rootFolder.traverse(data_center, RbVmomi::VIM::Datacenter) | |
unless dc.present? | |
# close connection | |
vim.close | |
# log error and abort | |
$evm.log("error", "No data_center named '#{data_center}' found in EMS #{ems.name} (or on host #{ems.hostname}, abort") | |
return MIQ_ABORT | |
end | |
# use the traverse! method which auto-creates resources that aren't there with the object type it should create (folder) | |
dc.vmFolder.traverse!(folder_path, RbVmomi::VIM::Folder) | |
# close connection | |
$evm.log("info", "Closed vCenter connection: '#{ems.hostname}'") | |
vim.close | |
else | |
$evm.log("info", "Folder '#{data_center_folder_path}' already exists") | |
end |
Another style comment, typically in ruby we don't use camelCase, such as with variables createFolder
, dataCenterFolderPath
, etc. They are done as snake case, so create_folder
and data_center_folder_path
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if !$evm.root['ext_management_system'].nil?
can also be expressed using a Rails helper method called.present?
withif $evm.root['ext_management_system].present?
..present?
will check for 0 length strings/arrays/hashes and also nil values. It has a counterpart, called.blank?
. In general,.blank?
and.present?
are preferred over.nil?
and!.nil?