class Chef::User
Public Class Methods
Source
# 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
Class Methods
Source
# File lib/chef/user.rb, line 152 def self.from_json(json) Chef::User.from_hash(Chef::JSONCompat.from_json(json)) end
Source
# 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) # OHC/OPC else response # OSC end if inflate users.inject({}) do |user_map, (name, _url)| user_map[name] = Chef::User.load(name) user_map end else users end end
Source
# File lib/chef/user.rb, line 173 def self.load(name) response = Chef::ServerAPI.new(Chef::Config[:chef_server_url], { api_version: "0" }).get("users/#{name}") Chef::User.from_hash(response) end
Source
# File lib/chef/user.rb, line 42 def initialize @name = "" @public_key = nil @private_key = nil @password = nil @admin = false end
Private Class Methods
Source
# File lib/chef/user.rb, line 182 def self.transform_ohc_list_response(response) new_response = {} response.each do |u| name = u["user"]["username"] new_response[name] = Chef::Config[:chef_server_url] + "/users/#{name}" end new_response end
Gross. Transforms an API response in the form of:
- { “user” => { “username” => USERNAME }}, …
-
into the form { “USERNAME” => “URI” }
Public Instance Methods
Source
# File lib/chef/user.rb, line 59 def admin(arg = nil) set_or_return(:admin, arg, kind_of: [TrueClass, FalseClass]) end
Source
# 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
Source
# 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
Source
# File lib/chef/user.rb, line 96 def destroy chef_rest_v0.delete("users/#{@name}") end
Source
# 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
Source
# File lib/chef/user.rb, line 54 def name(arg = nil) set_or_return(:name, arg, regex: /^[a-z0-9\-_]+$/) end
Source
# File lib/chef/user.rb, line 74 def password(arg = nil) set_or_return(:password, arg, kind_of: String) end
Source
# File lib/chef/user.rb, line 69 def private_key(arg = nil) set_or_return(:private_key, arg, kind_of: String) end
Source
# File lib/chef/user.rb, line 64 def public_key(arg = nil) set_or_return(:public_key, arg, kind_of: String) end
Source
# 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
Source
# 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
Source
# 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
Also aliased as: to_hash
Source
# File lib/chef/user.rb, line 92 def to_json(*a) Chef::JSONCompat.to_json(to_h, *a) end
Source
# 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