Skip to content

Commit

Permalink
Merge pull request #93 from jntullo/enhancement/generic_object_actions
Browse files Browse the repository at this point in the history
Generic Object method calling
  • Loading branch information
abellotti authored Oct 6, 2017
2 parents c25a180 + 0ada42c commit d82d214
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/controllers/api/base_controller/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def queue_object_action(object, summary, options)
:role => options[:role] || nil,
}

queue_options.merge!(options[:user]) if options.key?(:user)
queue_options[:zone] = object.my_zone if %w(ems_operations smartstate).include?(options[:role])

MiqTask.generic_action_with_callback(task_options, queue_options)
Expand Down
32 changes: 32 additions & 0 deletions app/controllers/api/generic_objects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,41 @@ def edit_resource(type, id, data)

private

def resource_custom_action_names(resource)
return [] unless resource.respond_to?(:property_methods)
resource.property_methods
end

def invoke_custom_action(type, resource, action, data)
result = begin
desc = "Invoked method #{action} for Generic Object id: #{resource.id}"
task_id = queue_object_action(resource, desc, queue_args(action, data))
action_result(true, desc, :task_id => task_id)
rescue => err
action_result(false, err.to_s)
end
add_href_to_result(result, type, resource.id)
log_result(result)
result
end

def retrieve_generic_object_definition(data)
definition_id = parse_id(data['generic_object_definition'], :generic_object_definitions)
resource_search(definition_id, :generic_object_definitions, collection_class(:generic_object_definitions))
end

def queue_args(action, data)
current_user = User.current_user
{
:method_name => action,
:args => data['parameters'],
:role => 'automate',
:user => {
:user_id => current_user.id,
:group_id => current_user.current_group.id,
:tenant_id => current_user.current_tenant.id
}
}
end
end
end
1 change: 1 addition & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@
:options:
- :collection
- :subcollection
- :custom_actions
:verbs: *gpd
:klass: GenericObject
:subcollections:
Expand Down
26 changes: 23 additions & 3 deletions spec/requests/generic_objects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,20 @@
object.add_to_property_association('services', service)
end

it 'returns a generic object with property_attributes' do
api_basic_authorize action_identifier(:generic_objects, :read, :resource_actions, :get)
it 'returns a generic object with property_attributes and custom method actions' do
api_basic_authorize action_identifier(:generic_objects, :read, :resource_actions, :get),
action_identifier(:generic_objects, :delete, :resource_actions, :delete)

get(api_generic_object_url(nil, object))

expected = {
'name' => 'object 1',
'property_attributes' => { 'widget' => 'a widget string', 'is_something' => true }
'property_attributes' => { 'widget' => 'a widget string', 'is_something' => true },
'actions' => a_collection_including(
{ 'name' => 'delete', 'method' => 'post', 'href' => api_generic_object_url(nil, object)},
{ 'name' => 'method_a', 'method' => 'post', 'href' => api_generic_object_url(nil, object)},
{ 'name' => 'method_b', 'method' => 'post', 'href' => api_generic_object_url(nil, object) }
)
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
Expand Down Expand Up @@ -282,6 +288,20 @@
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it 'can call a custom action on a generic object' do
api_basic_authorize

post(api_generic_object_url(nil, object), :params => { :action => 'method_a' })

expected = {
'success' => true,
'message' => "Invoked method method_a for Generic Object id: #{object.id}",
'task_href' => a_string_including(api_tasks_url)
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end
end

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

0 comments on commit d82d214

Please sign in to comment.