Skip to content

Commit

Permalink
Merge pull request #614 from agrare/collect_customization_specs
Browse files Browse the repository at this point in the history
Collect customization_specs
  • Loading branch information
Fryguy authored Sep 17, 2020
2 parents 41ebcfe + f6ecfb0 commit 333b9d0
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,12 @@ def parse_updates(vim, parser, updated_objects)
parser.parse_ext_management_system(ems, vim.serviceContent.about)

updated_objects.each do |managed_object, update_kind, cached_props|
props = cached_props

uncached_props = retrieve_uncached_props(managed_object)
props = uncached_props.present? ? cached_props.deep_merge(uncached_props) : cached_props
props = props.deep_merge(uncached_props) if uncached_props.present?

retrieve_extra_props(managed_object, props)

parser.parse(managed_object, update_kind, props)
rescue => err
Expand Down Expand Up @@ -265,6 +269,22 @@ def uncached_prop_set(obj)
@uncached_prop_set[obj.class.wsdl_name]
end

def retrieve_extra_props(obj, cached_props)
case obj.class.wsdl_name
when "CustomizationSpecManager"
retrieve_customization_spec(obj, cached_props)
end
end

def retrieve_customization_spec(spec_manager, cached_props)
cached_props[:info].to_a.each do |spec_info|
spec_info.props[:spec] = spec_manager.GetCustomizationSpec(:name => spec_info.name)&.spec
rescue RbVmomi::Fault => err
# Don't fail the refresh for issues with specific items
_log.warn("Failed to get customization spec for [#{spec_info.name}]: #{err}")
end
end

def parse_content_libraries(parser)
require 'vsphere-automation-content'
require 'vsphere-automation-cis'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ def destroy_property_filter(property_filter)
def ems_inventory_filter_spec(vim)
RbVmomi::VIM.PropertyFilterSpec(
:objectSet => [
extension_manager_object_spec(vim.serviceContent.extensionManager),
root_folder_object_spec(vim.serviceContent.rootFolder),
license_manager_object_spec(vim.serviceContent.licenseManager),
RbVmomi::VIM.ObjectSpec(:obj => vim.serviceContent.customizationSpecManager),
RbVmomi::VIM.ObjectSpec(:obj => vim.serviceContent.extensionManager),
RbVmomi::VIM.ObjectSpec(:obj => vim.serviceContent.rootFolder, :selectSet => root_folder_select_set),
RbVmomi::VIM.ObjectSpec(:obj => vim.serviceContent.licenseManager),
],
:propSet => ems_inventory_prop_set
)
end

def extension_manager_object_spec(extension_manager)
RbVmomi::VIM.ObjectSpec(:obj => extension_manager)
end

def ems_inventory_prop_set
property_set_from_file("ems_inventory")
end
Expand All @@ -41,18 +38,10 @@ def property_set_from_file(file_name)
end
end

def root_folder_object_spec(root)
RbVmomi::VIM.ObjectSpec(:obj => root, :selectSet => root_folder_select_set)
end

def root_folder_select_set
traversal_spec_from_file("root_folder")
end

def license_manager_object_spec(license_manager)
RbVmomi::VIM.ObjectSpec(:obj => license_manager)
end

