Class: Chef::User

Inherits:
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

#set_or_return, #validate

Methods included from Mixin::FromFile

#class_from_file, #from_file

Constructor Details

#initializeUser

Returns a new instance of User.



31
32
33
34
35
36
37
# File 'lib/chef/user.rb', line 31

def initialize
  @name = ''
  @public_key = nil
  @private_key = nil
  @password = nil
  @admin = false
end

Class Method Details

.from_hash(user_hash) ⇒ Object

Class Methods



128
129
130
131
132
133
134
135
136
# File 'lib/chef/user.rb', line 128

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 Also known as: json_create



138
139
140
# File 'lib/chef/user.rb', line 138

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

.list(inflate = false) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/chef/user.rb', line 146

def self.list(inflate=false)
  response = if inflate
               users = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest('users')
               users.map do |name|
                 Chef::User.load(name)
               end
             else
               Chef::REST.new(Chef::Config[:chef_server_url]).get_rest('users')
             end
  if response.is_a? Array
    transform_ohc_list_response(response)
  else
    response
  end
end

.load(name) ⇒ Object



162
163
164
165
# File 'lib/chef/user.rb', line 162

def self.load(name)
  response = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("users/#{name}")
  Chef::User.from_hash(response)
end

Instance Method Details

#admin(arg = nil) ⇒ Object



44
45
46
47
# File 'lib/chef/user.rb', line 44

def admin(arg=nil)
  set_or_return(:admin,
                arg, :kind_of => [TrueClass, FalseClass])
end

#createObject



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

def create
  payload = {:name => self.name, :admin => self.admin, :password => self.password }
  payload[:public_key] = public_key if public_key
  new_user =Chef::REST.new(Chef::Config[:chef_server_url]).post_rest("users", payload)
  Chef::User.from_hash(self.to_hash.merge(new_user))
end

#destroyObject



79
80
81
# File 'lib/chef/user.rb', line 79

def destroy
  Chef::REST.new(Chef::Config[:chef_server_url]).delete_rest("users/#{@name}")
end

#inspectObject



121
122
123
124
# File 'lib/chef/user.rb', line 121

def inspect
  "Chef::User name:'#{name}' admin:'#{admin.inspect}'" +
  "public_key:'#{public_key}' private_key:#{private_key}"
end

#name(arg = nil) ⇒ Object



39
40
41
42
# File 'lib/chef/user.rb', line 39

def name(arg=nil)
  set_or_return(:name, arg,
                :regex => /^[a-z0-9\-_]+$/)
end

#password(arg = nil) ⇒ Object



59
60
61
62
# File 'lib/chef/user.rb', line 59

def password(arg=nil)
  set_or_return(:password,
                arg, :kind_of => String)
end

#private_key(arg = nil) ⇒ Object



54
55
56
57
# File 'lib/chef/user.rb', line 54

def private_key(arg=nil)
  set_or_return(:private_key,
                arg, :kind_of => String)
end

#public_key(arg = nil) ⇒ Object



49
50
51
52
# File 'lib/chef/user.rb', line 49

def public_key(arg=nil)
  set_or_return(:public_key,
                arg, :kind_of => String)
end

#reregisterObject



110
111
112
113
114
115
# File 'lib/chef/user.rb', line 110

def reregister
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  reregistered_self = r.put_rest("users/#{name}", { :name => name, :admin => admin, :private_key => true })
  private_key(reregistered_self["private_key"])
  self
end

#save(new_key = false) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chef/user.rb', line 98

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

#to_hashObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/chef/user.rb', line 64

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



75
76
77
# File 'lib/chef/user.rb', line 75

def to_json(*a)
  to_hash.to_json(*a)
end

#to_sObject



117
118
119
# File 'lib/chef/user.rb', line 117

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

#update(new_key = false) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/chef/user.rb', line 90

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.new(Chef::Config[:chef_server_url]).put_rest("users/#{name}", payload)
  Chef::User.from_hash(self.to_hash.merge(updated_user))
end