Skip to content

Commit

Permalink
Merge pull request ManageIQ#14006 from jntullo/enhancement/repository…
Browse files Browse the repository at this point in the history
…_create

create configuration script sources
  • Loading branch information
abellotti authored Mar 16, 2017
2 parents 8d7ad61 + f2e1e79 commit e3e4ac4
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/controllers/api/configuration_script_sources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,31 @@ def delete_resource(type, id, _data = {})
action_result(false, err.to_s)
end

def create_resource(_type, _id, data)
validate_attrs(data)
manager_id = parse_id(data['manager_resource'], :providers)
raise 'Must specify a valid manager_resource href or id' unless manager_id
manager = resource_search(manager_id, :providers, collection_class(:providers))
type = ConfigurationScriptSource.class_for_manager(manager)
raise "ConfigurationScriptSource cannot be added to #{manager_ident(manager)}" unless type.respond_to?(:create_in_provider_queue)
task_id = type.create_in_provider_queue(manager.id, data.except('manager_resource'))
action_result(true, "Creating ConfigurationScriptSource for #{manager_ident(manager)}", :task_id => task_id)
rescue => err
action_result(false, err.to_s)
end

private

def config_script_src_ident(config_script_src)
"ConfigurationScriptSource id:#{config_script_src.id} name: '#{config_script_src.name}'"
end

def validate_attrs(data)
raise 'Must supply a manager resource' unless data['manager_resource']
end

def manager_ident(manager)
"Manager id:#{manager.id} name: '#{manager.name}'"
end
end
end
5 changes: 5 additions & 0 deletions app/models/configuration_script_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ class ConfigurationScriptSource < ApplicationRecord
belongs_to :manager, :class_name => "ExtManagementSystem"

virtual_total :total_payloads, :configuration_script_payloads

def self.class_for_manager(manager)
type = "#{manager.type}::ConfigurationScriptSource"
descendants.find { |klass| klass.name == type }
end
end
2 changes: 2 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,8 @@
:identifier: embedded_configuration_script_source_edit
- :name: delete
:identifier: embedded_configuration_script_source_delete
- :name: create
:identifier: embedded_configuration_script_source_add
:resource_actions:
:get:
- :name: read
Expand Down
8 changes: 8 additions & 0 deletions spec/models/configuration_script_source_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
describe ConfigurationScriptSource do
context '.class_for_manager' do
it 'returns the correct configuration script source' do
ems = FactoryGirl.create(:embedded_automation_manager_ansible)
expect(described_class.class_for_manager(ems)).to eq(ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScriptSource)
end
end
end
110 changes: 110 additions & 0 deletions spec/requests/api/configuration_script_sources_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
let(:provider) { FactoryGirl.create(:ext_management_system) }
let(:config_script_src) { FactoryGirl.create(:ansible_configuration_script_source, :manager => provider) }
let(:config_script_src_2) { FactoryGirl.create(:ansible_configuration_script_source, :manager => provider) }
let(:ansible_provider) { FactoryGirl.create(:provider_ansible_tower, :with_authentication) }
let(:manager) { ansible_provider.managers.first }

describe 'GET /api/configuration_script_sources' do
it 'lists all the configuration script sources with an appropriate role' do
Expand Down Expand Up @@ -205,6 +207,114 @@

expect(response).to have_http_status(:forbidden)
end

let(:create_params) do
{
:manager_resource => { :href => providers_url(manager.id) },
:description => 'Description',
:name => 'My Project',
:related => {}
}
end

it 'creates a configuration script source with appropriate role' do
api_basic_authorize collection_action_identifier(:configuration_script_sources, :create, :post)

expected = {
'results' => [
a_hash_including(
'success' => true,
'message' => a_string_including('Creating ConfigurationScriptSource'),
'task_id' => a_kind_of(Numeric)
)
]
}
run_post(configuration_script_sources_url, create_params)

expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it 'create a new configuration script source with manager_resource id' do
api_basic_authorize collection_action_identifier(:configuration_script_sources, :create, :post)
create_params[:manager_resource] = { :id => manager.id }

expected = {
'results' => [
a_hash_including(
'success' => true,
'message' => "Creating ConfigurationScriptSource for Manager id:#{manager.id} name: '#{manager.name}'",
'task_id' => a_kind_of(Numeric)
)
]
}
run_post(configuration_script_sources_url, create_params)

expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it 'can create new configuration script sources in bulk' do
api_basic_authorize collection_action_identifier(:configuration_script_sources, :create, :post)

expected = {
'results' => [
a_hash_including(
'success' => true,
'message' => a_string_including('Creating ConfigurationScriptSource'),
'task_id' => a_kind_of(Numeric)
),
a_hash_including(
'success' => true,
'message' => a_string_including('Creating ConfigurationScriptSource'),
'task_id' => a_kind_of(Numeric)
)
]
}
run_post(configuration_script_sources_url, :resources => [create_params, create_params])

expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it 'requires a manager_resource to be specified' do
api_basic_authorize collection_action_identifier(:configuration_script_sources, :create, :post)

run_post(configuration_script_sources_url, :resources => [create_params.except(:manager_resource)])

expected = {
'results' => [{
'success' => false,
'message' => 'Must supply a manager resource'
}]
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it 'requires a valid manager' do
api_basic_authorize collection_action_identifier(:configuration_script_sources, :create, :post)
create_params[:manager_resource] = { :href => users_url(10) }

run_post(configuration_script_sources_url, :resources => [create_params])

expected = {
'results' => [{
'success' => false,
'message' => 'Must specify a valid manager_resource href or id'
}]
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it 'forbids creation of new configuration script source without an appropriate role' do
api_basic_authorize

run_post(configuration_script_sources_url, create_params)

expect(response).to have_http_status(:forbidden)
end
end

describe 'DELETE /api/configuration_script_sources/:id' do
Expand Down

0 comments on commit e3e4ac4

Please sign in to comment.