Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #15979 - Add API for Ansible roles
- Loading branch information
Showing
17 changed files
with
215 additions
and
50 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
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,52 @@ | ||
module Api | ||
module V2 | ||
# API controller for Ansible Roles | ||
class AnsibleRolesController < ::Api::V2::BaseController | ||
include ::Api::Version2 | ||
|
||
before_action :find_resource, :only => [:show, :destroy] | ||
before_action :find_proxy, :only => [:import, :obsolete] | ||
before_action :create_importer, :only => [:import, :obsolete] | ||
|
||
api :GET, '/ansible/ansible_roles/:id', N_('Show role') | ||
param :id, :identifier, :required => true | ||
def show | ||
end | ||
|
||
api :GET, '/ansible/ansible_roles', N_('List Ansible roles') | ||
param_group :search_and_pagination, ::Api::V2::BaseController | ||
def index | ||
@ansible_roles = resource_scope_for_index | ||
end | ||
|
||
api :DELETE, '/ansible/ansible_roles/:id', N_('Deletes Ansible role') | ||
param :id, :identifier, :required => true | ||
def destroy | ||
process_response @ansible_role.destroy | ||
end | ||
|
||
api :POST, '/ansible/ansible_roles/import', N_('Import Ansible roles') | ||
param :proxy, Hash, N_('Smart Proxy to import from') | ||
def import | ||
@imported = @importer.import! | ||
end | ||
|
||
api :POST, '/ansible/ansible_roles/obsolete', N_('Obsolete Ansible roles') | ||
param :proxy, Hash, N_('Smart Proxy to import from') | ||
def obsolete | ||
@obsoleted = @importer.obsolete! | ||
end | ||
|
||
private | ||
|
||
def find_proxy | ||
return nil unless params[:proxy] | ||
@proxy = SmartProxy.authorized(:view_smart_proxies).find(params[:proxy]) | ||
end | ||
|
||
def create_importer | ||
@importer = ForemanAnsible::ApiRolesImporter.new(@proxy) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module ForemanAnsible | ||
# imports Ansible roles through API | ||
class ApiRolesImporter < RolesImporter | ||
def import! | ||
new_roles = import_role_names[:new] | ||
new_roles.map(&:save) | ||
new_roles | ||
end | ||
|
||
def obsolete! | ||
obsolete_roles = import_role_names[:obsolete] | ||
obsolete_roles.map(&:destroy) | ||
obsolete_roles | ||
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,26 @@ | ||
module ForemanAnsible | ||
# imports ansible roles through UI | ||
class UiRolesImporter < RolesImporter | ||
def import! | ||
import_role_names | ||
end | ||
|
||
def finish_import(changes) | ||
return unless changes.present? | ||
create_new_roles changes['new'] if changes['new'] | ||
delete_old_roles changes['obsolete'] if changes['obsolete'] | ||
end | ||
|
||
def create_new_roles(changes) | ||
changes.values.each do |new_role| | ||
::AnsibleRole.create(JSON.parse(new_role)) | ||
end | ||
end | ||
|
||
def delete_old_roles(changes) | ||
changes.values.each do |old_role| | ||
::AnsibleRole.find(JSON.parse(old_role)['id']).destroy | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
collection @imported => :imported | ||
|
||
extends 'api/v2/ansible_roles/show' |
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,3 @@ | ||
collection @ansible_roles | ||
|
||
extends 'api/v2/ansible_roles/show' |
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,3 @@ | ||
collection @obsoleted => :obsoleted | ||
|
||
extends 'api/v2/ansible_roles/show' |
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,3 @@ | ||
object @ansible_role | ||
|
||
attributes :name, :created_at, :updated_at |
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,24 @@ | ||
require 'test_plugin_helper' | ||
|
||
module Api | ||
module V2 | ||
class AnsibleRolesControllerTest < ActionController::TestCase | ||
setup do | ||
@role = FactoryGirl.create(:ansible_role) | ||
end | ||
|
||
test 'should get index' do | ||
get :index, {}, set_session_user | ||
response = JSON.parse(@response.body) | ||
refute_empty response['results'] | ||
assert_response :success | ||
end | ||
|
||
test 'should destroy' do | ||
delete :destroy, { :id => @role.id }, set_session_user | ||
assert_response :ok | ||
refute AnsibleRole.exists?(@role.id) | ||
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
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,28 @@ | ||
require 'test_plugin_helper' | ||
# unit tests for ApiRolesImporter | ||
class ApiRolesImporterTest < ActiveSupport::TestCase | ||
setup do | ||
@importer = ForemanAnsible::ApiRolesImporter.new | ||
first_name = 'test_user.test_name' | ||
second_name = 'some_user.some_role' | ||
@test_roles = [AnsibleRole.new(:name => first_name), | ||
AnsibleRole.new(:name => second_name)] | ||
end | ||
|
||
test 'should import roles' do | ||
@importer.stubs(:import_role_names).returns(:new => @test_roles) | ||
res = @importer.import! | ||
assert_equal 2, res.count | ||
assert AnsibleRole.find_by :name => @test_roles.first.name | ||
assert AnsibleRole.find_by :name => @test_roles.last.name | ||
end | ||
|
||
test 'should obsolete roles' do | ||
@importer.stubs(:import_role_names).returns(:obsolete => @test_roles) | ||
@test_roles.map(&:save) | ||
res = @importer.obsolete! | ||
assert_equal 2, res.count | ||
refute AnsibleRole.find_by :name => @test_roles.first.name | ||
refute AnsibleRole.find_by :name => @test_roles.last.name | ||
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,30 @@ | ||
require 'test_plugin_helper' | ||
# unit tests for UiRolesImporter | ||
class UiRolesImporterTest < ActiveSupport::TestCase | ||
setup do | ||
changed_roles | ||
@importer = ForemanAnsible::UiRolesImporter.new | ||
end | ||
|
||
test 'should create new role' do | ||
refute AnsibleRole.find_by(:name => @new_role[:name]) | ||
@importer.create_new_roles(@changes['new']) | ||
assert AnsibleRole.find_by(:name => @new_role[:name]) | ||
end | ||
|
||
test 'should delete old roles' do | ||
assert AnsibleRole.find_by(:name => @role.name) | ||
@importer.delete_old_roles(@changes['obsolete']) | ||
refute AnsibleRole.find_by(:name => @role.name) | ||
end | ||
|
||
private | ||
|
||
def changed_roles | ||
@role = FactoryGirl.create(:ansible_role) | ||
new_role_name = 'test_role.foreman' | ||
@new_role = { :id => nil, :name => new_role_name } | ||
@changes = { 'new' => { 'test_role.foreman' => @new_role.to_json }, | ||
'obsolete' => { @role.name => @role.to_json } } | ||
end | ||
end |