From 6c9058abb5b8a8f6155208d11ae83d9ed88c0cb4 Mon Sep 17 00:00:00 2001 From: Lucy Fu Date: Wed, 7 Dec 2016 13:19:41 -0500 Subject: [PATCH] Check tags, Pxe server and cloud init customization templates in region during provisioning. Only check for resources in the same region where the selected template locates. https://bugzilla.redhat.com/show_bug.cgi?id=1400991 --- app/models/miq_provision_virt_workflow.rb | 10 ++++++++++ app/models/miq_request_workflow.rb | 4 ++++ app/models/mixins/cloud_init_template_mixin.rb | 5 ++++- .../infra_manager/provision_workflow_spec.rb | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/app/models/miq_provision_virt_workflow.rb b/app/models/miq_provision_virt_workflow.rb index a2ec9ce3fb4..6ddf70796e2 100644 --- a/app/models/miq_provision_virt_workflow.rb +++ b/app/models/miq_provision_virt_workflow.rb @@ -357,6 +357,16 @@ def allowed_snapshots(_options = {}) result end + def allowed_tags(options = {}) + return {} if (source = load_ar_obj(get_source_vm)).blank? + super(options.merge(:region_number => source.region_number)) + end + + def allowed_pxe_servers(_options = {}) + return {} if (source = load_ar_obj(get_source_vm)).blank? + PxeServer.in_region(source.region_number).each_with_object({}) { |p, h| h[p.id] = p.name } + end + def get_source_vm get_source_and_targets[:vm] end diff --git a/app/models/miq_request_workflow.rb b/app/models/miq_request_workflow.rb index 2a426a2305e..a1aa284a1e9 100644 --- a/app/models/miq_request_workflow.rb +++ b/app/models/miq_request_workflow.rb @@ -544,6 +544,8 @@ def get_tags def allowed_tags(options = {}) return @tags unless @tags.nil? + region_number = options.delete(:region_number) + # TODO: Call allowed_tags properly from controller - it is currently hard-coded with no options passed field_options = @dialogs.fetch_path(:dialogs, :purpose, :fields, :vm_tags, :options) options = field_options unless field_options.nil? @@ -557,6 +559,7 @@ def allowed_tags(options = {}) single_select = options[:single_select].blank? ? [] : options[:single_select].collect(&:to_s) cats = Classification.visible.writeable.managed + cats = cats.in_region(region_number) if region_number cats.each do |t| next if exclude_list.include?(t.name) next unless include_list.blank? || include_list.include?(t.name) @@ -566,6 +569,7 @@ def allowed_tags(options = {}) end ents = Classification.visible.writeable.parent_ids(@tags.keys).with_tag_name + ents = ents.in_region(region_number) if region_number ents.each do |t| full_tag_name = "#{@tags[t.parent_id][:name]}/#{t.name}" next if exclude_list.include?(full_tag_name) diff --git a/app/models/mixins/cloud_init_template_mixin.rb b/app/models/mixins/cloud_init_template_mixin.rb index 5622d2ee1a1..c265b874712 100644 --- a/app/models/mixins/cloud_init_template_mixin.rb +++ b/app/models/mixins/cloud_init_template_mixin.rb @@ -1,9 +1,12 @@ module CloudInitTemplateMixin def allowed_cloud_init_customization_templates(_options = {}) result = [] + return result if (source = load_ar_obj(get_source_vm)).blank? + customization_template_id = get_value(@values[:customization_template_id]) @values[:customization_template_script] = nil if customization_template_id.nil? - result = CustomizationTemplateCloudInit.all.collect do |c| + + result = CustomizationTemplateCloudInit.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, [:name, :description, :updated_at]) end diff --git a/spec/models/manageiq/providers/redhat/infra_manager/provision_workflow_spec.rb b/spec/models/manageiq/providers/redhat/infra_manager/provision_workflow_spec.rb index 10eb36c9338..079c2644c33 100644 --- a/spec/models/manageiq/providers/redhat/infra_manager/provision_workflow_spec.rb +++ b/spec/models/manageiq/providers/redhat/infra_manager/provision_workflow_spec.rb @@ -128,6 +128,24 @@ def allowed_customization_templates(options) expect(workflow).to receive(:super_allowed_customization_templates).with(options) workflow.allowed_customization_templates(options) end + + it "should retrieve templates in region" do + template = FactoryGirl.create(:customization_template_cloud_init, :name => "test1") + + my_region_number = template.my_region_number + other_region_id = (my_region_number + 1) * template.class.rails_sequence_factor + 1 + pxe_image_type = FactoryGirl.create(:pxe_image_type, :name => "test_image", :id => other_region_id) + FactoryGirl.create(:customization_template_cloud_init, + :name => "test2", + :id => other_region_id, + :pxe_image_type => pxe_image_type) + + expect(workflow).to receive(:supports_native_clone?).and_return(true) + result = workflow.allowed_customization_templates + expect(result.size).to eq(1) + expect(result.first.id).to eq(template.id) + expect(result.first.name).to eq(template.name) + end end describe "#make_request" do