summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/api_client.rb18
-rw-r--r--spec/unit/api_client_spec.rb67
2 files changed, 84 insertions, 1 deletions
diff --git a/lib/chef/api_client.rb b/lib/chef/api_client.rb
index da05939c24..3502f96987 100644
--- a/lib/chef/api_client.rb
+++ b/lib/chef/api_client.rb
@@ -116,6 +116,11 @@ class Chef
client
end
+ def self.reregister(name)
+ api_client = load(name)
+ api_client.reregister
+ end
+
def self.list(inflate=false)
if inflate
response = Hash.new
@@ -131,7 +136,7 @@ class Chef
# Load a client by name via the API
def self.load(name)
- response = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("clients/#{name}")
+ response = Chef::REST.new(Chef::Config[:chef_server_url]).get("clients/#{name}")
if response.kind_of?(Chef::ApiClient)
response
else
@@ -166,6 +171,17 @@ class Chef
end
end
+ def reregister
+ r = Chef::REST.new(Chef::Config[:chef_server_url])
+ reregistered_self = r.put("clients/#{name}", { :name => name, :admin => admin, :private_key => true })
+ if reregistered_self.respond_to?(:[])
+ private_key(reregistered_self["private_key"])
+ else
+ private_key(reregistered_self.private_key)
+ end
+ self
+ end
+
# Create the client via the REST API
def create
Chef::REST.new(Chef::Config[:chef_server_url]).post_rest("clients", self)
diff --git a/spec/unit/api_client_spec.rb b/spec/unit/api_client_spec.rb
index e01243152e..4fab86053e 100644
--- a/spec/unit/api_client_spec.rb
+++ b/spec/unit/api_client_spec.rb
@@ -158,6 +158,73 @@ describe Chef::ApiClient do
end
end
+
+ describe "when requesting a new key" do
+ before do
+ @http_client = mock("Chef::REST mock")
+ Chef::REST.stub!(:new).and_return(@http_client)
+ end
+
+ context "and the client does not exist on the server" do
+ before do
+ @a_404_response = Net::HTTPNotFound.new("404 not found and such", nil, nil)
+ @a_404_exception = Net::HTTPServerException.new("404 not found exception", @a_404_response)
+
+ @http_client.should_receive(:get).with("clients/lost-my-key").and_raise(@a_404_exception)
+ end
+ it "raises a 404 error" do
+ lambda { Chef::ApiClient.reregister("lost-my-key") }.should raise_error(@a_404_exception)
+ end
+ end
+
+ context "and the client exists" do
+ before do
+ @api_client_without_key = Chef::ApiClient.new
+ @api_client_without_key.name("lost-my-key")
+ @http_client.should_receive(:get).with("clients/lost-my-key").and_return(@api_client_without_key)
+ end
+
+
+ context "and the client exists on a Chef 11-like server" do
+ before do
+ @api_client_with_key = Chef::ApiClient.new
+ @api_client_with_key.name("lost-my-key")
+ @api_client_with_key.private_key("the new private key")
+ @http_client.should_receive(:put).
+ with("clients/lost-my-key", :name => "lost-my-key", :admin => false, :private_key => true).
+ and_return(@api_client_with_key)
+ end
+
+ it "returns an ApiClient with a private key" do
+ response = Chef::ApiClient.reregister("lost-my-key")
+ # no sane == method for ApiClient :'(
+ response.should == @api_client_without_key
+ response.private_key.should == "the new private key"
+ response.name.should == "lost-my-key"
+ response.admin.should be_false
+ end
+ end
+
+ context "and the client exists on a Chef 10-like server" do
+ before do
+ @api_client_with_key = {"name" => "lost-my-key", "private_key" => "the new private key"}
+ @http_client.should_receive(:put).
+ with("clients/lost-my-key", :name => "lost-my-key", :admin => false, :private_key => true).
+ and_return(@api_client_with_key)
+ end
+
+ it "returns an ApiClient with a private key" do
+ response = Chef::ApiClient.reregister("lost-my-key")
+ # no sane == method for ApiClient :'(
+ response.should == @api_client_without_key
+ response.private_key.should == "the new private key"
+ response.name.should == "lost-my-key"
+ response.admin.should be_false
+ end
+ end
+
+ end
+ end
end