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

Support EmbeddedAnsible SCM credentials #19027

Merged
merged 1 commit into from
Aug 6, 2019
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
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
Copy link
Member

Choose a reason for hiding this comment

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

Just caught this re: #19027 (comment)

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) }
carbonin marked this conversation as resolved.
Show resolved Hide resolved
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?
Copy link
Member Author

Choose a reason for hiding this comment

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

FYI I really tried to DRY this up with respect to the attrs_for_sync_git_repository, by using something like changed.include_any?, but because one side has :url and the other side has :scm_url it got really ugly, so I'd prefer to just leave it this way for now.


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" }
Copy link
Member Author

Choose a reason for hiding this comment

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

This PR effectively makes scm_url a required attribute for a ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScriptSource, which I think is ok. Without this line you will consistently get a validation error on a GitRepository#url format.

end
end
Loading