Skip to content

Commit

Permalink
Fix CHEF-2281 Pass inflateed object to server api in data bag item cr…
Browse files Browse the repository at this point in the history
…eation
  • Loading branch information
Nuo Yan committed Apr 28, 2011
1 parent 9b78420 commit 73fb4fc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
6 changes: 3 additions & 3 deletions chef/lib/chef/data_bag_item.rb
Expand Up @@ -218,17 +218,17 @@ def cdb_save
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

Expand Down
21 changes: 11 additions & 10 deletions chef/lib/chef/knife/data_bag_create.rb
Expand Up @@ -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.
Expand Down Expand Up @@ -64,7 +64,7 @@ def run
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 })
Expand All @@ -73,16 +73,17 @@ def run
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
Expand Down
18 changes: 11 additions & 7 deletions chef/spec/unit/knife/data_bag_create_spec.rb
Expand Up @@ -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.
Expand All @@ -26,7 +26,7 @@ class ChefRest
def initialize
@args_received = []
end

def post_rest(*args)
@args_received << args
end
Expand All @@ -53,10 +53,12 @@ def post_rest(*args)

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
Expand All @@ -69,8 +71,10 @@ def post_rest(*args)
@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
Expand Down

0 comments on commit 73fb4fc

Please sign in to comment.