summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Danna <steve@opscode.com>2012-12-15 01:21:54 -0800
committerBryan McLellan <btm@opscode.com>2013-02-06 09:49:04 -0800
commit1afeec4c3275d2bdff87f2321c40f7968cf51fc6 (patch)
tree80674cabf2efdd7752627b88c0c57e4a6cff71d1
parent13774733fba0ac483a7212c6ef55517e0301a4ff (diff)
downloadchef-1afeec4c3275d2bdff87f2321c40f7968cf51fc6.tar.gz
Add Chef::User class
This commit adds a Chef::User class similar to the Chef::ApiClient class. Notable differences are the presence of a password attribute and the fact that the API will not return chef_type or json_type attributes. The latter means that the Chef::REST class will not automatically inflate API responses to Chef::User objects.
-rw-r--r--lib/chef/user.rb161
-rw-r--r--spec/unit/user_spec.rb221
2 files changed, 382 insertions, 0 deletions
diff --git a/lib/chef/user.rb b/lib/chef/user.rb
new file mode 100644
index 0000000000..b97cc3cfd2
--- /dev/null
+++ b/lib/chef/user.rb
@@ -0,0 +1,161 @@
+#
+# Author:: Steven Danna (steve@opscode.com)
+# Copyright:: Copyright 2012 Opscode, Inc.
+# License:: Apache License, Version 2.0
+#
+# 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.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+require 'chef/config'
+require 'chef/mixin/params_validate'
+require 'chef/mixin/from_file'
+require 'chef/mash'
+require 'chef/json_compat'
+require 'chef/search/query'
+
+class Chef
+ class User
+
+ include Chef::Mixin::FromFile
+ include Chef::Mixin::ParamsValidate
+
+ def initialize
+ @name = ''
+ @public_key = nil
+ @private_key = nil
+ @password = nil
+ @admin = false
+ end
+
+ def name(arg=nil)
+ set_or_return(:name, arg,
+ :regex => /^[\-[:alnum:]_\.]+$/)
+ end
+
+ def admin(arg=nil)
+ set_or_return(:admin,
+ arg, :kind_of => [TrueClass, FalseClass])
+ end
+
+ def public_key(arg=nil)
+ set_or_return(:public_key,
+ arg, :kind_of => String)
+ end
+
+ def private_key(arg=nil)
+ set_or_return(:private_key,
+ arg, :kind_of => String)
+ end
+
+ def password(arg=nil)
+ set_or_return(:password,
+ arg, :kind_of => String)
+ end
+
+ def to_hash
+ result = {
+ "name" => @name,
+ "public_key" => @public_key,
+ "admin" => @admin
+ }
+ result["private_key"] = @private_key if @private_key
+ result["password"] = @password if @password
+ result
+ end
+
+ def to_json(*a)
+ to_hash.to_json(*a)
+ end
+
+ def destroy
+ Chef::REST.new(Chef::Config[:chef_server_url]).delete_rest("users/#{@name}")
+ end
+
+ def create
+ payload = {:name => self.name, :admin => self.admin, :password => self.password }
+ payload[:public_key] = public_key if public_key
+ new_user =Chef::REST.new(Chef::Config[:chef_server_url]).post_rest("users", payload)
+ Chef::User.from_hash(self.to_hash.merge(new_user))
+ end
+
+ def update(new_key=false)
+ payload = {:name => name, :admin => admin}
+ payload[:private_key] = new_key if new_key
+ payload[:password] = password if password
+ updated_user = Chef::REST.new(Chef::Config[:chef_server_url]).put_rest("users/#{name}", payload)
+ Chef::User.from_hash(self.to_hash.merge(updated_user))
+ end
+
+ def save(new_key=false)
+ begin
+ create
+ rescue Net::HTTPServerException => e
+ if e.response.code == "409"
+ update(new_key)
+ else
+ raise e
+ end
+ end
+ end
+
+ def reregister
+ r = Chef::REST.new(Chef::Config[:chef_server_url])
+ reregistered_self = r.put_rest("users/#{name}", { :name => name, :admin => admin, :private_key => true })
+ private_key(reregistered_self["private_key"])
+ end
+
+ def to_s
+ "user[#{@name}]"
+ end
+
+ def inspect
+ "Chef::User name:'#{name}' admin:'#{admin.inspect}'" +
+ "public_key:'#{public_key}' private_key:#{private_key}"
+ end
+
+ # Class Methods
+
+ def self.from_hash(user_hash)
+ user = Chef::User.new
+ user.name user_hash['name']
+ user.private_key user_hash['private_key'] if user_hash.key?('private_key')
+ user.password user_hash['password'] if user_hash.key?('password')
+ user.public_key user_hash['public_key']
+ user.admin user_hash['admin']
+ user
+ end
+
+ def self.from_json(json)
+ Chef::User.from_hash(Chef::JSONCompat.from_json(json))
+ end
+
+ class << self
+ alias_method :json_create, :from_json
+ 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
+ else
+ Chef::REST.new(Chef::Config[:chef_server_url]).get_rest('users')
+ end
+ end
+
+ def self.load(name)
+ response = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("users/#{name}")
+ Chef::User.from_hash(response)
+ end
+ end
+end
diff --git a/spec/unit/user_spec.rb b/spec/unit/user_spec.rb
new file mode 100644
index 0000000000..0377e920fe
--- /dev/null
+++ b/spec/unit/user_spec.rb
@@ -0,0 +1,221 @@
+#
+# Author:: Steven Danna (steve@opscode.com)
+# Copyright:: Copyright (c) 2012 Opscode, Inc.
+# License:: Apache License, Version 2.0
+#
+# 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.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'spec_helper'
+
+require 'chef/user'
+require 'tempfile'
+
+describe Chef::User do
+ before(:each) do
+ @client = Chef::User.new
+ end
+
+ describe "initialize" do
+ it "should be a Chef::User" do
+ @client.should be_a_kind_of(Chef::User)
+ end
+ end
+
+ describe "name" do
+ it "should let you set the name to a string" do
+ @client.name("ops_master").should == "ops_master"
+ end
+
+ it "should return the current name" do
+ @client.name "ops_master"
+ @client.name.should == "ops_master"
+ end
+
+ it "should not accept spaces" do
+ lambda { @client.name "ops master" }.should raise_error(ArgumentError)
+ end
+
+ it "should throw an ArgumentError if you feed it anything but a string" do
+ lambda { @client.name Hash.new }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "admin" do
+ it "should let you set the admin bit" do
+ @client.admin(true).should == true
+ end
+
+ it "should return the current admin value" do
+ @client.admin true
+ @client.admin.should == true
+ end
+
+ it "should default to false" do
+ @client.admin.should == false
+ end
+
+ it "should throw an ArgumentError if you feed it anything but true or false" do
+ lambda { @client.name Hash.new }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "public_key" do
+ it "should let you set the public key" do
+ @client.public_key("super public").should == "super public"
+ end
+
+ it "should return the current public key" do
+ @client.public_key("super public")
+ @client.public_key.should == "super public"
+ end
+
+ it "should throw an ArgumentError if you feed it something lame" do
+ lambda { @client.public_key Hash.new }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "private_key" do
+ it "should let you set the private key" do
+ @client.private_key("super private").should == "super private"
+ end
+
+ it "should return the private key" do
+ @client.private_key("super private")
+ @client.private_key.should == "super private"
+ end
+
+ it "should throw an ArgumentError if you feed it something lame" do
+ lambda { @client.private_key Hash.new }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "when serializing to JSON" do
+ before(:each) do
+ @client.name("black")
+ @client.public_key("crowes")
+ @json = @client.to_json
+ end
+
+ it "serializes as a JSON object" do
+ @json.should match(/^\{.+\}$/)
+ end
+
+ it "includes the name value" do
+ @json.should include(%q{"name":"black"})
+ end
+
+ it "includes the public key value" do
+ @json.should include(%{"public_key":"crowes"})
+ end
+
+ it "includes the 'admin' flag" do
+ @json.should include(%q{"admin":false})
+ end
+
+ it "includes the private key when present" do
+ @client.private_key("monkeypants")
+ @client.to_json.should include(%q{"private_key":"monkeypants"})
+ end
+
+ it "does not include the private key if not present" do
+ @json.should_not include("private_key")
+ end
+
+ it "includes the password if present" do
+ @client.password "password"
+ @client.to_json.should include(%q{"password":"password"})
+ end
+
+ it "does not include the password if not present" do
+ @json.should_not include("password")
+ end
+ end
+
+ describe "when deserializing from JSON" do
+ before(:each) do
+ user = { "name" => "mr_spinks",
+ "public_key" => "turtles",
+ "private_key" => "pandas",
+ "password" => "password",
+ "admin" => true }
+ @user = Chef::User.from_json(user.to_json)
+ end
+
+ it "should deserialize to a Chef::User object" do
+ @user.should be_a_kind_of(Chef::User)
+ end
+
+ it "preserves the name" do
+ @user.name.should == "mr_spinks"
+ end
+
+ it "preserves the public key" do
+ @user.public_key.should == "turtles"
+ end
+
+ it "preserves the admin status" do
+ @user.admin.should be_true
+ end
+
+ it "includes the private key if present" do
+ @user.private_key.should == "pandas"
+ end
+
+ it "includes the password if present" do
+ @user.password.should == "password"
+ end
+
+ end
+
+ describe "API Interactions" do
+ before (:each) do
+ @user = Chef::User.new
+ @user.name "foobar"
+ @http_client = mock("Chef::REST mock")
+ Chef::REST.stub!(:new).and_return(@http_client)
+ end
+
+ describe "create" do
+ it "creates a new user via the API" do
+ @user.password "password"
+ @http_client.should_receive(:post_rest).with("users", {:name => "foobar", :admin => false, :password => "password"}).and_return({})
+ @user.create
+ end
+ end
+
+ describe "read" do
+ it "loads a named user from the API" do
+ @http_client.should_receive(:get_rest).with("users/foobar").and_return({"name" => "foobar", "admin" => true, "public_key" => "pubkey"})
+ user = Chef::User.load("foobar")
+ user.name.should == "foobar"
+ user.admin.should == true
+ user.public_key.should == "pubkey"
+ end
+ end
+
+ describe "update" do
+ it "updates an existing user on via the API" do
+ @http_client.should_receive(:put_rest).with("users/foobar", {:name => "foobar", :admin => false}).and_return({})
+ @user.update
+ end
+ end
+
+ describe "destroy" do
+ it "deletes the specified user via the API" do
+ @http_client.should_receive(:delete_rest).with("users/foobar")
+ @user.destroy
+ end
+ end
+ end
+end