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

Name service during provisioning from dialog input #16338

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
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
has_many :metrics, :as => :resource
Expand Down Expand Up @@ -52,6 +52,7 @@ class Service < ApplicationRecord
virtual_has_one :configuration_script

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 @@ -74,7 +75,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 @@ -253,7 +254,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 @@ -378,7 +379,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 @@ -427,4 +428,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?
Copy link
Contributor

Choose a reason for hiding this comment

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

@gmcculloug
Should we allow for designers to append a date_time to the service using something like
My_Service_%{date_time}
And we would put the current date_time in the name field.

Copy link
Contributor

Choose a reason for hiding this comment

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

the designer could leave the field as readonly so the end user ordering it doesn't have to set the service name.

Copy link
Member Author

Choose a reason for hiding this comment

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

Users can do this today through automate, I would rather not introduce it here yet.

Copy link
Contributor

Choose a reason for hiding this comment

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

@gmcculloug
From a usability perspective the service names are all going to be same out of the box. If the user provisions the same service multiple times they would all have the same name. And for services that don't use automate they won't have a way to discern one from the other.

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 @@ -198,23 +198,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 @@ -408,7 +408,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 @@ -419,7 +419,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 @@ -763,6 +763,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