Class: Chef::User

Inherits:
Object
  • Object
show all
Includes:
Mixin::FromFile, Mixin::ParamsValidate
Defined in:
lib/chef/user.rb

Instance Attribute Summary

Attributes included from Mixin::FromFile

#source_file

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ParamsValidate

#lazy, #set_or_return, #validate

Methods included from Mixin::FromFile

#class_from_file, #from_file

Constructor Details

#initializeUser

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

Class Methods



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



153
154
155
# File 'lib/chef/user.rb', line 153

def self.from_json(json)
  Chef::User.from_hash(Chef::JSONCompat.from_json(json))
end

.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) # 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

.load(name) ⇒ Object



174
175
176
177
# File 'lib/chef/user.rb', line 174

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

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_v0Object



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

#createObject



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

#destroyObject



97
98
99
# File 'lib/chef/user.rb', line 97

def destroy
  chef_rest_v0.delete("users/#{@name}")
end

#inspectObject



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

#reregisterObject



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_hObject 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_json(*a) ⇒ Object



93
94
95
# File 'lib/chef/user.rb', line 93

def to_json(*a)
  Chef::JSONCompat.to_json(to_h, *a)
end

#to_sObject



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