@@ -70,6 +70,14 @@ def initialize(couchdb=nil)
7070 @admin = false
7171 @couchdb = ( couchdb || Chef ::CouchDB . new )
7272 end
73+
74+ def chef_server_rest
75+ self . class . chef_server_rest
76+ end
77+
78+ def self . chef_server_rest
79+ Chef ::REST . new ( Chef ::Config [ :chef_server_url ] )
80+ end
7381
7482 # Gets or sets the client name.
7583 #
@@ -181,7 +189,7 @@ def self.list(inflate=false)
181189 end
182190 response
183191 else
184- Chef :: REST . new ( Chef :: Config [ :chef_server_url ] ) . get_rest ( "clients" )
192+ chef_server_rest . get_rest ( "clients" )
185193 end
186194 end
187195
@@ -195,7 +203,7 @@ def self.cdb_load(name, couchdb=nil)
195203
196204 # Load a client by name via the API
197205 def self . load ( name )
198- response = Chef :: REST . new ( Chef :: Config [ :chef_server_url ] ) . get_rest ( "clients/#{ name } " )
206+ response = chef_server_rest . get_rest ( "clients/#{ name } " )
199207 if response . kind_of? ( Chef ::ApiClient )
200208 response
201209 else
@@ -215,7 +223,7 @@ def cdb_destroy
215223
216224 # Remove this client via the REST API
217225 def destroy
218- Chef :: REST . new ( Chef :: Config [ :chef_server_url ] ) . delete_rest ( "clients/#{ @name } " )
226+ chef_server_rest . delete_rest ( "clients/#{ @name } " )
219227 end
220228
221229 # Save this client to the CouchDB
@@ -228,14 +236,14 @@ def save(new_key=false, validation=false)
228236 if validation
229237 r = Chef ::REST . new ( Chef ::Config [ :chef_server_url ] , Chef ::Config [ :validation_client_name ] , Chef ::Config [ :validation_key ] )
230238 else
231- r = Chef :: REST . new ( Chef :: Config [ :chef_server_url ] )
239+ r = chef_server_rest
232240 end
233241 # First, try and create a new registration
234242 begin
235243 r . post_rest ( "clients" , { :name => self . name , :admin => self . admin } )
236244 rescue Net ::HTTPServerException => e
237245 # If that fails, go ahead and try and update it
238- if e . response . code == "403 "
246+ if e . response . code == "409 "
239247 r . put_rest ( "clients/#{ name } " , { :name => self . name , :admin => self . admin , :private_key => new_key } )
240248 else
241249 raise e
@@ -245,7 +253,7 @@ def save(new_key=false, validation=false)
245253
246254 # Create the client via the REST API
247255 def create
248- Chef :: REST . new ( Chef :: Config [ :chef_server_url ] ) . post_rest ( "clients" , self )
256+ chef_server_rest . post_rest ( "clients" , self )
249257 end
250258
251259 # Set up our CouchDB design document
0 commit comments