Skip to content

Commit

Permalink
Fetch the customization spec when provisioning
Browse files Browse the repository at this point in the history
  • Loading branch information
agrare committed Aug 26, 2020
1 parent 3055c37 commit 8f061a8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def parse_customization_spec_manager(_object, kind, props)
:typ => spec_info[:type],
:description => spec_info[:description],
:last_update_time => spec_info[:last_update_time].to_s,
:spec => rbvmomi_to_vim_types(spec_info[:spec])
:spec => rbvmomi_to_basic_types(spec_info[:spec]).deep_stringify_keys
)
end
end
Expand Down Expand Up @@ -392,12 +392,10 @@ def lazy_find_managed_object(managed_object)
parent_collection.lazy_find(managed_object._ref)
end

def rbvmomi_to_vim_types(obj)
def rbvmomi_to_basic_types(obj)
case obj
when RbVmomi::BasicTypes::DataObject
VimHash.new(obj.class.wsdl_name) do |new_obj|
obj.props.each { |k, v| new_obj[k.to_s] = rbvmomi_to_vim_types(v) }
end
obj.props.transform_values { |v| rbvmomi_to_basic_types(v) }
when Array
obj.map { |v| rbvmomi_to_basic_types(v) }
else
Expand Down
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,18 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@
end

context 'build_customization_spec for windows template' do
before do

end
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 +112,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

0 comments on commit 8f061a8

Please sign in to comment.