summaryrefslogtreecommitdiff
path: root/lib/chef
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chef')
-rw-r--r--lib/chef/api_client.rb23
-rw-r--r--lib/chef/formatters/error_inspectors/api_error_formatting.rb8
-rw-r--r--lib/chef/knife/client_create.rb7
-rw-r--r--lib/chef/knife/osc_user_create.rb5
-rw-r--r--lib/chef/knife/osc_user_delete.rb9
-rw-r--r--lib/chef/knife/osc_user_edit.rb9
-rw-r--r--lib/chef/knife/osc_user_list.rb9
-rw-r--r--lib/chef/knife/osc_user_reregister.rb9
-rw-r--r--lib/chef/knife/osc_user_show.rb9
-rw-r--r--lib/chef/knife/user_create.rb116
-rw-r--r--lib/chef/knife/user_delete.rb56
-rw-r--r--lib/chef/knife/user_edit.rb46
-rw-r--r--lib/chef/knife/user_list.rb3
-rw-r--r--lib/chef/knife/user_reregister.rb48
-rw-r--r--lib/chef/knife/user_show.rb32
-rw-r--r--lib/chef/osc_user.rb2
-rw-r--r--lib/chef/user.rb43
17 files changed, 324 insertions, 110 deletions
diff --git a/lib/chef/api_client.rb b/lib/chef/api_client.rb
index d486cc9808..3b777b3574 100644
--- a/lib/chef/api_client.rb
+++ b/lib/chef/api_client.rb
@@ -1,7 +1,7 @@
#
# Author:: Adam Jacob (<adam@chef.io>)
# Author:: Nuo Yan (<nuo@chef.io>)
-# Copyright:: Copyright (c) 2015 Chef Software, Inc.
+# Copyright:: Copyright (c) 2008, 2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -54,10 +54,12 @@ class Chef
@chef_rest_v1 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url], {:api_version => "1"})
end
+ # will default to the current version (Chef::Authenticator::DEFAULT_SERVER_API_VERSION)
def http_api
@http_api ||= Chef::REST.new(Chef::Config[:chef_server_url])
end
+ # will default to the current version (Chef::Authenticator::DEFAULT_SERVER_API_VERSION)
def self.http_api
Chef::REST.new(Chef::Config[:chef_server_url])
end
@@ -304,15 +306,15 @@ class Chef
new_client.delete('chef_key')
end
- rescue Net::HTTPServerException => e
- # rescue API V0 if 406 and the server supports V0
- raise e unless handle_version_http_exception(e, SUPPORTED_API_VERSIONS[0], SUPPORTED_API_VERSIONS[-1])
+ rescue Net::HTTPServerException => e
+ # rescue API V0 if 406 and the server supports V0
+ raise e unless handle_version_http_exception(e, SUPPORTED_API_VERSIONS[0], SUPPORTED_API_VERSIONS[-1])
- # under API V0, a key pair will always be created unless public_key is
- # passed on initial POST
- payload[:public_key] = public_key unless public_key.nil?
+ # under API V0, a key pair will always be created unless public_key is
+ # passed on initial POST
+ payload[:public_key] = public_key unless public_key.nil?
- new_client = chef_rest_v0.post("clients", payload)
+ new_client = chef_rest_v0.post("clients", payload)
end
Chef::ApiClient.from_hash(self.to_hash.merge(new_client))
end
@@ -322,10 +324,5 @@ class Chef
"client[#{@name}]"
end
- # def inspect
- # "Chef::ApiClient name:'#{name}' admin:'#{admin.inspect}' validator:'#{validator}' " +
- # "public_key:'#{public_key}' private_key:'#{private_key}'"
- # end
-
end
end
diff --git a/lib/chef/formatters/error_inspectors/api_error_formatting.rb b/lib/chef/formatters/error_inspectors/api_error_formatting.rb
index d68b38e41f..05ee3132a7 100644
--- a/lib/chef/formatters/error_inspectors/api_error_formatting.rb
+++ b/lib/chef/formatters/error_inspectors/api_error_formatting.rb
@@ -75,10 +75,10 @@ E
max_server_version = version_header["max_version"]
error_description.section("Incompatible server API version:",<<-E)
-This version of the API that this Chef request specified is not supported by the Chef server you sent this request to
-The Chef server you sent the request to supports a min API version of #{min_server_version} and a max API version of #{max_server_version}
-Chef just made a request with an API version of #{client_api_version}
-Please either update your Chef client or server to be a compatible set
+This version of the API that this Chef request specified is not supported by the Chef server you sent this request to.
+The server supports a min API version of #{min_server_version} and a max API version of #{max_server_version}.
+Chef just made a request with an API version of #{client_api_version}.
+Please either update your Chef client or server to be a compatible set.
E
else
describe_http_error(error_description)
diff --git a/lib/chef/knife/client_create.rb b/lib/chef/knife/client_create.rb
index 17c1204c2d..570c1ee950 100644
--- a/lib/chef/knife/client_create.rb
+++ b/lib/chef/knife/client_create.rb
@@ -46,7 +46,7 @@ class Chef
option :public_key,
:short => "-p FILE",
:long => "--public-key",
- :description => "Set the initial default key for the client from a file on disk (cannot pass with --create-key)."
+ :description => "Set the initial default key for the client from a file on disk (cannot pass with --prevent-keygen)."
option :prevent_keygen,
:short => "-k",
@@ -61,7 +61,8 @@ class Chef
end
def create_client(client)
- client.create
+ # should not be using save :( bad behavior
+ client.save
end
def run
@@ -74,7 +75,7 @@ class Chef
exit 1
end
- unless config[:prevent_keygen]
+ if !config[:prevent_keygen] && !config[:public_key]
client.create_key(true)
end
diff --git a/lib/chef/knife/osc_user_create.rb b/lib/chef/knife/osc_user_create.rb
index d13a3a3615..c368296040 100644
--- a/lib/chef/knife/osc_user_create.rb
+++ b/lib/chef/knife/osc_user_create.rb
@@ -21,8 +21,7 @@ require 'chef/knife'
# DEPRECATION NOTE
# This code only remains to support users still operating with
# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# for OSC 11 ends. New development should occur in user_create.rb.
class Chef
class Knife
class OscUserCreate < Knife
@@ -95,4 +94,4 @@ class Chef
end
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/knife/osc_user_delete.rb b/lib/chef/knife/osc_user_delete.rb
index baadeec9ef..d6fbd4a6a9 100644
--- a/lib/chef/knife/osc_user_delete.rb
+++ b/lib/chef/knife/osc_user_delete.rb
@@ -19,10 +19,9 @@
require 'chef/knife'
# DEPRECATION NOTE
-# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# This code only remains to support users still operating with
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in the user_delete.rb.
class Chef
class Knife
@@ -49,4 +48,4 @@ class Chef
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/knife/osc_user_edit.rb b/lib/chef/knife/osc_user_edit.rb
index d8afb13859..4c38674d08 100644
--- a/lib/chef/knife/osc_user_edit.rb
+++ b/lib/chef/knife/osc_user_edit.rb
@@ -19,10 +19,9 @@
require 'chef/knife'
# DEPRECATION NOTE
-# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# This code only remains to support users still operating with
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in user_edit.rb.
class Chef
class Knife
@@ -56,4 +55,4 @@ class Chef
end
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/knife/osc_user_list.rb b/lib/chef/knife/osc_user_list.rb
index 10666b2c19..92f049cd19 100644
--- a/lib/chef/knife/osc_user_list.rb
+++ b/lib/chef/knife/osc_user_list.rb
@@ -19,10 +19,9 @@
require 'chef/knife'
# DEPRECATION NOTE
-# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# This code only remains to support users still operating with
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in user_list.rb.
class Chef
class Knife
@@ -45,4 +44,4 @@ class Chef
end
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/knife/osc_user_reregister.rb b/lib/chef/knife/osc_user_reregister.rb
index d940820546..a71e0aa677 100644
--- a/lib/chef/knife/osc_user_reregister.rb
+++ b/lib/chef/knife/osc_user_reregister.rb
@@ -19,10 +19,9 @@
require 'chef/knife'
# DEPRECATION NOTE
-# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# This code only remains to support users still operating with
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in user_reregister.rb.
class Chef
class Knife
@@ -62,4 +61,4 @@ class Chef
end
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/knife/osc_user_show.rb b/lib/chef/knife/osc_user_show.rb
index a383a764b2..6a41ddae88 100644
--- a/lib/chef/knife/osc_user_show.rb
+++ b/lib/chef/knife/osc_user_show.rb
@@ -19,10 +19,9 @@
require 'chef/knife'
# DEPRECATION NOTE
-# This code only remains to support users still operating with
-# Open Source Chef Server 11 and should be removed once support
-# for OSC 11 ends. New development should occur in the Chef Server 12
-# version of this file.
+# This code only remains to support users still operating with
+# Open Source Chef Server 11 and should be removed once support
+# for OSC 11 ends. New development should occur in user_show.rb.
class Chef
class Knife
@@ -52,4 +51,4 @@ class Chef
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/knife/user_create.rb b/lib/chef/knife/user_create.rb
index 18addda741..5332e863ae 100644
--- a/lib/chef/knife/user_create.rb
+++ b/lib/chef/knife/user_create.rb
@@ -1,7 +1,7 @@
#
# Author:: Steven Danna (<steve@chef.io>)
# Author:: Tyler Cloke (<tyler@chef.io>)
-# Copyright:: Copyright (c) 2015 Chef Software, Inc.
+# Copyright:: Copyright (c) 2012, 2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -18,6 +18,7 @@
#
require 'chef/knife'
+require 'chef/knife/osc_user_create'
class Chef
class Knife
@@ -37,7 +38,7 @@ class Chef
option :user_key,
:long => "--user-key FILENAME",
- :description => "Set the initial default key for the user from a file on disk (cannot pass with --create-key)."
+ :description => "Set the initial default key for the user from a file on disk (cannot pass with --prevent-keygen)."
option :prevent_keygen,
:short => "-k",
@@ -45,6 +46,18 @@ class Chef
:description => "API V1 only. Prevent server from generating a default key pair for you. Cannot be passed with --user-key.",
:boolean => true
+ option :admin,
+ :short => "-a",
+ :long => "--admin",
+ :description => "DEPRECATED: Open Source Chef 11 only. Create the user as an admin.",
+ :boolean => true
+
+ option :user_password,
+ :short => "-p PASSWORD",
+ :long => "--password PASSWORD",
+ :description => "DEPRECATED: Open Source Chef 11 only. Password for newly created user.",
+ :default => ""
+
banner "knife user create USERNAME DISPLAY_NAME FIRST_NAME LAST_NAME EMAIL PASSWORD (options)"
def user
@@ -55,52 +68,87 @@ class Chef
Chef::User.from_hash(hash).create
end
+ def osc_11_warning
+<<-EOF
+IF YOU ARE USING CHEF SERVER 12+, PLEASE FOLLOW THE INSTRUCTIONS UNDER knife user create --help.
+You only passed a single argument to knife user create.
+For backwards compatibility, when only a single argument is passed,
+knife user create assumes you want Open Source 11 Server user creation.
+knife user create for Open Source 11 Server is being deprecated.
+Open Source 11 Server user commands now live under the knife osc_user namespace.
+For backwards compatibility, we will forward this request to knife osc_user create.
+If you are using an Open Source 11 Server, please use that command to avoid this warning.
+If you are not using an Open Source Chef 11 Server install, please read knife user create --help for proper usage.
+EOF
+ end
+
+ def run_osc_11_user_create
+ Chef::Knife.run(ARGV, Chef::Application::Knife.options)
+ end
+
def run
- test_mandatory_field(@name_args[0], "username")
- user.username @name_args[0]
+ # DEPRECATION NOTE
+ # Remove this if statement and corrosponding code post OSC 11 support.
+ #
+ # If only 1 arg is passed, assume OSC 11 case.
+ if @name_args.length == 1
+ ui.warn(osc_11_warning)
- test_mandatory_field(@name_args[1], "display name")
- user.display_name @name_args[1]
+ # run osc_user_create with our input
+ ARGV.delete("user")
+ ARGV.unshift("osc_user")
+ run_osc_11_user_create
- test_mandatory_field(@name_args[2], "first name")
- user.first_name @name_args[2]
+ else # EC / CS 12 user create
- test_mandatory_field(@name_args[3], "last name")
- user.last_name @name_args[3]
+ test_mandatory_field(@name_args[0], "username")
+ user.username @name_args[0]
- test_mandatory_field(@name_args[4], "email")
- user.email @name_args[4]
+ test_mandatory_field(@name_args[1], "display name")
+ user.display_name @name_args[1]
- test_mandatory_field(@name_args[5], "password")
- user.password @name_args[5]
+ test_mandatory_field(@name_args[2], "first name")
+ user.first_name @name_args[2]
- if config[:user_key] && config[:prevent_keygen]
- show_usage
- ui.fatal("You cannot pass --user-key and --prevent-keygen")
- exit 1
- end
+ test_mandatory_field(@name_args[3], "last name")
+ user.last_name @name_args[3]
- unless config[:prevent_keygen]
- user.create_key(true)
- end
+ test_mandatory_field(@name_args[4], "email")
+ user.email @name_args[4]
- if config[:user_key]
- user.public_key File.read(File.expand_path(config[:user_key]))
- end
+ test_mandatory_field(@name_args[5], "password")
+ user.password @name_args[5]
+
+ if config[:user_key] && config[:prevent_keygen]
+ show_usage
+ ui.fatal("You cannot pass --user-key and --prevent-keygen")
+ exit 1
+ end
+
+ if !config[:prevent_keygen] && !config[:user_key]
+ user.create_key(true)
+ end
- output = edit_data(user)
- final_user = create_user_from_hash(output)
+ if config[:user_key]
+ user.public_key File.read(File.expand_path(config[:user_key]))
+ end
- ui.info("Created #{user}")
- if final_user.private_key
- if config[:file]
- File.open(config[:file], "w") do |f|
- f.print(final_user.private_key)
+ output = edit_data(user)
+ final_user = create_user_from_hash(output)
+
+ ui.info("Created #{user}")
+ if final_user.private_key
+ if config[:file]
+ File.open(config[:file], "w") do |f|
+ f.print(final_user.private_key)
+ end
+ else
+ ui.msg final_user.private_key
end
- else
- ui.msg final_user.private_key
end
end
+
+
end
end
end
diff --git a/lib/chef/knife/user_delete.rb b/lib/chef/knife/user_delete.rb
index b7af11bec8..3a46e33f84 100644
--- a/lib/chef/knife/user_delete.rb
+++ b/lib/chef/knife/user_delete.rb
@@ -29,6 +29,37 @@ class Chef
banner "knife user delete USER (options)"
+ def osc_11_warning
+<<-EOF
+The Chef Server you are using does not support the username field.
+This means it is an Open Source 11 Server.
+knife user delete for Open Source 11 Server is being deprecated.
+Open Source 11 Server user commands now live under the knife osc_user namespace.
+For backwards compatibility, we will forward this request to knife osc_user delete.
+If you are using an Open Source 11 Server, please use that command to avoid this warning.
+EOF
+ end
+
+ def run_osc_11_user_delete
+ Chef::Knife.run(ARGV, Chef::Application::Knife.options)
+ end
+
+ # DEPRECATION NOTE
+ # Delete this override method after OSC 11 support is dropped
+ def delete_object(user_name)
+ confirm("Do you really want to delete #{user_name}")
+
+ if Kernel.block_given?
+ object = block.call
+ else
+ object = Chef::User.load(user_name)
+ object.destroy
+ end
+
+ output(format_for_display(object)) if config[:print_after]
+ self.msg("Deleted #{user_name}")
+ end
+
def run
@user_name = @name_args[0]
@@ -38,9 +69,30 @@ class Chef
exit 1
end
- delete_object(Chef::User, @user_name)
- end
+ # DEPRECATION NOTE
+ #
+ # Below is modification of Chef::Knife.delete_object to detect OSC 11 server.
+ # When OSC 11 is deprecated, simply delete all this and go back to:
+ #
+ # delete_object(Chef::User, @user_name)
+ #
+ # Also delete our override of delete_object above
+ object = Chef::User.load(@user_name)
+
+ # OSC 11 case
+ if object.username.nil?
+ ui.warn(osc_11_warning)
+
+ # run osc_user_delete with our input
+ ARGV.delete("user")
+ ARGV.unshift("osc_user")
+ run_osc_11_user_delete
+ else # proceed with EC / CS delete
+ delete_object(@user_name)
+ end
+
+ end
end
end
end
diff --git a/lib/chef/knife/user_edit.rb b/lib/chef/knife/user_edit.rb
index ae319c8872..5bed51a1cc 100644
--- a/lib/chef/knife/user_edit.rb
+++ b/lib/chef/knife/user_edit.rb
@@ -29,6 +29,21 @@ class Chef
banner "knife user edit USER (options)"
+ def osc_11_warning
+<<-EOF
+The Chef Server you are using does not support the username field.
+This means it is an Open Source 11 Server.
+knife user edit for Open Source 11 Server is being deprecated.
+Open Source 11 Server user commands now live under the knife oc_user namespace.
+For backwards compatibility, we will forward this request to knife osc_user edit.
+If you are using an Open Source 11 Server, please use that command to avoid this warning.
+EOF
+ end
+
+ def run_osc_11_user_edit
+ Chef::Knife.run(ARGV, Chef::Application::Knife.options)
+ end
+
def run
@user_name = @name_args[0]
@@ -39,14 +54,31 @@ class Chef
end
original_user = Chef::User.load(@user_name).to_hash
- edited_user = edit_data(original_user)
- if original_user != edited_user
- user = Chef::User.from_hash(edited_user)
- user.update
- ui.msg("Saved #{user}.")
- else
- ui.msg("User unchaged, not saving.")
+
+ # DEPRECATION NOTE
+ # Remove this if statement and corrosponding code post OSC 11 support.
+ #
+ # if username is nil, we are in the OSC 11 case,
+ # forward to deprecated command
+ if original_user["username"].nil?
+ ui.warn(osc_11_warning)
+
+ # run osc_user_create with our input
+ ARGV.delete("user")
+ ARGV.unshift("osc_user")
+ run_osc_11_user_edit
+
+ else # EC / CS 12 user create
+ edited_user = edit_data(original_user)
+ if original_user != edited_user
+ user = Chef::User.from_hash(edited_user)
+ user.update
+ ui.msg("Saved #{user}.")
+ else
+ ui.msg("User unchaged, not saving.")
+ end
end
+
end
end
end
diff --git a/lib/chef/knife/user_list.rb b/lib/chef/knife/user_list.rb
index 5d2e735a73..7ae43dadc9 100644
--- a/lib/chef/knife/user_list.rb
+++ b/lib/chef/knife/user_list.rb
@@ -18,6 +18,8 @@
require 'chef/knife'
+# NOTE: only knife user command that is backwards compatible with OSC 11,
+# so no deprecation warnings are necessary.
class Chef
class Knife
class UserList < Knife
@@ -37,6 +39,7 @@ class Chef
def run
output(format_list_for_display(Chef::User.list))
end
+
end
end
end
diff --git a/lib/chef/knife/user_reregister.rb b/lib/chef/knife/user_reregister.rb
index 946150e6e4..c2b39761cc 100644
--- a/lib/chef/knife/user_reregister.rb
+++ b/lib/chef/knife/user_reregister.rb
@@ -29,6 +29,21 @@ class Chef
banner "knife user reregister USER (options)"
+ def osc_11_warning
+<<-EOF
+The Chef Server you are using does not support the username field.
+This means it is an Open Source 11 Server.
+knife user reregister for Open Source 11 Server is being deprecated.
+Open Source 11 Server user commands now live under the knife osc_user namespace.
+For backwards compatibility, we will forward this request to knife osc_user reregister.
+If you are using an Open Source 11 Server, please use that command to avoid this warning.
+EOF
+ end
+
+ def run_osc_11_user_reregister
+ Chef::Knife.run(ARGV, Chef::Application::Knife.options)
+ end
+
option :file,
:short => "-f FILE",
:long => "--file FILE",
@@ -43,16 +58,33 @@ class Chef
exit 1
end
- user = Chef::User.load(@user_name).reregister
- Chef::Log.debug("Updated user data: #{user.inspect}")
- key = user.private_key
- if config[:file]
- File.open(config[:file], "w") do |f|
- f.print(key)
+ user = Chef::User.load(@user_name)
+
+ # DEPRECATION NOTE
+ # Remove this if statement and corrosponding code post OSC 11 support.
+ #
+ # if username is nil, we are in the OSC 11 case,
+ # forward to deprecated command
+ if user.username.nil?
+ ui.warn(osc_11_warning)
+
+ # run osc_user_edit with our input
+ ARGV.delete("user")
+ ARGV.unshift("osc_user")
+ run_osc_11_user_reregister
+ else # EC / CS 12 case
+ user.reregister
+ Chef::Log.debug("Updated user data: #{user.inspect}")
+ key = user.private_key
+ if config[:file]
+ File.open(config[:file], "w") do |f|
+ f.print(key)
+ end
+ else
+ ui.msg key
end
- else
- ui.msg key
end
+
end
end
end
diff --git a/lib/chef/knife/user_show.rb b/lib/chef/knife/user_show.rb
index 61ea101e4c..d0e9f64f53 100644
--- a/lib/chef/knife/user_show.rb
+++ b/lib/chef/knife/user_show.rb
@@ -31,6 +31,21 @@ class Chef
banner "knife user show USER (options)"
+ def osc_11_warning
+<<-EOF
+The Chef Server you are using does not support the username field.
+This means it is an Open Source 11 Server.
+knife user show for Open Source 11 Server is being deprecated.
+Open Source 11 Server user commands now live under the knife osc_user namespace.
+For backwards compatibility, we will forward this request to knife osc_user show.
+If you are using an Open Source 11 Server, please use that command to avoid this warning.
+EOF
+ end
+
+ def run_osc_11_user_show
+ Chef::Knife.run(ARGV, Chef::Application::Knife.options)
+ end
+
def run
@user_name = @name_args[0]
@@ -41,7 +56,22 @@ class Chef
end
user = Chef::User.load(@user_name)
- output(format_for_display(user))
+
+ # DEPRECATION NOTE
+ # Remove this if statement and corrosponding code post OSC 11 support.
+ #
+ # if username is nil, we are in the OSC 11 case,
+ # forward to deprecated command
+ if user.username.nil?
+ ui.warn(osc_11_warning)
+
+ # run osc_user_edit with our input
+ ARGV.delete("user")
+ ARGV.unshift("osc_user")
+ run_osc_11_user_show
+ else
+ output(format_for_display(user))
+ end
end
end
diff --git a/lib/chef/osc_user.rb b/lib/chef/osc_user.rb
index d2e329c944..52bfd11108 100644
--- a/lib/chef/osc_user.rb
+++ b/lib/chef/osc_user.rb
@@ -26,7 +26,7 @@ require 'chef/search/query'
# DEPRECATION NOTE
# This class was previously Chef::User. It is the code to support the User object
# corrosponding to the Open Source Chef Server 11 and only still exists to support
-# users still on OSC 11.
+# users still on OSC 11.
#
# Chef::User now supports Chef Server 12.
#
diff --git a/lib/chef/user.rb b/lib/chef/user.rb
index 2ffba18005..1b5f454099 100644
--- a/lib/chef/user.rb
+++ b/lib/chef/user.rb
@@ -25,6 +25,12 @@ require 'chef/mixin/api_version_request_handling'
require 'chef/exceptions'
require 'chef/server_api'
+# OSC 11 BACKWARDS COMPATIBILITY NOTE (remove after OSC 11 support ends)
+#
+# In general, Chef::User is no longer expected to support Open Source Chef 11 Server requests.
+# The object that handles those requests has been moved to the Chef::OscUser namespace.
+#
+# Exception: self.list is backwards compatible with OSC 11
class Chef
class User
@@ -132,6 +138,7 @@ class Chef
end
def destroy
+ # will default to the current API version (Chef::Authenticator::DEFAULT_SERVER_API_VERSION)
Chef::REST.new(Chef::Config[:chef_server_url]).delete("users/#{@username}")
end
@@ -278,15 +285,17 @@ class Chef
def self.list(inflate=false)
response = Chef::REST.new(Chef::Config[:chef_server_url]).get('users')
- # Gross. Transforms an API response in the form of:
- # [ { "user" => { "username" => USERNAME }}, ...]
- # into the form
- # { "USERNAME" => "URI" }
- users = Hash.new
- response.each do |u|
- name = u['user']['username']
- users[name] = Chef::Config[:chef_server_url] + "/users/#{name}"
- end
+ users = if response.is_a?(Array)
+ # EC 11 / CS 12 V0, V1
+ # GET /organizations/<org>/users
+ transform_list_response(response)
+ else
+ # OSC 11
+ # GET /users
+ # EC 11 / CS 12 V0, V1
+ # GET /users
+ response # OSC
+ end
if inflate
users.inject({}) do |user_map, (name, _url)|
@@ -299,9 +308,25 @@ class Chef
end
def self.load(username)
+ # will default to the current API version (Chef::Authenticator::DEFAULT_SERVER_API_VERSION)
response = Chef::REST.new(Chef::Config[:chef_server_url]).get("users/#{username}")
Chef::User.from_hash(response)
end
+ # Gross. Transforms an API response in the form of:
+ # [ { "user" => { "username" => USERNAME }}, ...]
+ # into the form
+ # { "USERNAME" => "URI" }
+ def self.transform_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
+
+ private_class_method :transform_list_response
+
end
end