Class: Chef::ApiClient

Inherits:
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
    }
  }
}
INDEX_OBJECT_TYPE =
'client'.freeze

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.



72
73
74
75
76
77
78
79
80
# 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

Instance Attribute Details

#couchdbObject

Returns the value of attribute couchdb.



69
70
71
# File 'lib/chef/api_client.rb', line 69

def couchdb
  @couchdb
end

#couchdb_idObject

Returns the value of attribute couchdb_id.



69
70
71
# File 'lib/chef/api_client.rb', line 69

def couchdb_id
  @couchdb_id
end

#couchdb_revObject

Returns the value of attribute couchdb_rev.



69
70
71
# File 'lib/chef/api_client.rb', line 69

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.



177
178
179
180
181
# File 'lib/chef/api_client.rb', line 177

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:



200
201
202
# File 'lib/chef/api_client.rb', line 200

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



260
261
262
# File 'lib/chef/api_client.rb', line 260

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

.index_object_typeObject



65
66
67
# File 'lib/chef/api_client.rb', line 65

def self.index_object_type
  INDEX_OBJECT_TYPE
end

.json_create(o) ⇒ Object



164
165
166
167
168
169
170
171
172
173
# File 'lib/chef/api_client.rb', line 164

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



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

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



205
206
207
208
209
210
211
212
213
214
# File 'lib/chef/api_client.rb', line 205

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



98
99
100
101
102
103
104
# File 'lib/chef/api_client.rb', line 98

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

#cdb_destroyChef::ApiClient

Remove this client from the CouchDB

Returns:



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

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

#cdb_saveObject

Save this client to the CouchDB



230
231
232
# File 'lib/chef/api_client.rb', line 230

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

#createObject

Create the client via the REST API



255
256
257
# File 'lib/chef/api_client.rb', line 255

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)


134
135
136
137
138
139
# 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

#destroyObject

Remove this client via the REST API



225
226
227
# File 'lib/chef/api_client.rb', line 225

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.



86
87
88
89
90
91
92
# File 'lib/chef/api_client.rb', line 86

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.



122
123
124
125
126
127
128
# 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) ⇒ String

Gets or sets the public key.

Returns:

  • (String)

    The current value.



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

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



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/chef/api_client.rb', line 235

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)


145
146
147
148
149
150
151
152
153
154
155
# 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) ⇒ String

The JSON representation of the object.

Returns:

  • (String)

    the JSON string.



160
161
162
# File 'lib/chef/api_client.rb', line 160

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

#to_sObject

As a string



265
266
267
# File 'lib/chef/api_client.rb', line 265

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