Skip to content

Commit

Permalink
set internal service_type for transformation plan
Browse files Browse the repository at this point in the history
  • Loading branch information
bzwei committed Jul 16, 2018
1 parent 7f1f7d4 commit 81a7835
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 32 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
56 changes: 31 additions & 25 deletions app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class ServiceTemplate < ApplicationRecord
"vmware" => N_("VMware")
}.freeze

SERVICE_TYPE_UNKNOWN = 'unknown'.freeze
SERVICE_TYPE_ATOMIC = 'atomic'.freeze
SERVICE_TYPE_COMPOSITE = 'composite'.freeze
SERVICE_TYPE_INTERNAL = 'internal'.freeze

RESOURCE_ACTION_UPDATE_ATTRS = [:dialog,
:dialog_id,
:fqname,
Expand Down Expand Up @@ -65,7 +70,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_UNKNOWN
default_value_for(:generic_subtype) { |st| 'custom' if st.prov_type == 'generic' }

virtual_has_one :config_info, :class_name => "Hash"
Expand All @@ -74,6 +79,7 @@ class ServiceTemplate < ApplicationRecord
scope :without_service_template_catalog_id, -> { where(:service_template_catalog_id => nil) }
scope :with_existent_service_template_catalog_id, -> { where.not(:service_template_catalog_id => nil) }
scope :displayed, -> { where(:display => true) }
scope :public_service_templates, -> { where.not(:service_type => 'internal') }

def self.catalog_item_types
ci_types = Set.new(Rbac.filtered(ExtManagementSystem.all).flat_map(&:supported_catalog_types))
Expand Down Expand Up @@ -206,37 +212,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 @@ -452,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 @@ -538,4 +526,22 @@ def resource_actions_info
def generic_custom_buttons
CustomButton.buttons_for("Service")
end

def adjust_service_type
return if service_type == self.class::SERVICE_TYPE_INTERNAL # internal cannot turn to composite

if service_resources.blank?
svc_type = self.class.SERVICE_TYPE_UNKNOWN
else
svc_type = self.class::SERVICE_TYPE_ATOMIC
service_resources.each do |sr|
if sr.resource_type == 'Service' || sr.resource_type == 'ServiceTemplate'
svc_type = self.class::SERVICE_TYPE_COMPOSITE
break
end
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
3 changes: 2 additions & 1 deletion app/models/service_template_transformation_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def request_type
"transformation_plan"
end

default_value_for :service_type, SERVICE_TYPE_INTERNAL

def transformation_mapping
service_resources.find_by(:resource_type => 'TransformationMapping').resource
end
Expand Down Expand Up @@ -49,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
22 changes: 21 additions & 1 deletion spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@
end

it "should create an empty service template without a type" do
expect(@st1.service_type).to eq('unknown')
expect(@st1.service_type).to eq(described_class::SERVICE_TYPE_UNKNOWN)
expect(@st1.composite?).to be_falsey
expect(@st1.atomic?).to be_falsey
end
Expand All @@ -481,6 +481,14 @@
expect(@st1.composite?).to be_falsey
end

it "should remain as internal service template" do
@st1.service_type = described_class::SERVICE_TYPE_INTERNAL
st2 = FactoryGirl.create(:service_template, :name => 'Service Template 2')
@st1.add_resource(st2)
expect(@st1.service_resources.size).to eq(1)
expect(@st1.service_type).to eq(described_class::SERVICE_TYPE_INTERNAL)
end

context "with a VM Provision Request Template" do
before do
admin = FactoryGirl.create(:user_admin)
Expand Down Expand Up @@ -920,6 +928,18 @@
end
end
end

describe '.public_service_templates' do
before do
FactoryGirl.create(:service_template)
FactoryGirl.create(:service_template_transformation_plan)
end

it 'excludes internal service templates' do
expect(described_class.public_service_templates.count).to eq(1)
expect(described_class.public_service_templates.first).not_to be_kind_of(ServiceTemplateTransformationPlan)
end
end
end

def add_and_save_service(p, c)
Expand Down
1 change: 1 addition & 0 deletions spec/models/service_template_transformation_plan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
service_template = described_class.create_catalog_item(catalog_item_options)

expect(service_template.name).to eq('Transformation Plan')
expect(service_template.service_type).to eq(ServiceTemplate::SERVICE_TYPE_INTERNAL)
expect(service_template.transformation_mapping).to eq(transformation_mapping)
expect(service_template.vm_resources.collect(&:resource)).to match_array([vm1, vm2])
expect(service_template.vm_resources.collect(&:status)).to eq([ServiceResource::STATUS_QUEUED, ServiceResource::STATUS_QUEUED])
Expand Down

0 comments on commit 81a7835

Please sign in to comment.