summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortylercloke <tylercloke@gmail.com>2015-04-29 15:29:02 -0700
committertylercloke <tylercloke@gmail.com>2015-04-30 09:56:58 -0700
commit75cb417e2edbf21427d778b4675c806bdb312b3f (patch)
tree459ce71ec0b3184409430a2cc01005adabe8966e
parent2d7f8a477b68767373068f363f785b84b9b832ae (diff)
downloadchef-75cb417e2edbf21427d778b4675c806bdb312b3f.tar.gz
Changes Chef::Key update to allow updating the name of a key.
-rw-r--r--lib/chef/key.rb14
-rw-r--r--spec/unit/key_spec.rb15
2 files changed, 24 insertions, 5 deletions
diff --git a/lib/chef/key.rb b/lib/chef/key.rb
index 1ba0ab49c9..3c8229f859 100644
--- a/lib/chef/key.rb
+++ b/lib/chef/key.rb
@@ -159,12 +159,18 @@ class Chef
self.class.generate_fingerprint(@public_key)
end
- def update
- if @name.nil?
- raise Chef::Exceptions::MissingKeyAttribute, "the name field must be populated when update is called"
+ # set @name and pass put_name if you wish to update the name of an existing key put_name to @name
+ def update(put_name=nil)
+ if @name.nil? && put_name.nil?
+ raise Chef::Exceptions::MissingKeyAttribute, "the name field must be populated or you must pass a name to update when update is called"
end
- new_key = chef_rest.put_rest("#{api_base}/#{@actor}/keys/#{@name}", to_hash)
+ # If no name was passed, fall back to using @name in the PUT URL, otherwise
+ # use the put_name passed. This will update the a key by the name put_name
+ # to @name.
+ put_name = @name if put_name.nil?
+
+ new_key = chef_rest.put_rest("#{api_base}/#{@actor}/keys/#{put_name}", to_hash)
Chef::Key.from_hash(self.to_hash.merge(new_key))
end
diff --git a/spec/unit/key_spec.rb b/spec/unit/key_spec.rb
index 8ef7d24f21..6c631e200a 100644
--- a/spec/unit/key_spec.rb
+++ b/spec/unit/key_spec.rb
@@ -499,7 +499,7 @@ EOS
describe "update" do
shared_examples_for "update key" do
- context "when name is missing" do
+ context "when name is missing and no argument was passed to update" do
it "should raise an MissingKeyAttribute" do
expect { key.update }.to raise_error(Chef::Exceptions::MissingKeyAttribute)
end
@@ -516,11 +516,23 @@ EOS
key.update
end
end
+
+ context "when @name is not nil and a arg is passed to update" do
+ before do
+ key.name "new_name"
+ end
+
+ it "passes @name in the body and the arg in the PUT URL" do
+ expect(rest).to receive(:put_rest).with(update_name_url, key.to_hash).and_return({})
+ key.update("old_name")
+ end
+ end
end
context "when updating a user key" do
it_should_behave_like "update key" do
let(:url) { "users/#{key.actor}/keys/#{key.name}" }
+ let(:update_name_url) { "users/#{key.actor}/keys/old_name" }
let(:key) { user_key }
end
end
@@ -528,6 +540,7 @@ EOS
context "when updating a client key" do
it_should_behave_like "update key" do
let(:url) { "clients/#{client_key.actor}/keys/#{key.name}" }
+ let(:update_name_url) { "clients/#{client_key.actor}/keys/old_name" }
let(:key) { client_key }
end
end