Skip to content

Commit

Permalink
vmware_preprovision_clone_to_template method refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pkomanek committed Mar 26, 2020
1 parent bbad635 commit e387c8e
Showing 1 changed file with 107 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,115 @@
# 2. Target VC Folder
# 3. Tag Inheritance

set_folder = true
set_notes = true
set_tags = true

# Get provisioning object
prov = $evm.root["miq_provision"]

# Get Provision Type
prov_type = prov.provision_type
$evm.log("info", "Provision Type: <#{prov_type}>")

# Get template
template = prov.vm_template

# Get OS Type from the template platform
product = template.operating_system['product_name'] rescue ''
$evm.log("info", "Source Product: <#{product}>")

if set_notes
###################################
# Set the VM Description and VM Annotations as follows:
# The example would allow user input in provisioning dialog "vm_description"
# to be added to the VM notes
###################################
# Stamp VM with custom description
unless prov.get_option(:vm_description).nil?
vmdescription = prov.get_option(:vm_description)
prov.set_option(:vm_description, vmdescription)
$evm.log("info", "Provisioning object <:vmdescription> updated with <#{vmdescription}>")
end

# Setup VM Annotations
vm_notes = "Owner: #{prov.get_option(:owner_first_name)} #{prov.get_option(:owner_last_name)}"
vm_notes += "\nEmail: #{prov.get_option(:owner_email)}"
vm_notes += "\nSource Template: #{prov.vm_template.name}"
vm_notes += "\nCustom Description: #{vmdescription}" unless vmdescription.nil?
prov.set_vm_notes(vm_notes)
$evm.log("info", "Provisioning object <:vm_notes> updated with <#{vm_notes}>")
end
module ManageIQ
module Automate
module Infrastructure
module VM
module Provisioning
module StateMachines
module Methods
class VmwarePreprovisionCloneToTemplate
def initialize(handle = $evm)
@handle = handle
@folder = true
@notes = true
@tags = true
end

if set_folder
###################################
# Drop the VM in the targeted folder if no folder was chosen in the dialog
# The VC folder must exist for the VM to be placed correctly,
# Otherwise the VM will be placed at the Data Center level.
###################################
datacenter = template.v_owning_datacenter

if prov.get_option(:placement_folder_name).nil?
prov.set_folder(datacenter)
$evm.log("info", "Provisioning object <:placement_folder_name> updated with <#{prov.options[:placement_folder_name].inspect}>")
else
$evm.log("info", "Placing VM in folder: <#{prov.get_option(:placement_folder_name)}>")
end
end
def main
@handle.log("info", "Provision Type: <#{prov_type}>")
@handle.log("info", "Source Product: <#{product}>")

set_notes if @notes
set_folder if @folder
set_tags if @tags
end

private

def set_notes
vmdescription = prov.get_option(:vm_description)

# Setup VM Annotations
vm_notes = "Owner: #{prov.get_option(:owner_first_name)} #{prov.get_option(:owner_last_name)}"
vm_notes += "\nEmail: #{prov.get_option(:owner_email)}"
vm_notes += "\nSource Template: #{prov.vm_template.name}"
vm_notes += "\nCustom Description: #{vmdescription}" unless vmdescription.nil?
prov.set_vm_notes(vm_notes)
@handle.log("info", "Provisioning object <:vm_notes> updated with <#{vm_notes}>")
end

def set_folder
###################################
# Drop the VM in the targeted folder if no folder was chosen in the dialog
# The VC folder must exist for the VM to be placed correctly,
# Otherwise the VM will be placed at the Data Center level.
###################################
datacenter = template.v_owning_datacenter

if prov.get_option(:placement_folder_name).nil?
prov.set_folder(datacenter)
@handle.log("info", "Provisioning object <:placement_folder_name> updated with <#{prov.options[:placement_folder_name].inspect}>")
else
@handle.log("info", "Placing VM in folder: <#{prov.get_option(:placement_folder_name)}>")
end
end

if set_tags
###################################
#
# Inherit parent VM's tags and apply
# them to the published template
#
###################################

# List of tag categories to carry over
tag_categories_to_migrate = %w(environment department location function)

# Assign variables
prov_tags = prov.get_tags
$evm.log("info", "Inspecting Provisioning Tags: <#{prov_tags.inspect}>")
template_tags = template.tags
$evm.log("info", "Inspecting Template Tags: <#{template_tags.inspect}>")

# Loop through each source tag for matching categories
template_tags.each do |cat_tagname|
category, tag_value = cat_tagname.split('/')
$evm.log("info", "Processing Tag Category: <#{category}> Value: <#{tag_value}>")
next unless tag_categories_to_migrate.include?(category)
prov.add_tag(category, tag_value)
$evm.log("info", "Updating Provisioning Tags with Category: <#{category}> Value: <#{tag_value}>")
def set_tags
###################################
#
# Inherit parent VM's tags and apply
# them to the published template
#
###################################

# List of tag categories to carry over
tag_categories_to_migrate = %w[environment department location function]

# Assign variables
prov_tags = prov.get_tags
@handle.log("info", "Inspecting Provisioning Tags: <#{prov_tags.inspect}>")
template_tags = template.tags
@handle.log("info", "Inspecting Template Tags: <#{template_tags.inspect}>")

# Loop through each source tag for matching categories
template_tags.each do |cat_tagname|
category, tag_value = cat_tagname.split('/')
@handle.log("info", "Processing Tag Category: <#{category}> Value: <#{tag_value}>")
next unless tag_categories_to_migrate.include?(category)

prov.add_tag(category, tag_value)
@handle.log("info", "Updating Provisioning Tags with Category: <#{category}> Value: <#{tag_value}>")
end
end

def prov
@prov ||= @handle.root["miq_provision"].tap do |req|
raise 'miq_provision not specified' if req.nil?
end
end

def prov_type
@prov_type ||= prov.provision_type
end

def template
@template ||= prov.vm_template.tap do |req|
raise 'vm_template not specified' if req.nil?
end
end

def product
@product ||= template.operating_system&.product_name&.downcase
end
end
end
end
end
end
end
end
end

ManageIQ::Automate::Infrastructure::VM::Provisioning::StateMachines::Methods::VmwarePreprovisionCloneToTemplate.new.main

0 comments on commit e387c8e

Please sign in to comment.