Skip to content

Commit

Permalink
Merge pull request #17681 from bzwei/v2v_internal_type
Browse files Browse the repository at this point in the history
Refactor service_type to use constants
  • Loading branch information
gmcculloug authored Jul 27, 2018
2 parents 9da1017 + cb706b8 commit 1893e95
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 49 deletions.
2 changes: 1 addition & 1 deletion app/models/mixins/miq_provision_quota_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def vm_quota_values(pr, result)
def service_quota_values(request, result)
return unless request.service_template
request.service_template.service_resources.each do |sr|
if request.service_template.service_type == 'composite'
if request.service_template.service_type == ServiceTemplate::SERVICE_TYPE_COMPOSITE
bundle_quota_values(sr, result)
else
next if request.service_template.prov_type.starts_with?("generic")
Expand Down
47 changes: 22 additions & 25 deletions app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class ServiceTemplate < ApplicationRecord
"vmware" => N_("VMware")
}.freeze

SERVICE_TYPE_ATOMIC = 'atomic'.freeze
SERVICE_TYPE_COMPOSITE = 'composite'.freeze

RESOURCE_ACTION_UPDATE_ATTRS = [:dialog,
:dialog_id,
:fqname,
Expand Down Expand Up @@ -65,7 +68,7 @@ class ServiceTemplate < ApplicationRecord
virtual_column :archived, :type => :boolean
virtual_column :active, :type => :boolean

default_value_for :service_type, 'unknown'
default_value_for :service_type, SERVICE_TYPE_ATOMIC
default_value_for(:generic_subtype) { |st| 'custom' if st.prov_type == 'generic' }

virtual_has_one :config_info, :class_name => "Hash"
Expand Down Expand Up @@ -206,37 +209,19 @@ def create_service(service_task, parent_svc = nil)
svc
end

def set_service_type
svc_type = nil

if service_resources.size.zero?
svc_type = 'unknown'
else
service_resources.each do |sr|
if sr.resource_type == 'Service' || sr.resource_type == 'ServiceTemplate'
svc_type = 'composite'
break
end
end
svc_type = 'atomic' if svc_type.blank?
end

self.service_type = svc_type
end

def composite?
service_type.to_s.include?('composite')
service_type.to_s.include?(self.class::SERVICE_TYPE_COMPOSITE)
end

def atomic?
service_type.to_s.include?('atomic')
service_type.to_s.include?(self.class::SERVICE_TYPE_ATOMIC)
end

def type_display
case service_type
when "atomic" then "Item"
when "composite" then "Bundle"
when nil then "Unknown"
when self.class::SERVICE_TYPE_ATOMIC then "Item"
when self.class::SERVICE_TYPE_COMPOSITE then "Bundle"
when nil then "Unknown"
else
service_type.to_s.capitalize
end
Expand Down Expand Up @@ -455,7 +440,7 @@ def provision_workflow(user, dialog_options = nil, request_options = nil)

def add_resource(rsc, options = {})
super
set_service_type
adjust_service_type
end

def self.display_name(number = 1)
Expand Down Expand Up @@ -541,4 +526,16 @@ def resource_actions_info
def generic_custom_buttons
CustomButton.buttons_for("Service")
end

def adjust_service_type
svc_type = self.class::SERVICE_TYPE_ATOMIC
service_resources.try(:each) do |sr|
if sr.resource_type == 'Service' || sr.resource_type == 'ServiceTemplate'
svc_type = self.class::SERVICE_TYPE_COMPOSITE
break
end
end

self.service_type = svc_type
end
end
4 changes: 2 additions & 2 deletions app/models/service_template_ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def self.default_retirement_entry_point
# :reconfigure (same as provision)
#
def self.create_catalog_item(options, auth_user)
options = options.merge(:service_type => 'atomic', :prov_type => 'generic_ansible_playbook')
options = options.merge(:service_type => SERVICE_TYPE_ATOMIC, :prov_type => 'generic_ansible_playbook')
service_name = options[:name]
description = options[:description]
config_info = validate_config_info(options[:config_info])
Expand Down Expand Up @@ -115,7 +115,7 @@ def self.build_parameter_list(name, description, info)
private_class_method :build_parameter_list

def self.validate_config_info(info)
info[:provision][:fqname] ||= default_provisioning_entry_point('atomic') if info.key?(:provision)
info[:provision][:fqname] ||= default_provisioning_entry_point(SERVICE_TYPE_ATOMIC) if info.key?(:provision)
info[:reconfigure][:fqname] ||= default_reconfiguration_entry_point if info.key?(:reconfigure)

if info.key?(:retirement)
Expand Down
4 changes: 2 additions & 2 deletions app/models/service_template_container_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def self.default_retirement_entry_point
# :container_template_id or :container_template
#
def self.create_catalog_item(options, _auth_user = nil)
options = options.merge(:service_type => 'atomic', :prov_type => 'generic_container_template')
options = options.merge(:service_type => SERVICE_TYPE_ATOMIC, :prov_type => 'generic_container_template')
config_info = validate_config_info(options[:config_info])
enhanced_config = config_info.deep_merge(
:provision => {
Expand All @@ -34,7 +34,7 @@ def self.create_catalog_item(options, _auth_user = nil)
end

def self.validate_config_info(info)
info[:provision][:fqname] ||= default_provisioning_entry_point('atomic') if info.key?(:provision)
info[:provision][:fqname] ||= default_provisioning_entry_point(SERVICE_TYPE_ATOMIC) if info.key?(:provision)

# TODO: Add more validation for required fields
info
Expand Down
1 change: 0 additions & 1 deletion app/models/service_template_transformation_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def self.create_catalog_item(options, _auth_user = nil)
enhanced_config_info = validate_config_info(options)
default_options = {
:display => false,
:service_type => 'atomic',
:prov_type => 'transformation_plan'
}

Expand Down
48 changes: 30 additions & 18 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,30 @@
@st1 = FactoryGirl.create(:service_template, :name => 'Service Template 1')
end

it "with service_type of unknown" do
expect(@st1.type_display).to eq('Unknown')
it "with default service_type" do
expect(@st1.service_type).to eq("atomic")
expect(@st1.type_display).to eq('Item')
end

it "with service_type of atomic" do
@st1.update_attributes(:service_type => 'atomic')
@st1.update_attributes(:service_type => described_class::SERVICE_TYPE_ATOMIC)
expect(@st1.type_display).to eq('Item')
end

it "with service_type of composite" do
@st1.update_attributes(:service_type => 'composite')
@st1.update_attributes(:service_type => described_class::SERVICE_TYPE_COMPOSITE)
expect(@st1.type_display).to eq('Bundle')
end

it "with user service_type" do
@st1.update_attributes(:service_type => 'user')
expect(@st1.type_display).to eq('User')
end

it "with no service_type" do
@st1.update_attributes(:service_type => nil)
expect(@st1.type_display).to eq('Unknown')
end
end

context "#atomic?" do
Expand All @@ -240,13 +251,19 @@
end

it "with service_type of unknown" do
@st1.update_attributes(:service_type => 'user')
expect(@st1.atomic?).to be_falsey
end

it "with service_type of atomic" do
@st1.update_attributes(:service_type => 'atomic')
@st1.update_attributes(:service_type => described_class::SERVICE_TYPE_ATOMIC)
expect(@st1.atomic?).to be_truthy
end

it "with service_type of composite" do
@st1.update_attributes(:service_type => described_class::SERVICE_TYPE_COMPOSITE)
expect(@st1.atomic?).to be_falsey
end
end

context "#composite?" do
Expand All @@ -255,11 +272,17 @@
end

it "with service_type of unknown" do
@st1.update_attributes(:service_type => 'user')
expect(@st1.composite?).to be_falsey
end

it "with service_type of atomic" do
@st1.update_attributes(:service_type => described_class::SERVICE_TYPE_ATOMIC)
expect(@st1.composite?).to be_falsey
end

it "with service_type of composite" do
@st1.update_attributes(:service_type => 'composite')
@st1.update_attributes(:service_type => described_class::SERVICE_TYPE_COMPOSITE)
expect(@st1.composite?).to be_truthy
end
end
Expand Down Expand Up @@ -438,6 +461,7 @@
it "should create a valid service template" do
expect(@st1.guid).not_to be_empty
expect(@st1.service_resources.size).to eq(0)
expect(@st1.service_type).to eq(described_class::SERVICE_TYPE_ATOMIC)
end

it "should not set the owner for the service template" do
Expand All @@ -459,12 +483,6 @@
expect(@test_service.evm_owner.current_group.description).to eq(@user.current_group.description)
end

it "should create an empty service template without a type" do
expect(@st1.service_type).to eq('unknown')
expect(@st1.composite?).to be_falsey
expect(@st1.atomic?).to be_falsey
end

it "should create a composite service template" do
st2 = FactoryGirl.create(:service_template, :name => 'Service Template 2')
@st1.add_resource(st2)
Expand Down Expand Up @@ -515,12 +533,6 @@
@ptr = FactoryGirl.create(:miq_provision_request_template, :requester => user, :src_vm_id => @vm_template.id)
end

it 'unknown' do
expect(@st1.service_type).to eq "unknown"
expect(@st1.template_valid?).to be_truthy
expect(@st1.template_valid_error_message).to be_nil
end

context 'atomic' do
before { @st1.add_resource(@ptr) }

Expand Down

0 comments on commit 1893e95

Please sign in to comment.