Skip to content
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
28 changes: 23 additions & 5 deletions app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,31 @@ def self.create_catalog_item(options, auth_user)
end

def self.class_from_request_data(data)
request_type = data['prov_type']
if request_type.include?('generic_')
generic_type = request_type.split('generic_').last
"ServiceTemplate#{generic_type.camelize}".constantize
class_from_prov_type(data['prov_type'])
end

def self.class_from_prov_type(prov_type)
# This is the full list of acceptable values. This allows us to check for
# class name validity before calling constantize.
allowed_class_names = descendants.collect(&:name)

raise ArgumentError, "Invalid prov_type '#{prov_type}'" unless all_catalog_item_types.key?(prov_type)

# If we are given a "generic" provision type then assume that the prov_type
# maps to a valid class otherwise it is invalid
#
# Non-generic provision types however are able to work with the base
# ServiceTemplate class and thus we cannot raise an exception in this case.
if prov_type.starts_with?("generic_")
generic_type = prov_type.split('generic_').last
class_name = "ServiceTemplate#{generic_type.camelize}"
raise ArgumentError, "Invalid prov_type '#{prov_type}'" unless allowed_class_names.include?(class_name)
else
ServiceTemplate
class_name = "ServiceTemplate#{prov_type.camelize}"
class_name = "ServiceTemplate" unless allowed_class_names.include?(class_name)
Comment on lines +160 to +161
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

TODO would love to enforce provider subclasses of ServiceTemplates and Services so this can follow the same rules as the generic_ case above

end

class_name.constantize
end

def update_catalog_item(options, auth_user = nil)
Expand Down
34 changes: 34 additions & 0 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,40 @@
end
end

describe '.class_from_prov_type' do
it 'returns the correct generic type' do
template_class = ServiceTemplate.class_from_prov_type('generic_ansible_tower')

expect(template_class).to eq(ServiceTemplateAnsibleTower)
end

it 'raises an exception for an invalid generic type' do
expect { ServiceTemplate.class_from_prov_type('generic_invalid') }
.to raise_error(ArgumentError, /Invalid prov_type/)
end

it 'raises an exception when missing a ServiceTemplate sub-class for a valid prov_type' do
expect(described_class).to receive(:all_catalog_item_types).and_return("generic_invalid" => "Invalid but somehow kind of valid")
expect { ServiceTemplate.class_from_prov_type('generic_invalid') }
.to raise_error(ArgumentError, /Invalid prov_type/)
end

it 'returns the correct non-generic type mapping to ::ServiceTemplate' do
template_class = ServiceTemplate.class_from_prov_type('amazon')

expect(template_class).to eq(ServiceTemplate)
end

it 'returns the correct non-generic type when mapping to a ServiceTemplate subclass' do
expect(ServiceTemplate.class_from_prov_type("awx")).to eq(ServiceTemplateAwx)
end

it 'raises an exception for an invalid non-generic type' do
expect { ServiceTemplate.class_from_prov_type('invalid') }
.to raise_error(ArgumentError, /Invalid prov_type/)
end
end

let(:user) { FactoryBot.create(:user_with_group) }
let(:ra1) { FactoryBot.create(:resource_action, :action => 'Provision') }
let(:ra2) { FactoryBot.create(:resource_action, :action => 'Retirement') }
Expand Down
Loading