On Tuesday, February 12, 2013 at 2:58 PM, Jesse Campbell wrote:
-jesseI'm getting #<Net::HTTPServerException: 405 "Method Not Allowed">that happens whether the data bag exists or not.On Tue, Feb 12, 2013 at 5:38 PM, Daniel Condomitti < " target="_blank"> > wrote:You can specify the exceptions that you're rescuing instead of just blindly trapping all exceptions. What exception do you receive in Chef 11 when trying to retrieve a data bag that might not exist?
You could do something like the following; I'm sure there's a cleaner way but at least you're only attempting to create the new data bag if it doesn't already exist on the server.
databag_name = "sam-config-#{node.chef_environment}" databag = begin Chef::DataBag.load(databag_name) rescue Net::HTTPServerException => e raise e unless e.response.code == "404" Chef::DataBag.new(databag_name) endOn Tuesday, February 12, 2013 at 2:20 PM, Jesse Campbell wrote:
with chef 10 server and an admin client, I used to be able to create a data bag, then add items to it, like this:I needed to have the data bag creation in a rescue loop because in 10 server, there was no way without triggering an exception to ask the server if the data bag existed before trying to create it, so I would either get an exception asking if it was there, or I'd get an exception asking to create it.
dbag = "sam-config-#{node.chef_environment}"
begin
dbag_bag = Chef::DataBag.new
dbag_bag.name(dbag)
dbag_bag.save
rescue => e
Chef::Log.info( "Creating the data bag failed: #{e.inspect}" )
end
dbag_item = Chef::DataBagItem.new
dbag_item.data_bag(dbag)
Chef::Log.info( "Adding new item: #{id}" )
dbag_item.raw_data = item
dbag_item.save
Now, if it doesn't exist, I get #<Net::HTTPServerException: 405 "Method Not Allowed">Is there a new way to handle this (or perhaps a better one that works in both chef 10 and chef 11 without blowing up in my face all the time)?
Thanks!-Jesse
Archive powered by MHonArc 2.6.16.