Skip to content

Commit

Permalink
Merge pull request #17293 from djberg96/sysprep_templates
Browse files Browse the repository at this point in the history
Add support for sysprep customization templates
  • Loading branch information
gmcculloug authored May 31, 2018
2 parents f197805 + 4155045 commit 8b5c3e7
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ManageIQ::Providers::CloudManager::ProvisionWorkflow < ::MiqProvisionVirtWorkflow
include_concern "DialogFieldValidation"
include CloudInitTemplateMixin
include SysprepTemplateMixin

def allowed_availability_zones(_options = {})
source = load_ar_obj(get_source_vm)
Expand Down Expand Up @@ -59,10 +60,18 @@ def display_name_for_name_description(ci)
ci.description.blank? ? ci.name : "#{ci.name}: #{ci.description}"
end

# Override in provider subclass as needed.
#
def supports_cloud_init?
true
end

# Override in provider subclass as needed.
#
def supports_sysprep?
false
end

def set_or_default_hardware_field_values(_vm)
end

Expand All @@ -76,7 +85,10 @@ def show_customize_fields(fields, _platform)
end

def allowed_customization_templates(options = {})
allowed_cloud_init_customization_templates(options)
allowed = []
allowed.concat(allowed_cloud_init_customization_templates(options)) if supports_cloud_init?
allowed.concat(allowed_sysprep_customization_templates(options)) if supports_sysprep?
allowed
end

private
Expand Down
6 changes: 5 additions & 1 deletion app/models/miq_provision_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ def supports_cloud_init?
false
end

def supports_sysprep?
false
end

def supports_customization_template?
supports_pxe? || supports_iso? || supports_cloud_init?
supports_pxe? || supports_iso? || supports_cloud_init? || supports_sysprep?
end

def continue_request(values)
Expand Down
19 changes: 19 additions & 0 deletions app/models/mixins/sysprep_template_mixin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module SysprepTemplateMixin
def allowed_sysprep_customization_templates(_options = {})
result = []
return result if (source = load_ar_obj(get_source_vm)).blank?
return result unless source.platform.casecmp('windows').zero?

customization_template_id = get_value(@values[:customization_template_id])
@values[:customization_template_script] = nil if customization_template_id.nil?

result = CustomizationTemplateSysprep.in_region(source.region_number).all.collect do |c|
@values[:customization_template_script] = c.script if c.id == customization_template_id
build_ci_hash_struct(c, %i(name description updated_at))
end

result.compact!
@values[:customization_template_script] = nil if result.blank?
result
end
end
2 changes: 1 addition & 1 deletion spec/factories/customization_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
factory :customization_template_sysprep,
:parent => :customization_template,
:class => "CustomizationTemplateSysprep" do
sequence(:name) { |n| "customization_template_syspre_#{seq_padded_for_sorting(n)}" }
sequence(:name) { |n| "customization_template_sysprep_#{seq_padded_for_sorting(n)}" }
sequence(:description) { |n| "Customization Template Sysprep #{seq_padded_for_sorting(n)}" }
after(:build) do |x|
x.pxe_image_type ||= FactoryGirl.create(:pxe_image_type)
Expand Down
7 changes: 7 additions & 0 deletions spec/models/customization_template_sysprep_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe CustomizationTemplateSysprep do
context "#default_filename" do
it "should be unattend.xml" do
expect(described_class.new.default_filename).to eq('unattend.xml')
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,28 @@
let(:ems) { FactoryGirl.create(:ems_cloud) }
let(:network_manager) { FactoryGirl.create(:ems_network, :parent_ems_id => ems.id) }
let(:template) { FactoryGirl.create(:miq_template, :name => "template", :ext_management_system => ems) }

let(:cloud_init_template) { FactoryGirl.create(:customization_template_cloud_init) }
let(:sysprep_template) { FactoryGirl.create(:customization_template_sysprep) }

let(:workflow) do
stub_dialog
allow(User).to receive_messages(:server_timezone => "UTC")
allow_any_instance_of(described_class).to receive(:update_field_visibility)
described_class.new({:src_vm_id => template.id, :customization_template_id => cloud_init_template.id}, admin.userid)
end

let(:sysprep_workflow) do
stub_dialog
allow(User).to receive_messages(:server_timezone => "UTC")
allow_any_instance_of(described_class).to receive(:update_field_visibility)
described_class.new({:src_vm_id => template.id, :customization_template_id => sysprep_template.id}, admin.userid)
end

context "with allowed customization templates" do
it "#allowed_customization_templates" do
expect(workflow.allowed_customization_templates.first).to be_a(MiqHashStruct)
expect(sysprep_workflow.allowed_customization_templates.first).to be_a(MiqHashStruct)
end

it "should retrieve cloud-init templates when cloning" do
Expand All @@ -32,6 +43,24 @@
expect(template_hash[attr]).to eq cloud_init_template.send(attr)
end
end

it "should retrieve sysprep templates when cloning" do
options = {'key' => 'value' }
allow(sysprep_workflow).to receive(:supports_native_clone?).and_return(true)
allow(sysprep_workflow).to receive(:supports_sysprep?).and_return(true)
allow(sysprep_workflow).to receive(:load_ar_obj).and_return(template)
allow(template).to receive(:platform).and_return('windows')

result = sysprep_workflow.allowed_customization_templates(options)
customization_template = sysprep_workflow.instance_variable_get(:@values)[:customization_template_script]
template_hash = result.first.instance_variable_get(:@hash)

expect(customization_template).to eq sysprep_template.script
expect(template_hash).to be_a(Hash)
%i(id name description).each do |attr|
expect(template_hash[attr]).to eq sysprep_template.send(attr)
end
end
end

context "with empty relationships" do
Expand Down Expand Up @@ -198,5 +227,11 @@
expect(workflow.allowed_floating_ip_addresses).to eq(@ip1.id => @ip1.address, @ip2.id => @ip2.address)
end
end

context "#supports_sysprep?" do
it "returns the expected boolean value" do
expect(workflow.supports_sysprep?).to eql(false)
end
end
end
end

0 comments on commit 8b5c3e7

Please sign in to comment.