[katello-devel] ActiveResource as an alternative to custom RestClient-based Foreman api client
Ivan Nečas
inecas at redhat.com
Wed Sep 19 13:49:52 UTC 2012
That looks nice. As everything in Rails with basic CRUD. I've tried some
real-world example though, here are my notes:
I tried to use ActiveResource to bind candlepin consumer model
I had to fix the monkey patch for replacing the (.json) suffixes first, then
consumer = Candlepin::Consumer.first
consumer.releaseVer # good, returns Candlepin::Consumer::ReleaseVer
(note we haven't defined this class nowhere), I can access the
releaseVer value, although it's not really Ruby convention to use
camelCase for method names
consumer.releaseVer.releaseVer # => ""
consumer.releaseVer.releaseVer = "6Server"
consumer.save # 404, after some googling found out I have to use
set_primary_key method (not documented btw. but we're used to it)
conusmer.save # second time, "getting NoMethodError: undefined method
`strip' for nil:NilClass"
#fixed in newer version of ActiveResource, in 3.0.10 monkey patch needed
consumer.save # 500 from candlepin:
"org.codehaus.jackson.map.exc.UnrecognizedPropertyException"
# why? because candlepin expects releaseVer to be a string (not a Hash)
# so I tried this
consumer.releaseVer="6Server"
consumer.save # => true
# we updated now
consumer.reload
consumer.name = "test2"
consumer.save # getting the same error with releaseVer issue
# nothing but a hack can fix this
# also if you wanted to see the request params in the logging, you would
need to monkey patch
# while searching for a solution, I've also learned that support for
singleton resources were introduced quite late [1]
Personally, it was very frustrating morning. And I don't want to go
through this exercise on daily basis. I'm somehow still missing the pros
of this solution. One of the criticism of the previous one was that we
list the attributes in the client code. But it turns out it doesn't work
without it in activeresource for us neither. But with the previous
solution the list of params was at least in sync with the documentation.
I haven't found any resource that would say that ActiveResource deals
with caching. Calling Candlepin::Consumer.find(uuid) twice in a row
calls into Candlepin twice.
On the other hand, it seems that almost any customization in
ActiveResource needs a monkey patch = really fragile.
And for the last year I can not think of any issue connected directly to
the code in `lib/resources/` (a.k.a explicit bindings) that would not
happened with ActiveResource. It would be really nice if writing unit
tests was easier, but I don't see how ActiveResource would help us with
this.
Another issue I have with ActiveResource are all this implicit things
that make the code harder to maintain and understand (explaining the
meta-programming is always the hardest part in our Ruby course at Uni).
Imaging you're a user of the client library and see something like this:
class Foreman::User < Resources::Foreman::Base
end
And let's assume you have no deep understanding of the Rails philosophy,
let's say Katello or Foreman user (typical sysdamin) that wants to
script something or fix a bug and send the patch back. It's not obvious
that you can call.
users = Foreman::User.all
user = Foreman::User.find(id)
user.name = "New Name"
user.save
user.find(another_id)
user.destroy
So the next thing to do is to write documentation to the client library.
[1] - https://github.com/rails/activeresource/pull/4
-- Ivan
On 09/17/2012 08:04 PM, Dmitri Dolguikh wrote:
> Hey everybody,
>
> I finished the spike on using ActiveResource as a replacement for
> RestClient, only for communication with Foreman, and only to
> orchestrate user creation/deletion in Foreman. See the results at [1].
>
> Some post-spike thoughts/observations:
> - I monkey-patched ActiveResource to support OAuth, but the same can
> be achieved by subclassing ActiveResource::Base, which is the way to go.
> - The amount of custom-code is *very* minimal and it's mostly to
> support OAuth.
> - Not a fan of custom to_json, we could either use resource's schema
> (there are some drawbacks), or try using rabl [2] (like foreman folks do)
> - Should we decide to adopt this, we can eventually move both
> candlepin and pulp to use ActiveResource. I think it will be possible
> to drop our lazy_attributes in the process with some work, if we adopt
> rails caching.
>
>
> Comments, concerns?
> -d
>
>
> [1] https://github.com/witlessbird/katello/tree/foreman-ar-spike
> [2] https://github.com/nesquena/rabl
>
>
> _______________________________________________
> katello-devel mailing list
> katello-devel at redhat.com
> https://www.redhat.com/mailman/listinfo/katello-devel
--
Ivan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/katello-devel/attachments/20120919/284f94ed/attachment.htm>
More information about the katello-devel
mailing list