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
Fixes #15979 - Add API for Ansible roles #26
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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, '/asnible/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) | ||
assert response['results'].length > 0 | ||
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
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,31 @@ | ||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo, I'll fix it on merge.