Skip to content

Commit

Permalink
Merge pull request ManageIQ#16338 from gmcculloug/name_service_during…
Browse files Browse the repository at this point in the history
…_provisioning

Name service during provisioning from dialog input
(cherry picked from commit 5faba90)

https://bugzilla.redhat.com/show_bug.cgi?id=1511517
  • Loading branch information
mkanoor authored and d-m-u committed Jun 6, 2018
1 parent 51c74e5 commit 2c11445
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 20 deletions.
27 changes: 23 additions & 4 deletions app/models/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Service < ApplicationRecord
has_ancestry :orphan_strategy => :destroy

belongs_to :tenant
belongs_to :service_template # Template this service was cloned from
belongs_to :service_template # Template this service was cloned from

has_many :dialogs, -> { distinct }, :through => :service_template

Expand All @@ -47,6 +47,7 @@ class Service < ApplicationRecord
virtual_has_one :chargeback_report

before_validation :set_tenant_from_group
before_create :apply_dialog_settings

delegate :custom_actions, :custom_action_buttons, :to => :service_template, :allow_nil => true
delegate :provision_dialog, :to => :miq_request, :allow_nil => true
Expand All @@ -67,7 +68,7 @@ class Service < ApplicationRecord
virtual_column :power_state, :type => :string
virtual_column :power_status, :type => :string

validates_presence_of :name
validates :name, :presence => true

default_value_for :display, false
default_value_for :retired, false
Expand Down Expand Up @@ -231,7 +232,7 @@ def process_group_action(action, group_idx, direction)
begin
rsc = svc_rsc.resource
rsc_action = service_action(action, svc_rsc)
rsc_name = "#{rsc.class.name}:#{rsc.id}" + (rsc.respond_to?(:name) ? ":#{rsc.name}" : "")
rsc_name = "#{rsc.class.name}:#{rsc.id}" + (rsc.respond_to?(:name) ? ":#{rsc.name}" : "")
if rsc_action.nil?
_log.info "Not Processing action for Service:<#{name}:#{id}>, RSC:<#{rsc_name}}> in Group Idx:<#{group_idx}>"
elsif rsc.respond_to?(rsc_action)
Expand Down Expand Up @@ -356,7 +357,7 @@ def generate_chargeback_report(options = {})
end

def chargeback_yaml
yaml = YAML.load_file(File.join(Rails.root, "product/chargeback/chargeback_vm_monthly.yaml"))
yaml = YAML.load_file(Rails.root.join('product', 'chargeback', 'chargeback_vm_monthly.yaml'))
yaml["db_options"][:options][:service_id] = id
yaml["title"] = chargeback_report_name
yaml
Expand Down Expand Up @@ -396,4 +397,22 @@ def remove_from_service(parenent_service)
update(:parent => nil)
parenent_service.remove_resource(self)
end

private

def apply_dialog_settings
dialog_options = options[:dialog] || {}

%w(dialog_service_name dialog_service_description).each do |field_name|
send(field_name, dialog_options[field_name]) if dialog_options.key?(field_name)
end
end

def dialog_service_name(value)
self.name = value if value.present?
end

def dialog_service_description(value)
self.description = value if value.present?
end
end
69 changes: 53 additions & 16 deletions spec/models/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@
before do
@zone1 = FactoryGirl.create(:small_environment)
allow(MiqServer).to receive(:my_server).and_return(@zone1.miq_servers.first)
@vm = FactoryGirl.create(:vm_vmware)
@vm_1 = FactoryGirl.create(:vm_vmware)
@vm_2 = FactoryGirl.create(:vm_vmware)
@vm = FactoryGirl.create(:vm_vmware)
@vm1 = FactoryGirl.create(:vm_vmware)
@vm2 = FactoryGirl.create(:vm_vmware)

@service = FactoryGirl.create(:service)
@service_c1 = FactoryGirl.create(:service, :service => @service)
@service_c2 = FactoryGirl.create(:service, :service => @service_c1)
@service = FactoryGirl.create(:service)
@service_c1 = FactoryGirl.create(:service, :service => @service)
@service_c2 = FactoryGirl.create(:service, :service => @service_c1)
@service << @vm
@service_c1 << @vm_1
@service_c2 << @vm_1
@service_c2 << @vm_2
@service_c1 << @vm1
@service_c2 << @vm1
@service_c2 << @vm2
@service.service_resources.first.start_action = "Power On"
@service.service_resources.first.stop_action = "Power Off"
@service.save
Expand Down Expand Up @@ -159,23 +159,23 @@
end

it "#direct_vms" do
expect(@service_c1.direct_vms).to match_array [@vm_1]
expect(@service_c1.direct_vms).to match_array [@vm1]
expect(@service.direct_vms).to match_array [@vm]
end

it "#all_vms" do
expect(@service_c1.all_vms).to match_array [@vm_1, @vm_1, @vm_2]
expect(@service.all_vms).to match_array [@vm, @vm_1, @vm_1, @vm_2]
expect(@service_c1.all_vms).to match_array [@vm1, @vm1, @vm2]
expect(@service.all_vms).to match_array [@vm, @vm1, @vm1, @vm2]
end

it "#direct_service" do
expect(@vm.direct_service).to eq(@service)
expect(@vm_1.direct_service).to eq(@service_c1)
expect(@vm1.direct_service).to eq(@service_c1)
end

it "#service" do
expect(@vm.service).to eq(@service)
expect(@vm_1.service).to eq(@service)
expect(@vm1.service).to eq(@service)
end
end

Expand Down Expand Up @@ -369,7 +369,7 @@
context "Chargeback report generation" do
before do
@vm = FactoryGirl.create(:vm_vmware)
@vm_1 = FactoryGirl.create(:vm_vmware)
@vm1 = FactoryGirl.create(:vm_vmware)
@service = FactoryGirl.create(:service)
@service.name = "Test_Service_1"
@service << @vm
Expand All @@ -380,7 +380,7 @@
it "queue request to generate chargeback report for each service" do
@service_c1 = FactoryGirl.create(:service, :service => @service)
@service_c1.name = "Test_Service_2"
@service_c1 << @vm_1
@service_c1 << @vm1
@service_c1.save

expect(MiqQueue).to receive(:put).twice
Expand Down Expand Up @@ -712,6 +712,43 @@
end
end

context 'service naming' do
it 'without empty options hash' do
expect(Service.create(:name => 'test').name).to eq('test')
end

it 'with empty dialog options' do
expect(Service.create(:name => 'test', :options => {:dialog => {}}).name).to eq('test')
end

it 'with dialog option dialog_service_name' do
expect(Service.create(:name => 'test', :options => {:dialog => {'dialog_service_name' => 'name from dialog'}}).name)
.to eq('name from dialog')
end
end

context 'service description' do
it 'without empty options hash' do
expect(Service.create(:name => 'test').description).to be_blank
end

it 'with empty dialog options' do
expect(Service.create(:name => 'test',
:description => 'test description',
:options => {:dialog => {}}).description)
.to eq('test description')
end

it 'with dialog option dialog_service_description' do
expect(Service.create(:name => 'test',
:description => 'test description',
:options => {
:dialog => {'dialog_service_description' => 'test description from dialog'}
}).description)
.to eq('test description from dialog')
end
end

def create_deep_tree
@service = FactoryGirl.create(:service)
@service_c1 = FactoryGirl.create(:service, :service => @service)
Expand Down

0 comments on commit 2c11445

Please sign in to comment.