<s> import hashlib <EOL> import time <EOL> from libcloud . utils . py3 import b <EOL> from libcloud . common . types import InvalidCredsError , LibcloudError <EOL> from libcloud . common . types import MalformedResponseError <EOL> from libcloud . common . base import ConnectionUserAndKey , JsonResponse <EOL> from libcloud . compute . base import NodeLocation <EOL> HOST = '<STR_LIT>' <EOL> PORTS_BY_SECURITY = { True : <NUM_LIT> , False : <NUM_LIT> } <EOL> API_VERSION = '<STR_LIT>' <EOL> __all__ = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> class GoGridResponse ( JsonResponse ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . driver = BaseGoGridDriver <EOL> super ( GoGridResponse , self ) . __init__ ( * args , ** kwargs ) <EOL> def success ( self ) : <EOL> if self . status == <NUM_LIT> : <EOL> raise InvalidCredsError ( '<STR_LIT>' , self . driver ) <EOL> if self . status == <NUM_LIT> : <EOL> raise InvalidCredsError ( '<STR_LIT>' , <EOL> self . driver ) <EOL> if not self . body : <EOL> return None <EOL> try : <EOL> return self . parse_body ( ) [ '<STR_LIT:status>' ] == '<STR_LIT:success>' <EOL> except ValueError : <EOL> raise MalformedResponseError ( '<STR_LIT>' , <EOL> body = self . body , <EOL> driver = self . driver ) <EOL> def parse_error ( self ) : <EOL> try : <EOL> return self . parse_body ( ) [ "<STR_LIT:list>" ] [ <NUM_LIT:0> ] [ "<STR_LIT:message>" ] <EOL> except ( ValueError , KeyError ) : <EOL> return None <EOL> class GoGridConnection ( ConnectionUserAndKey ) : <EOL> """<STR_LIT>""" <EOL> host = HOST <EOL> responseCls = GoGridResponse <EOL> def add_default_params ( self , params ) : <EOL> params [ "<STR_LIT>" ] = self . user_id <EOL> params [ "<STR_LIT:v>" ] = API_VERSION <EOL> params [ "<STR_LIT>" ] = '<STR_LIT>' <EOL> params [ "<STR_LIT>" ] = self . get_signature ( self . user_id , self . key ) <EOL> return params <EOL> def get_signature ( self , key , secret ) : <EOL> """<STR_LIT>""" <EOL> m = hashlib . md5 ( b ( key + secret + str ( int ( time . time ( ) ) ) ) ) <EOL> return m . hexdigest ( ) <EOL> def request ( self , action , params = None , data = '<STR_LIT>' , headers = None , method = '<STR_LIT:GET>' , <EOL> raw = False ) : <EOL> return super ( GoGridConnection , self ) . request ( action , params , data , <EOL> headers , method , raw ) <EOL> class GoGridIpAddress ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , id , ip , public , state , subnet ) : <EOL> self . id = id <EOL> self . ip = ip <EOL> self . public = public <EOL> self . state = state <EOL> self . subnet = subnet <EOL> class BaseGoGridDriver ( object ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> def _get_ip ( self , element ) : <EOL> return element . get ( '<STR_LIT>' ) . get ( '<STR_LIT>' ) <EOL> def _to_ip ( self , element ) : <EOL> ip = GoGridIpAddress ( id = element [ '<STR_LIT:id>' ] , <EOL> ip = element [ '<STR_LIT>' ] , <EOL> public = element [ '<STR_LIT>' ] , <EOL> subnet = element [ '<STR_LIT>' ] , <EOL> state = element [ "<STR_LIT:state>" ] [ "<STR_LIT:name>" ] ) <EOL> ip . location = self . _to_location ( element [ '<STR_LIT>' ] ) <EOL> return ip <EOL> def _to_ips ( self , object ) : <EOL> return [ self . _to_ip ( el ) <EOL> for el in object [ '<STR_LIT:list>' ] ] <EOL> def _to_location ( self , element ) : <EOL> location = NodeLocation ( id = element [ '<STR_LIT:id>' ] , <EOL> name = element [ '<STR_LIT:name>' ] , <EOL> country = "<STR_LIT>" , <EOL> driver = self . connection . driver ) <EOL> return location <EOL> def _to_locations ( self , object ) : <EOL> return [ self . _to_location ( el ) <EOL> for el in object [ '<STR_LIT:list>' ] ] <EOL> def ex_list_ips ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> params = { } <EOL> if "<STR_LIT>" in kwargs and kwargs [ "<STR_LIT>" ] is not None : <EOL> params [ "<STR_LIT>" ] = { True : "<STR_LIT>" , <EOL> False : "<STR_LIT>" } [ kwargs [ "<STR_LIT>" ] ] <EOL> if "<STR_LIT>" in kwargs and kwargs [ "<STR_LIT>" ] is not None : <EOL> params [ "<STR_LIT>" ] = { True : "<STR_LIT>" , <EOL> False : "<STR_LIT>" } [ kwargs [ "<STR_LIT>" ] ] <EOL> if "<STR_LIT:location>" in kwargs and kwargs [ '<STR_LIT:location>' ] is not None : <EOL> params [ '<STR_LIT>' ] = kwargs [ '<STR_LIT:location>' ] . id <EOL> response = self . connection . request ( '<STR_LIT>' , params = params ) <EOL> ips = self . _to_ips ( response . object ) <EOL> return ips <EOL> def _get_first_ip ( self , location = None ) : <EOL> ips = self . ex_list_ips ( public = True , assigned = False , location = location ) <EOL> try : <EOL> return ips [ <NUM_LIT:0> ] . ip <EOL> except IndexError : <EOL> raise LibcloudError ( '<STR_LIT>' , <EOL> self . driver ) </s>
