Class: Chef::ApiClient
- Includes:
- Mixin::FromFile, Mixin::ParamsValidate
- Defined in:
- lib/chef/api_client.rb,
lib/chef/api_client/registration.rb
Defined Under Namespace
Classes: Registration
Class Method Summary collapse
- .http_api ⇒ Object
- .json_create(o) ⇒ Object
- .list(inflate = false) ⇒ Object
-
.load(name) ⇒ Object
Load a client by name via the API.
- .reregister(name) ⇒ Object
Instance Method Summary collapse
-
#admin(arg = nil) ⇒ True/False
Gets or sets whether this client is an admin.
-
#create ⇒ Object
Create the client via the REST API.
-
#destroy ⇒ Object
Remove this client via the REST API.
- #http_api ⇒ Object
-
#initialize ⇒ ApiClient
constructor
Create a new Chef::ApiClient object.
- #inspect ⇒ 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.
- #reregister ⇒ Object
-
#save ⇒ 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.
-
#validator(arg = nil) ⇒ Boolean
Gets or sets whether this client is a validator.
Methods included from Mixin::ParamsValidate
#lazy, #set_or_return, #validate
Methods included from Mixin::FromFile
Constructor Details
#initialize ⇒ ApiClient
Create a new Chef::ApiClient object.
34 35 36 37 38 39 40 |
# File 'lib/chef/api_client.rb', line 34 def initialize @name = '' @public_key = nil @private_key = nil @admin = false @validator = false end |
Class Method Details
.http_api ⇒ Object
137 138 139 |
# File 'lib/chef/api_client.rb', line 137 def self.http_api Chef::REST.new(Chef::Config[:chef_server_url]) end |
.json_create(o) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/chef/api_client.rb', line 127 def self.json_create(o) client = Chef::ApiClient.new client.name(o["name"] || o["clientname"]) client.private_key(o["private_key"]) if o.key?("private_key") client.public_key(o["public_key"]) client.admin(o["admin"]) client.validator(o["validator"]) client end |
.list(inflate = false) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/chef/api_client.rb', line 146 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 http_api.get("clients") end end |
.load(name) ⇒ Object
Load a client by name via the API
160 161 162 163 164 165 166 167 |
# File 'lib/chef/api_client.rb', line 160 def self.load(name) response = http_api.get("clients/#{name}") if response.kind_of?(Chef::ApiClient) response else json_create(response) end end |
.reregister(name) ⇒ Object
141 142 143 144 |
# File 'lib/chef/api_client.rb', line 141 def self.reregister(name) api_client = load(name) api_client.reregister end |
Instance Method Details
#admin(arg = nil) ⇒ True/False
Gets or sets whether this client is an admin.
58 59 60 61 62 63 64 |
# File 'lib/chef/api_client.rb', line 58 def admin(arg=nil) set_or_return( :admin, arg, :kind_of => [ TrueClass, FalseClass ] ) end |
#create ⇒ Object
Create the client via the REST API
199 200 201 |
# File 'lib/chef/api_client.rb', line 199 def create http_api.post("clients", self) end |
#destroy ⇒ Object
Remove this client via the REST API
170 171 172 |
# File 'lib/chef/api_client.rb', line 170 def destroy http_api.delete("clients/#{@name}") end |
#http_api ⇒ Object
213 214 215 |
# File 'lib/chef/api_client.rb', line 213 def http_api @http_api ||= Chef::REST.new(Chef::Config[:chef_server_url]) end |
#inspect ⇒ Object
208 209 210 211 |
# File 'lib/chef/api_client.rb', line 208 def inspect "Chef::ApiClient name:'#{name}' admin:'#{admin.inspect}' validator:'#{validator}' " + "public_key:'#{public_key}' private_key:'#{private_key}'" end |
#name(arg = nil) ⇒ String
Gets or sets the client name.
46 47 48 49 50 51 52 |
# File 'lib/chef/api_client.rb', line 46 def name(arg=nil) set_or_return( :name, arg, :regex => /^[\-[:alnum:]_\.]+$/ ) end |
#private_key(arg = nil) ⇒ String
Gets or sets the private key.
95 96 97 98 99 100 101 |
# File 'lib/chef/api_client.rb', line 95 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.
70 71 72 73 74 75 76 |
# File 'lib/chef/api_client.rb', line 70 def public_key(arg=nil) set_or_return( :public_key, arg, :kind_of => String ) end |
#reregister ⇒ Object
188 189 190 191 192 193 194 195 196 |
# File 'lib/chef/api_client.rb', line 188 def reregister reregistered_self = http_api.put("clients/#{name}", { :name => name, :admin => admin, :validator => validator, :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 ⇒ Object
Save this client via the REST API, returns a hash including the private key
175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/chef/api_client.rb', line 175 def save begin http_api.put("clients/#{name}", { :name => self.name, :admin => self.admin, :validator => self.validator}) rescue Net::HTTPServerException => e # If that fails, go ahead and try and update it if e.response.code == "404" http_api.post("clients", {:name => self.name, :admin => self.admin, :validator => self.validator }) else raise e end end end |
#to_hash ⇒ Hash
The hash representation of the object. Includes the name and public_key. Private key is included if available.
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/chef/api_client.rb', line 107 def to_hash result = { "name" => @name, "public_key" => @public_key, "validator" => @validator, "admin" => @admin, 'json_class' => self.class.name, "chef_type" => "client" } result["private_key"] = @private_key if @private_key result end |
#to_json(*a) ⇒ String
The JSON representation of the object.
123 124 125 |
# File 'lib/chef/api_client.rb', line 123 def to_json(*a) Chef::JSONCompat.to_json(to_hash, *a) end |
#to_s ⇒ Object
As a string
204 205 206 |
# File 'lib/chef/api_client.rb', line 204 def to_s "client[#{@name}]" end |
#validator(arg = nil) ⇒ Boolean
Gets or sets whether this client is a validator.
83 84 85 86 87 88 89 |
# File 'lib/chef/api_client.rb', line 83 def validator(arg=nil) set_or_return( :validator, arg, :kind_of => [TrueClass, FalseClass] ) end |