Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Jillian Tullo committed Jun 29, 2017
1 parent c93c91b commit 176c8ca
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 29 deletions.
55 changes: 39 additions & 16 deletions app/controllers/mixins/ems_common_angular.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,34 @@ def update_ems_button_save
end

def update_ems_button_validate
ems_type = model.model_from_emstype(params[:emstype])
result, details = realtime_authentication_check
render_validation_result(result, details)
end

def realtime_authentication_check(verify_ems = nil)
verify_ems ||= find_record_with_rbac(model, params[:id])
set_ems_record_vars(verify_ems, :validate)
@in_a_form = true
verify_ems.authentication_check(params[:cred_type], :save => false, :database => params[:metrics_database_name])
end

result, error = queue_authentication_check(ems_type)
def create_ems_button_validate
@in_a_form = true
# TODO: queue authentication checks for all providers, not just cloud
result, details = if params[:controller] == "ems_cloud"
queue_authentication_check
else
ems = model.model_from_emstype(params[:emstype]).new
realtime_authentication_check(ems)
end
render_validation_result(result, details)
end

def render_validation_result(result, details)
if result
add_flash(_("Credential validation was successful"))
else
add_flash(_("Credential validation was not successful: %{details}") % {:details => error}, :error)
add_flash(_("Credential validation was not successful: %{details}") % {:details => details}, :error)
end

render :json => {:message => @flash_array.last(1)[0][:message], :level => @flash_array.last(1)[0][:level]}
Expand All @@ -75,12 +94,14 @@ def create

case params[:button]
when "add" then create_ems_button_add
when "validate" then update_ems_button_validate
when "validate" then create_ems_button_validate
when "cancel" then create_ems_button_cancel
end
end

def queue_authentication_check(ems_type)
def queue_authentication_check
ems_type = model.model_from_emstype(params[:emstype])

task_opts = {
:action => "Validate EMS Provider Credentials",
:userid => session[:userid]
Expand Down Expand Up @@ -108,17 +129,19 @@ def queue_authentication_check(ems_type)
end

def get_task_args(ems)
if ems == ManageIQ::Providers::Openstack::CloudManager
auth_url = ems.class.auth_url(params[:default_hostname], params[:default_api_port])
[params[:default_userid], params[:default_password], auth_url]
elsif ems == ManageIQ::Providers::Amazon::CloudManager
[params[:default_userid], params[:default_password], :EC2, params[:provider_region], true]
elsif ems == ManageIQ::Providers::Azure::CloudManager
[params[:default_userid], params[:default_password], params[:azure_tenant_id], params[:subscription], params[:proxy_uri], params[:provider_region]]
elsif ems == ManageIQ::Providers::Vmware::CloudManager
[params[:default_hostname], params[:default_api_port], params[:default_userid], params[:default_password], true]
elsif ems == ManageIQ::Providers::Google::CloudManager
[params[:project], params[:service_account], {:service => "compute"}]
user, password = params[:default_userid], MiqPassword.encrypt(params[:default_password])
case
when ems == ManageIQ::Providers::Openstack::CloudManager
auth_url = ems.auth_url(params[:default_hostname], params[:default_api_port])
[user, password, auth_url]
when ems == ManageIQ::Providers::Amazon::CloudManager
[user, password, :EC2, params[:provider_region], nil, true]
when ems == ManageIQ::Providers::Azure::CloudManager
[user, password, params[:azure_tenant_id], params[:subscription], params[:proxy_uri], params[:provider_region]]
when ems == ManageIQ::Providers::Vmware::CloudManager
[params[:default_hostname], params[:default_api_port], user, password, true]
when ems == ManageIQ::Providers::Google::CloudManager
[params[:project], params[:service_account], {:service => "compute"}]
end
end

Expand Down
42 changes: 29 additions & 13 deletions spec/controllers/ems_cloud_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,28 +290,44 @@

context "#update_ems_button_validate" do
let(:mocked_ems) { double(ManageIQ::Providers::Openstack::CloudManager, :id => 1) }
it "calls authentication_check with save = false if validation is done for an existing record" do

it "calls authentication_check with save = false" do
allow(controller).to receive(:set_ems_record_vars)
allow(controller).to receive(:render)
controller.instance_variable_set(:@_params,
:button => "validate",
:id => mocked_ems.id,
:cred_type => "default")
allow(controller).to receive(:find_record_with_rbac).and_return(mocked_ems)
controller.instance_variable_set(:@_params, :button => "validate", :id => mocked_ems.id, :cred_type => "default")

expect(mocked_ems).to receive(:authentication_check).with("default", hash_including(:save => false))
controller.send(:update_ems_button_validate, mocked_ems)
controller.send(:update_ems_button_validate)
end
end

context "#create_ems_button_validate" do
let(:mocked_infra_class) { ManageIQ::Providers::Redhat::InfraManager }
let(:mocked_infra) { double(ManageIQ::Providers::Redhat::InfraManager) }

it "queues the authentication type if it is a cloud provider" do
allow(controller).to receive(:render)
allow(ExtManagementSystem).to receive(:model_from_emstype)
controller.instance_variable_set(:@_params, :controller => "ems_cloud")

it "calls authentication_check with save = false if validation is done for a new record" do
expect(MiqTask).to receive(:generic_action_with_callback)
expect(MiqTask).to receive(:wait_for_taskid)
controller.send(:create_ems_button_validate)
end

it "does not queue the authentication check if it is not a cloud provider" do
allow(controller).to receive(:set_ems_record_vars)
allow(controller).to receive(:render)
controller.instance_variable_set(:@_params,
:button => "validate",
:default_password => "[FILTERED]",
:cred_type => "default")
expect(mocked_ems).to receive(:authentication_check).with("default", hash_including(:save => false))
controller.send(:update_ems_button_validate, mocked_ems)
allow(ExtManagementSystem).to receive(:model_from_emstype).and_return(mocked_infra_class)
allow(mocked_infra_class).to receive(:new).and_return(mocked_infra)
controller.instance_variable_set(:@_params, :controller => "ems_infra", :cred_type => "default")

expect(mocked_infra).to receive(:authentication_check).with("default", hash_including(:save => false))
controller.send(:create_ems_button_validate)
end
end

describe "#test_toolbars" do
before do
allow(controller).to receive(:check_privileges).and_return(true)
Expand Down

0 comments on commit 176c8ca

Please sign in to comment.