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

create service template REST api #12594

Merged
merged 9 commits into from
Feb 15, 2017
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: 7 additions & 0 deletions app/controllers/api/service_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ class ServiceTemplatesController < BaseController

before_action :set_additional_attributes, :only => [:show]

def create_resource(_type, _id, data)
catalog_item_type = ServiceTemplate.class_from_request_data(data)
catalog_item_type.create_catalog_item(data.deep_symbolize_keys, @auth_user)
rescue => err
raise BadRequestError, "Could not create Service Template - #{err}"
end

private

def set_additional_attributes
Expand Down
10 changes: 10 additions & 0 deletions app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ def self.create_catalog_item(options, auth_user)
end
end

def self.class_from_request_data(data)
request_type = data['prov_type']
if request_type.include?('generic_')
generic_type = request_type.split('generic_').last
"ServiceTemplate#{generic_type.camelize}".constantize
Copy link
Member

Choose a reason for hiding this comment

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

/cc @bzwei @gmcculloug you guys ok with this constantize ?

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

else
ServiceTemplate
end
end

def readonly?
return true if super
blueprint.try(:published?)
Expand Down
2 changes: 2 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,8 @@
:identifier: catalogitem_edit
- :name: delete
:identifier: catalogitem_delete
- :name: create
:identifier: atomic_catalogitem_new
:subcollection_actions:
:post:
- :name: edit
Expand Down
14 changes: 14 additions & 0 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,20 @@
end
end

describe '.class_from_request_data' do
it 'returns the correct generic type' do
template_class = ServiceTemplate.class_from_request_data('prov_type' => 'generic_ansible_tower')

expect(template_class).to eq(ServiceTemplateAnsibleTower)
end

it 'returns the correct non generic type' do
template_class = ServiceTemplate.class_from_request_data('prov_type' => 'amazon')

expect(template_class).to eq(ServiceTemplate)
end
end

describe '#create_catalog_item' do
let(:user) { FactoryGirl.create(:user_with_group) }
let(:ra1) { FactoryGirl.create(:resource_action, :action => 'Provision') }
Expand Down
124 changes: 124 additions & 0 deletions spec/requests/api/service_templates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,128 @@
expect(response).to have_http_status(:ok)
end
end

describe "Service Templates create" do
let(:ems) { FactoryGirl.create(:ems_amazon) }
let(:vm) { FactoryGirl.create(:vm_amazon, :ems_id => ems.id) }
let(:flavor) { FactoryGirl.create(:flavor_amazon) }
let(:dialog) { FactoryGirl.create(:miq_dialog_provision) }
let(:service_dialog) { FactoryGirl.create(:dialog) }
let(:template_parameters) do
{
:name => 'Atomic Service Template',
:service_type => 'atomic',
:prov_type => 'amazon',
:display => 'false',
:config_info => {
:miq_request_dialog_name => dialog.name,
:placement_auto => [true, 1],
:number_of_vms => [1, '1'],
:src_vm_id => [vm.id, vm.name],
:vm_name => 'AtomicVMName',
:schedule_type => ["immediately", "Immediately on Approval"],
:instance_type => [flavor.id, flavor.name],
:src_ems_id => [ems.id, ems.name],
:provision => {
:fqname => ra1.fqname,
:dialog_id => service_dialog.id
},
:retirement => {
:fqname => ra2.fqname,
:dialog_id => service_dialog.id
}
}
}
end

it 'rejects requests without appropriate role' do
api_basic_authorize

run_post(service_templates_url, :name => 'foobar')

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

it 'can create a single service template ' do
api_basic_authorize collection_action_identifier(:service_templates, :create)

expected = {
'results' => a_collection_including(
a_hash_including(
'name' => 'Atomic Service Template',
'display' => false,
'service_type' => 'atomic',
'prov_type' => 'amazon'
)
)
}

expect do
run_post(service_templates_url, template_parameters)
end.to change(ServiceTemplate, :count).by(1)
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it 'can create multiple service templates' do
api_basic_authorize collection_action_identifier(:service_templates, :create)

template_hash = {
'name' => 'Atomic Service Template',
'display' => false,
'service_type' => 'atomic',
'prov_type' => 'amazon'
}
expected = {
'results' => a_collection_including(
a_hash_including(
template_hash
),
a_hash_including(
template_hash
)
)
}
expect do
run_post(service_templates_url, :action => 'create', :resources => [template_parameters, template_parameters])
end.to change(ServiceTemplate, :count).by(2)
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it 'can create other resource types' do
api_basic_authorize collection_action_identifier(:service_templates, :create)
template = FactoryGirl.create(:orchestration_template)
template_parameters = {
:name => 'Orchestration Template',
:service_type => 'atomic',
:prov_type => 'generic_orchestration',
:display => 'false',
:description => 'a description',
:config_info => {
:template_id => template.id,
:manager_id => ems.id,
:provision => {
:fqname => ra1.fqname,
:dialog_id => service_dialog.id
},
:retirement => {
:fqname => ra2.fqname,
:dialog_id => service_dialog.id
}
}
}

expected = {
'results' => [a_hash_including(
'type' => 'ServiceTemplateOrchestration'
)]
}
expect do
run_post(service_templates_url, template_parameters)
end.to change(ServiceTemplateOrchestration, :count).by(1)
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end
end
end