chef 11.0.0 → 11.2.0.rc.1
Sign up to get free protection for your applications and to get access to all the features.
- data/distro/common/html/chef-client.8.html +3 -3
- data/distro/common/html/chef-expander.8.html +3 -3
- data/distro/common/html/chef-expanderctl.8.html +3 -3
- data/distro/common/html/chef-server-webui.8.html +3 -3
- data/distro/common/html/chef-server.8.html +3 -3
- data/distro/common/html/chef-shell.1.html +7 -7
- data/distro/common/html/chef-solo.8.html +3 -3
- data/distro/common/html/chef-solr.8.html +3 -3
- data/distro/common/html/knife-bootstrap.1.html +3 -3
- data/distro/common/html/knife-client.1.html +4 -4
- data/distro/common/html/knife-configure.1.html +3 -3
- data/distro/common/html/knife-cookbook-site.1.html +6 -6
- data/distro/common/html/knife-cookbook.1.html +7 -7
- data/distro/common/html/knife-data-bag.1.html +3 -3
- data/distro/common/html/knife-environment.1.html +3 -3
- data/distro/common/html/knife-exec.1.html +4 -4
- data/distro/common/html/knife-index.1.html +4 -4
- data/distro/common/html/knife-node.1.html +4 -4
- data/distro/common/html/knife-role.1.html +6 -6
- data/distro/common/html/knife-search.1.html +5 -5
- data/distro/common/html/knife-ssh.1.html +3 -3
- data/distro/common/html/knife-status.1.html +4 -4
- data/distro/common/html/knife-tag.1.html +3 -3
- data/distro/common/html/knife.1.html +11 -11
- data/distro/common/man/man1/chef-shell.1 +1 -1
- data/distro/common/man/man1/knife-bootstrap.1 +1 -1
- data/distro/common/man/man1/knife-client.1 +1 -1
- data/distro/common/man/man1/knife-configure.1 +1 -1
- data/distro/common/man/man1/knife-cookbook-site.1 +1 -1
- data/distro/common/man/man1/knife-cookbook.1 +1 -1
- data/distro/common/man/man1/knife-data-bag.1 +1 -1
- data/distro/common/man/man1/knife-environment.1 +1 -1
- data/distro/common/man/man1/knife-exec.1 +1 -1
- data/distro/common/man/man1/knife-index.1 +1 -1
- data/distro/common/man/man1/knife-node.1 +1 -1
- data/distro/common/man/man1/knife-role.1 +1 -1
- data/distro/common/man/man1/knife-search.1 +1 -1
- data/distro/common/man/man1/knife-ssh.1 +1 -1
- data/distro/common/man/man1/knife-status.1 +1 -1
- data/distro/common/man/man1/knife-tag.1 +1 -1
- data/distro/common/man/man1/knife.1 +1 -1
- data/distro/common/man/man8/chef-client.8 +1 -1
- data/distro/common/man/man8/chef-expander.8 +1 -1
- data/distro/common/man/man8/chef-expanderctl.8 +1 -1
- data/distro/common/man/man8/chef-server-webui.8 +1 -1
- data/distro/common/man/man8/chef-server.8 +1 -1
- data/distro/common/man/man8/chef-solo.8 +1 -1
- data/distro/common/man/man8/chef-solr.8 +1 -1
- data/lib/chef/knife/configure.rb +16 -13
- data/lib/chef/knife/user_create.rb +93 -0
- data/lib/chef/knife/user_delete.rb +46 -0
- data/lib/chef/knife/user_edit.rb +53 -0
- data/lib/chef/knife/user_list.rb +42 -0
- data/lib/chef/knife/user_reregister.rb +59 -0
- data/lib/chef/knife/user_show.rb +52 -0
- data/lib/chef/user.rb +182 -0
- data/lib/chef/version.rb +1 -1
- data/spec/unit/knife/configure_spec.rb +23 -26
- data/spec/unit/knife/user_create_spec.rb +86 -0
- data/spec/unit/knife/user_delete_spec.rb +39 -0
- data/spec/unit/knife/user_edit_spec.rb +42 -0
- data/spec/unit/knife/user_list_spec.rb +32 -0
- data/spec/unit/knife/user_reregister_spec.rb +53 -0
- data/spec/unit/knife/user_show_spec.rb +41 -0
- data/spec/unit/user_spec.rb +255 -0
- metadata +1246 -1227
@@ -0,0 +1,93 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Steven Danna (<steve@opscode.com>)
|
3
|
+
# Copyright:: Copyright (c) 2012 Opscode, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'chef/knife'
|
20
|
+
|
21
|
+
class Chef
|
22
|
+
class Knife
|
23
|
+
class UserCreate < Knife
|
24
|
+
|
25
|
+
deps do
|
26
|
+
require 'chef/user'
|
27
|
+
require 'chef/json_compat'
|
28
|
+
end
|
29
|
+
|
30
|
+
option :file,
|
31
|
+
:short => "-f FILE",
|
32
|
+
:long => "--file FILE",
|
33
|
+
:description => "Write the private key to a file"
|
34
|
+
|
35
|
+
option :admin,
|
36
|
+
:short => "-a",
|
37
|
+
:long => "--admin",
|
38
|
+
:description => "Create the user as an admin",
|
39
|
+
:boolean => true
|
40
|
+
|
41
|
+
option :user_password,
|
42
|
+
:short => "-p PASSWORD",
|
43
|
+
:long => "--password PASSWORD",
|
44
|
+
:description => "Password for newly created user",
|
45
|
+
:default => ""
|
46
|
+
|
47
|
+
option :user_key,
|
48
|
+
:long => "--user-key FILENAME",
|
49
|
+
:description => "Public key for newly created user. By default a key will be created for you."
|
50
|
+
|
51
|
+
banner "knife user create USER (options)"
|
52
|
+
|
53
|
+
def run
|
54
|
+
@user_name = @name_args[0]
|
55
|
+
|
56
|
+
if @user_name.nil?
|
57
|
+
show_usage
|
58
|
+
ui.fatal("You must specify a user name")
|
59
|
+
exit 1
|
60
|
+
end
|
61
|
+
|
62
|
+
if config[:user_password].length == 0
|
63
|
+
show_usage
|
64
|
+
ui.fatal("You must specify a non-blank password")
|
65
|
+
exit 1
|
66
|
+
end
|
67
|
+
|
68
|
+
user = Chef::User.new
|
69
|
+
user.name(@user_name)
|
70
|
+
user.admin(config[:admin])
|
71
|
+
user.password config[:user_password]
|
72
|
+
|
73
|
+
if config[:user_key]
|
74
|
+
user.public_key File.read(File.expand_path(config[:user_key]))
|
75
|
+
end
|
76
|
+
|
77
|
+
output = edit_data(user)
|
78
|
+
user = Chef::User.from_hash(output).create
|
79
|
+
|
80
|
+
ui.info("Created #{user}")
|
81
|
+
if user.private_key
|
82
|
+
if config[:file]
|
83
|
+
File.open(config[:file], "w") do |f|
|
84
|
+
f.print(user.private_key)
|
85
|
+
end
|
86
|
+
else
|
87
|
+
puts user.private_key
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Steven Danna (<steve@opscode.com>)
|
3
|
+
# Copyright:: Copyright (c) 2012 Opscode, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'chef/knife'
|
20
|
+
|
21
|
+
class Chef
|
22
|
+
class Knife
|
23
|
+
class UserDelete < Knife
|
24
|
+
|
25
|
+
deps do
|
26
|
+
require 'chef/user'
|
27
|
+
require 'chef/json_compat'
|
28
|
+
end
|
29
|
+
|
30
|
+
banner "knife user delete USER (options)"
|
31
|
+
|
32
|
+
def run
|
33
|
+
@user_name = @name_args[0]
|
34
|
+
|
35
|
+
if @user_name.nil?
|
36
|
+
show_usage
|
37
|
+
ui.fatal("You must specify a user name")
|
38
|
+
exit 1
|
39
|
+
end
|
40
|
+
|
41
|
+
delete_object(Chef::User, @user_name)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Steven Danna (<steve@opscode.com>)
|
3
|
+
# Copyright:: Copyright (c) 2012 Opscode, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'chef/knife'
|
20
|
+
|
21
|
+
class Chef
|
22
|
+
class Knife
|
23
|
+
class UserEdit < Knife
|
24
|
+
|
25
|
+
deps do
|
26
|
+
require 'chef/user'
|
27
|
+
require 'chef/json_compat'
|
28
|
+
end
|
29
|
+
|
30
|
+
banner "knife user edit USER (options)"
|
31
|
+
|
32
|
+
def run
|
33
|
+
@user_name = @name_args[0]
|
34
|
+
|
35
|
+
if @user_name.nil?
|
36
|
+
show_usage
|
37
|
+
ui.fatal("You must specify a user name")
|
38
|
+
exit 1
|
39
|
+
end
|
40
|
+
|
41
|
+
original_user = Chef::User.load(@user_name).to_hash
|
42
|
+
edited_user = edit_data(original_user)
|
43
|
+
if original_user != edited_user
|
44
|
+
user = Chef::User.from_hash(edited_user)
|
45
|
+
user.update
|
46
|
+
ui.msg("Saved #{user}.")
|
47
|
+
else
|
48
|
+
ui.msg("User unchaged, not saving.")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Steven Danna (<steve@opscode.com>)
|
3
|
+
# Copyright:: Copyright (c) 2012 Opscode, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'chef/knife'
|
20
|
+
|
21
|
+
class Chef
|
22
|
+
class Knife
|
23
|
+
class UserList < Knife
|
24
|
+
|
25
|
+
deps do
|
26
|
+
require 'chef/user'
|
27
|
+
require 'chef/json_compat'
|
28
|
+
end
|
29
|
+
|
30
|
+
banner "knife user list (options)"
|
31
|
+
|
32
|
+
option :with_uri,
|
33
|
+
:short => "-w",
|
34
|
+
:long => "--with-uri",
|
35
|
+
:description => "Show corresponding URIs"
|
36
|
+
|
37
|
+
def run
|
38
|
+
output(format_list_for_display(Chef::User.list))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Steven Danna (<steve@opscode.com>)
|
3
|
+
# Copyright:: Copyright (c) 2012 Opscode, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'chef/knife'
|
20
|
+
|
21
|
+
class Chef
|
22
|
+
class Knife
|
23
|
+
class UserReregister < Knife
|
24
|
+
|
25
|
+
deps do
|
26
|
+
require 'chef/user'
|
27
|
+
require 'chef/json_compat'
|
28
|
+
end
|
29
|
+
|
30
|
+
banner "knife user reregister USER (options)"
|
31
|
+
|
32
|
+
option :file,
|
33
|
+
:short => "-f FILE",
|
34
|
+
:long => "--file FILE",
|
35
|
+
:description => "Write the private key to a file"
|
36
|
+
|
37
|
+
def run
|
38
|
+
@user_name = @name_args[0]
|
39
|
+
|
40
|
+
if @user_name.nil?
|
41
|
+
show_usage
|
42
|
+
ui.fatal("You must specify a user name")
|
43
|
+
exit 1
|
44
|
+
end
|
45
|
+
|
46
|
+
user = Chef::User.load(@user_name).reregister
|
47
|
+
Chef::Log.debug("Updated user data: #{user.inspect}")
|
48
|
+
key = user.private_key
|
49
|
+
if config[:file]
|
50
|
+
File.open(config[:file], "w") do |f|
|
51
|
+
f.print(key)
|
52
|
+
end
|
53
|
+
else
|
54
|
+
ui.msg key
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Steven Danna (<steve@opscode.com>)
|
3
|
+
# Copyright:: Copyright (c) 2009 Opscode, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'chef/knife'
|
20
|
+
|
21
|
+
class Chef
|
22
|
+
class Knife
|
23
|
+
class UserShow < Knife
|
24
|
+
|
25
|
+
deps do
|
26
|
+
require 'chef/user'
|
27
|
+
require 'chef/json_compat'
|
28
|
+
end
|
29
|
+
|
30
|
+
banner "knife user show USER (options)"
|
31
|
+
|
32
|
+
option :attribute,
|
33
|
+
:short => "-a ATTR",
|
34
|
+
:long => "--attribute ATTR",
|
35
|
+
:description => "Show only one attribute"
|
36
|
+
|
37
|
+
def run
|
38
|
+
@user_name = @name_args[0]
|
39
|
+
|
40
|
+
if @user_name.nil?
|
41
|
+
show_usage
|
42
|
+
ui.fatal("You must specify a user name")
|
43
|
+
exit 1
|
44
|
+
end
|
45
|
+
|
46
|
+
user = Chef::User.load(@user_name)
|
47
|
+
output(format_for_display(user))
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/chef/user.rb
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Steven Danna (steve@opscode.com)
|
3
|
+
# Copyright:: Copyright 2012 Opscode, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
require 'chef/config'
|
19
|
+
require 'chef/mixin/params_validate'
|
20
|
+
require 'chef/mixin/from_file'
|
21
|
+
require 'chef/mash'
|
22
|
+
require 'chef/json_compat'
|
23
|
+
require 'chef/search/query'
|
24
|
+
|
25
|
+
class Chef
|
26
|
+
class User
|
27
|
+
|
28
|
+
include Chef::Mixin::FromFile
|
29
|
+
include Chef::Mixin::ParamsValidate
|
30
|
+
|
31
|
+
def initialize
|
32
|
+
@name = ''
|
33
|
+
@public_key = nil
|
34
|
+
@private_key = nil
|
35
|
+
@password = nil
|
36
|
+
@admin = false
|
37
|
+
end
|
38
|
+
|
39
|
+
def name(arg=nil)
|
40
|
+
set_or_return(:name, arg,
|
41
|
+
:regex => /^[a-z0-9\-_]+$/)
|
42
|
+
end
|
43
|
+
|
44
|
+
def admin(arg=nil)
|
45
|
+
set_or_return(:admin,
|
46
|
+
arg, :kind_of => [TrueClass, FalseClass])
|
47
|
+
end
|
48
|
+
|
49
|
+
def public_key(arg=nil)
|
50
|
+
set_or_return(:public_key,
|
51
|
+
arg, :kind_of => String)
|
52
|
+
end
|
53
|
+
|
54
|
+
def private_key(arg=nil)
|
55
|
+
set_or_return(:private_key,
|
56
|
+
arg, :kind_of => String)
|
57
|
+
end
|
58
|
+
|
59
|
+
def password(arg=nil)
|
60
|
+
set_or_return(:password,
|
61
|
+
arg, :kind_of => String)
|
62
|
+
end
|
63
|
+
|
64
|
+
def to_hash
|
65
|
+
result = {
|
66
|
+
"name" => @name,
|
67
|
+
"public_key" => @public_key,
|
68
|
+
"admin" => @admin
|
69
|
+
}
|
70
|
+
result["private_key"] = @private_key if @private_key
|
71
|
+
result["password"] = @password if @password
|
72
|
+
result
|
73
|
+
end
|
74
|
+
|
75
|
+
def to_json(*a)
|
76
|
+
to_hash.to_json(*a)
|
77
|
+
end
|
78
|
+
|
79
|
+
def destroy
|
80
|
+
Chef::REST.new(Chef::Config[:chef_server_url]).delete_rest("users/#{@name}")
|
81
|
+
end
|
82
|
+
|
83
|
+
def create
|
84
|
+
payload = {:name => self.name, :admin => self.admin, :password => self.password }
|
85
|
+
payload[:public_key] = public_key if public_key
|
86
|
+
new_user =Chef::REST.new(Chef::Config[:chef_server_url]).post_rest("users", payload)
|
87
|
+
Chef::User.from_hash(self.to_hash.merge(new_user))
|
88
|
+
end
|
89
|
+
|
90
|
+
def update(new_key=false)
|
91
|
+
payload = {:name => name, :admin => admin}
|
92
|
+
payload[:private_key] = new_key if new_key
|
93
|
+
payload[:password] = password if password
|
94
|
+
updated_user = Chef::REST.new(Chef::Config[:chef_server_url]).put_rest("users/#{name}", payload)
|
95
|
+
Chef::User.from_hash(self.to_hash.merge(updated_user))
|
96
|
+
end
|
97
|
+
|
98
|
+
def save(new_key=false)
|
99
|
+
begin
|
100
|
+
create
|
101
|
+
rescue Net::HTTPServerException => e
|
102
|
+
if e.response.code == "409"
|
103
|
+
update(new_key)
|
104
|
+
else
|
105
|
+
raise e
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def reregister
|
111
|
+
r = Chef::REST.new(Chef::Config[:chef_server_url])
|
112
|
+
reregistered_self = r.put_rest("users/#{name}", { :name => name, :admin => admin, :private_key => true })
|
113
|
+
private_key(reregistered_self["private_key"])
|
114
|
+
self
|
115
|
+
end
|
116
|
+
|
117
|
+
def to_s
|
118
|
+
"user[#{@name}]"
|
119
|
+
end
|
120
|
+
|
121
|
+
def inspect
|
122
|
+
"Chef::User name:'#{name}' admin:'#{admin.inspect}'" +
|
123
|
+
"public_key:'#{public_key}' private_key:#{private_key}"
|
124
|
+
end
|
125
|
+
|
126
|
+
# Class Methods
|
127
|
+
|
128
|
+
def self.from_hash(user_hash)
|
129
|
+
user = Chef::User.new
|
130
|
+
user.name user_hash['name']
|
131
|
+
user.private_key user_hash['private_key'] if user_hash.key?('private_key')
|
132
|
+
user.password user_hash['password'] if user_hash.key?('password')
|
133
|
+
user.public_key user_hash['public_key']
|
134
|
+
user.admin user_hash['admin']
|
135
|
+
user
|
136
|
+
end
|
137
|
+
|
138
|
+
def self.from_json(json)
|
139
|
+
Chef::User.from_hash(Chef::JSONCompat.from_json(json))
|
140
|
+
end
|
141
|
+
|
142
|
+
class << self
|
143
|
+
alias_method :json_create, :from_json
|
144
|
+
end
|
145
|
+
|
146
|
+
def self.list(inflate=false)
|
147
|
+
response = if inflate
|
148
|
+
users = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest('users')
|
149
|
+
users.map do |name|
|
150
|
+
Chef::User.load(name)
|
151
|
+
end
|
152
|
+
else
|
153
|
+
Chef::REST.new(Chef::Config[:chef_server_url]).get_rest('users')
|
154
|
+
end
|
155
|
+
if response.is_a? Array
|
156
|
+
transform_ohc_list_response(response)
|
157
|
+
else
|
158
|
+
response
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def self.load(name)
|
163
|
+
response = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("users/#{name}")
|
164
|
+
Chef::User.from_hash(response)
|
165
|
+
end
|
166
|
+
|
167
|
+
private
|
168
|
+
|
169
|
+
# Gross. Transforms an API response in the form of:
|
170
|
+
# [ { "user" => { "username" => USERNAME }}, ...]
|
171
|
+
# into the form
|
172
|
+
# { "USERNAME" => "URI" }
|
173
|
+
def self.transform_ohc_list_response(response)
|
174
|
+
new_response = Hash.new
|
175
|
+
response.each do |u|
|
176
|
+
name = u['user']['username']
|
177
|
+
new_response[name] = Chef::Config[:chef_server_url] + "/users/#{name}"
|
178
|
+
end
|
179
|
+
new_response
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|