<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import logging <EOL> import json <EOL> log = logging . getLogger ( __name__ ) <EOL> try : <EOL> import dns . query <EOL> import dns . update <EOL> import dns . tsigkeyring <EOL> dns_support = True <EOL> except ImportError as e : <EOL> dns_support = False <EOL> import salt . utils <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if dns_support : <EOL> return '<STR_LIT>' <EOL> return ( False , '<STR_LIT>' ) <EOL> def _config ( name , key = None , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> if key is None : <EOL> key = name <EOL> if name in kwargs : <EOL> value = kwargs [ name ] <EOL> else : <EOL> value = __salt__ [ '<STR_LIT>' ] ( '<STR_LIT>' . format ( key ) ) <EOL> if not value : <EOL> value = None <EOL> return value <EOL> def _get_keyring ( keyfile ) : <EOL> keyring = None <EOL> if keyfile : <EOL> with salt . utils . fopen ( keyfile ) as _f : <EOL> keyring = dns . tsigkeyring . from_text ( json . load ( _f ) ) <EOL> return keyring <EOL> def add_host ( zone , name , ttl , ip , nameserver = '<STR_LIT:127.0.0.1>' , replace = True , <EOL> ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> res = update ( zone , name , ttl , '<STR_LIT:A>' , ip , nameserver , replace , ** kwargs ) <EOL> if res is False : <EOL> return False <EOL> fqdn = '<STR_LIT>' . format ( name , zone ) <EOL> parts = ip . split ( '<STR_LIT:.>' ) [ : : - <NUM_LIT:1> ] <EOL> popped = [ ] <EOL> while len ( parts ) > <NUM_LIT:1> : <EOL> p = parts . pop ( <NUM_LIT:0> ) <EOL> popped . append ( p ) <EOL> zone = '<STR_LIT>' . format ( '<STR_LIT:.>' . join ( parts ) , '<STR_LIT>' ) <EOL> name = '<STR_LIT:.>' . join ( popped ) <EOL> ptr = update ( zone , name , ttl , '<STR_LIT>' , fqdn , nameserver , replace , <EOL> ** kwargs ) <EOL> if ptr : <EOL> return True <EOL> return res <EOL> def delete_host ( zone , name , nameserver = '<STR_LIT:127.0.0.1>' , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> fqdn = '<STR_LIT>' . format ( name , zone ) <EOL> request = dns . message . make_query ( fqdn , '<STR_LIT:A>' ) <EOL> answer = dns . query . udp ( request , nameserver ) <EOL> try : <EOL> ips = [ i . address for i in answer . answer [ <NUM_LIT:0> ] . items ] <EOL> except IndexError : <EOL> ips = [ ] <EOL> res = delete ( zone , name , nameserver = nameserver , ** kwargs ) <EOL> fqdn = fqdn + '<STR_LIT:.>' <EOL> for ip in ips : <EOL> parts = ip . split ( '<STR_LIT:.>' ) [ : : - <NUM_LIT:1> ] <EOL> popped = [ ] <EOL> while len ( parts ) > <NUM_LIT:1> : <EOL> p = parts . pop ( <NUM_LIT:0> ) <EOL> popped . append ( p ) <EOL> zone = '<STR_LIT>' . format ( '<STR_LIT:.>' . join ( parts ) , '<STR_LIT>' ) <EOL> name = '<STR_LIT:.>' . join ( popped ) <EOL> ptr = delete ( zone , name , '<STR_LIT>' , fqdn , nameserver = nameserver , <EOL> ** kwargs ) <EOL> if ptr : <EOL> res = True <EOL> return res <EOL> def update ( zone , name , ttl , rdtype , data , nameserver = '<STR_LIT:127.0.0.1>' , <EOL> replace = False , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> name = str ( name ) <EOL> fqdn = '<STR_LIT>' . format ( name , zone ) <EOL> request = dns . message . make_query ( fqdn , rdtype ) <EOL> answer = dns . query . udp ( request , nameserver ) <EOL> rdtype = dns . rdatatype . from_text ( rdtype ) <EOL> rdata = dns . rdata . from_text ( dns . rdataclass . IN , rdtype , data ) <EOL> keyring = _get_keyring ( _config ( '<STR_LIT>' , ** kwargs ) ) <EOL> keyname = _config ( '<STR_LIT>' , ** kwargs ) <EOL> keyalgorithm = _config ( '<STR_LIT>' , <EOL> ** kwargs ) or '<STR_LIT>' <EOL> is_exist = False <EOL> for rrset in answer . answer : <EOL> if rdata in rrset . items : <EOL> if ttl == rrset . ttl : <EOL> if len ( answer . answer ) >= <NUM_LIT:1> or len ( rrset . items ) >= <NUM_LIT:1> : <EOL> is_exist = True <EOL> break <EOL> dns_update = dns . update . Update ( zone , keyring = keyring , keyname = keyname , <EOL> keyalgorithm = keyalgorithm ) <EOL> if replace : <EOL> dns_update . replace ( name , ttl , rdata ) <EOL> elif not is_exist : <EOL> dns_update . add ( name , ttl , rdata ) <EOL> answer = dns . query . udp ( dns_update , nameserver ) <EOL> if answer . rcode ( ) > <NUM_LIT:0> : <EOL> return False <EOL> return True <EOL> def delete ( zone , name , rdtype = None , data = None , nameserver = '<STR_LIT:127.0.0.1>' , <EOL> ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> name = str ( name ) <EOL> fqdn = '<STR_LIT>' . format ( name , zone ) <EOL> request = dns . message . make_query ( fqdn , ( rdtype or '<STR_LIT>' ) ) <EOL> answer = dns . query . udp ( request , nameserver ) <EOL> if not answer . answer : <EOL> return None <EOL> keyring = _get_keyring ( _config ( '<STR_LIT>' , ** kwargs ) ) <EOL> keyname = _config ( '<STR_LIT>' , ** kwargs ) <EOL> keyalgorithm = _config ( '<STR_LIT>' , <EOL> ** kwargs ) or '<STR_LIT>' <EOL> dns_update = dns . update . Update ( zone , keyring = keyring , keyname = keyname , <EOL> keyalgorithm = keyalgorithm ) <EOL> if rdtype : <EOL> rdtype = dns . rdatatype . from_text ( rdtype ) <EOL> if data : <EOL> rdata = dns . rdata . from_text ( dns . rdataclass . IN , rdtype , data ) <EOL> dns_update . delete ( name , rdata ) <EOL> else : <EOL> dns_update . delete ( name , rdtype ) <EOL> else : <EOL> dns_update . delete ( name ) <EOL> answer = dns . query . udp ( dns_update , nameserver ) <EOL> if answer . rcode ( ) > <NUM_LIT:0> : <EOL> return False <EOL> return True </s>
