Class: Chef::ApiClient
- 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
-
#couchdb_id ⇒ Object
Returns the value of attribute couchdb_id.
-
#couchdb_rev ⇒ Object
Returns the value of attribute couchdb_rev.
Class Method Summary collapse
-
.cdb_list(inflate = false) ⇒ Object
List all the Chef::ApiClient objects in the CouchDB.
-
.cdb_load(name) ⇒ Chef::ApiClient
Load a client by name from CouchDB.
-
.create_design_document ⇒ Object
Set up our CouchDB design document.
- .json_create(o) ⇒ Object
- .list(inflate = false) ⇒ Object
-
.load(name) ⇒ Object
Load a client by name via the API.
Instance Method Summary collapse
-
#admin(arg = nil) ⇒ True/False
Gets or sets whether this client is an admin.
-
#cdb_destroy ⇒ Chef::ApiClient
Remove this client from the CouchDB.
-
#cdb_save ⇒ Object
Save this client to the CouchDB.
-
#create ⇒ Object
Create the client via the REST API.
-
#create_keys ⇒ True
Creates a new public/private key pair, and populates the public_key and private_key attributes.
-
#destroy ⇒ Object
Remove this client via the REST API.
-
#initialize ⇒ ApiClient
constructor
Create a new Chef::ApiClient object.
-
#name(arg = nil) ⇒ String
Gets or sets the client name.
-
#private_key(arg = nil) ⇒ String
Gets or sets the private key.
-
#public_key(arg = nil) ⇒ String
Gets or sets the public key.
-
#save(new_key = false, validation = false) ⇒ Object
Save this client via the REST API, returns a hash including the private key.
-
#to_hash ⇒ Hash
The hash representation of the object.
-
#to_json(*a) ⇒ String
The JSON representation of the object.
-
#to_s ⇒ Object
As a string.
Methods included from Mixin::ParamsValidate
Methods included from Mixin::FromFile
Constructor Details
Instance Attribute Details
#couchdb_id ⇒ Object
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_rev ⇒ Object
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
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_document ⇒ Object
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.
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_destroy ⇒ Chef::ApiClient
Remove this client from the CouchDB
214 215 216 |
# File 'lib/chef/api_client.rb', line 214 def cdb_destroy @couchdb.delete("client", @name, @couchdb_rev) end |
#cdb_save ⇒ Object
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 |
#create ⇒ Object
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_keys ⇒ True
Creates a new public/private key pair, and populates the public_key and private_key attributes.
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 |
#destroy ⇒ Object
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.
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.
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.
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_hash ⇒ Hash
The hash representation of the object. Includes the name and public_key, but never the private key.
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.
149 150 151 |
# File 'lib/chef/api_client.rb', line 149 def to_json(*a) to_hash.to_json(*a) end |
#to_s ⇒ Object
As a string
263 264 265 |
# File 'lib/chef/api_client.rb', line 263 def to_s "client[#{@name}]" end |