Parent

Class/Module Index [+]

Quicksearch

Chef::ApiClient

Attributes

couchdb[RW]
couchdb_id[RW]
couchdb_rev[RW]

Public Class Methods

cdb_list(inflate=false, couchdb=nil) click to toggle source

List all the Chef::ApiClient objects in the CouchDB. If inflate is set to true, you will get the full list of all ApiClients, fully inflated.

# File lib/chef/api_client.rb, line 178
def self.cdb_list(inflate=false, couchdb=nil)
  rs = (couchdb || Chef::CouchDB.new).list("clients", inflate)
  lookup = (inflate ? "value" : "key")
  rs["rows"].collect { |r| r[lookup] }
end
cdb_load(name, couchdb=nil) click to toggle source

Load a client by name from CouchDB

@params [String] The name of the client to load @return [Chef::ApiClient] The resulting Chef::ApiClient object

# File lib/chef/api_client.rb, line 206
def self.cdb_load(name, couchdb=nil)
  (couchdb || Chef::CouchDB.new).load("client", name)
end
create_design_document(couchdb=nil) click to toggle source

Set up our CouchDB design document

# File lib/chef/api_client.rb, line 277
def self.create_design_document(couchdb=nil)
  (couchdb ||= Chef::CouchDB.new).create_design_document("clients", DESIGN_DOCUMENT)
end
index_object_type() click to toggle source
# File lib/chef/api_client.rb, line 65
def self.index_object_type
  INDEX_OBJECT_TYPE
end
json_create(o) click to toggle source
# File lib/chef/api_client.rb, line 164
def self.json_create(o)
  client = Chef::ApiClient.new
  client.name(o["name"] || o["clientname"])
  client.private_key(o["private_key"]) if o["private_key"]
  client.public_key(o["public_key"])
  client.admin(o["admin"])
  client.couchdb_rev = o["_rev"]
  client.couchdb_id = o["_id"]
  client.index_id = client.couchdb_id
  client
end
list(inflate=false) click to toggle source
# File lib/chef/api_client.rb, line 189
def self.list(inflate=false)
  if inflate
    response = Hash.new
    Chef::Search::Query.new.search(:client) do |n|
      n = self.json_create(n) if n.instance_of?(Hash)
      response[n.name] = n
    end
    response
  else
    Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("clients")
  end
end
load(name) click to toggle source

Load a client by name via the API

# File lib/chef/api_client.rb, line 211
def self.load(name)
  response = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("clients/#{name}")
  if response.kind_of?(Chef::ApiClient)
    response
  else
    client = Chef::ApiClient.new
    client.name(response['clientname'])
    client
  end
end
new(couchdb=nil) click to toggle source

Create a new Chef::ApiClient object.

# File lib/chef/api_client.rb, line 72
def initialize(couchdb=nil)
  @name = ''
  @public_key = nil
  @private_key = nil
  @couchdb_rev = nil
  @couchdb_id = nil
  @admin = false
  @couchdb = (couchdb || Chef::CouchDB.new)
end
reregister(name) click to toggle source
# File lib/chef/api_client.rb, line 184
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.

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

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

Remove this client from the CouchDB

@params [String] The name of the client to delete @return [Chef::ApiClient] The last version of the object

# File lib/chef/api_client.rb, line 226
def cdb_destroy
  @couchdb.delete("client", @name, @couchdb_rev)
end
cdb_save() click to toggle source

Save this client to the CouchDB

# File lib/chef/api_client.rb, line 236
def cdb_save
  @couchdb_rev = @couchdb.store("client", @name, self)["rev"]
end
create() click to toggle source

Create the client via the REST API

# File lib/chef/api_client.rb, line 272
def create
  Chef::REST.new(Chef::Config[:chef_server_url]).post_rest("clients", self)
end
create_keys() click to toggle source

Creates a new public/private key pair, and populates the public_key and private_key attributes.

@return [True]

# File lib/chef/api_client.rb, line 134
def create_keys
  results = Chef::Certificate.gen_keypair(self.name)
  self.public_key(results[0].to_s)
  self.private_key(results[1].to_s)
  true
end
destroy() click to toggle source

Remove this client via the REST API

# File lib/chef/api_client.rb, line 231
def destroy
  Chef::REST.new(Chef::Config[:chef_server_url]).delete_rest("clients/#{@name}")
end
inspect() click to toggle source
# File lib/chef/api_client.rb, line 286
def inspect
  "Chef::ApiClient name:'#{name}' admin:'#{admin.inspect}' " +
  "public_key:'#{public_key}' private_key:'#{private_key}'"
end
name(arg=nil) click to toggle source

Gets or sets the client name.

@params [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 86
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.

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

# File lib/chef/api_client.rb, line 122
def private_key(arg=nil)
  set_or_return(
    :private_key,
    arg,
    :kind_of => String
  )
end
public_key(arg=nil) click to toggle source

Gets or sets the public key.

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

# File lib/chef/api_client.rb, line 110
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 260
def reregister
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  reregistered_self = r.put_rest("clients/#{name}", { :name => name, :admin => admin, :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(new_key=false, validation=false) 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 241
def save(new_key=false, validation=false)
  if validation
    r = Chef::REST.new(Chef::Config[:chef_server_url], Chef::Config[:validation_client_name], Chef::Config[:validation_key])
  else
    r = Chef::REST.new(Chef::Config[:chef_server_url])
  end
  # First, try and create a new registration
  begin
    r.post_rest("clients", {:name => self.name, :admin => self.admin })
  rescue Net::HTTPServerException => e
    # If that fails, go ahead and try and update it
    if e.response.code == "409"
      r.put_rest("clients/#{name}", { :name => self.name, :admin => self.admin, :private_key => new_key })
    else
      raise e
    end
  end
end
to_hash() click to toggle source

The hash representation of the object. Includes the name and public_key, but never the private key.

@return [Hash]

# File lib/chef/api_client.rb, line 145
def to_hash
  result = {
    "name" => @name,
    "public_key" => @public_key,
    "admin" => @admin,
    'json_class' => self.class.name,
    "chef_type" => "client"
  }
  result["_rev"] = @couchdb_rev if @couchdb_rev
  result
end
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 160
def to_json(*a)
  to_hash.to_json(*a)
end
to_s() click to toggle source

As a string

# File lib/chef/api_client.rb, line 282
def to_s
  "client[#{@name}]"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.