Skip to content

Commit

Permalink
Expose DDF schema for a provider when asking using OPTIONS + type
Browse files Browse the repository at this point in the history
  • Loading branch information
skateman committed Sep 6, 2019
1 parent f9bb012 commit 8fb1bd1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
51 changes: 33 additions & 18 deletions app/controllers/api/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,9 @@ def import_vm_resource(type, id = nil, data = {})
end

def options
providers_options = ManageIQ::Providers::BaseManager.leaf_subclasses.inject({}) do |po, ems|
po.merge(ems.ems_type => ems.options_description)
end

supported_providers = ExtManagementSystem.supported_types_for_create.map do |klass|
if klass.supports_regions?
regions = klass.parent::Regions.all.sort_by { |r| r[:description] }.map { |r| r.slice(:name, :description) }
end

{
:title => klass.description,
:type => klass.to_s,
:kind => klass.to_s.demodulize.sub(/Manager$/, '').underscore,
:regions => regions
}.compact
end

render_options(:providers, "provider_settings" => providers_options, "supported_providers" => supported_providers)
options = providers_options
options['provider_form_schema'] = provider_options(params[:type]) if params[:type]
render_options(:providers, options)
end

def pause_resource(type, id, _data)
Expand Down Expand Up @@ -131,6 +116,36 @@ def change_password_resource(type, id, data = {})

private

def provider_options(type)
klass = type.safe_constantize

raise BadRequestError, "Invalid provider - #{type}" unless klass.try(:<, ExtManagementSystem)
raise BadRequestError, "No DDF specified for - #{type}" unless klass.respond_to?(:params_for_create)

klass.params_for_create
end

def providers_options
providers_options = ManageIQ::Providers::BaseManager.leaf_subclasses.inject({}) do |po, ems|
po.merge(ems.ems_type => ems.options_description)
end

supported_providers = ExtManagementSystem.supported_types_for_create.map do |klass|
if klass.supports_regions?
regions = klass.parent::Regions.all.sort_by { |r| r[:description] }.map { |r| r.slice(:name, :description) }
end

{
:title => klass.description,
:type => klass.to_s,
:kind => klass.to_s.demodulize.sub(/Manager$/, '').underscore,
:regions => regions
}.compact
end

{ "provider_settings" => providers_options, "supported_providers" => supported_providers }
end

# Process password change request for a single resource
#
# @raise [BadRequestError] if no id is passed or some required data is missing
Expand Down
25 changes: 25 additions & 0 deletions spec/requests/providers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,31 @@ def gen_import_request
)
expect(response.parsed_body["data"]["provider_settings"]["kubernetes"]["proxy_settings"]["settings"]["http_proxy"]["label"]).to eq('HTTP Proxy')
end

context 'single provider queried' do
it 'raises an error if the provider is invalid' do
options("#{api_providers_url}?type=foo")
expect_bad_request("Invalid provider - foo")
end

context 'valid provider' do
before do
class DummyProvider; end
allow(DummyProvider).to receive(:<).with(ExtManagementSystem).and_return(true)
end

it 'raises an error if the provider has no DDF' do
options("#{api_providers_url}?type=DummyProvider")
expect_bad_request("No DDF specified for - DummyProvider")
end

it 'calls the .params_for_create on the provider' do
expect(DummyProvider).to receive(:params_for_create).and_return('foo' => 'bar')
options("#{api_providers_url}?type=DummyProvider")
expect(response.parsed_body['data']['provider_form_schema']).to eq('foo' => 'bar')
end
end
end
end

context 'GET /api/providers/:id/vms' do
Expand Down

0 comments on commit 8fb1bd1

Please sign in to comment.