<s> """<STR_LIT>""" <EOL> import json <EOL> import sys <EOL> from libcloud . common . base import ConnectionKey , JsonResponse <EOL> from libcloud . common . exceptions import BaseHTTPError <EOL> from libcloud . common . types import InvalidCredsError , MalformedResponseError <EOL> from libcloud . dns . base import DNSDriver , Zone , Record <EOL> from libcloud . dns . types import ZoneDoesNotExistError , ZoneAlreadyExistsError <EOL> from libcloud . dns . types import Provider , RecordType <EOL> from libcloud . utils . py3 import httplib <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> ] <EOL> class PowerDNSResponse ( JsonResponse ) : <EOL> def success ( self ) : <EOL> i = int ( self . status ) <EOL> return i >= <NUM_LIT:200> and i <= <NUM_LIT> <EOL> def parse_error ( self ) : <EOL> if self . status == httplib . UNAUTHORIZED : <EOL> raise InvalidCredsError ( '<STR_LIT>' ) <EOL> try : <EOL> body = self . parse_body ( ) <EOL> except MalformedResponseError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> body = '<STR_LIT>' % ( e . value , e . body ) <EOL> try : <EOL> errors = [ body [ '<STR_LIT:error>' ] ] <EOL> except TypeError : <EOL> return '<STR_LIT>' % ( body , self . status ) <EOL> try : <EOL> errors . append ( body [ '<STR_LIT>' ] ) <EOL> except KeyError : <EOL> pass <EOL> return '<STR_LIT>' % ( '<STR_LIT:U+0020>' . join ( errors ) , self . status ) <EOL> class PowerDNSConnection ( ConnectionKey ) : <EOL> responseCls = PowerDNSResponse <EOL> def add_default_headers ( self , headers ) : <EOL> headers [ '<STR_LIT>' ] = self . key <EOL> return headers <EOL> class PowerDNSDriver ( DNSDriver ) : <EOL> type = Provider . POWERDNS <EOL> name = '<STR_LIT>' <EOL> website = '<STR_LIT>' <EOL> connectionCls = PowerDNSConnection <EOL> RECORD_TYPE_MAP = { <EOL> RecordType . A : '<STR_LIT:A>' , <EOL> RecordType . AAAA : '<STR_LIT>' , <EOL> RecordType . AFSDB : '<STR_LIT>' , <EOL> RecordType . CERT : '<STR_LIT>' , <EOL> RecordType . CNAME : '<STR_LIT>' , <EOL> RecordType . DNSKEY : '<STR_LIT>' , <EOL> RecordType . DS : '<STR_LIT>' , <EOL> RecordType . HINFO : '<STR_LIT>' , <EOL> RecordType . KEY : '<STR_LIT>' , <EOL> RecordType . LOC : '<STR_LIT>' , <EOL> RecordType . MX : '<STR_LIT>' , <EOL> RecordType . NAPTR : '<STR_LIT>' , <EOL> RecordType . NS : '<STR_LIT>' , <EOL> RecordType . NSEC : '<STR_LIT>' , <EOL> RecordType . OPENPGPKEY : '<STR_LIT>' , <EOL> RecordType . PTR : '<STR_LIT>' , <EOL> RecordType . RP : '<STR_LIT>' , <EOL> RecordType . RRSIG : '<STR_LIT>' , <EOL> RecordType . SOA : '<STR_LIT>' , <EOL> RecordType . SPF : '<STR_LIT>' , <EOL> RecordType . SSHFP : '<STR_LIT>' , <EOL> RecordType . SRV : '<STR_LIT>' , <EOL> RecordType . TLSA : '<STR_LIT>' , <EOL> RecordType . TXT : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , key , secret = None , secure = False , host = None , port = None , <EOL> api_version = '<STR_LIT>' , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . ex_server = '<STR_LIT:localhost>' <EOL> if api_version == '<STR_LIT>' : <EOL> self . api_root = '<STR_LIT>' <EOL> elif api_version == '<STR_LIT>' : <EOL> self . api_root = '<STR_LIT>' <EOL> else : <EOL> raise NotImplementedError ( '<STR_LIT>' % <EOL> api_version ) <EOL> super ( PowerDNSDriver , self ) . __init__ ( key = key , secure = secure , <EOL> host = host , port = port , <EOL> ** kwargs ) <EOL> def create_record ( self , name , zone , type , data , extra = None ) : <EOL> """<STR_LIT>""" <EOL> action = '<STR_LIT>' % ( self . api_root , self . ex_server , <EOL> zone . id ) <EOL> if extra is None or extra . get ( '<STR_LIT>' , None ) is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> record = { <EOL> '<STR_LIT:content>' : data , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : extra [ '<STR_LIT>' ] , <EOL> '<STR_LIT:type>' : type , <EOL> } <EOL> payload = { '<STR_LIT>' : [ { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:type>' : type , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ record ] <EOL> } ] <EOL> } <EOL> try : <EOL> self . connection . request ( action = action , data = json . dumps ( payload ) , <EOL> method = '<STR_LIT>' ) <EOL> except BaseHTTPError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if e . code == httplib . UNPROCESSABLE_ENTITY and e . message . startswith ( '<STR_LIT>' ) : <EOL> raise ZoneDoesNotExistError ( zone_id = zone . id , driver = self , <EOL> value = e . message ) <EOL> raise e <EOL> return Record ( id = None , name = name , data = data , <EOL> type = type , zone = zone , driver = self , ttl = extra [ '<STR_LIT>' ] ) <EOL> def create_zone ( self , domain , type = None , ttl = None , extra = { } ) : <EOL> """<STR_LIT>""" <EOL> action = '<STR_LIT>' % ( self . api_root , self . ex_server ) <EOL> if extra is None or extra . get ( '<STR_LIT>' , None ) is None : <EOL> msg = '<STR_LIT>' <EOL> raise ValueError ( msg ) <EOL> payload = { '<STR_LIT:name>' : domain , '<STR_LIT>' : '<STR_LIT>' } <EOL> payload . update ( extra ) <EOL> zone_id = domain + '<STR_LIT:.>' <EOL> try : <EOL> self . connection . request ( action = action , data = json . dumps ( payload ) , <EOL> method = '<STR_LIT:POST>' ) <EOL> except BaseHTTPError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if e . code == httplib . UNPROCESSABLE_ENTITY and e . message . startswith ( "<STR_LIT>" % domain ) : <EOL> raise ZoneAlreadyExistsError ( zone_id = zone_id , driver = self , <EOL> value = e . message ) <EOL> raise e <EOL> return Zone ( id = zone_id , domain = domain , type = None , ttl = None , <EOL> driver = self , extra = extra ) <EOL> def delete_record ( self , record ) : <EOL> """<STR_LIT>""" <EOL> action = '<STR_LIT>' % ( self . api_root , self . ex_server , <EOL> record . zone . id ) <EOL> payload = { '<STR_LIT>' : [ { '<STR_LIT:name>' : record . name , <EOL> '<STR_LIT:type>' : record . type , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ] <EOL> } <EOL> try : <EOL> self . connection . request ( action = action , data = json . dumps ( payload ) , <EOL> method = '<STR_LIT>' ) <EOL> except BaseHTTPError : <EOL> return False <EOL> return True <EOL> def delete_zone ( self , zone ) : <EOL> """<STR_LIT>""" <EOL> action = '<STR_LIT>' % ( self . api_root , self . ex_server , <EOL> zone . id ) <EOL> try : <EOL> self . connection . request ( action = action , method = '<STR_LIT>' ) <EOL> except BaseHTTPError : <EOL> return False <EOL> return True <EOL> def get_zone ( self , zone_id ) : <EOL> """<STR_LIT>""" <EOL> action = '<STR_LIT>' % ( self . api_root , self . ex_server , <EOL> zone_id ) <EOL> try : <EOL> response = self . connection . request ( action = action , method = '<STR_LIT:GET>' ) <EOL> except BaseHTTPError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if e . code == httplib . UNPROCESSABLE_ENTITY : <EOL> raise ZoneDoesNotExistError ( zone_id = zone_id , driver = self , <EOL> value = e . message ) <EOL> raise e <EOL> return self . _to_zone ( response . object ) <EOL> def list_records ( self , zone ) : <EOL> """<STR_LIT>""" <EOL> action = '<STR_LIT>' % ( self . api_root , self . ex_server , <EOL> zone . id ) <EOL> try : <EOL> response = self . connection . request ( action = action , method = '<STR_LIT:GET>' ) <EOL> except BaseHTTPError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if e . code == httplib . UNPROCESSABLE_ENTITY and e . message . startswith ( '<STR_LIT>' ) : <EOL> raise ZoneDoesNotExistError ( zone_id = zone . id , driver = self , <EOL> value = e . message ) <EOL> raise e <EOL> return self . _to_records ( response , zone ) <EOL> def list_zones ( self ) : <EOL> """<STR_LIT>""" <EOL> action = '<STR_LIT>' % ( self . api_root , self . ex_server ) <EOL> response = self . connection . request ( action = action , method = '<STR_LIT:GET>' ) <EOL> return self . _to_zones ( response ) <EOL> def update_record ( self , record , name , type , data , extra = None ) : <EOL> """<STR_LIT>""" <EOL> action = '<STR_LIT>' % ( self . api_root , self . ex_server , <EOL> record . zone . id ) <EOL> if extra is None or extra . get ( '<STR_LIT>' , None ) is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> updated_record = { <EOL> '<STR_LIT:content>' : data , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : extra [ '<STR_LIT>' ] , <EOL> '<STR_LIT:type>' : type , <EOL> } <EOL> payload = { '<STR_LIT>' : [ { '<STR_LIT:name>' : record . name , <EOL> '<STR_LIT:type>' : record . type , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> { '<STR_LIT:name>' : name , <EOL> '<STR_LIT:type>' : type , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ updated_record ] <EOL> } ] <EOL> } <EOL> try : <EOL> self . connection . request ( action = action , data = json . dumps ( payload ) , <EOL> method = '<STR_LIT>' ) <EOL> except BaseHTTPError : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if e . code == httplib . UNPROCESSABLE_ENTITY and e . message . startswith ( '<STR_LIT>' ) : <EOL> raise ZoneDoesNotExistError ( zone_id = record . zone . id , <EOL> driver = self , value = e . message ) <EOL> raise e <EOL> return Record ( id = None , name = name , data = data , type = type , <EOL> zone = record . zone , driver = self , ttl = extra [ '<STR_LIT>' ] ) <EOL> def _to_zone ( self , item ) : <EOL> extra = { } <EOL> for e in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] : <EOL> extra [ e ] = item [ e ] <EOL> return Zone ( id = item [ '<STR_LIT:id>' ] , domain = item [ '<STR_LIT:name>' ] , type = None , <EOL> ttl = None , driver = self , extra = extra ) <EOL> def _to_zones ( self , items ) : <EOL> zones = [ ] <EOL> for item in items . object : <EOL> zones . append ( self . _to_zone ( item ) ) <EOL> return zones <EOL> def _to_record ( self , item , zone ) : <EOL> return Record ( id = None , name = item [ '<STR_LIT:name>' ] , data = item [ '<STR_LIT:content>' ] , <EOL> type = item [ '<STR_LIT:type>' ] , zone = zone , driver = self , <EOL> ttl = item [ '<STR_LIT>' ] ) <EOL> def _to_records ( self , items , zone ) : <EOL> records = [ ] <EOL> for item in items . object [ '<STR_LIT>' ] : <EOL> records . append ( self . _to_record ( item , zone ) ) <EOL> return records </s>
