Skip to content

Commit

Permalink
Add SCM credential support for EmbeddedAnsible
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryguy committed Aug 5, 2019
1 parent e530796 commit 1043290
Show file tree
Hide file tree
Showing 4 changed files with 376 additions and 275 deletions.
7 changes: 4 additions & 3 deletions app/models/git_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class GitRepository < ApplicationRecord
include AuthenticationMixin
belongs_to :authentication

GIT_REPO_DIRECTORY = Rails.root.join('data/git_repos')

Expand Down Expand Up @@ -158,9 +159,9 @@ def handling_worktree_errors
def worktree_params
params = {:path => directory_name}
params[:certificate_check] = method(:self_signed_cert_cb) if verify_ssl == OpenSSL::SSL::VERIFY_NONE
if authentications.any?
params[:username] = default_authentication.userid
params[:password] = default_authentication.password
if (auth = authentication || default_authentication)
params[:username] = auth.userid
params[:password] = auth.password
end
params
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScri
default_value_for :scm_type, "git"
default_value_for :scm_branch, "master"

belongs_to :git_repository, :dependent => :destroy
belongs_to :git_repository, :autosave => true, :dependent => :destroy
before_validation :sync_git_repository

include ManageIQ::Providers::EmbeddedAnsible::CrudCommon

Expand Down Expand Up @@ -46,12 +47,35 @@ def raw_delete_in_provider
end

def git_repository
super || begin
transaction do
update!(:git_repository => GitRepository.create!(:url => scm_url))
(super || (ensure_git_repository && super)).tap { |r| sync_git_repository(r) }
end

private def ensure_git_repository
transaction do
repo = GitRepository.create!(attrs_for_sync_git_repository)
if new_record?
self.git_repository_id = repo.id
elsif !update_columns(:git_repository_id => repo.id) # rubocop:disable Rails/SkipsModelValidations
raise ActiveRecord::RecordInvalid, "git_repository_id could not be set"
end
super
end
true
end

private def sync_git_repository(git_repository = nil)
return unless name_changed? || scm_url_changed? || authentication_id_changed?

git_repository ||= self.git_repository
git_repository.attributes = attrs_for_sync_git_repository
end

private def attrs_for_sync_git_repository
{
:name => name,
:url => scm_url,
:authentication_id => authentication_id,
:verify_ssl => OpenSSL::SSL::VERIFY_NONE
}
end

def sync
Expand Down
4 changes: 3 additions & 1 deletion spec/factories/configuration_script_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@

factory :embedded_ansible_configuration_script_source,
:parent => :configuration_script_source,
:class => "ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScriptSource"
:class => "ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScriptSource" do
scm_url { "https://example.com/foo.git" }
end
end
Loading

0 comments on commit 1043290

Please sign in to comment.