Class: Chef::ApiClient

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

Defined Under Namespace

Classes: Registration

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

#initializeApiClient

Create a new Chef::ApiClient object.


40
41
42
43
44
45
46
# File 'lib/chef/api_client.rb', line 40

def initialize
  @name = ""
  @public_key = nil
  @private_key = nil
  @admin = false
  @validator = false
end

Class Method Details

.from_hash(o) ⇒ Object


133
134
135
136
137
138
139
140
141
# File 'lib/chef/api_client.rb', line 133

def self.from_hash(o)
  client = Chef::ApiClient.new
  client.name(o["name"] || o["clientname"])
  client.private_key(o["private_key"]) if o.key?("private_key")
  client.public_key(o["public_key"])
  client.admin(o["admin"])
  client.validator(o["validator"])
  client
end

.from_json(j) ⇒ Object


143
144
145
# File 'lib/chef/api_client.rb', line 143

def self.from_json(j)
  from_hash(Chef::JSONCompat.parse(j))
end

.http_apiObject


147
148
149
# File 'lib/chef/api_client.rb', line 147

def self.http_api
  Chef::ServerAPI.new(Chef::Config[:chef_server_url], { :api_version => "0" })
end

.list(inflate = false) ⇒ Object


156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/chef/api_client.rb', line 156

def self.list(inflate = false)
  if inflate
    response = Hash.new
    Chef::Search::Query.new.search(:client) do |n|
      n = json_create(n) if n.instance_of?(Hash)
      response[n.name] = n
    end
    response
  else
    http_api.get("clients")
  end
end

.load(name) ⇒ Object

Load a client by name via the API


170
171
172
173
174
175
176
177
# File 'lib/chef/api_client.rb', line 170

def self.load(name)
  response = http_api.get("clients/#{name}")
  if response.kind_of?(Chef::ApiClient)
    response
  else
    from_hash(response)
  end
end

.reregister(name) ⇒ Object


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

def self.reregister(name)
  api_client = load(name)
  api_client.reregister
end

Instance Method Details

#admin(arg = nil) ⇒ True/False

Gets or sets whether this client is an admin.

Returns:

  • (True/False)

    The current value


64
65
66
67
68
69
70
# File 'lib/chef/api_client.rb', line 64

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

#createObject

Create the client via the REST API


207
208
209
# File 'lib/chef/api_client.rb', line 207

def create
  http_api.post("clients", self)
end

#destroyObject

Remove this client via the REST API


180
181
182
# File 'lib/chef/api_client.rb', line 180

def destroy
  http_api.delete("clients/#{@name}")
end

#http_apiObject


221
222
223
# File 'lib/chef/api_client.rb', line 221

def http_api
  @http_api ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url], { :api_version => "0" })
end

#inspectObject


216
217
218
219
# File 'lib/chef/api_client.rb', line 216

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

#name(arg = nil) ⇒ String

Gets or sets the client name.

Returns:

  • (String)

    The current value of the name.


52
53
54
55
56
57
58
# File 'lib/chef/api_client.rb', line 52

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

#private_key(arg = nil) ⇒ String

Gets or sets the private key.

Returns:

  • (String)

    The current value.


101
102
103
104
105
106
107
# File 'lib/chef/api_client.rb', line 101

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

#public_key(arg = nil) ⇒ String

Gets or sets the public key.

Returns:

  • (String)

    The current value.


76
77
78
79
80
81
82
# File 'lib/chef/api_client.rb', line 76

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

#reregisterObject


196
197
198
199
200
201
202
203
204
# File 'lib/chef/api_client.rb', line 196

def reregister
  reregistered_self = http_api.put("clients/#{name}", { :name => name, :admin => admin, :validator => validator, :private_key => true })
  if reregistered_self.respond_to?(:[])
    private_key(reregistered_self["private_key"])
  else
    private_key(reregistered_self.private_key)
  end
  self
end

#saveObject

Save this client via the REST API, returns a hash including the private key


185
186
187
188
189
190
191
192
193
194
# File 'lib/chef/api_client.rb', line 185

def save
  http_api.put("clients/#{name}", { :name => name, :admin => admin, :validator => validator })
rescue Net::HTTPServerException => e
  # If that fails, go ahead and try and update it
  if e.response.code == "404"
    http_api.post("clients", { :name => name, :admin => admin, :validator => validator })
  else
    raise e
  end
end

#to_hashHash

The hash representation of the object. Includes the name and public_key. Private key is included if available.

Returns:

  • (Hash)

113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/chef/api_client.rb', line 113

def to_hash
  result = {
    "name" => @name,
    "public_key" => @public_key,
    "validator" => @validator,
    "admin" => @admin,
    "json_class" => self.class.name,
    "chef_type" => "client",
  }
  result["private_key"] = @private_key if @private_key
  result
end

#to_json(*a) ⇒ String

The JSON representation of the object.

Returns:

  • (String)

    the JSON string.


129
130
131
# File 'lib/chef/api_client.rb', line 129

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

#to_sObject

As a string


212
213
214
# File 'lib/chef/api_client.rb', line 212

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

#validator(arg = nil) ⇒ Boolean

Gets or sets whether this client is a validator.

Returns:

  • (Boolean)

    The current value


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

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