-
Notifications
You must be signed in to change notification settings - Fork 985
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
fixes #2931 - API v2 hosts JSON response, add *_name for each *_id #844
Conversation
Default response is *_name for all *_id fields. I would like to make this flexible for performance reasons if a user just wants hosts?fields=id,name to not run on the associated queries |
Can we maybe move *_name methods out of the model? We'll need such functionality across whole API. I can imagine a helper in the rabl templates. Something like:
|
@tstrachota, I didn't find documentation on creating rabl helpers. Can you send me an example. |
It seems that UI has own helpers to get the association names, so I wouldn't pollute the model with the *_name methods (maybe just in case of complex ones).
I'm also worried about the performance of the index command as you mentioned. Could we limit the columns displayed by default to lets say
to match UI? So I suggest to split details.rabl to index_datails and details (extended by index_details) It may be better to also move org and location section to separate Rabl partial Makes sense? |
@isratrade I believe there is no docs on how to extend rabl. You can find Tomas's attempt here https://github.com/tstrachota/katello/blob/409aa954f39e0bc6afa0cb4f447e2dd7b06f1b4a/config/initializers/rabl_init.rb. We didn't use this in the end, but associated_name could look similar to this
|
@tstrachota, @mbacovsky, makes sense. I'll work on this today. |
@tstrachota, @mbacovsky, PR re-committed with your suggested changes #index extends details.rabl only I included the boolean checks for org/loc in the method associated_attributes rather than having a separate partial. |
Looks good , I like the way you included loc and orgs into asociated_attrs. I'd probably prefer following notation of associated attrs, to visually match rabl's attributes, but nothing crucial:
The rabl helper deserves some refactoring I have a question about host_parameters. Why we have host_parameters in host and do not have domain parameters in domain? Are these the same case? Shouldn't we unify it? |
@mbacovsky, yes rabl usually doesn't allow mixing symbols and hashes in the the same line like this (what I did). associated_attributes :model, :location, :operatingsystem => :to_label, hostgroup => :to_label as their code expects the all attributes to be the same as the first one. If it's a hash, then they use find_pair. I'll update it I'm definitely agree to some re factoring of the rabl helper. |
@mbacovsky, recommited |
@isratrade it looks good! Rabl template is quite clean now. I'd maybe do slight refactoring like this (I removed the comments to shorten the example): def associated_ids *attrs
attrs.each do |a|
if a.kind_of? Hash
a.each do |klass, method_name|
resource_id_nodes(klass, method_name) if show_node?(a)
end
elsif a.kind_of? Symbol or a.kind_of? String
resource_id_nodes(a, :name) if show_node?(a)
end
end
end
def resource_id_nodes(klass, method_name)
attributes klass.to_s+'_id'
node_name = klass.to_s+'_name'
node(node_name) do |obj|
obj.send(klass).try(method_name)
end
end I think it would be also handy to have such helpers for associated collections. Something like: def associated_collection_ids *attrs
attrs.each do |a|
if a.kind_of? Hash
a.each do |klass, method_name|
collection_id_nodes(klass, method_name) if show_node?(a)
end
elsif a.kind_of? Symbol or a.kind_of? String
collection_id_nodes(a, :name) if show_node?(a)
end
end
end
def collection_id_nodes(klass, method_name)
attributes klass.to_s.singularize+'_ids'
node_name = klass.to_s.singularize+'_names'
node(node_name) do |obj|
obj.send(klass).collect{|a| a.try(method_name) }
end
end I changed the names a bit but it's only cosmetics. I don't insist on them at all. |
@isratrade, looks good, if Tomas's refactoring is included I'm okay with merge. I prefer to stick with original associated_attributes and associated_collections naming though. Thanks for the effort, this addition will be very helpfull! |
@mbacovsky, @tstrachota, I changed naming and some other minor changes to @tstrachota's refactoring. I also used in #show associated_collections :host_parameters I see this as a spike, so let's add some testing after we are happy with this convention. We also need to decide what to include in hosts#show by default. The host parameters were in v1, so I just included it in v2, but I think smart variables and smart class parameters should also be included. If not, PR #829 allows a user to GET "api/hosts/:host_id/smart_variables" |
Looks good. +1 to merge. |
it is starting to look powerful. The spike approach is good, when hosts are 'perfect' we can use it as pattern for fixing others controllers ourselves as we go through them during the CLI work. For me this is ready to merge when the host_parameters are resolved. I think the use of associated_collections in the show is not getting us there as the parameters are key/value so printing just the keys makes no sense to me. If I understand it correctly the associated_collections when used e.g. in architectures
would return
which is not the case for host's parameters. I suggest to remove host_parameters anyway to have more restfull API and used /host/:id/parameters instead. And one minor thing 😉 - it may be more intuitive to rename detail.json.rabl to brief.json.rabl, or base as we use it more in that sense. |
@mbacovsky, @tstrachota I will include in the "API v2 conventions" etherpad doc (to be sent) about the file naming (details, brief, etc) as well as which associations to "expand" by default. |
Thanks for removing host_parameters. I saw in the comment you were mentioning 'brief' in file naming conventions, but there is 'details' still in the code base... just checking it was intentional 😉 |
@mbacovsky, about naming convention of partial, I just thought we could wait for the etherpad discussion after I send an email to -dev. In the meantime, I renamed details.json.rabl it to base.json.rabl. There could be an additional partial call brief, short, small, etc that just include :id, :name |
@tstrachota, @mbacovsky, getter and setters *_names methods automatically added in PR #850 for all "has_many" in ActiveRecord. I think it's OK to have the getter methods *_names in AR even though we have a Rabl helper as well. |
👍 |
Ok, now the two ways duplicate the same thing. Do we want to keep both rabl helpers and model methods? |
closing in favor or PR #850 |
…lass (cherry picked from commit 83683ed) Conflicts: app/views/puppetclasses/index.html.erb
No description provided.