summaryrefslogtreecommitdiff
path: root/lib/chef/user.rb
diff options
context:
space:
mode:
authortylercloke <tylercloke@gmail.com>2015-05-27 14:32:24 -0700
committertylercloke <tylercloke@gmail.com>2015-06-05 10:38:48 -0700
commit9b9d376f2e86cd2738c2c2928f77bf48f1dd20ee (patch)
tree226227e7e32f421a0dde0f1ad2046059eff449c3 /lib/chef/user.rb
parent20f7e1c78c55d2a16d5033bc2bbe5904d225adb0 (diff)
downloadchef-9b9d376f2e86cd2738c2c2928f77bf48f1dd20ee.tar.gz
Added versioned Chef Object API support code and repaired Chef::User.create.
Also added Chef::User.create V1 API support. Chef::User.create will attempt to use V1 of the server API, and if it fails, it will fall back to V0.
Diffstat (limited to 'lib/chef/user.rb')
-rw-r--r--lib/chef/user.rb158
1 files changed, 136 insertions, 22 deletions
diff --git a/lib/chef/user.rb b/lib/chef/user.rb
index 42fa6b5fa1..4e0d12e3b5 100644
--- a/lib/chef/user.rb
+++ b/lib/chef/user.rb
@@ -21,31 +21,96 @@ require 'chef/mixin/from_file'
require 'chef/mash'
require 'chef/json_compat'
require 'chef/search/query'
+require 'chef/versioned_rest'
+require 'chef/mixin/api_version_request_handling'
+require 'chef/exceptions'
class Chef
class User
include Chef::Mixin::FromFile
include Chef::Mixin::ParamsValidate
+ include Chef::VersionedRest
+ include Chef::ApiVersionRequestHandling
+
+ SUPPORTED_API_VERSIONS = [0,1]
def initialize
- @name = ''
+ @username = nil
+ @display_name = nil
+ @first_name = nil
+ @middle_name = nil
+ @last_name = nil
+ @email = nil
+ @password = nil
@public_key = nil
@private_key = nil
+ @create_key = nil
@password = nil
@admin = false
end
- def name(arg=nil)
- set_or_return(:name, arg,
+ def chef_rest_v0
+ @chef_rest_v0 ||= get_versioned_rest_object(Chef::Config[:chef_server_url], "0")
+ end
+
+ def chef_rest_v1
+ @chef_rest_v1 ||= get_versioned_rest_object(Chef::Config[:chef_server_url], "1")
+ end
+
+ def chef_root_rest_v0
+ @chef_root_rest_v0 ||= get_versioned_rest_object(Chef::Config[:chef_server_root], "0")
+ end
+
+ def chef_root_rest_v1
+ @chef_root_rest_v1 ||= get_versioned_rest_object(Chef::Config[:chef_server_root], "1")
+ end
+
+ def username(arg=nil)
+ set_or_return(:username, arg,
:regex => /^[a-z0-9\-_]+$/)
end
+ def display_name(arg=nil)
+ set_or_return(:display_name,
+ arg, :kind_of => String)
+ end
+
+ def first_name(arg=nil)
+ set_or_return(:first_name,
+ arg, :kind_of => String)
+ end
+
+ def middle_name(arg=nil)
+ set_or_return(:middle_name,
+ arg, :kind_of => String)
+ end
+
+ def last_name(arg=nil)
+ set_or_return(:last_name,
+ arg, :kind_of => String)
+ end
+
+ def email(arg=nil)
+ set_or_return(:email,
+ arg, :kind_of => String)
+ end
+
+ def password(arg=nil)
+ set_or_return(:password,
+ arg, :kind_of => String)
+ end
+
def admin(arg=nil)
set_or_return(:admin,
arg, :kind_of => [TrueClass, FalseClass])
end
+ def create_key(arg=nil)
+ set_or_return(:create_key, arg,
+ :kind_of => [TrueClass, FalseClass])
+ end
+
def public_key(arg=nil)
set_or_return(:public_key,
arg, :kind_of => String)
@@ -63,12 +128,17 @@ class Chef
def to_hash
result = {
- "name" => @name,
- "public_key" => @public_key,
+ "username" => @username,
"admin" => @admin
}
- result["private_key"] = @private_key if @private_key
+ result["display_name"] = @display_name if @display_name
+ result["first_name"] = @first_name if @first_name
+ result["middle_name"] = @middle_name if @middle_name
+ result["last_name"] = @last_name if @last_name
+ result["email"] = @email if @email
result["password"] = @password if @password
+ result["public_key"] = @public_key if @public_key
+ result["private_key"] = @private_key if @private_key
result
end
@@ -77,21 +147,59 @@ class Chef
end
def destroy
- Chef::REST.new(Chef::Config[:chef_server_url]).delete_rest("users/#{@name}")
+ Chef::REST.new(Chef::Config[:chef_server_url]).delete("users/#{@username}")
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)
+ # try v1, fail back to v0 if v1 not supported
+ begin
+ payload = {
+ :username => @username,
+ :display_name => @display_name,
+ :first_name => @first_name,
+ :last_name => @last_name,
+ :email => @email,
+ :password => @password,
+ :admin => @admin
+ }
+ payload[:public_key] = @public_key if @public_key
+ payload[:create_key] = @create_key if @create_key
+ payload[:middle_name] = @middle_name if @middle_name
+ raise Chef::Exceptions::InvalidUserAttribute, "You cannot set both public_key and create_key for create." if @create_key && @public_key
+ new_user = chef_root_rest_v1.post("users", payload)
+
+ # get the private_key out of the chef_key hash if it exists
+ if new_user['chef_key']
+ if new_user['chef_key']['private_key']
+ new_user['private_key'] = new_user['chef_key']['private_key']
+ end
+ new_user['public_key'] = new_user['chef_key']['public_key']
+ new_user.delete('chef_key')
+ end
+ rescue Net::HTTPServerException => e
+ raise e unless handle_version_http_exception(e, SUPPORTED_API_VERSIONS[0], SUPPORTED_API_VERSIONS[-1])
+ payload = {
+ :username => @username,
+ :display_name => @display_name,
+ :first_name => @first_name,
+ :last_name => @last_name,
+ :email => @email,
+ :password => @password,
+ :admin => @admin
+ }
+ payload[:middle_name] = @middle_name if @middle_name
+ payload[:public_key] = @public_key if @public_key
+ new_user = chef_root_rest_v0.post("users", payload)
+ end
+
Chef::User.from_hash(self.to_hash.merge(new_user))
end
def update(new_key=false)
- payload = {:name => name, :admin => admin}
+ payload = {:username => username, :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)
+ updated_user = Chef::REST.new(Chef::Config[:chef_server_url]).put("users/#{username}", payload)
Chef::User.from_hash(self.to_hash.merge(updated_user))
end
@@ -109,17 +217,17 @@ class Chef
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 })
+ reregistered_self = r.put("users/#{username}", { :username => username, :admin => admin, :private_key => true })
private_key(reregistered_self["private_key"])
self
end
def to_s
- "user[#{@name}]"
+ "user[#{@username}]"
end
def inspect
- "Chef::User name:'#{name}' admin:'#{admin.inspect}'" +
+ "Chef::User username:'#{username}' admin:'#{admin.inspect}'" +
"public_key:'#{public_key}' private_key:#{private_key}"
end
@@ -127,11 +235,17 @@ class Chef
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.username user_hash['username']
user.admin user_hash['admin']
+ user.display_name user_hash['display_name'] if user_hash.key?('display_name')
+ user.first_name user_hash['first_name'] if user_hash.key?('first_name')
+ user.middle_name user_hash['middle_name'] if user_hash.key?('middle_name')
+ user.last_name user_hash['last_name'] if user_hash.key?('last_name')
+ user.email user_hash['email'] if user_hash.key?('email')
+ user.password user_hash['password'] if user_hash.key?('password')
+ user.public_key user_hash['public_key'] if user_hash.key?('public_key')
+ user.private_key user_hash['private_key'] if user_hash.key?('private_key')
+ user.create_key user_hash['create_key'] if user_hash.key?('create_key')
user
end
@@ -144,7 +258,7 @@ class Chef
end
def self.list(inflate=false)
- response = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest('users')
+ response = Chef::REST.new(Chef::Config[:chef_server_url]).get('users')
users = if response.is_a?(Array)
transform_ohc_list_response(response) # OHC/OPC
else
@@ -160,8 +274,8 @@ class Chef
end
end
- def self.load(name)
- response = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("users/#{name}")
+ def self.load(username)
+ response = Chef::REST.new(Chef::Config[:chef_server_url]).get("users/#{username}")
Chef::User.from_hash(response)
end