Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement