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

Tasks: delete support #220

Merged
merged 6 commits into from
Dec 5, 2017
Merged

Tasks: delete support #220

merged 6 commits into from
Dec 5, 2017

Conversation

himdel
Copy link
Contributor

@himdel himdel commented Nov 21, 2017

This adds deletion for MiqTask, both via DELETE and via POST {action:"delete"}, and bulk deletion (POST {action:"delete", resources: [...]}).

Closes #203

(Needed for ManageIQ/manageiq-ui-classic#2708.)


Is it OK to reuse existing identifiers (miq_task_delete) or is that frowned upon? :)

@jntullo
Copy link

jntullo commented Nov 21, 2017

Hi @himdel can you add some specs for this? Is there any reason you didn't add delete on the collection (for bulk delete)? and you can reuse existing identifiers :)

@himdel
Copy link
Contributor Author

himdel commented Nov 21, 2017

@jntullo Sure, will do :) (wasn't quite sure if we wanted to spec methods that aren't technically there, but definitely can't hurt :))

As for bulk delete.. I just didn't think of it :).. but there I have questions.. (I'm not seeing any docs on custom collection actions, sorry :).)

Collection actions are named foo_collection, similar to foo_resource or is there a different convention? I'm not seeing any such delete_collection in the generic code, should I add it there, or should this be strictly for task? Or .. am I misunderstanding and does it magically know to call the resource action on all the resources mentioned?

@himdel
Copy link
Contributor Author

himdel commented Nov 21, 2017

... Never mind :).

It's really that easy, it does seem to call the resource action for each of the resources .. so while I still don't know how to custom collection action, I don't seem to need it right now :).

Addded, on to the specs now :)

@himdel
Copy link
Contributor Author

himdel commented Nov 21, 2017

@jntullo Specs added, WDYT? :)

Copy link

@jntullo jntullo left a comment

Choose a reason for hiding this comment

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

Looking good @himdel !! Just a few minor things/nits 😃


delete(url)

expect(response).to have_http_status(204)
Copy link

Choose a reason for hiding this comment

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

For the http status, could you use :forbidden rather than 204? will help to keep our specs consistent

Copy link
Contributor Author

@himdel himdel Nov 22, 2017

Choose a reason for hiding this comment

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

Probably not :forbidden ... I guess something like :empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out :no_content, according to http://billpatrianakos.me/blog/2013/10/13/list-of-rails-status-code-symbols/

That's why I prefer the numerical values, no confusion about what it could be :).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(fixed)


post(url, :params => data)

expect(response).to have_http_status(200)
Copy link

Choose a reason for hiding this comment

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

Similarly, you could use :ok instead of 200

:resources => [
{:href => api_task_url(nil, task.id)},
{:href => api_task_url(nil, task2.id)},
],
Copy link

Choose a reason for hiding this comment

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

nit, but no , here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed :) Funnily enough, in ui, trailing commas are strongly encouradged :).

post(url, :params => data)

expect(response).to have_http_status(200)
expect_deleted(task, task2)
Copy link

Choose a reason for hiding this comment

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

Generally, the API is concerned with the responses rather than validating the objects, something like this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, I don't care about the response at all, only that it actually deleted the thing :).

Should I add something like that then?

But.. more and more this looks like I'm testing your generic functions for one specific case, which is identical to all the other specific cases.

Would you be interested in specs for the generic resource_delete instead?

Copy link
Contributor Author

@himdel himdel Nov 22, 2017

Choose a reason for hiding this comment

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

Or at least a set of helpers so that we can write the tests once, and then only give that the things that should be different?

That task_spec should not really have to be more code than something like..

describe 'TasksController' do
  let(:task) { FactoryGirl.create(:miq_task, :state => MiqTask::STATE_FINISHED) }
  let(:task2) { FactoryGirl.create(:miq_task, :state => MiqTask::STATE_FINISHED) }

  test_generic_delete(:methods => [:delete, :post, :bulk], :resources => [:task, :task2])
end

it 'bulk deletes' do
api_basic_authorize collection_action_identifier(:tasks, :delete)

