-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #36971 - GUI to allow cloning of Ansible roles from VCS
- Loading branch information
Showing
21 changed files
with
1,339 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V2 | ||
class VcsCloneController < ::Api::V2::BaseController | ||
include ::ForemanAnsible::ProxyAPI | ||
|
||
rescue_from ActionController::ParameterMissing do |e| | ||
render json: { 'error' => e.message }, status: :bad_request | ||
end | ||
|
||
rescue_from Git::GitExecuteError do |_e| | ||
head :internal_server_error | ||
end | ||
|
||
api :GET, '/vcs_clone/get_repo_info', N_('Returns information about the repo') | ||
param :vcs_url, String, N_('Url of the repo'), :required => true | ||
error 400, :desc => N_('Parameter unfulfilled') | ||
error 500, :desc => N_('Git error') | ||
def repo_information | ||
vcs_url = params.require(:vcs_url) | ||
remote = Git.ls_remote(vcs_url).slice('head', 'branches', 'tags') | ||
remote['vcs_url'] = vcs_url | ||
render json: remote | ||
end | ||
|
||
api :GET, '/vcs_clone/get_installed_roles', N_('Returns an array of roles installed on the provided proxy') | ||
formats ['json'] | ||
param :smart_proxy, Array, N_('Name of the SmartProxy'), :required => true | ||
error 400, :desc => N_('Parameter unfulfilled') | ||
error 500, :desc => N_('Internal server error') | ||
def installed_roles | ||
smart_proxy = params.require(:smart_proxy) | ||
ansible_proxy = SmartProxy.find_by(name: smart_proxy) | ||
proxy_api = find_proxy_api(ansible_proxy) | ||
installed = proxy_api.list_installed | ||
render json: installed | ||
rescue Foreman::Exception | ||
head :internal_server_error | ||
end | ||
|
||
api :POST, '/vcs_clone/install', N_('Launches a task to install the provided role') | ||
formats ['json'] | ||
param :repo_info, Hash, :desc => N_('Dictionary containing info about the role to be installed') do | ||
param :vcs_url, String, :desc => N_('Url of the repo'), :required => true | ||
param :name, String, :desc => N_('Name of the repo'), :required => true | ||
param :ref, String, :desc => N_('Branch / Tag / Commit reference'), :required => true | ||
param :update, String, :desc => N_('Whether an existing role with the provided name should be updated'), :default => false | ||
end | ||
param :smart_proxy, Array, N_('Array of SmartProxies the role should get installed to') | ||
error 400, :desc => N_('Parameter unfulfilled') | ||
error 500, :desc => N_('Internal server error') | ||
def install_role | ||
payload = params.require(:vcs_clone). | ||
permit( | ||
repo_info: [ | ||
:vcs_url, | ||
:name, | ||
:ref, | ||
:update | ||
], | ||
smart_proxy: [] | ||
) | ||
|
||
payload['repo_info']['update'] = false unless payload['repo_info'].key? 'update' | ||
|
||
# 'smart_proxy' is an array to later allow a role to be installed on multiple SPs | ||
ansible_proxy = SmartProxy.find_by(name: payload['smart_proxy'][0]) | ||
|
||
job = CloneAnsibleRole.perform_later(payload['repo_info'], ansible_proxy) | ||
task = ForemanTasks::Task.find_by(external_id: job.provider_job_id) | ||
render json: { | ||
task: task | ||
}, status: :ok | ||
rescue Foreman::Exception | ||
head :internal_server_error | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class CloneAnsibleRole < ::ApplicationJob | ||
queue_as :default | ||
|
||
def humanized_name | ||
'Clone Ansible Role from VCS' | ||
end | ||
|
||
def perform(repo_info, proxy) | ||
vcs_cloner = ForemanAnsible::VcsCloner.new(proxy) | ||
vcs_cloner.install_role repo_info | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module ForemanAnsible | ||
class VcsCloner | ||
include ::ForemanAnsible::ProxyAPI | ||
|
||
def initialize(proxy = nil) | ||
@ansible_proxy = proxy | ||
end | ||
|
||
def install_role(info) | ||
proxy_api.vcs_clone_install info | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_plugin_helper' | ||
|
||
module Api | ||
module V2 | ||
class VcsCloneControllerTest < ActionController::TestCase | ||
describe('#repo_information') do | ||
test 'requests repo-information' do | ||
Git.stubs(:ls_remote).returns({ 'head' => {}, 'branches' => {}, 'tags' => {} }) | ||
|
||
get :repo_information, params: { "vcs_url": 'https://github.com/theforeman/foreman.git' }, session: set_session_user | ||
response = JSON.parse(@response.body) | ||
assert_response :success | ||
assert_equal({ 'head' => {}, | ||
'branches' => {}, | ||
'tags' => {}, | ||
'vcs_url' => 'https://github.com/theforeman/foreman.git' }, response) | ||
end | ||
|
||
test 'handles missing parameter' do | ||
get :repo_information, params: { "vcs_urll": 'https://github.com/theforeman/foreman.git' }, session: set_session_user | ||
response = JSON.parse(@response.body) | ||
assert_response :bad_request | ||
assert_equal({ 'error' => "param is missing or the value is empty: vcs_url\nDid you mean? vcs_urll\n vcs_clone\n controller\n action" }, | ||
response) | ||
end | ||
|
||
test 'handles exception in "GIT" module' do | ||
Git.stubs(:ls_remote).raises(Git::GitExecuteError) | ||
|
||
get :repo_information, params: { "vcs_url": 'https://github.com/theforeman/foreman.git' }, session: set_session_user | ||
assert_response :internal_server_error | ||
end | ||
end | ||
|
||
describe '#installed_roles' do | ||
test 'requests installed roles' do | ||
roles_array = %w[ansible_role_1 ansible_role_2] | ||
|
||
ProxyAPI::Ansible.any_instance.expects(:list_installed).returns(roles_array) | ||
|
||
get :installed_roles, params: { "smart_proxy": FactoryBot.create(:smart_proxy, :with_ansible).name }, session: set_session_user | ||
response = JSON.parse(@response.body) | ||
|
||
assert_response :success | ||
assert_equal(roles_array, response) | ||
end | ||
|
||
test 'handles erroneous smart_proxy value' do | ||
get :installed_roles, params: { "smart_proxy": 'something_unknown' }, session: set_session_user | ||
|
||
assert_response :internal_server_error | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.