Skip to content

Commit

Permalink
Merge pull request #195 from jntullo/bz/graceful_fail_on_delete
Browse files Browse the repository at this point in the history
Gracefully fail on resource create
(cherry picked from commit 4812838)

https://bugzilla.redhat.com/show_bug.cgi?id=1513117
  • Loading branch information
chrisarcand authored and simaishi committed Nov 14, 2017
1 parent 5fd0240 commit 2450b78
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/controllers/api/base_controller/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def add_resource(type, _id, data)
data.delete(sc.to_s)
end
end
validate_type(klass, data['type']) if data['type']
resource = klass.new(data)
if resource.save
add_subcollection_data_to_resource(resource, type, subcollection_data)
Expand Down Expand Up @@ -122,6 +123,12 @@ def refresh_dialog_fields_action(dialog, refresh_fields, resource_ident)

private

def validate_type(klass, type)
klass.descendant_get(type)
rescue ArgumentError => err
raise BadRequestError, "Invalid type #{type} specified - #{err}"
end

def add_subcollection_data_to_resource(resource, type, subcollection_data)
subcollection_data.each do |sc, sc_data|
typed_target = "#{sc}_assign_resource"
Expand Down
15 changes: 15 additions & 0 deletions spec/requests/orchestration_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@

expect_bad_request(/Resource id or href should not be specified/)
end

it 'fails gracefully with invalid type specified' do
api_basic_authorize collection_action_identifier(:orchestration_templates, :create)

post(api_orchestration_templates_url, :params => { :type => 'OrchestrationTemplateCfn' })

expected = {
'error' => a_hash_including(
'kind' => 'bad_request',
'message' => a_string_including('Invalid type OrchestrationTemplateCfn specified')
)
}
expect(response).to have_http_status(:bad_request)
expect(response.parsed_body).to include(expected)
end
end

context 'orchestration_template edit' do
Expand Down

0 comments on commit 2450b78

Please sign in to comment.