url = api_tasks_url
Copy link

Choose a reason for hiding this comment

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

are you only using this variable in one place? May not be necessary to assign it

it 'deletes on POST' do
api_basic_authorize resource_action_identifier(:tasks, :delete)

url = api_task_url(nil, task.id)
Copy link

Choose a reason for hiding this comment

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

You may not need to assign this to a variable if you're only calling it once, same as above and below

Copy link
Contributor Author

@himdel himdel Nov 22, 2017

Choose a reason for hiding this comment

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

Sure I don't.

But is it not clearer to write..

url = function call with not-so clear params
data = create the structure

post(url, :params => data)

than to have it all in one blob, like

  post(api_task_url(nil, task.id), :params => {
      :action    => 'delete',
      :resources => [
        {:href => api_task_url(nil, task.id)},
        {:href => api_task_url(nil, task2.id)},
      ],
    })

?

But of course your repo, your rules, just tell me you're sure you want it that way :).

Copy link
Contributor

Choose a reason for hiding this comment

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

My two penneth: I think "clearer" is always going to be a bit subjective in these cases. I personally find your counter example clearer.

both via DELETE and via POST {action:"delete"}
```
API.post('/api/tasks', {
  action: "delete",
  resources: [
    { href: 'http://localhost:3000/api/tasks/10000000054726' },
    { href: 'http://localhost:3000/api/tasks/10000000054727' },
  ],
});
```
similar to `collection_action_identifier` except for resource actions
@himdel
Copy link
Contributor Author

himdel commented Nov 29, 2017

@jntullo Can you check again please?

I've added the spec for status and message in response, and removed the extra vars.

@himdel
Copy link
Contributor Author

himdel commented Nov 29, 2017

@miq-bot add_label gaprindashvili/yes

apparently miq-bot complains about readable hashes - moving back to vars
@himdel
Copy link
Contributor Author

himdel commented Nov 29, 2017

.. and I can't fix both miq-bot and not using vars for data while still being readable .. undoing that change.

And not touching this again :D.

@abellotti abellotti self-assigned this Dec 5, 2017
@abellotti abellotti added this to the Sprint 75 Ending Dec 11, 2017 milestone Dec 5, 2017
:action => 'delete',
:resources => [
{:href => api_task_url(nil, task.id)},
{:href => api_task_url(nil, task2.id)}
Copy link
Member

Choose a reason for hiding this comment

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

Hi @himdel minor, but on all the api_task_url calls, prefer just the object being specified i.e.

api_task_url(nil, task)

@abellotti
Copy link
Member

LGTM!! minor nit on the rails url helper usage in specs.

@himdel
Copy link
Contributor Author

himdel commented Dec 5, 2017

Thanks @abellotti, updated :)

@abellotti
Copy link
Member

Thanks @himdel for the update and the API enhancement !! 👍 will merge when 🍏

@miq-bot
Copy link
Member

miq-bot commented Dec 5, 2017

Checked commits https://github.com/himdel/manageiq-api/compare/8cc67b298383313f5616d8e518113343a44a8585~...74b36b6d10a7c803f80108e893b4fa05c24455ab with ruby 2.3.3, rubocop 0.47.1, haml-lint 0.20.0, and yamllint 1.10.0
2 files checked, 0 offenses detected
Everything looks fine. 👍

@abellotti abellotti merged commit 85f3153 into ManageIQ:master Dec 5, 2017
@himdel himdel deleted the tasks-delete branch December 5, 2017 18:32
simaishi pushed a commit that referenced this pull request Dec 11, 2017
Tasks: delete support
(cherry picked from commit 85f3153)
@simaishi
Copy link
Contributor

Gaprindashvili backport details:

$ git log -1
commit 80834992e9cfb29b48b8cf37febf64d8ecdf2398
Author: Alberto Bellotti <abellotti@users.noreply.github.com>
Date:   Tue Dec 5 13:17:36 2017 -0500

    Merge pull request #220 from himdel/tasks-delete
    
    Tasks: delete support
    (cherry picked from commit 85f315312fff5ba1fb41bbc71fb833766272d49e)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants