Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom_button and custom_button_set copy to service_template copy #18494

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions app/models/service_template/copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@ def template_copy(new_name = "Copy of " + name + Time.zone.now.to_s)
ActiveRecord::Base.transaction do
dup.tap do |template|
template.update_attributes(:name => new_name, :display => false)
service_resources.each do |sr|
resource = sr.resource.respond_to?(:service_template_resource_copy) ? sr.resource.service_template_resource_copy : sr.resource
template.add_resource(resource, sr)
end
service_resources.each { |service_resource| resource_copy(service_resource, template) }
custom_buttons.each { |custom_button| custom_button_copy(custom_button, template) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the bug ... custom_buttons are not custom buttons assigned to this service template.

If you want that, you need to use direct_custom_buttons.
custom_buttons gives you those, and every single custom button for any ServiceTemplate .. and you don't want to copy those.

(that should fix #18954)

custom_button_sets.each { |custom_button_set| custom_button_set_copy(custom_button_set, template) }
end.save!
end
end
end

def resource_copy(service_resource, template)
resource = service_resource.resource.respond_to?(:service_template_resource_copy) ? service_resource.resource.service_template_resource_copy : service_resource.resource
template.add_resource(resource, service_resource)
end

def custom_button_copy(custom_button, template)
custom_button.copy(:applies_to => template)
end

def custom_button_set_copy(custom_button_set, template)
custom_button_set.deep_copy(:owner => template)
end
end
138 changes: 85 additions & 53 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,58 +269,82 @@
end

context "#template_copy" do
let(:service_template_ansible_tower) { FactoryBot.create(:service_template_ansible_tower, :name => "thing") }
let(:service_template_orchestration) { FactoryBot.create(:service_template_orchestration, :name => "thing2") }
let(:service_template_ansible_tower) { FactoryBot.create(:service_template_ansible_tower, :name => "new_template") }
let(:service_template_orchestration) { FactoryBot.create(:service_template_orchestration, :name => "new_template2") }
let(:custom_button) { FactoryBot.create(:custom_button, :applies_to_class => "Service") }
let(:custom_button_set) { FactoryBot.create(:custom_button_set, :owner => @st1) }
before do
@st1 = FactoryBot.create(:service_template)
end

context "with given name" do
it "without resource " do
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy("drew")
@st1.template_copy("new_template")
expect(ServiceTemplate.count).to eq(2)
expect(ServiceTemplate.find_by(:name => "drew")).not_to be(nil)
expect(ServiceTemplate.find_by(:name => "drew").display).to be(false)
expect(ServiceTemplate.find_by(:name => "drew").guid).not_to eq(@st1.guid)
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(new_service_template.display).to be(false)
expect(new_service_template.guid).not_to eq(@st1.guid)
end

it "with custom button" do
custom_button
expect(@st1.custom_buttons.count).to eq(1)
@st1.template_copy("new_template")
expect(ServiceTemplate.count).to eq(2)
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(new_service_template.display).to be(false)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.custom_buttons.count).to eq(2)
end

it "with custom button set" do
custom_button_set.add_member(custom_button)
expect(@st1.custom_button_sets.count).to eq(1)
@st1.template_copy("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(new_service_template.display).to be(false)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.custom_button_sets.count).to eq(1)
end

it "with non-copyable resource (configuration script base)" do
@st1.add_resource(FactoryBot.create(:configuration_script_base))
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy("thing")
@st1.template_copy("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(ServiceTemplate.find_by(:name => "thing").service_resources).not_to be(nil)
expect(@st1.service_resources.first.resource).not_to be(nil)
expect(ServiceTemplate.find_by(:name => "thing").service_resources.first.resource).to eq(@st1.service_resources.first.resource)
expect(new_service_template.service_resources.first.resource).to eq(@st1.service_resources.first.resource)
expect(ConfigurationScriptBase.count).to eq(1)
expect(ServiceTemplate.find_by(:name => "thing").display).to be(false)
expect(ServiceTemplate.find_by(:name => "thing").guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.guid).not_to eq(@st1.guid)
end

it "with non-copyable resource (ext management system)" do
@st1.add_resource(FactoryBot.create(:ext_management_system))
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy("thing")
@st1.template_copy("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(ServiceTemplate.find_by(:name => "thing").service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(new_service_template.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(ExtManagementSystem.count).to eq(1)
expect(ServiceTemplate.find_by(:name => "thing").guid).not_to eq(@st1.guid)
expect(ServiceTemplate.find_by(:name => "thing").service_resources).not_to be(nil)
expect(ServiceTemplate.find_by(:name => "thing").display).to be(false)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(@st1.service_resources.first.resource).not_to be(nil)
end

it "with non-copyable resource (orchestration template)" do
@st1.add_resource(FactoryBot.create(:orchestration_template))
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy("thing")
@st1.template_copy("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(ServiceTemplate.find_by(:name => "thing").service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(new_service_template.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(OrchestrationTemplate.count).to eq(1)
expect(ServiceTemplate.find_by(:name => "thing").guid).not_to eq(@st1.guid)
expect(ServiceTemplate.find_by(:name => "thing").service_resources).not_to be(nil)
expect(ServiceTemplate.find_by(:name => "thing").display).to be(false)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(@st1.service_resources.first.resource).not_to be(nil)
end

Expand All @@ -330,12 +354,13 @@
ptr = FactoryBot.create(:miq_provision_request_template, :requester => admin, :src_vm_id => vm_template.id)
@st1.add_resource(ptr)
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy("thing1")
@st1.template_copy("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(MiqProvisionRequestTemplate.count).to eq(2)
expect(ServiceTemplate.find_by(:name => "thing1").guid).not_to eq(@st1.guid)
expect(ServiceTemplate.find_by(:name => "thing1").display).to be(false)
expect(ServiceTemplate.find_by(:name => "thing1").service_resources).not_to be(nil)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources).not_to be(nil)
expect(@st1.service_resources.first.resource).not_to be(nil)
end

Expand All @@ -347,13 +372,13 @@
@st1.service_resources.first.update_attributes(:scaling_min => 4)
expect(ServiceTemplate.count).to eq(1)
expect(@st1.service_resources.first.scaling_min).to eq(4)
@st1.template_copy("thing1")
@st1.template_copy("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(MiqProvisionRequestTemplate.count).to eq(2)
expect(ServiceTemplate.find_by(:name => "thing1").guid).not_to eq(@st1.guid)
expect(ServiceTemplate.find_by(:name => "thing1").display).to be(false)
expect(ServiceTemplate.find_by(:name => "thing1").service_resources.first.scaling_min).to eq(4)
expect(ServiceTemplate.find_by(:name => "thing1").service_resources).not_to be(nil)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources.first.scaling_min).to eq(4)
expect(@st1.service_resources.first.resource).not_to be(nil)
end

Expand All @@ -363,12 +388,13 @@
ptr = FactoryBot.create(:miq_provision_request_template, :requester => admin, :src_vm_id => vm_template.id)
service_template_ansible_tower.add_resource(ptr)
expect(ServiceTemplate.count).to eq(2)
service_template_ansible_tower.template_copy("thing1")
service_template_ansible_tower.template_copy("new_template_copy")
new_service_template = ServiceTemplate.find_by(:name => "new_template_copy")
expect(ServiceTemplate.count).to eq(3)
expect(MiqProvisionRequestTemplate.count).to eq(2)
expect(ServiceTemplate.find_by(:name => "thing1").guid).not_to eq(service_template_ansible_tower.guid)
expect(ServiceTemplate.find_by(:name => "thing1").display).to be(false)
expect(ServiceTemplate.find_by(:name => "thing1").service_resources).not_to be(nil)
expect(new_service_template.guid).not_to eq(service_template_ansible_tower.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources).not_to be(nil)
expect(service_template_ansible_tower.service_resources.first.resource).not_to be(nil)
end

Expand All @@ -378,12 +404,13 @@
ptr = FactoryBot.create(:miq_provision_request_template, :requester => admin, :src_vm_id => vm_template.id)
service_template_orchestration.add_resource(ptr)
expect(ServiceTemplate.count).to eq(2)
service_template_orchestration.template_copy("thing1")
service_template_orchestration.template_copy("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(3)
expect(MiqProvisionRequestTemplate.count).to eq(2)
expect(ServiceTemplate.find_by(:name => "thing1").guid).not_to eq(service_template_orchestration.guid)
expect(ServiceTemplate.find_by(:name => "thing1").display).to be(false)
expect(ServiceTemplate.find_by(:name => "thing1").service_resources).not_to be(nil)
expect(new_service_template.guid).not_to eq(service_template_orchestration.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources).not_to be(nil)
expect(service_template_orchestration.service_resources.first.resource).not_to be(nil)
end
end
Expand All @@ -392,47 +419,51 @@
it "without resource" do
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy
new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%")
expect(ServiceTemplate.count).to eq(2)
expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").guid).not_to eq(@st1.guid)
expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").display).to be(false)
expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").service_resources.count).to eq(0)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources.count).to eq(0)
expect(@st1.service_resources.count).to eq(0)
end

it "with non-copyable resource (configuration_script_base)" do
@st1.add_resource(FactoryBot.create(:configuration_script_base))
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy
new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%")
expect(ServiceTemplate.count).to eq(2)
expect(ServiceTemplate.where("name ILIKE ?", "Copy of service%").first.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(new_service_template.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(ConfigurationScriptBase.count).to eq(1)
expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").display).to be(false)
expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.guid).not_to eq(@st1.guid)
end

it "with non-copyable resource (ext management system)" do
@st1.add_resource(FactoryBot.create(:ext_management_system))
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy
new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%")
expect(ServiceTemplate.count).to eq(2)
expect(ServiceTemplate.where("name ILIKE ?", "Copy of service%").first.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(ExtManagementSystem.count).to eq(1)
expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").guid).not_to eq(@st1.guid)
expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").display).to be(false)
expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").service_resources).not_to be(nil)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources).not_to be(nil)
expect(@st1.service_resources.first.resource).not_to be(nil)
end

it "with non-copyable resource (orchestration template)" do
@st1.add_resource(FactoryBot.create(:orchestration_template))
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy
new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%")
expect(ServiceTemplate.count).to eq(2)
expect(ServiceTemplate.where("name ILIKE ?", "Copy of service%").first.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(OrchestrationTemplate.count).to eq(1)
expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").guid).not_to eq(@st1.guid)
expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").display).to be(false)
expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").service_resources).not_to be(nil)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources).not_to be(nil)
expect(@st1.service_resources.first.resource).not_to be(nil)
end

Expand All @@ -443,11 +474,12 @@
@st1.add_resource(ptr)
expect(ServiceTemplate.count).to eq(1)
@st1.template_copy
new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%")
expect(ServiceTemplate.count).to eq(2)
expect(MiqProvisionRequestTemplate.count).to eq(2)
expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").guid).not_to eq(@st1.guid)
expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").display).to be(false)
expect(ServiceTemplate.find_by("name ILIKE ?", "Copy of service%").service_resources).not_to be(nil)
expect(new_service_template.guid).not_to eq(@st1.guid)
expect(new_service_template.display).to be(false)
expect(new_service_template.service_resources).not_to be(nil)
expect(@st1.service_resources.first.resource).not_to be(nil)
end
end
Expand Down