def traversal_spec_from_file(file_name)
engine_root = ManageIQ::Providers::Vmware::Engine.root
hash = YAML.load_file(engine_root.join("config", "traversal_specs", "#{file_name}.yml"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ def parse_compute_resource(object, kind, props)
end
alias parse_cluster_compute_resource parse_compute_resource

def parse_customization_spec_manager(_object, kind, props)
return if kind == "leave"

props[:info].to_a.each do |spec_info|
persister.customization_specs.build(
:name => spec_info[:name],
:typ => spec_info[:type],
:description => spec_info[:description],
:last_update_time => spec_info[:last_update_time].to_s,
:spec => rbvmomi_to_basic_types(spec_info[:spec]).deep_stringify_keys
)
end
end

def parse_datacenter(object, kind, props)
persister.ems_folders.targeted_scope << object._ref
return if kind == "leave"
Expand Down Expand Up @@ -391,4 +405,15 @@ def lazy_find_managed_object(managed_object)
parent_collection = persister.vim_class_to_collection(managed_object)
parent_collection.lazy_find(managed_object._ref)
end

def rbvmomi_to_basic_types(obj)
case obj
when RbVmomi::BasicTypes::DataObject
obj.props.transform_values { |v| rbvmomi_to_basic_types(v) }
when Array
obj.map { |v| rbvmomi_to_basic_types(v) }
else
obj
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ def build_customization_spec

# If an existing VC customization spec was selected connect to VC and get the spec
custom_spec_name = get_option(:sysprep_custom_spec).to_s.strip
custom_spec_name = nil if custom_spec_name == "__VC__NONE__"

sysprep_spec_override = get_option(:sysprep_spec_override)
spec = load_customization_spec(custom_spec_name)
spec = spec.spec unless spec.nil?
spec = load_customization_spec(custom_spec_name) if custom_spec_name

_log.info "Loaded custom spec [#{custom_spec_name}]. Override flag: [#{sysprep_spec_override}]"
if spec && sysprep_spec_override == false
adjust_nicSettingMap(spec)
Expand Down Expand Up @@ -197,18 +199,20 @@ def validate_customization_spec(custom_spec)
end

def load_customization_spec(custom_spec_name)
custom_spec_name = nil if custom_spec_name == "__VC__NONE__"
unless custom_spec_name.blank?
_log.info "Using customization spec [#{custom_spec_name}]"
cs = source.ext_management_system.customization_specs.find_by_id(custom_spec_name)
cs = source.ext_management_system.customization_specs.find_by(:name => custom_spec_name) if cs.nil?
raise MiqException::MiqProvisionError, "Customization Specification [#{custom_spec_name}] does not exist." if cs.nil?
raise MiqException::MiqProvisionError, "Customization Specification [#{custom_spec_name}] for OS type [#{cs[:typ]}] does not match the template VM OS" if cs[:typ].downcase != source.platform
_log.info "Using customization spec [#{cs.name}]"
return cs
else
_log.info "Customization spec name is empty, no spec will be loaded."
return nil
_log.info("Using customization spec [#{custom_spec_name}]")
cs = source.ext_management_system.customization_specs.find_by(:id => custom_spec_name)
cs = source.ext_management_system.customization_specs.find_by(:name => custom_spec_name) if cs.nil?

raise MiqException::MiqProvisionError, "Customization Specification [#{custom_spec_name}] does not exist." if cs.nil?
raise MiqException::MiqProvisionError, "Customization Specification [#{custom_spec_name}] for OS type [#{cs[:typ]}] does not match the template VM OS" if cs[:typ].downcase != source.platform

_log.info("Using customization spec [#{cs.name}]")

source.ext_management_system.with_provider_connection do |vim|
vim.getVimCustomizationSpecManager&.getCustomizationSpec(cs.name)&.spec
rescue Handsoap::Fault => err
_log.warn("Failed to load customization spec [#{cs.name}]: #{err}")
nil
end
end

Expand Down
2 changes: 2 additions & 0 deletions config/property_specs/ems_inventory.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,5 @@
- licenses
:ExtensionManager:
- extensionList
:CustomizationSpecManager:
- info
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@

context 'build_customization_spec for windows template' do
it 'loads existing spec' do
expect(prov_vm).to receive(:load_customization_spec).and_return(spec)
expect(prov_vm.build_customization_spec).to(eq(spec))
end

Expand All @@ -108,14 +109,17 @@
spec_for_compare.identity.userData.orgName = options[:sysprep_organization]
options[:sysprep_full_name] = 'sysprep_full_name_value'
spec_for_compare.identity.userData.fullName = options[:sysprep_full_name]
spec_for_compare.identity.identification = {}
spec_for_compare.identity.identification = VimHash.new

expect(prov_vm).to receive(:load_customization_spec).and_return(spec)
expect(prov_vm.build_customization_spec).to(eq(spec_for_compare))
end

it 'creates a new spec' do
custom_spec.spec = nil
options[:sysprep_full_name] = 'sysprep_full_name_value'
options[:sysprep_organization] = 'sysprep_organization_value'
expect(prov_vm).to receive(:load_customization_spec).and_return(nil)
expect(prov_vm.build_customization_spec).to(eq(new_spec))
end
end
Expand Down
Loading

0 comments on commit 333b9d0

Please sign in to comment.