From b8b0a737651d4ee591c1ab68bb2833b81d2d426c Mon Sep 17 00:00:00 2001 From: Douglas Gabriel Date: Fri, 26 Jan 2018 14:05:09 -0300 Subject: [PATCH] Adding method to change password of a provider on its client --- .../providers/physical_infra_manager.rb | 45 +++++++++++++++++++ app/models/mixins/supports_feature_mixin.rb | 1 + .../physical_infra_manager_spec.rb | 13 ++++++ 3 files changed, 59 insertions(+) diff --git a/app/models/manageiq/providers/physical_infra_manager.rb b/app/models/manageiq/providers/physical_infra_manager.rb index 8eb7678e631..e76e15eaf56 100644 --- a/app/models/manageiq/providers/physical_infra_manager.rb +++ b/app/models/manageiq/providers/physical_infra_manager.rb @@ -50,5 +50,50 @@ def console_supported? def console_url raise MiqException::Error, _("Console not supported") end + + supports :change_password + + # Changes the password of userId on provider client and database. + # + # @param [current_password] password currently used for connected userId in provider client + # @param [new_password] password that will replace the current one + # + # @return [Boolean] true if the password was changed successfully + def change_password(current_password, new_password, auth_type = :default) + raw_change_password(current_password, new_password) + update_authentication(auth_type => {:userid => authentication_userid, :password => new_password}) + + true + end + + def change_password_queue(userid, current_password, new_password, auth_type = :default) + task_opts = { + :action => "Changing the password for Physical Provider named '#{name}'", + :userid => userid + } + + queue_opts = { + :class_name => self.class.name, + :instance_id => id, + :method_name => 'change_password', + :role => 'ems_operations', + :zone => my_zone, + :args => [current_password, new_password, auth_type] + } + + MiqTask.generic_action_with_callback(task_opts, queue_opts) + end + + # This method must provide a way to change password on provider client. + # + # @param [_current_password] password currently used for connected userId in provider client + # @param [_new_password] password that will replace the current one + # + # @return [Boolean] true if the password was changed successfully + # + # @raise [MiqException::Error] containing the error message if was not changed successfully + def raw_change_password(_current_password, _new_password) + raise NotImplementedError, _("must be implemented in subclass") + end end end diff --git a/app/models/mixins/supports_feature_mixin.rb b/app/models/mixins/supports_feature_mixin.rb index bb6e8464d9d..139d5bba4e9 100644 --- a/app/models/mixins/supports_feature_mixin.rb +++ b/app/models/mixins/supports_feature_mixin.rb @@ -137,6 +137,7 @@ module SupportsFeatureMixin :block_storage => 'Block Storage', :object_storage => 'Object Storage', :vm_import => 'VM Import', + :change_password => 'Change Password' }.freeze # Whenever this mixin is included we define all features as unsupported by default. diff --git a/spec/models/manageiq/providers/physical_infra_manager/physical_infra_manager_spec.rb b/spec/models/manageiq/providers/physical_infra_manager/physical_infra_manager_spec.rb index 0c63d8a518f..101440b997e 100644 --- a/spec/models/manageiq/providers/physical_infra_manager/physical_infra_manager_spec.rb +++ b/spec/models/manageiq/providers/physical_infra_manager/physical_infra_manager_spec.rb @@ -61,4 +61,17 @@ :hostname => "0.0.0.0") expect { ps.console_url }.to raise_error(MiqException::Error) end + + context "#change_password" do + it "should update the provider password" do + pim = FactoryGirl.create(:generic_physical_infra, + :name => "LXCA", + :hostname => "0.0.0.0") + allow(pim).to receive(:raw_change_password) { true } + + current_password = "current_pass" + new_password = "new_pass" + expect(pim.change_password(current_password, new_password)).to be_truthy + end + end end