Class: Chef::UserV1
Constant Summary
collapse
- SUPPORTED_API_VERSIONS =
[0, 1].freeze
Instance Attribute Summary
#source_file
Class Method Summary
collapse
Instance Method Summary
collapse
#reregister_only_v0_supported_error_msg, #server_client_api_version_intersection
#lazy, #set_or_return, #validate
#class_from_file, #from_file
Constructor Details
#initialize ⇒ UserV1
Returns a new instance of UserV1.
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/chef/user_v1.rb', line 44
def initialize
@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
end
|
Class Method Details
.from_hash(user_hash) ⇒ Object
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
# File 'lib/chef/user_v1.rb', line 261
def self.from_hash(user_hash)
user = Chef::UserV1.new
user.username user_hash["username"]
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
|
.from_json(json) ⇒ Object
.list(inflate = false) ⇒ Object
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
# File 'lib/chef/user_v1.rb', line 280
def self.list(inflate = false)
response = Chef::ServerAPI.new(Chef::Config[:chef_server_url]).get("users")
users = if response.is_a?(Array)
transform_list_response(response)
else
response end
if inflate
users.inject({}) do |user_map, (name, _url)|
user_map[name] = Chef::UserV1.load(name)
user_map
end
else
users
end
end
|
Instance Method Details
#chef_root_rest_v0 ⇒ Object
57
58
59
|
# File 'lib/chef/user_v1.rb', line 57
def chef_root_rest_v0
@chef_root_rest_v0 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_root], { api_version: "0" })
end
|
#chef_root_rest_v1 ⇒ Object
61
62
63
|
# File 'lib/chef/user_v1.rb', line 61
def chef_root_rest_v1
@chef_root_rest_v1 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_root], { api_version: "1" })
end
|
#create ⇒ Object
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/chef/user_v1.rb', line 142
def create
begin
payload = {
username: @username,
display_name: @display_name,
email: @email,
}
payload[:first_name] = @first_name unless @first_name.nil?
payload[:last_name] = @last_name unless @last_name.nil?
payload[:password] = @password unless @password.nil?
payload[:public_key] = @public_key unless @public_key.nil?
payload[:create_key] = @create_key unless @create_key.nil?
payload[:middle_name] = @middle_name unless @middle_name.nil?
raise Chef::Exceptions::InvalidUserAttribute, "You cannot set both public_key and create_key for create." if !@create_key.nil? && !@public_key.nil?
new_user = chef_root_rest_v1.post("users", payload)
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::HTTPClientException => e
supported_versions = server_client_api_version_intersection(e, SUPPORTED_API_VERSIONS)
raise e unless supported_versions && supported_versions.include?(0)
payload = {
username: @username,
display_name: @display_name,
first_name: @first_name,
last_name: @last_name,
email: @email,
password: @password,
}
payload[:middle_name] = @middle_name unless @middle_name.nil?
payload[:public_key] = @public_key unless @public_key.nil?
new_user = chef_root_rest_v0.post("users", payload)
end
Chef::UserV1.from_hash(to_h.merge(new_user))
end
|
#create_key(arg = nil) ⇒ Object
95
96
97
98
|
# File 'lib/chef/user_v1.rb', line 95
def create_key(arg = nil)
set_or_return(:create_key, arg,
kind_of: [TrueClass, FalseClass])
end
|
#display_name(arg = nil) ⇒ Object
70
71
72
73
|
# File 'lib/chef/user_v1.rb', line 70
def display_name(arg = nil)
set_or_return(:display_name,
arg, kind_of: String)
end
|
#email(arg = nil) ⇒ Object
90
91
92
93
|
# File 'lib/chef/user_v1.rb', line 90
def email(arg = nil)
set_or_return(:email,
arg, kind_of: String)
end
|
#first_name(arg = nil) ⇒ Object
75
76
77
78
|
# File 'lib/chef/user_v1.rb', line 75
def first_name(arg = nil)
set_or_return(:first_name,
arg, kind_of: String)
end
|
#last_name(arg = nil) ⇒ Object
85
86
87
88
|
# File 'lib/chef/user_v1.rb', line 85
def last_name(arg = nil)
set_or_return(:last_name,
arg, kind_of: String)
end
|
#middle_name(arg = nil) ⇒ Object
80
81
82
83
|
# File 'lib/chef/user_v1.rb', line 80
def middle_name(arg = nil)
set_or_return(:middle_name,
arg, kind_of: String)
end
|
#password(arg = nil) ⇒ Object
110
111
112
113
|
# File 'lib/chef/user_v1.rb', line 110
def password(arg = nil)
set_or_return(:password,
arg, kind_of: String)
end
|
#private_key(arg = nil) ⇒ Object
105
106
107
108
|
# File 'lib/chef/user_v1.rb', line 105
def private_key(arg = nil)
set_or_return(:private_key,
arg, kind_of: String)
end
|
#public_key(arg = nil) ⇒ Object
100
101
102
103
|
# File 'lib/chef/user_v1.rb', line 100
def public_key(arg = nil)
set_or_return(:public_key,
arg, kind_of: String)
end
|
#reregister ⇒ Object
Note: remove after API v0 no longer supported by client (and knife command).
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
# File 'lib/chef/user_v1.rb', line 234
def reregister
begin
payload = to_h.merge({ "private_key" => true })
reregistered_self = chef_root_rest_v0.put("users/#{username}", payload)
private_key(reregistered_self["private_key"])
rescue Net::HTTPClientException => e
if e.response.code == "406" && e.response["x-ops-server-api-version"]
= Chef::JSONCompat.from_json(e.response["x-ops-server-api-version"])
min_version = ["min_version"]
max_version = ["max_version"]
error_msg = reregister_only_v0_supported_error_msg(max_version, min_version)
raise Chef::Exceptions::OnlyApiVersion0SupportedForAction.new(error_msg)
else
raise e
end
end
self
end
|
#save(new_key = false) ⇒ Object
223
224
225
226
227
228
229
230
231
|
# File 'lib/chef/user_v1.rb', line 223
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/chef/user_v1.rb', line 115
def to_h
result = {
"username" => @username,
}
result["display_name"] = @display_name unless @display_name.nil?
result["first_name"] = @first_name unless @first_name.nil?
result["middle_name"] = @middle_name unless @middle_name.nil?
result["last_name"] = @last_name unless @last_name.nil?
result["email"] = @email unless @email.nil?
result["password"] = @password unless @password.nil?
result["public_key"] = @public_key unless @public_key.nil?
result["private_key"] = @private_key unless @private_key.nil?
result["create_key"] = @create_key unless @create_key.nil?
result
end
|
#to_json(*a) ⇒ Object
133
134
135
|
# File 'lib/chef/user_v1.rb', line 133
def to_json(*a)
Chef::JSONCompat.to_json(to_h, *a)
end
|
#to_s ⇒ Object
256
257
258
|
# File 'lib/chef/user_v1.rb', line 256
def to_s
"user[#{@username}]"
end
|
#update(new_key = false) ⇒ Object
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
# File 'lib/chef/user_v1.rb', line 190
def update(new_key = false)
begin
payload = { username: username }
payload[:display_name] = display_name unless display_name.nil?
payload[:first_name] = first_name unless first_name.nil?
payload[:middle_name] = middle_name unless middle_name.nil?
payload[:last_name] = last_name unless last_name.nil?
payload[:email] = email unless email.nil?
payload[:password] = password unless password.nil?
payload[:public_key] = public_key unless public_key.nil?
payload[:private_key] = new_key if new_key
updated_user = chef_root_rest_v1.put("users/#{username}", payload)
rescue Net::HTTPClientException => e
if e.response.code == "400"
error = Chef::JSONCompat.from_json(e.response.body)["error"].first
error_match = /Since Server API v1, all keys must be updated via the keys endpoint/.match(error)
if error_match.nil?
raise e
end
else supported_versions = server_client_api_version_intersection(e, SUPPORTED_API_VERSIONS)
raise e unless supported_versions && supported_versions.include?(0)
end
updated_user = chef_root_rest_v0.put("users/#{username}", payload)
end
Chef::UserV1.from_hash(to_h.merge(updated_user))
end
|
#username(arg = nil) ⇒ Object
65
66
67
68
|
# File 'lib/chef/user_v1.rb', line 65
def username(arg = nil)
set_or_return(:username, arg,
regex: /^[a-z0-9\-_]+$/)
end
|