99import itertools
1010import json
1111import logging
12+ import random
13+ import time
1214
1315from SoftLayer import exceptions
1416from SoftLayer import utils
15- import time
1617
17- logger = logging .getLogger (__name__ )
18+ LOGGER = logging .getLogger (__name__ )
1819
1920DEFAULT_SUBNET_MASK = ',' .join (['hardware' ,
2021 'datacenter' ,
@@ -645,44 +646,45 @@ def get_nas_credentials(self, identifier, **kwargs):
645646 """
646647 result = self .network_storage .getObject (id = identifier , ** kwargs )
647648 return result
648-
649+
649650 def wait_for_sg_request (self , group_id , request_id , limit = 60 , delay = 5 ):
650651 """Wait for a Security Group API request to complete.
651652
652- Security Group API requests may trigger firewall updates that complete
653+ Security Group API requests may trigger firewall updates that complete
653654 ansynchronously. Firewall update completion is signalled
654655 via an Audit Log event. This method polls Security Group audit logs until the
655656 specified request is complete (either successfully or unsuccessfully).
656657
657658 :param int group_id: The security group ID with the pending API request
658- :param int request_id: The request ID of the API request we want to verify completion for.
659- Security Group APIs that may complete asynchronously contain this value in the requestId field of their
660- return value.
659+ :param int request_id: The request ID of the API request we want to verify completion
660+ for. Security Group APIs that may complete asynchronously contain this value in the
661+ requestId field of their return value.
661662 :param int limit: The maximum amount of time to wait for completion.
662663 :param int delay: The number of seconds to sleep before polling checks. Defaults to 5.
663664
664665 Example::
665666
666- # Will return once firewall updates for Security Group API request 'abc123' are
667+ # Will return once firewall updates for Security Group API request 'abc123' are
667668 complete for security group 123456.
668669 net_mgr.wait_for_sg_request(123456, 'abc123')
669670 """
670-
671+
671672 # Audit log event name for successfully API completion
672- SUCCESS_EVENT_NAME = 'Network Component Added to Security Group'
673-
673+ success_event_name = 'Network Component Added to Security Group'
674+
674675 # Audit log event name for unsuccessful API completion
675- FAILURE_EVENT_NAME = 'Network Component Removed from Security Group'
676-
677- logger .debug ('wait %s seconds for request %s completion on Security Group %s' % (limit , request_id , group_id ))
676+ failure_event_name = 'Network Component Removed from Security Group'
677+
678+ LOGGER .debug ('wait %s seconds for request %s completion on Security Group %s' ,
679+ limit , request_id , group_id )
678680 wait_until = time .time () + limit
679-
681+
680682 for sg_id in itertools .repeat (group_id ):
681683 try :
682684 # get all event logs for the specified security group
683685 logs = self .client .call ("Event_Log" ,
684- 'getAllObjects' ,
685- filter = {'objectId' : {'operation' : sg_id }})
686+ 'getAllObjects' ,
687+ filter = {'objectId' : {'operation' : sg_id }})
686688
687689 #
688690 # look for a log for the indicated request, there will be
@@ -696,43 +698,47 @@ def wait_for_sg_request(self, group_id, request_id, limit=60, delay=5):
696698 if not 'requestId' in metadata :
697699 continue
698700 if metadata ['requestId' ] == request_id :
699- if one_log ['eventName' ] == SUCCESS_EVENT_NAME or one_log ['eventName' ] == FAILURE_EVENT_NAME :
700- completion_log = one_log
701+ event_name = one_log ['eventName' ]
702+ if event_name == success_event_name or event_name == failure_event_name :
703+ completion_log = one_log
701704 break
702705
703706 #
704707 # check if it's a successful or failure completion log
705708 #
706709 if completion_log is not None :
707- if completion_log ['eventName' ] == SUCCESS_EVENT_NAME :
708- # return True if firewall updates for the Security Group request
710+ if completion_log ['eventName' ] == success_event_name :
711+ # return True if firewall updates for the Security Group request
709712 # completed successfully.
710- logger .debug ('request %s complete successfully' % request_id )
713+ LOGGER .debug ('request %s complete successfully' , request_id )
711714 return True
712- elif completion_log ['eventName' ] == FAILURE_EVENT_NAME :
713- logger .debug ('request %s complete with failure' % request_id )
714- raise exceptions .SoftLayerError ("Security Group %s request %s did not complete"
715- " within the specified timeout (%s seconds)" % (sg_id , request_id , limit ))
715+ elif completion_log ['eventName' ] == failure_event_name :
716+ LOGGER .debug ('request %s complete with failure' , request_id )
717+ raise exceptions .SoftLayerError ("Security Group %s request %s did not "
718+ "complete within the specified timeout"
719+ " (%s seconds)"
720+ % (sg_id , request_id , limit ))
716721 else :
717- logger .warning ('unexpected log event: %s' % completion_log ['eventName' ])
722+ LOGGER .warning ('unexpected log event: %s' , completion_log ['eventName' ])
718723 raise exceptions .SoftLayerError ("Security Group internal error, rcvd %s "
719- % completion_log ['eventName' ])
724+ % completion_log ['eventName' ])
720725
721726 #
722727 # no completion log yet, delay and try again
723728 #
724- logger .info ("%s not complete." , str (request_id ))
725-
726- except exceptions .SoftLayerAPIError as exception :
729+ LOGGER .info ("%s not complete." , str (request_id ))
730+
731+ except exceptions .SoftLayerAPIError :
727732 # if the call is excepting unexpectedly, scale back how
728733 # frequently we call it.
729734 delay = (delay * 2 ) + random .randint (0 , 9 )
730- logger .exception ()
731-
735+ LOGGER .exception ()
736+
732737 now = time .time ()
733738 if now > wait_until :
734739 raise exceptions .SoftLayerError ("Security Group %s request %s did not complete"
735- " within the specified timeout %s" % (sg_id , request_id , limit ))
740+ " within the specified timeout %s"
741+ % (sg_id , request_id , limit ))
736742
737- logger .debug ('Auto retry in %s seconds' , str (min (delay , wait_until - now )))
743+ LOGGER .debug ('Auto retry in %s seconds' , str (min (delay , wait_until - now )))
738744 time .sleep (min (delay , wait_until - now ))
0 commit comments