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.
42
43
44
45
46
47
48
|
# File 'lib/chef/user.rb', line 42
def initialize
@name = ""
@public_key = nil
@private_key = nil
@password = nil
@admin = false
end
|
Class Method Details
.from_hash(user_hash) ⇒ Object
142
143
144
145
146
147
148
149
150
|
# File 'lib/chef/user.rb', line 142
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/chef/user.rb', line 156
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
59
60
61
62
|
# File 'lib/chef/user.rb', line 59
def admin(arg = nil)
set_or_return(:admin,
arg, kind_of: [TrueClass, FalseClass])
end
|
#chef_rest_v0 ⇒ Object
50
51
52
|
# File 'lib/chef/user.rb', line 50
def chef_rest_v0
@chef_rest_v0 ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url], { api_version: "0" })
end
|
#create ⇒ Object
100
101
102
103
104
105
|
# File 'lib/chef/user.rb', line 100
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
96
97
98
|
# File 'lib/chef/user.rb', line 96
def destroy
chef_rest_v0.delete("users/#{@name}")
end
|
#inspect ⇒ Object
135
136
137
138
|
# File 'lib/chef/user.rb', line 135
def inspect
"Chef::User name:'#{name}' admin:'#{admin.inspect}'" +
"public_key:'#{public_key}' private_key:#{private_key}"
end
|
#name(arg = nil) ⇒ Object
54
55
56
57
|
# File 'lib/chef/user.rb', line 54
def name(arg = nil)
set_or_return(:name, arg,
regex: /^[a-z0-9\-_]+$/)
end
|
#password(arg = nil) ⇒ Object
74
75
76
77
|
# File 'lib/chef/user.rb', line 74
def password(arg = nil)
set_or_return(:password,
arg, kind_of: String)
end
|
#private_key(arg = nil) ⇒ Object
69
70
71
72
|
# File 'lib/chef/user.rb', line 69
def private_key(arg = nil)
set_or_return(:private_key,
arg, kind_of: String)
end
|
#public_key(arg = nil) ⇒ Object
64
65
66
67
|
# File 'lib/chef/user.rb', line 64
def public_key(arg = nil)
set_or_return(:public_key,
arg, kind_of: String)
end
|
#reregister ⇒ Object
125
126
127
128
129
|
# File 'lib/chef/user.rb', line 125
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
115
116
117
118
119
120
121
122
123
|
# File 'lib/chef/user.rb', line 115
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
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/chef/user.rb', line 79
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
131
132
133
|
# File 'lib/chef/user.rb', line 131
def to_s
"user[#{@name}]"
end
|
#update(new_key = false) ⇒ Object
107
108
109
110
111
112
113
|
# File 'lib/chef/user.rb', line 107
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
|