Class: Chef::ApiClient

Inherits:
Object show all
Includes:
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ParamsValidate

#set_or_return, #validate

Methods included from Mixin::FromFile

#class_from_file, #from_file

Constructor Details

#initializeApiClient

Create a new Chef::ApiClient object.



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

def initialize
  @name = '' 
  @public_key = nil
  @private_key = nil
  @couchdb_rev = nil
  @couchdb_id = nil
  @admin = false
  @couchdb = Chef::CouchDB.new 
end

Instance Attribute Details

#couchdb_idObject

Returns the value of attribute couchdb_id.



58
59
60
# File 'lib/chef/api_client.rb', line 58

def couchdb_id
  @couchdb_id
end

#couchdb_revObject

Returns the value of attribute couchdb_rev.



58
59
60
# File 'lib/chef/api_client.rb', line 58

def couchdb_rev
  @couchdb_rev
end

Class Method Details

.cdb_list(inflate = false) ⇒ 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.



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

def self.cdb_list(inflate=false)
  couchdb = Chef::CouchDB.new
  rs = couchdb.list("clients", inflate)
  if inflate
    rs["rows"].collect { |r| r["value"] }
  else
    rs["rows"].collect { |r| r["key"] }
  end
end

.cdb_load(name) ⇒ Chef::ApiClient

Load a client by name from CouchDB

Returns:



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

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

.create_design_documentObject

Set up our CouchDB design document



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

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

.json_create(o) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/chef/api_client.rb', line 153

def self.json_create(o)
  client = Chef::ApiClient.new
  client.name(o["name"])
  client.public_key(o["public_key"])
  client.admin(o["admin"])
  client.couchdb_rev = o["_rev"] if o.has_key?("_rev")
  client.couchdb_id = o["_id"] if o.has_key?("_id")
  client
end

.list(inflate = false) ⇒ Object



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

def self.list(inflate=false)
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  if inflate
    response = Hash.new
    Chef::Search::Query.new.search(:client) do |n|
      response[n.name] = n
    end
    response
  else
    r.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
208
# File 'lib/chef/api_client.rb', line 198

def self.load(name)
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  response = r.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



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

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

#cdb_destroyChef::ApiClient

Remove this client from the CouchDB

Returns:



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

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

#cdb_saveObject

Save this client to the CouchDB



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

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

#createObject

Create the client via the REST API



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

def create
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  r.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)


123
124
125
126
127
128
# File 'lib/chef/api_client.rb', line 123

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



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

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

#name(arg = nil) ⇒ String

Gets or sets the client name.

Returns:

  • (String)

    The current value of the name.



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

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.



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

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.



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

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



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

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 == "403"
      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)


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

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.



149
150
151
# File 'lib/chef/api_client.rb', line 149

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

#to_sObject

As a string



263
264
265
# File 'lib/chef/api_client.rb', line 263

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