@@ -138,6 +138,7 @@ def create_node(self, image, name=None, size=None, location=None, ex_group_name=
138138 edit_vm = get_href (vm , "edit" )
139139 headers = {"Accept" : self .NODE_MIME_TYPE }
140140 vm = self .connection .request (edit_vm , headers = headers ).object
141+
141142 return self ._to_node (vm , self )
142143
143144 def destroy_node (self , node ):
@@ -163,6 +164,8 @@ def destroy_node(self, node):
163164 if state in ["ALLOCATED" , "CONFIGURED" , "LOCKED" , "UNKNOWN" ]:
164165 raise LibcloudError ("Invalid Node state" , self )
165166
167+ res = None
168+
166169 if state != "NOT_ALLOCATED" :
167170 # prepare the element that forces the undeploy
168171 vm_task = ET .Element ("virtualmachinetask" )
@@ -183,9 +186,11 @@ def destroy_node(self, node):
183186 )
184187
185188 # pylint: disable=maybe-no-member
186- if state == "NOT_ALLOCATED" or res .async_success ():
189+
190+ if state == "NOT_ALLOCATED" or (res and res .async_success ()):
187191 # pylint: enable=maybe-no-member
188192 self .connection .request (action = node .extra ["uri_id" ], method = "DELETE" )
193+
189194 return True
190195 else :
191196 return False
@@ -231,6 +236,7 @@ def ex_run_node(self, node):
231236 edit_vm = get_href (e_vm , "edit" )
232237 headers = {"Accept" : self .NODE_MIME_TYPE }
233238 e_vm = self .connection .request (edit_vm , headers = headers ).object
239+
234240 return self ._to_node (e_vm , self )
235241
236242 def ex_populate_cache (self ):
@@ -270,14 +276,17 @@ def ex_populate_cache(self):
270276 "/admin/datacenters" , headers = dcs_headers , params = params
271277 ).object
272278 dc_dict = {}
279+
273280 for dc in e_dcs .findall ("datacenter" ):
274281 key = get_href (dc , "self" )
275282 dc_dict [key ] = dc
276283
277284 # Populate locations name cache
278285 self .connection .cache ["locations" ] = {}
286+
279287 for e_vdc in e_vdcs .findall ("virtualDatacenter" ):
280288 loc = get_href (e_vdc , "location" )
289+
281290 if loc is not None :
282291 self .connection .cache ["locations" ][loc ] = get_href (e_vdc , "edit" )
283292
@@ -344,6 +353,8 @@ def ex_destroy_group(self, group):
344353 error = "Can not destroy group because of current state"
345354 raise LibcloudError (error , self )
346355
356+ res = None
357+
347358 if state == "DEPLOYED" :
348359 # prepare the element that forces the undeploy
349360 vm_task = ET .Element ("virtualmachinetask" )
@@ -366,10 +377,12 @@ def ex_destroy_group(self, group):
366377 )
367378
368379 # pylint: disable=maybe-no-member
369- if state == "NOT_DEPLOYED" or res .async_success ():
380+
381+ if state == "NOT_DEPLOYED" or (res and res .async_success ()):
370382 # pylint: enable=maybe-no-member
371383 # The node is no longer deployed. Unregister it.
372384 self .connection .request (action = group .uri , method = "DELETE" )
385+
373386 return True
374387 else :
375388 return False
@@ -384,18 +397,21 @@ def ex_list_groups(self, location=None):
384397 :return: the list of :class:`NodeGroup`
385398 """
386399 groups = []
400+
387401 for vdc in self ._get_locations (location ):
388402 link_vdc = self .connection .cache ["locations" ][vdc ]
389403 hdr_vdc = {"Accept" : self .VDC_MIME_TYPE }
390404 e_vdc = self .connection .request (link_vdc , headers = hdr_vdc ).object
391405 apps_link = get_href (e_vdc , "virtualappliances" )
392406 hdr_vapps = {"Accept" : self .VAPPS_MIME_TYPE }
393407 vapps = self .connection .request (apps_link , headers = hdr_vapps ).object
408+
394409 for vapp in vapps .findall ("virtualAppliance" ):
395410 nodes = []
396411 vms_link = get_href (vapp , "virtualmachines" )
397412 headers = {"Accept" : self .NODES_MIME_TYPE }
398413 vms = self .connection .request (vms_link , headers = headers ).object
414+
399415 for vm in vms .findall ("virtualMachine" ):
400416 nodes .append (self ._to_node (vm , self ))
401417 group = NodeGroup (self , vapp .findtext ("name" ), nodes , get_href (vapp , "edit" ))
@@ -419,9 +435,11 @@ def list_images(self, location=None):
419435 repos = self .connection .request (uri , headers = repos_hdr ).object
420436
421437 images = []
438+
422439 for repo in repos .findall ("datacenterRepository" ):
423440 # filter by location. Skips when the name of the location
424441 # is different from the 'datacenterRepository' element
442+
425443 for vdc in self ._get_locations (location ):
426444 # Check if the virtual datacenter belongs to this repo
427445 link_vdc = self .connection .cache ["locations" ][vdc ]
@@ -439,10 +457,12 @@ def list_images(self, location=None):
439457 templates = self .connection .request (
440458 url_templates , params , headers = headers
441459 ).object
460+
442461 for templ in templates .findall ("virtualMachineTemplate" ):
443462 # Avoid duplicated templates
444463 id_template = templ .findtext ("id" )
445464 ids = [image .id for image in images ]
465+
446466 if id_template not in ids :
447467 images .append (self ._to_nodeimage (templ , self , get_href (repo , "edit" )))
448468
@@ -456,6 +476,7 @@ def list_locations(self):
456476 user
457477 :rtype: ``list`` of :class:`NodeLocation`
458478 """
479+
459480 return list (self .connection .cache ["locations" ].keys ())
460481
461482 def list_nodes (self , location = None ):
@@ -491,6 +512,7 @@ def list_sizes(self, location=None):
491512 :return: The list of sizes
492513 :rtype: ``list`` of :class:`NodeSizes`
493514 """
515+
494516 return [
495517 NodeSize (
496518 id = 1 ,
@@ -543,6 +565,7 @@ def reboot_node(self, node):
543565 reboot_uri = node .extra ["uri_id" ] + "/action/reset"
544566 reboot_hdr = {"Accept" : self .AR_MIME_TYPE }
545567 res = self .connection .async_request (action = reboot_uri , method = "POST" , headers = reboot_hdr )
568+
546569 return res .async_success () # pylint: disable=maybe-no-member
547570
548571 # -------------------------
@@ -579,6 +602,7 @@ def _deploy_remote(self, e_vm):
579602 res = self .connection .async_request (
580603 action = link_deploy , method = "POST" , data = tostring (vm_task ), headers = headers
581604 )
605+
582606 if not res .async_success (): # pylint: disable=maybe-no-member
583607 raise LibcloudError ("Could not run the node" , self )
584608
@@ -589,6 +613,7 @@ def _to_location(self, vdc, dc, driver):
589613 identifier = vdc .findtext ("id" )
590614 name = vdc .findtext ("name" )
591615 country = dc .findtext ("name" )
616+
592617 return NodeLocation (identifier , name , country , driver )
593618
594619 def _to_node (self , vm , driver ):
@@ -610,10 +635,13 @@ def _to_node(self, vm, driver):
610635 public_ips = []
611636 nics_hdr = {"Accept" : self .NICS_MIME_TYPE }
612637 nics_element = self .connection .request (get_href (vm , "nics" ), headers = nics_hdr ).object
638+
613639 for nic in nics_element .findall ("nic" ):
614640 ip = nic .findtext ("ip" )
641+
615642 for link in nic .findall ("link" ):
616643 rel = link .attrib ["rel" ]
644+
617645 if rel == "privatenetwork" :
618646 private_ips .append (ip )
619647 elif rel in ["publicnetwork" , "externalnetwork" , "unmanagednetwork" ]:
@@ -645,12 +673,14 @@ def _to_nodeimage(self, template, driver, repo):
645673 url = get_href (template , "edit" )
646674 hdreqd = template .findtext ("hdRequired" )
647675 extra = {"repo" : repo , "url" : url , "hdrequired" : hdreqd }
676+
648677 return NodeImage (identifier , name , driver , extra )
649678
650679 def _get_locations (self , location = None ):
651680 """
652681 Returns the locations as a generator.
653682 """
683+
654684 if location is not None :
655685 yield location
656686 else :
@@ -660,6 +690,7 @@ def _get_enterprise_id(self):
660690 """
661691 Returns the identifier of the logged user's enterprise.
662692 """
693+
663694 return self .connection .cache ["enterprise" ].findtext ("id" )
664695
665696 def _define_create_node_location (self , image , location ):
@@ -670,11 +701,13 @@ def _define_create_node_location(self, image, location):
670701 location will be created.
671702 """
672703 # First, get image location
704+
673705 if not image :
674706 error = "'image' parameter is mandatory"
675707 raise LibcloudError (error , self )
676708
677709 # Get the location argument
710+
678711 if location :
679712 if location not in self .list_locations ():
680713 raise LibcloudError ("Location does not exist" )
@@ -683,14 +716,17 @@ def _define_create_node_location(self, image, location):
683716 # the input location
684717 loc = None
685718 target_loc = None
719+
686720 for candidate_loc in self ._get_locations (location ):
687721 link_vdc = self .connection .cache ["locations" ][candidate_loc ]
688722 hdr_vdc = {"Accept" : self .VDC_MIME_TYPE }
689723 e_vdc = self .connection .request (link_vdc , headers = hdr_vdc ).object
724+
690725 for img in self .list_images (candidate_loc ):
691726 if img .id == image .id :
692727 loc = e_vdc
693728 target_loc = candidate_loc
729+
694730 break
695731
696732 if loc is None :
@@ -705,6 +741,7 @@ def _define_create_node_group(self, xml_loc, loc, group_name=None):
705741
706742 If we can not find any group, create it into argument 'location'
707743 """
744+
708745 if not group_name :
709746 group_name = NodeGroup .DEFAULT_GROUP_NAME
710747
@@ -713,14 +750,17 @@ def _define_create_node_group(self, xml_loc, loc, group_name=None):
713750 groups_hdr = {"Accept" : self .VAPPS_MIME_TYPE }
714751 vapps_element = self .connection .request (groups_link , headers = groups_hdr ).object
715752 target_group = None
753+
716754 for vapp in vapps_element .findall ("virtualAppliance" ):
717755 if vapp .findtext ("name" ) == group_name :
718756 uri_vapp = get_href (vapp , "edit" )
757+
719758 return NodeGroup (self , vapp .findtext ("name" ), uri = uri_vapp )
720759
721760 # target group not found: create it. Since it is an extension of
722761 # the basic 'libcloud' functionality, we try to be as flexible as
723762 # possible.
763+
724764 if target_group is None :
725765 return self .ex_create_group (group_name , loc )
726766
@@ -732,6 +772,7 @@ def _define_create_node_node(self, group, name=None, size=None, image=None):
732772 the API before to create it into the target hypervisor.
733773 """
734774 vm = ET .Element ("virtualMachine" )
775+
735776 if name :
736777 vmname = ET .SubElement (vm , "label" )
737778 vmname .text = name
@@ -795,4 +836,5 @@ def destroy(self):
795836 Destroys the group delegating the execution to
796837 :class:`AbiquoNodeDriver`.
797838 """
839+
798840 return self .driver .ex_destroy_group (self )
0 commit comments