@@ -259,6 +259,13 @@ def __init__(self):
259259 self ._session = Session ()
260260 self ._settings = Settings ()
261261
262+ def _dispatch (self , method , path , body = None ,
263+ headers = {'content-type' : 'application/json' }, ** kwargs ):
264+ func = getattr (self ._session , method .lower ())
265+ url = urlparse .urljoin (self ._settings ['controller' ], path , ** kwargs )
266+ response = func (url , data = body , headers = headers )
267+ return response
268+
262269 def auth_register (self , args ):
263270 """Register a user at a controller."""
264271 controller = args ['<controller>' ]
@@ -271,7 +278,7 @@ def auth_register(self, args):
271278 email = args .get ('--email' )
272279 if not email :
273280 email = raw_input ('email: ' )
274- url = controller + ' api/register'
281+ url = urlparse . urljoin ( controller , '/ api/register')
275282 payload = {'username' : username , 'password' : password , 'email' : email }
276283 response = self ._session .post (url , data = payload , allow_redirects = False )
277284 if response .status_code == requests .codes .created : # @UndefinedVariable
@@ -285,6 +292,7 @@ def auth_register(self, args):
285292 print ('Login failed' )
286293 return
287294 # add ssh keys before formations are created
295+ print
288296 self .keys_add ({})
289297 else :
290298 print ('Registration failed' , response .content )
@@ -300,7 +308,7 @@ def auth_login(self, args):
300308 password = args .get ('--password' )
301309 if not password :
302310 password = getpass ('password: ' )
303- url = controller + ' api/auth/login/'
311+ url = urlparse . urljoin ( controller , '/ api/auth/login/')
304312 payload = {'username' : username , 'password' : password }
305313 # prime cookies for login
306314 self ._session .get (url , headers = headers )
@@ -321,7 +329,7 @@ def auth_logout(self, args):
321329 """Clear a user's session and unauthenticate her."""
322330 controller = self ._settings .get ('controller' )
323331 if controller :
324- self ._session . get ( controller + ' api/auth/logout/' )
332+ self ._dispatch ( 'get' , '/ api/auth/logout/' )
325333 self ._session .cookies .clear ()
326334 self ._session .cookies .save ()
327335 self ._settings ['controller' ] = None
@@ -332,9 +340,7 @@ def backends_list(self, args):
332340 formation = args .get ('--formation' )
333341 if not formation :
334342 formation = self ._session .formation
335- response = self ._session .get (
336- '{}api/formations/{}/backends' .format (
337- self ._settings ['controller' ], formation ))
343+ response = self ._dispatch ('get' ,'/formations/{}/backends' .format (formation ))
338344 if response .status_code == requests .codes .ok : # @UndefinedVariable
339345 print ('=== {0}' .format (formation ))
340346 data = response .json ()
@@ -350,10 +356,9 @@ def builds_create(self, args):
350356 data = sys .stdin .read ()
351357 # url / sha / slug_size / procfile / checksum
352358 j = json .loads (data )
353- response = self ._session .post (
354- '{}api/formations/{}/builds' .format (
355- self ._settings ['controller' ], formation ),
356- data = json .dumps (j ), headers = {'content-type' : 'application/json' })
359+ response = self ._dispatch ('post' ,
360+ '/formations/{}/builds' .format (formation ),
361+ body = json .dumps (j ))
357362 if response .status_code == requests .codes .created : # @UndefinedVariable
358363 print ('Build created.' )
359364 else :
@@ -363,8 +368,7 @@ def builds_list(self, args):
363368 formation = args .get ('--formation' )
364369 if not formation :
365370 formation = self ._session .formation
366- response = self ._session .get ('{}api/formations/{}/builds' .format (
367- self ._settings ['controller' ], formation ))
371+ response = self ._dispatch ('get' , '/formations/{}/builds' .format (formation ))
368372 if response .status_code == requests .codes .ok : # @UndefinedVariable
369373 print ('=== {0}' .format (formation ))
370374 data = response .json ()
@@ -378,9 +382,7 @@ def config_list(self, args):
378382 formation = args .get ('--formation' )
379383 if not formation :
380384 formation = self ._session .formation
381- response = self ._session .get (
382- '{}api/formations/{}/config' .format (
383- self ._settings ['controller' ], formation ))
385+ response = self ._dispatch ('get' , '/formations/{}/config' .format (formation ))
384386 if response .status_code == requests .codes .ok : # @UndefinedVariable
385387 config = response .json ()
386388 values = json .loads (config ['values' ])
@@ -400,11 +402,9 @@ def config_set(self, args):
400402 if not formation :
401403 formation = self ._session .formation
402404 body = {'values' : json .dumps (dictify (args ['<var>=<value>' ]))}
403- response = self ._session .post (
404- '{}api/formations/{}/config' .format (
405- self ._settings ['controller' ], formation ),
406- data = json .dumps (body ),
407- headers = {'content-type' : 'application/json' })
405+ response = self ._dispatch ('post' ,
406+ '/formations/{}/config' .format (formation ),
407+ json .dumps (body ))
408408 if response .status_code == requests .codes .created : # @UndefinedVariable
409409 config = response .json ()
410410 values = json .loads (config ['values' ])
@@ -427,11 +427,9 @@ def config_unset(self, args):
427427 for k in args .get ('<key>' ):
428428 values [k ] = None
429429 body = {'values' : json .dumps (values )}
430- response = self ._session .post (
431- '{}api/formations/{}/config' .format (
432- self ._settings ['controller' ], formation ),
433- data = json .dumps (body ),
434- headers = {'content-type' : 'application/json' })
430+ response = self ._dispatch ('post' ,
431+ '/formations/{}/config' .format (formation ),
432+ data = json .dumps (body ))
435433 if response .status_code == requests .codes .created : # @UndefinedVariable
436434 config = response .json ()
437435 values = json .loads (config ['values' ])
@@ -450,8 +448,8 @@ def containers_list(self, args):
450448 formation = args .get ('--formation' )
451449 if not formation :
452450 formation = self ._session .formation
453- response = self ._session . get ( '{}api/formations/{}/containers' . format (
454- self . _settings [ 'controller' ], formation ))
451+ response = self ._dispatch ( 'get' ,
452+ '/formations/{}/containers' . format ( formation ))
455453 if response .status_code == requests .codes .ok : # @UndefinedVariable
456454 print ('=== {0}' .format (formation ))
457455 data = response .json ()
@@ -487,18 +485,15 @@ def keys_add(self, args):
487485 body = {'id' : key_id , 'public' : '{0} {1}' .format (key_type , key_str )}
488486 sys .stdout .write ('Uploading {0} to Deis... ' .format (path ))
489487 sys .stdout .flush ()
490- response = self ._session .post ('{}api/keys' .format (
491- self ._settings ['controller' ]), json .dumps (body ),
492- headers = {'content-type' : 'application/json' })
488+ response = self ._dispatch ('post' , '/api/keys' , json .dumps (body ))
493489 if response .status_code == requests .codes .created : # @UndefinedVariable
494490 print ('done' )
495491 else :
496492 print ('Error!' , response .text )
497493
498494 def keys_list (self , args ):
499495 """List SSH keys for the logged in user."""
500- response = self ._session .get ('{}api/keys' .format (
501- self ._settings ['controller' ]))
496+ response = self ._dispatch ('get' , '/api/keys' )
502497 if response .status_code == requests .codes .ok : # @UndefinedVariable
503498 data = response .json ()
504499 if data ['count' ] == 0 :
@@ -517,8 +512,7 @@ def keys_remove(self, args):
517512 key = args .get ('<key>' )
518513 sys .stdout .write ('Removing {0} SSH Key... ' .format (key ))
519514 sys .stdout .flush ()
520- response = self ._session .delete ('{}api/keys/{}' .format (
521- self ._settings ['controller' ], key ))
515+ response = self ._dispatch ('delete' , '/keys/{}' .format (key ))
522516 if response .status_code == requests .codes .no_content : # @UndefinedVariable
523517 print ('done' )
524518 else :
@@ -533,20 +527,16 @@ def flavors_create(self, args):
533527 opt = args .get ('--' + fld )
534528 if opt :
535529 body .update ({fld : opt })
536- controller = self ._settings ['controller' ]
537- response = self ._session .post (
538- controller + 'api/flavors/' , data = body )
530+ response = self ._dispatch ('post' , '/api/flavors' , json .dumps (body ))
539531 if response .status_code == requests .codes .created : # @UndefinedVariable
540532 print ('{0[id]}' .format (response .json ()))
541533 else :
542534 print ('Error!' , response .text )
543535
544536 def flavors_delete (self , args ):
545537 """Delete a node flavor by ID."""
546- node = args .get ('<id>' )
547- response = self ._session .delete (
548- '{}api/flavors/{}' .format (
549- self ._settings ['controller' ], node ))
538+ flavor = args .get ('<id>' )
539+ response = self ._dispatch ('delete' , '/api/flavors/{}' .format (flavor ))
550540 if response .status_code == requests .codes .no_content : # @UndefinedVariable
551541 pass
552542 else :
@@ -555,18 +545,15 @@ def flavors_delete(self, args):
555545 def flavors_info (self , args ):
556546 """Show detail of a node flavor."""
557547 flavor = args .get ('<flavor>' )
558- response = self ._session .get (
559- '{}api/flavors/{}' .format (
560- self ._settings ['controller' ], flavor ))
548+ response = self ._dispatch ('get' , '/api/flavors/{}' .format (flavor ))
561549 if response .status_code == requests .codes .ok : # @UndefinedVariable
562550 print (json .dumps (response .json (), indent = 2 ))
563551 else :
564552 print ('Error!' , response .text )
565553
566554 def flavors_list (self , args ):
567555 """List flavors for a user."""
568- response = self ._session .get (
569- self ._settings ['controller' ] + 'api/flavors/' )
556+ response = self ._dispatch ('get' , '/api/flavors' )
570557 if response .status_code == requests .codes .ok : # @UndefinedVariable
571558 data = response .json ()
572559 for item in data ['results' ]:
@@ -579,14 +566,13 @@ def formations_create(self, args):
579566 body = {'image' : args ['--image' ], 'flavor' : args ['--flavor' ]}
580567 if '--id' in args :
581568 body .update ({'id' : args ['--id' ]})
582- controller = self ._settings ['controller' ]
583- response = self ._session .post (
584- controller + 'api/formations/' , data = body )
569+ response = self ._dispatch ('post' , '/api/formations' ,
570+ json .dumps (body ))
585571 if response .status_code == requests .codes .created : # @UndefinedVariable
586572 data = response .json ()
587573 formation = data ['id' ]
588574 print ('Created ' + formation )
589- hostname = urlparse .urlparse (controller ).netloc
575+ hostname = urlparse .urlparse (self . _settings [ ' controller' ] ).netloc
590576 try :
591577 subprocess .check_call (
592578 ['git' , 'remote' , 'add' , '-f' , 'deis' ,
@@ -599,8 +585,7 @@ def formations_create(self, args):
599585
600586 def formations_list (self , args ):
601587 """List formations for a user."""
602- response = self ._session .get (
603- self ._settings ['controller' ] + 'api/formations/' )
588+ response = self ._dispatch ('get' , '/api/formations' )
604589 if response .status_code == requests .codes .ok : # @UndefinedVariable
605590 data = response .json ()
606591 for item in data ['results' ]:
@@ -626,9 +611,7 @@ def formations_destroy(self, args):
626611 print ('Destroy aborted' )
627612 return
628613 print 'Destroying {0}...' .format (formation ),
629- response = self ._session .delete (
630- '{}api/formations/{}' .format (
631- self ._settings ['controller' ], formation ))
614+ response = self ._dispatch ('delete' , '/api/formations/{}' .format (formation ))
632615 if response .status_code in (requests .codes .no_content , requests .codes .not_found ): # @UndefinedVariable
633616 print ('done' )
634617 try :
@@ -649,11 +632,9 @@ def formations_scale(self, args):
649632 for type_num in args .get ('<type=num>' ):
650633 typ , count = type_num .split ('=' )
651634 body .update ({typ : int (count )})
652- response = self ._session .post (
653- '{}api/formations/{}/scale' .format (
654- self ._settings ['controller' ], formation ),
655- data = json .dumps (body ),
656- headers = {'content-type' : 'application/json' })
635+ response = self ._dispatch ('post' ,
636+ '/api/formations/{}/scale' .format (formation ),
637+ json .dumps (body ))
657638 if response .status_code == requests .codes .ok : # @UndefinedVariable
658639 databag = json .loads (response .content )
659640 print (json .dumps (databag , indent = 2 ))
@@ -664,9 +645,8 @@ def formations_calculate(self, args):
664645 formation = args .get ('--formation' )
665646 if not formation :
666647 formation = self ._session .formation
667- response = self ._session .post (
668- '{}api/formations/{}/calculate' .format (
669- self ._settings ['controller' ], formation ))
648+ response = self ._dispatch ('post' ,
649+ '/api/formations/{}/calculate' .format (formation ))
670650 if response .status_code == requests .codes .ok : # @UndefinedVariable
671651 databag = json .loads (response .content )
672652 print (json .dumps (databag , indent = 2 ))
@@ -677,9 +657,8 @@ def formations_balance(self, args):
677657 formation = args .get ('--formation' )
678658 if not formation :
679659 formation = self ._session .formation
680- response = self ._session .post (
681- '{}api/formations/{}/balance' .format (
682- self ._settings ['controller' ], formation ))
660+ response = self ._dispatch ('post' ,
661+ '/api/formations/{}/balance' .format (formation ))
683662 if response .status_code == requests .codes .ok : # @UndefinedVariable
684663 databag = json .loads (response .content )
685664 print (json .dumps (databag , indent = 2 ))
@@ -690,9 +669,8 @@ def formations_converge(self, args):
690669 formation = args .get ('--formation' )
691670 if not formation :
692671 formation = self ._session .formation
693- response = self ._session .post (
694- '{}api/formations/{}/converge' .format (
695- self ._settings ['controller' ], formation ))
672+ response = self ._dispatch ('post' ,
673+ '/api/formations/{}/converge' .format (formation ))
696674 if response .status_code == requests .codes .ok : # @UndefinedVariable
697675 databag = json .loads (response .content )
698676 print (json .dumps (databag , indent = 2 ))
@@ -702,9 +680,7 @@ def formations_converge(self, args):
702680 def nodes_info (self , args ):
703681 """Show detail of a provider."""
704682 node = args .get ('<node>' )
705- response = self ._session .get (
706- '{}api/nodes/{}' .format (
707- self ._settings ['controller' ], node ))
683+ response = self ._dispatch ('get' , '/api/nodes/{}' .format (node ))
708684 if response .status_code == requests .codes .ok : # @UndefinedVariable
709685 print (json .dumps (response .json (), indent = 2 ))
710686 else :
@@ -715,9 +691,8 @@ def nodes_list(self, args):
715691 formation = args .get ('--formation' )
716692 if not formation :
717693 formation = self ._session .formation
718- response = self ._session .get (
719- '{}api/formations/{}/nodes' .format (
720- self ._settings ['controller' ], formation ))
694+ response = self ._dispatch ('get' ,
695+ '/api/formations/{}/nodes' .format (formation ))
721696 if response .status_code == requests .codes .ok : # @UndefinedVariable
722697 print ('=== {0}' .format (formation ))
723698 data = response .json ()
@@ -730,8 +705,8 @@ def nodes_list(self, args):
730705 def nodes_delete (self , args ):
731706 """Delete a node by ID."""
732707 node = args .get ('--id' )
733- response = self ._session . delete (
734- '{} api/nodes/{}' .format (self . _settings [ 'controller' ], node ))
708+ response = self ._dispatch ( 'delete' ,
709+ '/ api/nodes/{}' .format (node ))
735710 if response .status_code == requests .codes .no_content : # @UndefinedVariable
736711 print ('Node deleted successfully' )
737712 else :
@@ -752,19 +727,16 @@ def providers_create(self, args):
752727 if not id :
753728 id = type # @ReservedAssignment
754729 body = {'id' : id , 'type' : type , 'creds' : json .dumps (creds )}
755- response = self ._session .post (
756- '{}api/providers' .format (self ._settings ['controller' ]),
757- data = json .dumps (body ),
758- headers = {'content-type' : 'application/json' })
730+ response = self ._dispatch ('post' , '/api/providers' ,
731+ json .dumps (body ))
759732 if response .status_code == requests .codes .created : # @UndefinedVariable
760733 print ('{0[id]}' .format (response .json ()))
761734 else :
762735 print ('Error!' , response .text )
763736
764737 def providers_list (self , args ):
765738 """List providers."""
766- response = self ._session .get (
767- '{}api/providers' .format (self ._settings ['controller' ]))
739+ response = self ._dispatch ('get' , '/api/providers' )
768740 if response .status_code == requests .codes .ok : # @UndefinedVariable
769741 data = response .json ()
770742 for item in data ['results' ]:
@@ -775,9 +747,7 @@ def providers_list(self, args):
775747 def providers_info (self , args ):
776748 """Show detail of a provider."""
777749 provider = args .get ('<provider>' )
778- response = self ._session .get (
779- '{}api/providers/{}' .format (
780- self ._settings ['controller' ], provider ))
750+ response = self ._dispatch ('get' , '/api/providers/{}' .format (provider ))
781751 if response .status_code == requests .codes .ok : # @UndefinedVariable
782752 print (json .dumps (response .json (), indent = 2 ))
783753 else :
@@ -788,9 +758,7 @@ def proxies_list(self, args):
788758 formation = args .get ('--formation' )
789759 if not formation :
790760 formation = self ._session .formation
791- response = self ._session .get (
792- '{}api/formations/{}/proxies' .format (
793- self ._settings ['controller' ], formation ))
761+ response = self ._dispatch ('get' , '/api/formations/{}/proxies' .format (formation ))
794762 if response .status_code == requests .codes .ok : # @UndefinedVariable
795763 print ('=== {0}' .format (formation ))
796764 data = response .json ()
0 commit comments