-
Notifications
You must be signed in to change notification settings - Fork 897
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
add_resource to Service api #14409
add_resource to Service api #14409
Conversation
@jntullo Cannot apply the following label because they are not recognized: service |
@@ -1932,6 +1932,8 @@ | |||
:identifier: service_admin | |||
- :name: delete | |||
:identifier: service_delete | |||
- :name: add_resource | |||
:identifier: service_edit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mkanoor can you verify this product feature used? I didn't see one for assigning resources
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jntullo let's add this action on the collection too for completeness so we support bulk add_resources too. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jntullo I think the service_edit seems appropriate here
spec/requests/api/services_spec.rb
Outdated
'message' => "Assigned resources to Service id:#{svc.id} name:'#{svc.name}'" | ||
} | ||
expect(response).to have_http_status(:ok) | ||
expect(response.parsed_body).to eq(expected) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jntullo can you add an expect to make sure the svc got the vms added to it ? Thanks.
@jntullo @abellotti @gmcculloug So one of the suggestions that @gmcculloug had was to move this method from the service model to app/models/vm.rb called 'add_to_service' so in this PR we would call instead of
we would call
This would allow us to create other models that can have the add_to_service method implemented if they allow the resource to be added to service and we would not have to change the REST API. I can create a separate PR for the Vm.add_to_service which we can call from here. |
sounds good @mkanoor |
https://www.pivotaltracker.com/story/show/142057167 add add_resource to the collection
resource_type, resource_id = parse_href(resource_ref['href']) | ||
resource = resource_search(resource_id, resource_type, collection_class(resource_type)) | ||
raise "Cannot assign #{resource_type} to #{service_ident(svc)}" unless resource.respond_to? :add_to_service | ||
resource.add_to_service(svc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jntullo Should we make a validation pass first and make sure all elements in the array respond to :add_to_service before we even attempt to even add a single one. Otherwise we would have added some and raised an error for the first one that doesn't respond to :added_to_service and we can't undo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mkanoor hm. I can see the case for that. What do you think about sending back an individual response to each resource instead? so the response will tell you which ones failed and which ones succeeded
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abellotti What is the normal pattern in REST API, when we get an array of objects to work with do we send individual responses and how do we handle errors for one of the elements in the array, e.g. if the resource has been deleted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abellotti for other similar actions, ie bulk tagging, we limit it to the collection so that we can return all of the individual results. Should we do something similar here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
normal pattern as is an action result showing success/failure for each action. we do bend the rules on bulk assignment of tags on vms via the assign_tags, in which case we return one per assignment for the same reason you mentioned here above, see app/controllers/api/subcollections/tags.rb's assign_tags method. We could do something similar here.
svc = resource_search(id, type, collection_class(type)) | ||
data['resources'].each do |resource_ref| | ||
resource_type, resource_id = parse_href(resource_ref['href']) | ||
resource = resource_search(resource_id, resource_type, collection_class(resource_type)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jntullo
Should we validate if the resource can be found, since we carry hrefs if the object is deleted in the meantime we won't have a resource?
with the change from:
to:
I wonder if the API signature should change and be an action on the vm, individual or bulk.
on individual vm:
supporting href and id in the service reference. Thoughts ? |
depends on if it will be extended to other resources that can be added or not, otherwise the API will need a new action on that resource. |
@mkanoor are there plans to support adding other classes to services in the future ? any ideas on which ? |
Ok, had a chat with @mkanoor, future resources could include orchestration_templates, load_balancers, and others. While some CI's are meant for the one service, things like load_balancers could be implemented to support multiple services. @jntullo let's stick with the add_service action on services, but we do need to change the signature. Problem with using href for the vm above is problematic as it does not allow this action to be implemented for bulk, i.e. on the collection. Could you update to support the following signature: Targeting a single service:
Doing bulk add_service on multiple services:
And enable it for collection (and add collection level tests). Thanks. |
@abellotti I pushed up some changes, but I have some concerns about the use of this type of action on a resource because of the expected response (a single action result). I don't think that we have any other examples of returning multiple results for a single resource, unless I am mistaken, because bulk tagging is limited to a collection. I played around with returning:
and wanted to get your thoughts around that. Another consideration is passing back the individual results within the single action result:
|
what do we do today with assign_tags, even though on a collection, for the one resource it targets, you can specify multiple tags with "tags" : [ ... ] ? |
@abellotti we return the result of each individual tagging attempt. I think that it might be good to take that same approach with a descriptive description, ie |
I see the issue with resources on the individual service, I still think we should support the same on both resource and for collection for bulk for consistency. Let's just support a single per request (or bulk) via "resource" : { "href" ... } in the payload, and drop the "resources" for now, we can enhance later. See my diffs in abellotti@c4ab8d2 For adding multiple resources to the one service, this would simply be a bulk call to /api/services, with the href of the same service specified for each request.
Doing bulk add_service on multiple services, also to add multiple vms to a service:
|
Thanks @jntullo for updating the PR. 😍 will merge when 🍏 |
more specs
Checked commits jntullo/manageiq@d755f99~...573c8c1 with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0 |
Adds "add_resource" action to Service resources
https://www.pivotaltracker.com/story/show/142057167
@miq-bot add_label enhancement, api, service
@miq-bot assign @abellotti
cc: @mkanoor