<s> import time <EOL> import hashlib <EOL> import hmac <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> from libcloud . utils . py3 import httplib <EOL> from libcloud . utils . py3 import urlencode <EOL> from libcloud . common . base import ConnectionUserAndKey , JsonResponse <EOL> from libcloud . common . types import InvalidCredsError , LibcloudError <EOL> from libcloud . storage . base import Container , StorageDriver <EOL> class NimbusResponse ( JsonResponse ) : <EOL> valid_response_codes = [ httplib . OK , httplib . NOT_FOUND , httplib . CONFLICT , <EOL> httplib . BAD_REQUEST ] <EOL> def success ( self ) : <EOL> return self . status in self . valid_response_codes <EOL> def parse_error ( self ) : <EOL> if self . status in [ httplib . UNAUTHORIZED ] : <EOL> raise InvalidCredsError ( self . body ) <EOL> raise LibcloudError ( '<STR_LIT>' % ( self . status ) , <EOL> driver = self . connection . driver ) <EOL> class NimbusConnection ( ConnectionUserAndKey ) : <EOL> host = '<STR_LIT>' <EOL> responseCls = NimbusResponse <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . id = kwargs . pop ( '<STR_LIT:id>' ) <EOL> super ( NimbusConnection , self ) . __init__ ( * args , ** kwargs ) <EOL> def pre_connect_hook ( self , params , headers ) : <EOL> timestamp = str ( int ( time . time ( ) ) ) <EOL> signature = self . _calculate_signature ( user_id = self . user_id , <EOL> method = self . method , <EOL> params = params , <EOL> path = self . action , <EOL> timestamp = timestamp , <EOL> key = self . key ) <EOL> headers [ '<STR_LIT>' ] = timestamp <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' % ( self . id , signature ) <EOL> return params , headers <EOL> def _calculate_signature ( self , user_id , method , params , path , timestamp , <EOL> key ) : <EOL> if params : <EOL> uri_path = path + '<STR_LIT:?>' + urlencode ( params ) <EOL> else : <EOL> uri_path = path <EOL> string_to_sign = [ user_id , method , str ( timestamp ) , uri_path ] <EOL> string_to_sign = '<STR_LIT:\n>' . join ( string_to_sign ) <EOL> hmac_value = hmac . new ( key , string_to_sign , hashlib . sha256 ) <EOL> return hmac_value . hexdigest ( ) <EOL> class NimbusStorageDriver ( StorageDriver ) : <EOL> name = '<STR_LIT>' <EOL> website = '<STR_LIT>' <EOL> connectionCls = NimbusConnection <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . user_id = kwargs [ '<STR_LIT>' ] <EOL> super ( NimbusStorageDriver , self ) . __init__ ( * args , ** kwargs ) <EOL> def iterate_containers ( self ) : <EOL> response = self . connection . request ( '<STR_LIT>' % <EOL> ( self . user_id ) ) <EOL> return self . _to_containers ( response . object ) <EOL> def create_container ( self , container_name ) : <EOL> params = { '<STR_LIT:action>' : '<STR_LIT>' , '<STR_LIT:name>' : container_name } <EOL> response = self . connection . request ( '<STR_LIT>' % <EOL> ( self . user_id ) , <EOL> params = params , <EOL> method = '<STR_LIT:POST>' ) <EOL> return self . _to_container ( response . object ) <EOL> def _to_containers ( self , data ) : <EOL> for item in data : <EOL> yield self . _to_container ( item ) <EOL> def _to_container ( self , data ) : <EOL> name = data [ <NUM_LIT:0> ] <EOL> extra = { '<STR_LIT>' : data [ <NUM_LIT:2> ] } <EOL> return Container ( name = name , extra = extra , driver = self ) <EOL> def _ex_connection_class_kwargs ( self ) : <EOL> result = { '<STR_LIT:id>' : self . user_id } <EOL> return result </s>
