class Chef::ApiClient

Public Class Methods

from_hash(o) click to toggle source
# File lib/chef/api_client.rb, line 135
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) click to toggle source
# File lib/chef/api_client.rb, line 145
def self.from_json(j)
  from_hash(Chef::JSONCompat.parse(j))
end
http_api() click to toggle source
# File lib/chef/api_client.rb, line 149
def self.http_api
  Chef::ServerAPI.new(Chef::Config[:chef_server_url], { api_version: "0" })
end
list(inflate = false) click to toggle source
# File lib/chef/api_client.rb, line 158
def self.list(inflate = false)
  if inflate
    response = {}
    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) click to toggle source

Load a client by name via the API

# File lib/chef/api_client.rb, line 172
def self.load(name)
  response = http_api.get("clients/#{name}")
  if response.is_a?(Chef::ApiClient)
    response
  else
    from_hash(response)
  end
end
new() click to toggle source

Create a new Chef::ApiClient object.

# File lib/chef/api_client.rb, line 40
def initialize
  @name = ""
  @public_key = nil
  @private_key = nil
  @admin = false
  @validator = false
end
reregister(name) click to toggle source
# File lib/chef/api_client.rb, line 153
def self.reregister(name)
  api_client = load(name)
  api_client.reregister
end

Public Instance Methods

admin(arg = nil) click to toggle source

Gets or sets whether this client is an admin.

@param [Optional True/False] Should be true or false - default is false. @return [True/False] The current value

# File lib/chef/api_client.rb, line 64
def admin(arg = nil)
  set_or_return(
    :admin,
    arg,
    kind_of: [ TrueClass, FalseClass ]
  )
end
create() click to toggle source

Create the client via the REST API

# File lib/chef/api_client.rb, line 209
def create
  http_api.post("clients", self)
end
destroy() click to toggle source

Remove this client via the REST API

# File lib/chef/api_client.rb, line 182
def destroy
  http_api.delete("clients/#{@name}")
end
http_api() click to toggle source
# File lib/chef/api_client.rb, line 223
def http_api
  @http_api ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url], { api_version: "0" })
end
inspect() click to toggle source
# File lib/chef/api_client.rb, line 218
def inspect
  "Chef::ApiClient name:'#{name}' admin:'#{admin.inspect}' validator:'#{validator}' " +
    "public_key:'#{public_key}' private_key:'#{private_key}'"
end
name(arg = nil) click to toggle source

Gets or sets the client name.

@param [Optional String] The name must be alpha-numeric plus - and _. @return [String] The current value of the name.

# File lib/chef/api_client.rb, line 52
def name(arg = nil)
  set_or_return(
    :name,
    arg,
    regex: /^[\-[:alnum:]_\.]+$/
  )
end
private_key(arg = nil) click to toggle source

Gets or sets the private key.

@param [Optional String] The string representation of the private key. @return [String] The current value.

# 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) click to toggle source

Gets or sets the public key.

@param [Optional String] The string representation of the public key. @return [String] The current value.

# File lib/chef/api_client.rb, line 76
def public_key(arg = nil)
  set_or_return(
    :public_key,
    arg,
    kind_of: String
  )
end
reregister() click to toggle source
# File lib/chef/api_client.rb, line 198
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
save() click to toggle source

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

# File lib/chef/api_client.rb, line 187
def save
  http_api.put("clients/#{name}", { name: name, admin: admin, validator: validator })
rescue Net::HTTPClientException => 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_h() click to toggle source

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

@return [Hash]

# File lib/chef/api_client.rb, line 113
def to_h
  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
Also aliased as: to_hash
to_hash()
Alias for: to_h
to_json(*a) click to toggle source

The JSON representation of the object.

@return [String] the JSON string.

# File lib/chef/api_client.rb, line 131
def to_json(*a)
  Chef::JSONCompat.to_json(to_h, *a)
end
to_s() click to toggle source

As a string

# File lib/chef/api_client.rb, line 214
def to_s
  "client[#{@name}]"
end
validator(arg = nil) click to toggle source

Gets or sets whether this client is a validator.

@param [Boolean] arg whether or not the client is a validator. If

`nil`, retrieves the already-set value.

@return [Boolean] The current value

# File lib/chef/api_client.rb, line 89
def validator(arg = nil)
  set_or_return(
    :validator,
    arg,
    kind_of: [TrueClass, FalseClass]
  )
end