summaryrefslogtreecommitdiff
path: root/lib/chef/knife
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/knife
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/knife')
-rw-r--r--lib/chef/knife/user_create.rb74
1 files changed, 47 insertions, 27 deletions
diff --git a/lib/chef/knife/user_create.rb b/lib/chef/knife/user_create.rb
index 4130f06878..992bbbe3c9 100644
--- a/lib/chef/knife/user_create.rb
+++ b/lib/chef/knife/user_create.rb
@@ -1,6 +1,7 @@
#
-# Author:: Steven Danna (<steve@opscode.com>)
-# Copyright:: Copyright (c) 2012 Opscode, Inc.
+# Author:: Steven Danna (<steve@chef.io>)
+# Author:: Tyler Cloke (<tyler@chef.io>)
+# Copyright:: Copyright (c) 2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,6 +23,8 @@ class Chef
class Knife
class UserCreate < Knife
+ attr_accessor :user_field
+
deps do
require 'chef/user'
require 'chef/json_compat'
@@ -30,61 +33,78 @@ class Chef
option :file,
:short => "-f FILE",
:long => "--file FILE",
- :description => "Write the private key to a file"
+ :description => "Write the private key to a file, if returned by the server. A private key will be returned when both --user-key and --no-key are NOT passed. In that case, the server will generate a default key for you and return the private key it creates."
option :admin,
:short => "-a",
:long => "--admin",
- :description => "Create the user as an admin",
+ :description => "Create the user as an admin (only relevant for Open Source Chef Server 11).",
:boolean => true
- option :user_password,
- :short => "-p PASSWORD",
- :long => "--password PASSWORD",
- :description => "Password for newly created user",
- :default => ""
-
option :user_key,
:long => "--user-key FILENAME",
- :description => "Public key for newly created user. By default a key will be created for you."
+ :description => "Public key for newly created user. Path to a public key you provide instead of having the server generate one. If --user-key is not passed, the server will create a 'default' key for you, unless you passed --no-key. Note that --user-key cannot be passed with --no-key."
+
+ option :no_key,
+ :long => "--no-key",
+ :description => "Do not create a 'default' public key for this new user. This prevents the server generating a public key by default. Cannot be passed with --user-key (requires server API version 1)."
+
+ banner "knife user create USERNAME DISPLAY_NAME FIRST_NAME LAST_NAME EMAIL PASSWORD (options)"
- banner "knife user create USER (options)"
+ def user
+ @user_field ||= Chef::User.new
+ end
+
+ def create_user_from_hash(hash)
+ Chef::User.from_hash(hash).create
+ end
def run
- @user_name = @name_args[0]
+ test_mandatory_field(@name_args[0], "username")
+ user.username @name_args[0]
- if @user_name.nil?
- show_usage
- ui.fatal("You must specify a user name")
- exit 1
- end
+ test_mandatory_field(@name_args[1], "display name")
+ user.display_name @name_args[1]
+
+ test_mandatory_field(@name_args[2], "first name")
+ user.first_name @name_args[2]
+
+ test_mandatory_field(@name_args[3], "last name")
+ user.last_name @name_args[3]
- if config[:user_password].length == 0
+ test_mandatory_field(@name_args[4], "email")
+ user.email @name_args[4]
+
+ test_mandatory_field(@name_args[5], "password")
+ user.password @name_args[5]
+
+ if config[:user_key] && config[:no_key]
show_usage
- ui.fatal("You must specify a non-blank password")
+ ui.fatal("You cannot pass --user-key and --no-key")
exit 1
end
- user = Chef::User.new
- user.name(@user_name)
user.admin(config[:admin])
- user.password config[:user_password]
+
+ unless config[:no_key]
+ user.create_key(true)
+ end
if config[:user_key]
user.public_key File.read(File.expand_path(config[:user_key]))
end
output = edit_data(user)
- user = Chef::User.from_hash(output).create
+ final_user = create_user_from_hash(output)
ui.info("Created #{user}")
- if user.private_key
+ if final_user.private_key
if config[:file]
File.open(config[:file], "w") do |f|
- f.print(user.private_key)
+ f.print(final_user.private_key)
end
else
- ui.msg user.private_key
+ ui.msg final_user.private_key
end
end
end