summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Danna <steve@opscode.com>2012-12-22 09:22:51 -0800
committerBryan McLellan <btm@opscode.com>2013-02-06 09:51:02 -0800
commit42d5ab44d53c02a731a7cc5ca987d728f6dc2f59 (patch)
treeea1d04b98dd8a3f7e661bf40dd38748ead837c8b
parent4d45c9fb1bb4ec70c342fee963c3ac9b1134fdba (diff)
downloadchef-42d5ab44d53c02a731a7cc5ca987d728f6dc2f59.tar.gz
Chef::User.list should handle responses from Hosted/Private Chef
The API responses from a GET to the "users" endpoint currently returns a different response on Hosted and Private Chef. Chef::User.list transforms this response into form returned by the open source server.
-rw-r--r--lib/chef/user.rb32
-rw-r--r--spec/unit/user_spec.rb20
2 files changed, 46 insertions, 6 deletions
diff --git a/lib/chef/user.rb b/lib/chef/user.rb
index b97cc3cfd2..994cbb5773 100644
--- a/lib/chef/user.rb
+++ b/lib/chef/user.rb
@@ -143,13 +143,18 @@ class Chef
end
def self.list(inflate=false)
- if inflate
- users = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest('users')
- users.map do |name|
- Chef::User.load(name)
- end
+ response = if inflate
+ users = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest('users')
+ users.map do |name|
+ Chef::User.load(name)
+ end
+ else
+ Chef::REST.new(Chef::Config[:chef_server_url]).get_rest('users')
+ end
+ if response.is_a? Array
+ transform_ohc_list_response(response)
else
- Chef::REST.new(Chef::Config[:chef_server_url]).get_rest('users')
+ response
end
end
@@ -157,5 +162,20 @@ class Chef
response = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("users/#{name}")
Chef::User.from_hash(response)
end
+
+ private
+
+ # Gross. Transforms an API response in the form of:
+ # [ { "user" => { "username" => USERNAME }}, ...]
+ # into the form
+ # { "USERNAME" => "URI" }
+ def self.transform_ohc_list_response(response)
+ new_response = Hash.new
+ response.each do |u|
+ name = u['user']['username']
+ new_response[name] = Chef::Config[:chef_server_url] + "/users/#{name}"
+ end
+ new_response
+ end
end
end
diff --git a/spec/unit/user_spec.rb b/spec/unit/user_spec.rb
index 0377e920fe..9d011fc0e0 100644
--- a/spec/unit/user_spec.rb
+++ b/spec/unit/user_spec.rb
@@ -186,6 +186,26 @@ describe Chef::User do
Chef::REST.stub!(:new).and_return(@http_client)
end
+ describe "list" do
+ before(:each) do
+ Chef::Config[:chef_server_url] = "http://www.example.com"
+ @osc_response = { "admin" => "http://www.example.com/users/admin"}
+ @ohc_response = [ { "user" => { "username" => "admin" }} ]
+ end
+
+ it "lists all clients on an OSC server" do
+ @http_client.stub!(:get_rest).with("users").and_return(@osc_response)
+ Chef::User.list.should == @osc_response
+ end
+
+ it "lists all clients on an OHC/OPC server" do
+ @http_client.stub!(:get_rest).with("users").and_return(@ohc_response)
+ # We expect that Chef::User.list will give a consistent response
+ # so OHC API responses should be transformed to OSC-style output.
+ Chef::User.list.should == @osc_response
+ end
+ end
+
describe "create" do
it "creates a new user via the API" do
@user.password "password"