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

Update API to use descendant_get to return valid types #14521

Merged
merged 2 commits into from
Jul 24, 2017
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ gem "kubeclient", "~>2.4.0", :require => false # For s
gem "manageiq-api-client", "~>0.1.0", :require => false
gem "manageiq-network_discovery", "~>0.1.1", :require => false
gem "mime-types", "~>2.6.1", :path => "mime-types-redirector"
gem "more_core_extensions", "~>3.2"
gem "more_core_extensions", "~>3.3"
gem "nakayoshi_fork", "~>0.0.3" # provides a more CoW friendly fork (GC a few times before fork)
gem "net-ldap", "~>0.14.0", :require => false
gem "net-ping", "~>1.7.4", :require => false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ def create_resource(_type, _id, data)
manager_id = parse_id(data['manager_resource'], :providers)
raise 'Must specify a valid manager_resource href or id' unless manager_id
manager = resource_search(manager_id, :providers, collection_class(:providers))
type = ConfigurationScriptSource.class_for_manager(manager)
raise "ConfigurationScriptSource cannot be added to #{manager_ident(manager)}" unless type.respond_to?(:create_in_provider_queue)
task_id = type.create_in_provider_queue(manager.id, data.except('manager_resource').deep_symbolize_keys)

type = "#{manager.type}::ConfigurationScriptSource"
klass = ConfigurationScriptSource.descendant_get(type)
raise "ConfigurationScriptSource cannot be added to #{manager_ident(manager)}" unless klass.respond_to?(:create_in_provider_queue)

task_id = klass.create_in_provider_queue(manager.id, data.except('manager_resource'))
action_result(true, "Creating ConfigurationScriptSource for #{manager_ident(manager)}", :task_id => task_id)
rescue => err
action_result(false, err.to_s)
Expand Down
9 changes: 0 additions & 9 deletions app/models/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,6 @@ def self.build_credential_options
end
end

def self.class_from_request_data(data)
Copy link
Member

Choose a reason for hiding this comment

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

I didn't look, are there no other callers? Can you grep not only manageiq but all providers to make sure we update all callers?

I usually do this now that we have so many extracted repos..

$ bundle show rails
/Users/joerafaniello/.gem/ruby/2.4.1/gems/rails-5.0.4

Take that path and basically grep the grandparent directory so it includes not only gems but bundler git gems:

grep -ir "class_from_request_data" /Users/joerafaniello/.gem/ruby/2.4.1

Or something like that

Copy link
Author

Choose a reason for hiding this comment

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

@jrafanie yeah this is the only caller - this was a method I created for the API back in March to be replaced by this update

if !data.key?('type') || data['type'] == to_s
return self
end
type = descendants.find { |klass| klass.name == data['type'] }
raise _('Must be an Authentication type') unless type
type
end

private

def set_credentials_changed_on
Expand Down
5 changes: 0 additions & 5 deletions app/models/configuration_script_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,4 @@ class ConfigurationScriptSource < ApplicationRecord
belongs_to :manager, :class_name => "ExtManagementSystem"

virtual_total :total_payloads, :configuration_script_payloads

def self.class_for_manager(manager)
type = "#{manager.type}::ConfigurationScriptSource"
descendants.find { |klass| klass.name == type }
end
end
2 changes: 1 addition & 1 deletion lib/services/api/authentication_service.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Api
class AuthenticationService
def self.create_authentication_task(manager_resource, attrs)
klass = ::Authentication.class_from_request_data(attrs)
klass = ::Authentication.descendant_get(attrs['type'])
# TODO: Temporary validation - remove
raise 'type not currently supported' unless klass.respond_to?(:create_in_provider_queue)
klass.create_in_provider_queue(manager_resource.id, attrs.deep_symbolize_keys)
Expand Down
24 changes: 0 additions & 24 deletions spec/models/authentication_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,4 @@
expect(described_class.new(:status => 'Error').retryable_status?).to be_truthy
end
end

context '.class_from_request_data' do
it 'raises an error if it is not an authentication type' do
expect do
described_class.class_from_request_data('type' => 'ServiceTemplate')
end.to raise_error(RuntimeError, 'Must be an Authentication type')
end

it 'returns self if no type is specified' do
expect(described_class.class_from_request_data({})).to eq(described_class)
end

it 'returns self if Authentication is specified' do
expect(described_class.class_from_request_data('type' => 'Authentication')).to eq(described_class)
end

it 'returns the specified type' do
data = {
'type' => 'ManageIQ::Providers::EmbeddedAnsible::AutomationManager::MachineCredential'
}
expect(described_class.class_from_request_data(data))
.to eq(ManageIQ::Providers::EmbeddedAnsible::AutomationManager::MachineCredential)
end
end
end
8 changes: 0 additions & 8 deletions spec/models/configuration_script_source_spec.rb

This file was deleted.