<s> __all__ = [ <EOL> '<STR_LIT>' <EOL> ] <EOL> from libcloud . utils . misc import merge_valid_keys , get_new_obj <EOL> from libcloud . common . linode import ( API_ROOT , LinodeException , <EOL> LinodeConnection , LinodeResponse ) <EOL> from libcloud . dns . types import Provider , RecordType <EOL> from libcloud . dns . types import ZoneDoesNotExistError , RecordDoesNotExistError <EOL> from libcloud . dns . base import DNSDriver , Zone , Record <EOL> VALID_ZONE_EXTRA_PARAMS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:status>' , '<STR_LIT>' ] <EOL> VALID_RECORD_EXTRA_PARAMS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> class LinodeDNSResponse ( LinodeResponse ) : <EOL> def _make_excp ( self , error ) : <EOL> result = super ( LinodeDNSResponse , self ) . _make_excp ( error ) <EOL> if isinstance ( result , LinodeException ) and result . code == <NUM_LIT:5> : <EOL> context = self . connection . context <EOL> if context [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> result = ZoneDoesNotExistError ( value = '<STR_LIT>' , <EOL> driver = self . connection . driver , <EOL> zone_id = context [ '<STR_LIT:id>' ] ) <EOL> elif context [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> result = RecordDoesNotExistError ( value = '<STR_LIT>' , <EOL> driver = self . connection . driver , <EOL> record_id = context [ '<STR_LIT:id>' ] ) <EOL> return result <EOL> class LinodeDNSConnection ( LinodeConnection ) : <EOL> responseCls = LinodeDNSResponse <EOL> class LinodeDNSDriver ( DNSDriver ) : <EOL> type = Provider . LINODE <EOL> name = '<STR_LIT>' <EOL> website = '<STR_LIT>' <EOL> connectionCls = LinodeDNSConnection <EOL> RECORD_TYPE_MAP = { <EOL> RecordType . NS : '<STR_LIT>' , <EOL> RecordType . MX : '<STR_LIT>' , <EOL> RecordType . A : '<STR_LIT:A>' , <EOL> RecordType . AAAA : '<STR_LIT>' , <EOL> RecordType . CNAME : '<STR_LIT>' , <EOL> RecordType . TXT : '<STR_LIT>' , <EOL> RecordType . SRV : '<STR_LIT>' , <EOL> } <EOL> def list_zones ( self ) : <EOL> params = { '<STR_LIT>' : '<STR_LIT>' } <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> zones = self . _to_zones ( data ) <EOL> return zones <EOL> def list_records ( self , zone ) : <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : zone . id } <EOL> self . connection . set_context ( context = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : zone . id } ) <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> records = self . _to_records ( items = data , zone = zone ) <EOL> return records <EOL> def get_zone ( self , zone_id ) : <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : zone_id } <EOL> self . connection . set_context ( context = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : zone_id } ) <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> zones = self . _to_zones ( data ) <EOL> if len ( zones ) != <NUM_LIT:1> : <EOL> raise ZoneDoesNotExistError ( value = '<STR_LIT>' , driver = self , zone_id = zone_id ) <EOL> return zones [ <NUM_LIT:0> ] <EOL> def get_record ( self , zone_id , record_id ) : <EOL> zone = self . get_zone ( zone_id = zone_id ) <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : zone_id , <EOL> '<STR_LIT>' : record_id } <EOL> self . connection . set_context ( context = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : record_id } ) <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> records = self . _to_records ( items = data , zone = zone ) <EOL> if len ( records ) != <NUM_LIT:1> : <EOL> raise RecordDoesNotExistError ( value = '<STR_LIT>' , driver = self , <EOL> record_id = record_id ) <EOL> return records [ <NUM_LIT:0> ] <EOL> def create_zone ( self , domain , type = '<STR_LIT>' , ttl = None , extra = None ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : type , <EOL> '<STR_LIT>' : domain } <EOL> if ttl : <EOL> params [ '<STR_LIT>' ] = ttl <EOL> merged = merge_valid_keys ( params = params , <EOL> valid_keys = VALID_ZONE_EXTRA_PARAMS , <EOL> extra = extra ) <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> zone = Zone ( id = data [ '<STR_LIT>' ] , domain = domain , type = type , ttl = ttl , <EOL> extra = merged , driver = self ) <EOL> return zone <EOL> def update_zone ( self , zone , domain = None , type = None , ttl = None , extra = None ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : zone . id } <EOL> if type : <EOL> params [ '<STR_LIT>' ] = type <EOL> if domain : <EOL> params [ '<STR_LIT>' ] = domain <EOL> if ttl : <EOL> params [ '<STR_LIT>' ] = ttl <EOL> merged = merge_valid_keys ( params = params , <EOL> valid_keys = VALID_ZONE_EXTRA_PARAMS , <EOL> extra = extra ) <EOL> self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> updated_zone = get_new_obj ( obj = zone , klass = Zone , <EOL> attributes = { '<STR_LIT>' : domain , <EOL> '<STR_LIT:type>' : type , '<STR_LIT>' : ttl , <EOL> '<STR_LIT>' : merged } ) <EOL> return updated_zone <EOL> def create_record ( self , name , zone , type , data , extra = None ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : zone . id , <EOL> '<STR_LIT:Name>' : name , '<STR_LIT>' : data , <EOL> '<STR_LIT>' : self . RECORD_TYPE_MAP [ type ] } <EOL> merged = merge_valid_keys ( params = params , <EOL> valid_keys = VALID_RECORD_EXTRA_PARAMS , <EOL> extra = extra ) <EOL> result = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> record = Record ( id = result [ '<STR_LIT>' ] , name = name , type = type , <EOL> data = data , extra = merged , zone = zone , driver = self , <EOL> ttl = merged . get ( '<STR_LIT>' , None ) ) <EOL> return record <EOL> def update_record ( self , record , name = None , type = None , data = None , <EOL> extra = None ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : record . id , '<STR_LIT>' : record . zone . id } <EOL> if name : <EOL> params [ '<STR_LIT:Name>' ] = name <EOL> if data : <EOL> params [ '<STR_LIT>' ] = data <EOL> if type is not None : <EOL> params [ '<STR_LIT>' ] = self . RECORD_TYPE_MAP [ type ] <EOL> merged = merge_valid_keys ( params = params , <EOL> valid_keys = VALID_RECORD_EXTRA_PARAMS , <EOL> extra = extra ) <EOL> self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> updated_record = get_new_obj ( obj = record , klass = Record , <EOL> attributes = { '<STR_LIT:name>' : name , '<STR_LIT:data>' : data , <EOL> '<STR_LIT:type>' : type , <EOL> '<STR_LIT>' : merged } ) <EOL> return updated_record <EOL> def delete_zone ( self , zone ) : <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : zone . id } <EOL> self . connection . set_context ( context = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : zone . id } ) <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> return '<STR_LIT>' in data <EOL> def delete_record ( self , record ) : <EOL> params = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : record . zone . id , '<STR_LIT>' : record . id } <EOL> self . connection . set_context ( context = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : record . id } ) <EOL> data = self . connection . request ( API_ROOT , params = params ) . objects [ <NUM_LIT:0> ] <EOL> return '<STR_LIT>' in data <EOL> def _to_zones ( self , items ) : <EOL> """<STR_LIT>""" <EOL> zones = [ ] <EOL> for item in items : <EOL> zones . append ( self . _to_zone ( item ) ) <EOL> return zones <EOL> def _to_zone ( self , item ) : <EOL> """<STR_LIT>""" <EOL> extra = { '<STR_LIT>' : item [ '<STR_LIT>' ] , '<STR_LIT:status>' : item [ '<STR_LIT>' ] , <EOL> '<STR_LIT:description>' : item [ '<STR_LIT>' ] } <EOL> zone = Zone ( id = item [ '<STR_LIT>' ] , domain = item [ '<STR_LIT>' ] , <EOL> type = item [ '<STR_LIT>' ] , ttl = item [ '<STR_LIT>' ] , driver = self , <EOL> extra = extra ) <EOL> return zone <EOL> def _to_records ( self , items , zone = None ) : <EOL> """<STR_LIT>""" <EOL> records = [ ] <EOL> for item in items : <EOL> records . append ( self . _to_record ( item = item , zone = zone ) ) <EOL> return records <EOL> def _to_record ( self , item , zone = None ) : <EOL> """<STR_LIT>""" <EOL> extra = { '<STR_LIT>' : item [ '<STR_LIT>' ] , '<STR_LIT>' : item [ '<STR_LIT>' ] , <EOL> '<STR_LIT:port>' : item [ '<STR_LIT>' ] , '<STR_LIT>' : item [ '<STR_LIT>' ] } <EOL> type = self . _string_to_record_type ( item [ '<STR_LIT>' ] ) <EOL> record = Record ( id = item [ '<STR_LIT>' ] , name = item [ '<STR_LIT>' ] , type = type , <EOL> data = item [ '<STR_LIT>' ] , zone = zone , driver = self , <EOL> ttl = item [ '<STR_LIT>' ] , extra = extra ) <EOL> return record </s>
