Class: Chef::User
Instance Attribute Summary
#source_file
Class Method Summary
collapse
Instance Method Summary
collapse
#lazy, #set_or_return, #validate
#class_from_file, #from_file
Constructor Details
#initialize ⇒ User
Returns a new instance of User.
43
44
45
46
47
48
49
|
# File 'lib/chef/user.rb', line 43
def initialize
@name = ""
@public_key = nil
@private_key = nil
@password = nil
@admin = false
end
|
Class Method Details
.from_hash(user_hash) ⇒ Object
143
144
145
146
147
148
149
150
151
|
# File 'lib/chef/user.rb', line 143
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
|
.from_json(json) ⇒ Object
.list(inflate = false) ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/chef/user.rb', line 157
def self.list(inflate = false)
response = Chef::ServerAPI.new(Chef::Config[:chef_server_url], { api_version: "0" }).get("users")
users = if response.is_a?(Array)
transform_ohc_list_response(response) else
response end
if inflate
users.inject({}) do |user_map, (name, _url)|
user_map[name] = Chef::User.load(name)
user_map
end
else
users
end
end
|
Instance Method Details
#admin(arg = nil) ⇒ Object
60
61
62
63
|
# File 'lib/chef/user.rb', line 60
def admin(arg = nil)
set_or_return(:admin,
arg, kind_of: [TrueClass, FalseClass])
end
|
#chef_rest_v0 ⇒ Object
51
52
53
|
# File 'lib/chef/user.rb', line 51
def chef_rest_v0
@chef_rest_v0 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url], { api_version: "0" })
end
|
#create ⇒ Object
101
102
103
104
105
106
|
# File 'lib/chef/user.rb', line 101
def create
payload = { name: name, admin: admin, password: password }
payload[:public_key] = public_key if public_key
new_user = chef_rest_v0.post("users", payload)
Chef::User.from_hash(to_h.merge(new_user))
end
|
#destroy ⇒ Object
97
98
99
|
# File 'lib/chef/user.rb', line 97
def destroy
chef_rest_v0.delete("users/#{@name}")
end
|
#inspect ⇒ Object
136
137
138
139
|
# File 'lib/chef/user.rb', line 136
def inspect
"Chef::User name:'#{name}' admin:'#{admin.inspect}'" +
"public_key:'#{public_key}' private_key:#{private_key}"
end
|
#name(arg = nil) ⇒ Object
55
56
57
58
|
# File 'lib/chef/user.rb', line 55
def name(arg = nil)
set_or_return(:name, arg,
regex: /^[a-z0-9\-_]+$/)
end
|
#password(arg = nil) ⇒ Object
75
76
77
78
|
# File 'lib/chef/user.rb', line 75
def password(arg = nil)
set_or_return(:password,
arg, kind_of: String)
end
|
#private_key(arg = nil) ⇒ Object
70
71
72
73
|
# File 'lib/chef/user.rb', line 70
def private_key(arg = nil)
set_or_return(:private_key,
arg, kind_of: String)
end
|
#public_key(arg = nil) ⇒ Object
65
66
67
68
|
# File 'lib/chef/user.rb', line 65
def public_key(arg = nil)
set_or_return(:public_key,
arg, kind_of: String)
end
|
#reregister ⇒ Object
126
127
128
129
130
|
# File 'lib/chef/user.rb', line 126
def reregister
reregistered_self = chef_rest_v0.put("users/#{name}", { name: name, admin: admin, private_key: true })
private_key(reregistered_self["private_key"])
self
end
|
#save(new_key = false) ⇒ Object
116
117
118
119
120
121
122
123
124
|
# File 'lib/chef/user.rb', line 116
def save(new_key = false)
create
rescue Net::HTTPClientException => e
if e.response.code == "409"
update(new_key)
else
raise e
end
end
|
#to_h ⇒ Object
Also known as:
to_hash
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/chef/user.rb', line 80
def to_h
result = {
"name" => @name,
"public_key" => @public_key,
"admin" => @admin,
}
result["private_key"] = @private_key if @private_key
result["password"] = @password if @password
result
end
|
#to_s ⇒ Object
132
133
134
|
# File 'lib/chef/user.rb', line 132
def to_s
"user[#{@name}]"
end
|
#update(new_key = false) ⇒ Object
108
109
110
111
112
113
114
|
# File 'lib/chef/user.rb', line 108
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_v0.put("users/#{name}", payload)
Chef::User.from_hash(to_h.merge(updated_user))
end
|