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

Include resource_actions and picture in service_template copy #18973

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
16 changes: 15 additions & 1 deletion app/models/service_template/copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ def template_copy(new_name = "Copy of " + name + Time.zone.now.to_s)
dup.tap do |template|
template.update_attributes(:name => new_name, :display => false)
service_resources.each { |service_resource| resource_copy(service_resource, template) }
resource_action_copy(template)
picture_copy(template) if picture

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

private

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)
Expand All @@ -26,4 +32,12 @@ def custom_button_copy(custom_button, template)
def custom_button_set_copy(custom_button_set, template)
custom_button_set.deep_copy(:owner => template)
end

def resource_action_copy(template)
template.resource_actions << resource_actions.collect(&:dup)
bdunne marked this conversation as resolved.
Show resolved Hide resolved
end

def picture_copy(template)
template.picture = picture.dup
end
end
265 changes: 265 additions & 0 deletions spec/models/service_template/copy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
describe ServiceTemplate do
describe "#template_copy" do
let(:service_template_ansible_tower) { FactoryBot.create(:service_template_ansible_tower) }
let(:service_template_orchestration) { FactoryBot.create(:service_template_orchestration) }
let(:custom_button) { FactoryBot.create(:custom_button, :applies_to => @st1) }
let(:custom_button_for_service) { 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("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)
end

it "with custom button copy only direct_custom_buttons" do
custom_button
custom_button_for_service
expect(@st1.custom_buttons.count).to eq(2)
number_of_service_templates = ServiceTemplate.count
@st1.template_copy("new_template")
expect(ServiceTemplate.count).to eq(number_of_service_templates + 1)
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.direct_custom_buttons.count).to eq(@st1.direct_custom_buttons.count)
end

it "with custom button it can copy a copy" do
custom_button
custom_button_for_service
expect(@st1.custom_buttons.count).to eq(2)
number_of_service_templates = ServiceTemplate.count
@st1.template_copy("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
new_service_template.template_copy("Copy of a copy")
expect(ServiceTemplate.count).to eq(number_of_service_templates + 2)
copy_of_copy = ServiceTemplate.find_by(:name => "Copy of a copy")
expect(copy_of_copy.display).to be(false)
expect(copy_of_copy.guid).not_to eq(new_service_template.guid)
expect(copy_of_copy.direct_custom_buttons.count).to eq(new_service_template.direct_custom_buttons.count)
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("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(@st1.service_resources.first.resource).not_to be(nil)
expect(new_service_template.service_resources.first.resource).to eq(@st1.service_resources.first.resource)
expect(ConfigurationScriptBase.count).to eq(1)
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_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(new_service_template.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(ExtManagementSystem.count).to eq(1)
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("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(new_service_template.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(OrchestrationTemplate.count).to eq(1)
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 copyable resource" do
admin = FactoryBot.create(:user_admin)
vm_template = FactoryBot.create(:vm_openstack, :ext_management_system => FactoryBot.create(:ext_management_system))
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("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(MiqProvisionRequestTemplate.count).to eq(2)
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)
bdunne marked this conversation as resolved.
Show resolved Hide resolved
expect(@st1.service_resources.first.resource).not_to be(nil)
end

it "with copyable resource copies sr options" do
admin = FactoryBot.create(:user_admin)
vm_template = FactoryBot.create(:vm_openstack, :ext_management_system => FactoryBot.create(:ext_management_system))
ptr = FactoryBot.create(:miq_provision_request_template, :requester => admin, :src_vm_id => vm_template.id)
@st1.add_resource(ptr)
@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("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(2)
expect(MiqProvisionRequestTemplate.count).to eq(2)
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

it "service template ansible tower with copyable resource" do
admin = FactoryBot.create(:user_admin)
vm_template = FactoryBot.create(:vm_openstack, :ext_management_system => FactoryBot.create(:ext_management_system))
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("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(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

it "service template orchestration with copyable resource" do
admin = FactoryBot.create(:user_admin)
vm_template = FactoryBot.create(:vm_openstack, :ext_management_system => FactoryBot.create(:ext_management_system))
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("new_template")
new_service_template = ServiceTemplate.find_by(:name => "new_template")
expect(ServiceTemplate.count).to eq(3)
expect(MiqProvisionRequestTemplate.count).to eq(2)
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

context "without given name" do
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(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(new_service_template.service_resources.first.resource_id).to eq(@st1.service_resources.first.resource_id)
expect(ConfigurationScriptBase.count).to eq(1)
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(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(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 copyable resource" do
admin = FactoryBot.create(:user_admin)
vm_template = FactoryBot.create(:vm_openstack, :ext_management_system => FactoryBot.create(:ext_management_system))
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
new_service_template = ServiceTemplate.find_by("name ILIKE ?", "Copy of service%")
expect(ServiceTemplate.count).to eq(2)
expect(MiqProvisionRequestTemplate.count).to eq(2)
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

context "picture" do
it "creates a duplicate picture" do
@st1.picture = { :content => 'foobar', :extension => 'jpg' }
new_template = @st1.template_copy

expect(@st1.picture.id).to_not eq(new_template.picture.id)
expect(@st1.picture.content).to eq(new_template.picture.content)
end

it "leave picture nil when source template is nil" do
new_template = @st1.template_copy

expect(@st1.picture).to be_nil
expect(new_template.picture).to be_nil
end
end

context "resource_actions" do
it "duplicates resource_actions" do
@st1.resource_actions << [
FactoryBot.create(:resource_action, :action => "Provision"),
FactoryBot.create(:resource_action, :action => "Retire")
]

new_template = @st1.template_copy
expect(new_template.resource_actions.pluck(:action)).to match_array(%w[Provision Retire])
end
end
end
end
Loading