Class: Chef::ApiClient

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

Constant Summary collapse

DESIGN_DOCUMENT =
{
  "version" => 1,
  "language" => "javascript",
  "views" => {
    "all" => {
      "map" => <<-EOJS
      function(doc) { 
        if (doc.chef_type == "client") {
          emit(doc.name, doc);
        }
      }
      EOJS
    },
    "all_id" => {
      "map" => <<-EOJS
      function(doc) { 
        if (doc.chef_type == "client") {
          emit(doc.name, doc.name);
        }
      }
      EOJS
    }
  }
}

Instance Attribute Summary collapse

Attributes included from IndexQueue::Indexable

#index_id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IndexQueue::Indexable

#add_to_index, #delete_from_index, included, #index_object_type, #with_indexer_metadata

Methods included from Mixin::ParamsValidate

#set_or_return, #validate

Methods included from Mixin::FromFile

#class_from_file, #from_file

Constructor Details

#initialize(couchdb = nil) ⇒ ApiClient

Create a new Chef::ApiClient object.



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

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

Instance Attribute Details

#couchdbObject

Returns the value of attribute couchdb.



62
63
64
# File 'lib/chef/api_client.rb', line 62

def couchdb
  @couchdb
end

#couchdb_idObject

Returns the value of attribute couchdb_id.



62
63
64
# File 'lib/chef/api_client.rb', line 62

def couchdb_id
  @couchdb_id
end

#couchdb_revObject

Returns the value of attribute couchdb_rev.



62
63
64
# File 'lib/chef/api_client.rb', line 62

def couchdb_rev
  @couchdb_rev
end

Class Method Details

.cdb_list(inflate = false, couchdb = nil) ⇒ Object

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.



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

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) ⇒ Chef::ApiClient

Load a client by name from CouchDB

Returns:



193
194
195
# File 'lib/chef/api_client.rb', line 193

def self.cdb_load(name, couchdb=nil)
  (couchdb || Chef::CouchDB.new).load("client", name)
end

.create_design_document(couchdb = nil) ⇒ Object

Set up our CouchDB design document



253
254
255
# File 'lib/chef/api_client.rb', line 253

def self.create_design_document(couchdb=nil)
  (couchdb ||= Chef::CouchDB.new).create_design_document("clients", DESIGN_DOCUMENT)
end

.json_create(o) ⇒ Object



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

def self.json_create(o)
  client = Chef::ApiClient.new
  client.name(o["name"] || o["clientname"])
  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) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/chef/api_client.rb', line 176

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) ⇒ Object

Load a client by name via the API



198
199
200
201
202
203
204
205
206
207
# File 'lib/chef/api_client.rb', line 198

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

Instance Method Details

#admin(arg = nil) ⇒ True/False

Gets or sets whether this client is an admin.

Returns:

  • (True/False)

    The current value



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

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

#cdb_destroyChef::ApiClient

Remove this client from the CouchDB

Returns:



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

def cdb_destroy
  @couchdb.delete("client", @name, @couchdb_rev)
end

#cdb_saveObject

Save this client to the CouchDB



223
224
225
# File 'lib/chef/api_client.rb', line 223

def cdb_save
  @couchdb_rev = @couchdb.store("client", @name, self)["rev"]
end

#createObject

Create the client via the REST API



248
249
250
# File 'lib/chef/api_client.rb', line 248

def create
  Chef::REST.new(Chef::Config[:chef_server_url]).post_rest("clients", self)
end

#create_keysTrue

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

Returns:

  • (True)


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

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

#destroyObject

Remove this client via the REST API



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

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

#name(arg = nil) ⇒ String

Gets or sets the client name.

Returns:

  • (String)

    The current value of the name.



79
80
81
82
83
84
85
# File 'lib/chef/api_client.rb', line 79

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.



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

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

#public_key(arg = nil) ⇒ String

Gets or sets the public key.

Returns:

  • (String)

    The current value.



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

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

#save(new_key = false, validation = false) ⇒ Object

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



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/chef/api_client.rb', line 228

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_hashHash

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

Returns:

  • (Hash)


138
139
140
141
142
143
144
145
146
147
148
# File 'lib/chef/api_client.rb', line 138

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) ⇒ String

The JSON representation of the object.

Returns:

  • (String)

    the JSON string.



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

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

#to_sObject

As a string



258
259
260
# File 'lib/chef/api_client.rb', line 258

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