###fromcollectionsimportdefaultdictfrom..provider.planimportPlanfrom..recordimportRecordfrom.baseimportBaseProcessor# Mark anything octoDNS is managing that way it can know it's safe to modify or# delete. We'll take ownership of existing records that we're told to manage# and thus "own" them going forward.
[docs]defprocess_source_zone(self,desired,*args,**kwargs):forrecordindesired.records:# Then create and add an ownership TXT for each of themrecord_name=record.name.replace('*','_wildcard')ifrecord.name:name=f'{self.txt_name}.{record._type}.{record_name}'else:name=f'{self.txt_name}.{record._type}'txt=Record.new(desired,name,{'type':'TXT','ttl':self.txt_ttl,'value':self.txt_value},)# add these w/lenient to cover the case when the ownership record# for a NS delegation record should technically live in the subzonedesired.add_record(txt,lenient=True)returndesired
[docs]defprocess_plan(self,plan,*args,**kwargs):ifnotplan:# If we don't have any change there's nothing to doreturnplan# First find all the ownership infoowned=defaultdict(dict)# We need to look for ownership in both the desired and existing# states, many things will show up in both, but that's fine.forrecordinlist(plan.existing.records)+list(plan.desired.records):ifself._is_ownership(record):pieces=record.name.split('.',2)iflen(pieces)>2:_,_type,name=piecesname=name.replace('_wildcard','*')else:_type=pieces[1]name=''owned[name][_type.upper()]=True# Cases:# - Configured in source# - We'll fully CRU/manage it adding ownership TXT,# thanks to process_source_zone, if needed# - Not in source# - Has an ownership TXT - delete it & the ownership TXT# - Does not have an ownership TXT - don't delete it# - Special records like octodns-meta# - Should be left alone and should not have ownerthis TXTsfiltered_changes=[]forchangeinplan.changes:record=change.recordif(notself._is_ownership(record)andrecord._typenotinowned[record.name]andrecord.name!='octodns-meta'):# It's not an ownership TXT, it's not owned, and it's not# special we're going to ignore itcontinue# We own this record or owned it up until now so whatever the# change is we should dofiltered_changes.append(change)ifnotfiltered_changes:returnNoneelifplan.changes!=filtered_changes:returnPlan(plan.existing,plan.desired,filtered_changes,plan.exists,plan.update_pcent_threshold,plan.delete_pcent_threshold,)returnplan