Class: Chef::User

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

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


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


141
142
143
144
145
146
147
148
149
# File 'lib/chef/user.rb', line 141

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


151
152
153
# File 'lib/chef/user.rb', line 151

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

.list(inflate = false) ⇒ Object


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/chef/user.rb', line 155

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


172
173
174
175
# File 'lib/chef/user.rb', line 172

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


99
100
101
102
103
104
# File 'lib/chef/user.rb', line 99

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_hash.merge(new_user))
end

#destroyObject


95
96
97
# File 'lib/chef/user.rb', line 95

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

#inspectObject


134
135
136
137
# File 'lib/chef/user.rb', line 134

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


124
125
126
127
128
# File 'lib/chef/user.rb', line 124

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


114
115
116
117
118
119
120
121
122
# File 'lib/chef/user.rb', line 114

def save(new_key = false)
  create
rescue Net::HTTPServerException => e
  if e.response.code == "409"
    update(new_key)
  else
    raise e
  end
end

#to_hashObject


80
81
82
83
84
85
86
87
88
89
# File 'lib/chef/user.rb', line 80

def to_hash
  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


91
92
93
# File 'lib/chef/user.rb', line 91

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

#to_sObject


130
131
132
# File 'lib/chef/user.rb', line 130

def to_s
  "user[#{@name}]"
end

#update(new_key = false) ⇒ Object


106
107
108
109
110
111
112
# File 'lib/chef/user.rb', line 106

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_hash.merge(updated_user))
end