torrancew / hiera-foreman

A hiera backend that queries the Foreman for data
34 stars 15 forks source link

Support SmartVar Querying #4

Open torrancew opened 12 years ago

torrancew commented 12 years ago

hiera-foreman currently only supports ENC-style lookups. This should be extended to allow smartvar searching as well.

mchugh19 commented 12 years ago

Since I worked out issue 1, I figured I would tackle this as well. I think it's all working. Everything is based on the smart var looker upper from foreman: https://github.com/theforeman/puppet-foreman/blob/master/lib/puppet/parser/functions/smartvar.rb

So might not be the cleanest, but here you go: http://pastie.org/4450481

torrancew commented 12 years ago

Excellent! Could you possibly do this as a GH fork, and file a pull request? If you're unfamiliar with the process, I'm happy to help you out, but that will make it easy for me to pull your exact changeset, and modify/clean up as needed.

Thanks again, @mchugh19!

torrancew commented 12 years ago

@mchugh19 - Could you test out the smartvar_queries branch? This should incorporate all of your changes and DRY things up a bit.

mchugh19 commented 12 years ago

In testing this morning, it looks like the smartvar search is does not work.

torrancew commented 12 years ago

@mchugh19 - Can you provide a bit more data - perhaps a capture of the error message, etc?

mchugh19 commented 12 years ago

On asking about it on IRC, I was told: mcm0245> the wiki/API page says that a GET on /lookupkeys should return the list of smartvars mcm0245> I can get this to return the value of smart_test http://foreman.host/hosts/client.host/lookup_keys/smart_test mcm0245> but leaving off the smart_test key at the end, reutrns nothing instead of list of keys ohadlevy> mcm0245: seems like an issue, try asking for puppetclasses/id/lookup_keuy ohadlevy> mcm0245: we'll fix it hopefully with the new api work ohadlevy> mcm0245: you can get them scoped in classes, or we can apply a simple fix :) ohadlevy> mcm0245: http://pastie.org/4466525 ohadlevy> mcm0245: it seems like a bug in 1.0x

After applying the patch I can get the url: https://FOREMANSERVER/lookup_keys to return a value, but in a different format than is currently expected. $ curl -k -H "Accept: application/json" -u USERNAME:PASSWORD -X GET https://FOREMANSERVER/lookup_keys [{"lookup_key":{"default_value":"Default smart var value for smart_test","id":5,"description":"Testing smart vars","key":"smart_test"}},{"lookup_key":{"default_value":"smart_test2","id":6,"description":"testing a not assigned class","key":"smart_test2"}}]

Which is an array for the keys smart_test and smart_test2

Alternatively looking up the key directly always works (even without patching) $ curl -k -H "Accept: application/json" -u USERNAME:PASSWORD -X GET https://FOREMANSERVER/hosts/NODEFQDN/lookup_keys/smart_test2 {"value":"smart_test2 for fqdn"}

mchugh19 commented 12 years ago

So the current issue with the script is the change from looking up the smartvar key directly, to trying to grab the hash of all keys (which fails)

But I wonder how much this even matters. A smartvar is attached to a class. If the class is attached to a node, the smartvar gets displayed in the host's yaml file and the regular ENC lookup picks it up just fine. The only time that a query like: https://FOREMAN-SERVER/hosts/NODE-NAME/lookup_keys/SMART-VARIABLE-KEY

Will return anything that is not already in the ENC lookup, is if the class that the smartvar belongs to is not attached to the host. But if that is the case, then I can't see a reason why you would need to look up the value of the smartvar.