Skip to content

Commit

Permalink
api: copy orchestration template
Browse files Browse the repository at this point in the history
For the SSUI we need to have possibility to copy OrchestrationTemplate
via API
  • Loading branch information
Artyom Lukianov committed Jan 4, 2017
1 parent 7bfb5f3 commit d3aeb20
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/controllers/api/orchestration_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,15 @@ def delete_resource(type, id, data = {})
super
resource.raw_destroy if resource.kind_of?(OrchestrationTemplateVnfd)
end

def copy_resource(type, id, data = {})
resource = resource_search(id, type, collection_class(type))
resource.dup.tap do |new_resource|
new_resource.assign_attributes(data)
new_resource.save!
end
rescue => err
raise BadRequestError, "Failed to copy orchestration template - #{err}"
end
end
end
4 changes: 4 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2085,12 +2085,16 @@
:identifier: orchestration_template_edit
- :name: delete
:identifier: orchestration_template_remove
- :name: copy
:identifier: orchestration_template_copy
:resource_actions:
:post:
- :name: edit
:identifier: orchestration_template_edit
- :name: delete
:identifier: orchestration_template_remove
- :name: copy
:identifier: orchestration_template_copy
:delete:
- :name: delete
:identifier: orchestration_template_remove
Expand Down
84 changes: 84 additions & 0 deletions spec/requests/api/orchestration_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,88 @@
expect(OrchestrationTemplate.exists?(hot.id)).to be_falsey
end
end

context 'orchestration template copy' do
it 'forbids orchestration template copy without an appropriate role' do
api_basic_authorize

orchestration_template = FactoryGirl.create(:orchestration_template_cfn)
new_content = "{ 'Description': 'Test content 1' }\n"

run_post(
orchestration_templates_url(orchestration_template.id),
gen_request(:copy, :content => new_content)
)

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

it 'forbids orchestration template copy with no content specified' do
api_basic_authorize collection_action_identifier(:orchestration_templates, :copy)

orchestration_template = FactoryGirl.create(:orchestration_template_cfn)

run_post(orchestration_templates_url(orchestration_template.id), gen_request(:copy))

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

it 'can copy single orchestration template with a different content' do
api_basic_authorize collection_action_identifier(:orchestration_templates, :copy)

orchestration_template = FactoryGirl.create(:orchestration_template_cfn)
new_content = "{ 'Description': 'Test content 1' }\n"

expected = {
'content' => new_content,
'name' => orchestration_template.name,
'description' => orchestration_template.description,
'draft' => orchestration_template.draft,
'orderable' => orchestration_template.orderable
}

expect do
run_post(
orchestration_templates_url(orchestration_template.id),
gen_request(:copy, :content => new_content)
)
end.to change(OrchestrationTemplateCfn, :count).by(1)

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
expect(response.parsed_body['id']).to_not equal(orchestration_template.id)
end

it 'can copy multiple orchestration templates with a different content' do
api_basic_authorize collection_action_identifier(:orchestration_templates, :copy)

orchestration_template = FactoryGirl.create(:orchestration_template_cfn)
new_content = "{ 'Description': 'Test content 1' }\n"
orchestration_template_2 = FactoryGirl.create(:orchestration_template_cfn)
new_content_2 = "{ 'Description': 'Test content 2' }\n"

expected = {
'results' => a_collection_containing_exactly(
a_hash_including('content' => new_content),
a_hash_including('content' => new_content_2)
)
}

expect do
run_post(
orchestration_templates_url,
gen_request(
:copy,
[
{:id => orchestration_template.id, :content => new_content},
{:id => orchestration_template_2.id, :content => new_content_2}
]
)
)
end.to change(OrchestrationTemplateCfn, :count).by(2)

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

0 comments on commit d3aeb20

Please sign in to comment.