class DDNSDomainRecordManager(object): """ Manager class used to manage local domain record and remote domain records """ def __init__(self, config): self.config = config self.resolver = YunResolver(self.config.access_id, self.config.access_key, self.config.debug) self.local_record_list = self.get_local_record_list() def get_local_record_list(self): """ Create local domain record objectes based on local config info :return: list of LocalDomainRecord objects """ local_record_list = [] for section in self.config.get_domain_record_sections(): try: local_record = LocalDomainRecord(self.config, section) except ValueError: continue local_record_list.append(local_record) return local_record_list def find_local_record(self, remote_record): """ Find LocalDomainRecord based on RemoteDomainRecord :param RemoteDomainRecord :return: LocalDomainRecord or None """ for local_record in self.local_record_list: if all( getattr(local_record, attr) == getattr( remote_record, attr) for attr in ('domainname', 'rr', 'type')): return local_record return None def fetch_remote_record(self, local_record): """ Fetch RemoteDomainReord from Aliyun server by using LocalDomainRecord info :param LocalDomainRecord :return: RemoteDomainRecord or None """ # Aliyun use fuzzy matching pattern for RR and type keyword fuzzy_matched_list = self.resolver.describe_domain_records( local_record.domainname, rr_keyword=local_record.rr, type_keyword=local_record.type) if not fuzzy_matched_list: DDNSUtils.err("Failed to fetch remote DomainRecords.") return None exact_matched_list = [] check_keys = ('DomainName', 'RR', 'Type') for rec in fuzzy_matched_list: if all( rec.get(key, None) == getattr(local_record, key.lower()) for key in check_keys): exact_matched_list.append(rec) if not exact_matched_list: return None if len(exact_matched_list) > 1: DDNSUtils.err( "Duplicate DomainRecord in Aliyun: {rec.subdomain}.{rec.domainname}" .format(rec=local_record)) return None try: remote_record = RemoteDomainRecord(exact_matched_list[0]) except Exception as ex: raise ex return remote_record def update(self, remote_record, current_public_ip, record_type='A'): """ Update RemoteDomainRecord 's value to current public IP on Aliyun server :param RemoteDomainRecord: :param current public IP :return: True or False """ return self.resolver.update_domain_record( remote_record.recordid, rr=remote_record.rr, record_value=current_public_ip, record_type=record_type)
class DDNSDomainRecordManager(object): """ Manager class used to manage local domain record and remote domain records """ def __init__(self, config): self.config = config self.resolver = YunResolver(self.config.access_id, self.config.access_key, self.config.debug) self.local_record_list = self.get_local_record_list() def get_local_record_list(self): """ Create local domain record objectes based on local config info :return: list of LocalDomainRecord objects """ local_record_list = [] for section in self.config.get_domain_record_sections(): try: local_record = LocalDomainRecord(self.config, section) except ValueError: continue local_record_list.append(local_record) return local_record_list def find_local_record(self, remote_record): """ Find LocalDomainRecord based on RemoteDomainRecord :param RemoteDomainRecord :return: LocalDomainRecord or None """ for local_record in self.local_record_list: if all(getattr(local_record, attr) == getattr(remote_record, attr) for attr in ('domainname', 'rr', 'type')): return local_record return None def fetch_remote_record(self, local_record): """ Fetch RemoteDomainReord from Aliyun server by using LocalDomainRecord info :param LocalDomainRecord :return: RemoteDomainRecord or None """ # Aliyun use fuzzy matching pattern for RR and type keyword fuzzy_matched_list = self.resolver.describe_domain_records(local_record.domainname, rr_keyword=local_record.rr, type_keyword=local_record.type) if not fuzzy_matched_list: DDNSUtils.err("Failed to fetch remote DomainRecords.") return None exact_matched_list = [] check_keys = ('DomainName', 'RR', 'Type') for rec in fuzzy_matched_list: if all(rec.get(key, None) == getattr(local_record, key.lower()) for key in check_keys): exact_matched_list.append(rec) if not exact_matched_list: return None if len(exact_matched_list) > 1: DDNSUtils.err("Duplicate DomainRecord in Aliyun: {rec.subdomain}.{rec.domainname}" .format(rec=local_record)) return None try: remote_record = RemoteDomainRecord(exact_matched_list[0]) except Exception as ex: raise ex return remote_record def update(self, remote_record, current_public_ip): """ Update RemoteDomainRecord 's value to current public IP on Aliyun server :param RemoteDomainRecord: :param current public IP :return: True or False """ return self.resolver.update_domain_record(remote_record.recordid, rr=remote_record.rr, record_value=current_public_ip)