diff options
author | Nuo Yan <nuo@opscode.com> | 2011-04-27 18:28:04 -0700 |
---|---|---|
committer | Nuo Yan <nuo@opscode.com> | 2011-04-27 18:28:34 -0700 |
commit | 73fb4fcebdea221df2af67a49320acde34057744 (patch) | |
tree | 2d17d4615ece37d4174ef3f23d37846c4292f453 | |
parent | 9b78420cd317c2105e7522cfa7127fd7b7773d15 (diff) | |
download | chef-73fb4fcebdea221df2af67a49320acde34057744.tar.gz |
Fix CHEF-2281 Pass inflateed object to server api in data bag item creation
-rw-r--r-- | chef/lib/chef/data_bag_item.rb | 6 | ||||
-rw-r--r-- | chef/lib/chef/knife/data_bag_create.rb | 21 | ||||
-rw-r--r-- | chef/spec/unit/knife/data_bag_create_spec.rb | 18 |
3 files changed, 25 insertions, 20 deletions
diff --git a/chef/lib/chef/data_bag_item.rb b/chef/lib/chef/data_bag_item.rb index 962c408656..5a5ca473e7 100644 --- a/chef/lib/chef/data_bag_item.rb +++ b/chef/lib/chef/data_bag_item.rb @@ -218,17 +218,17 @@ class Chef def save(item_id=@raw_data['id']) r = chef_server_rest begin - r.put_rest("data/#{data_bag}/#{item_id}", @raw_data) + r.put_rest("data/#{data_bag}/#{item_id}", self) rescue Net::HTTPServerException => e raise e unless e.response.code == "404" - r.post_rest("data/#{data_bag}", @raw_data) + r.post_rest("data/#{data_bag}", self) end self end # Create this Data Bag Item via RESTful API def create - chef_server_rest.post_rest("data/#{data_bag}", @raw_data) + chef_server_rest.post_rest("data/#{data_bag}", self) self end diff --git a/chef/lib/chef/knife/data_bag_create.rb b/chef/lib/chef/knife/data_bag_create.rb index 7ef62e59c3..a329bcec7b 100644 --- a/chef/lib/chef/knife/data_bag_create.rb +++ b/chef/lib/chef/knife/data_bag_create.rb @@ -7,9 +7,9 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -64,7 +64,7 @@ class Chef stdout.puts("You must specify a data bag name") exit 1 end - + # create the data bag begin rest.post_rest("data", { "name" => @data_bag_name }) @@ -73,16 +73,17 @@ class Chef raise unless e.to_s =~ /^409/ ui.info("Data bag #{@data_bag_name} already exists") end - + # if an item is specified, create it, as well if @data_bag_item_name create_object({ "id" => @data_bag_item_name }, "data_bag_item[#{@data_bag_item_name}]") do |output| - item = if use_encryption - Chef::EncryptedDataBagItem.encrypt_data_bag_item(output, - read_secret) - else - output - end + item = Chef::DataBagItem.from_hash( + if use_encryption + Chef::EncryptedDataBagItem.encrypt_data_bag_item(output, read_secret) + else + output + end) + item.data_bag(@data_bag_name) rest.post_rest("data/#{@data_bag_name}", item) end end diff --git a/chef/spec/unit/knife/data_bag_create_spec.rb b/chef/spec/unit/knife/data_bag_create_spec.rb index df0599f4ab..1c529cd418 100644 --- a/chef/spec/unit/knife/data_bag_create_spec.rb +++ b/chef/spec/unit/knife/data_bag_create_spec.rb @@ -7,9 +7,9 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -26,7 +26,7 @@ module ChefSpecs def initialize @args_received = [] end - + def post_rest(*args) @args_received << args end @@ -53,10 +53,12 @@ describe Chef::Knife::DataBagCreate do it "creates a data bag item when given two arguments" do @knife.name_args = ['sudoing_admins', 'ME'] - user_supplied_json = {"login_name" => "alphaomega", "id" => "ME"}.to_json - @knife.should_receive(:create_object).and_yield(user_supplied_json) + user_supplied_hash = {"login_name" => "alphaomega", "id" => "ME"} + data_bag_item = Chef::DataBagItem.from_hash(user_supplied_hash) + data_bag_item.data_bag("sudoing_admins") + @knife.should_receive(:create_object).and_yield(user_supplied_hash) @rest.should_receive(:post_rest).with("data", {'name' => 'sudoing_admins'}).ordered - @rest.should_receive(:post_rest).with("data/sudoing_admins", user_supplied_json).ordered + @rest.should_receive(:post_rest).with("data/sudoing_admins", data_bag_item).ordered @knife.run end @@ -69,8 +71,10 @@ describe Chef::Knife::DataBagCreate do @secret) @knife.name_args = ['sudoing_admins', 'ME'] @knife.should_receive(:create_object).and_yield(@plain_data) + data_bag_item = Chef::DataBagItem.from_hash(@enc_data) + data_bag_item.data_bag("sudoing_admins") @rest.should_receive(:post_rest).with("data", {'name' => 'sudoing_admins'}).ordered - @rest.should_receive(:post_rest).with("data/sudoing_admins", @enc_data).ordered + @rest.should_receive(:post_rest).with("data/sudoing_admins", data_bag_item).ordered end it "creates an encrypted data bag item via --secret" do |