Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. # require rbvmomi, should be installed already because it is used by CFME
  2. require 'rbvmomi'
  3.  
  4. # todo: input options/dialog values/etc
  5. data_center = "RHC-DC-VMware"
  6. folder_path = "test_folder/test_folder/test_folder2"
  7.  
  8. # todo: possibly validate to make sure folder_path doesn't start with '/' and/or if it does remove it
  9.  
  10. # determine full path .. the "vm" is because it is a vm folder
  11. data_center_folder_path = "#{data_center}/vm/#{folder_path}"
  12. full_folder_path = "data_centers/#{data_center_folder_path}"
  13.  
  14. # get EMS object
  15. ems = nil
  16.  
  17. # get ems, could add other sources (event, host, vm, etc) to get or try and get a list of providers if we wanted to
  18. if $evm.root['ext_management_system'].present?
  19. # get ems from root
  20. ems = $evm.root['ext_management_system']
  21. else
  22. # error out
  23. return MIQ_STOP
  24. end
  25.  
  26. # log found EMS
  27. $evm.log("info", "Found EMS: #{ems.name}")
  28.  
  29. # create folder by default
  30. create_folder = true
  31. # get folders from ems, there may be an easier way to find/index the existing folders but this should be fast enough
  32. ems.ems_folders.each do |folder|
  33. # if a folder path is present then analyze against chosen path
  34. existing_folder_path = folder.folder_path
  35. if existing_folder_path.present?
  36. $evm.log("info", "Found existing data_center/folder path: '#{existing_folder_path}'")
  37. # if the folder path that was chosen
  38. # equals the existing folder path
  39. # then break and signal that we don't
  40. # need to create the folder
  41. if full_folder_path == existing_folder_path
  42. create_folder = false
  43. break
  44. end # folder path is equal to existing path
  45. end # folder path is not nil
  46. end # for-each folder path
  47.  
  48. # create folder logic
  49. if create_folder
  50. # connect to vCenter
  51. $evm.log("info", "Connecting to vCenter: '#{ems.hostname}' with user '#{ems.authentication_userid}'")
  52. vim = RbVmomi::VIM.connect(host: ems.hostname, user: ems.authentication_userid, password: ems.authentication_password, insecure: true)
  53.  
  54. # traverse to/create folder path
  55. $evm.log("info", "Creating path: '#{folder_path}' in DC '#{data_center}'")
  56.  
  57. # using this we traverse into the DC's and get one from the data center option
  58. dc = vim.serviceInstance.content.rootFolder.traverse(data_center, RbVmomi::VIM::Datacenter)
  59. unless dc.present?
  60. # close connection
  61. vim.close
  62. # log error and abort
  63. $evm.log("error", "No data_center named '#{data_center}' found in EMS #{ems.name} (or on host #{ems.hostname}, abort")
  64. return MIQ_ABORT
  65. end
  66.  
  67. # use the traverse! method which auto-creates resources that aren't there with the object type it should create (folder)
  68. dc.vmFolder.traverse!(folder_path, RbVmomi::VIM::Folder)
  69.  
  70. # close connection
  71. $evm.log("info", "Closed vCenter connection: '#{ems.hostname}'")
  72. vim.close
  73. else
  74. $evm.log("info", "Folder '#{data_center_folder_path}' already exists")
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement