octodns.processor.templating
Classes
|
Record templating using python format. |
Exceptions
|
- class octodns.processor.templating.Templating(id, *args, trailing_dots=True, context={}, **kwargs)[source]
Bases:
BaseProcessorRecord templating using python format. For simple records like TXT and CAA that is the value itself. For multi-field records like MX or SRV it’s the text portions, exchange and target respectively.
Example Processor Config:
templating: class: octodns.processor.templating.Templating # When `trailing_dots` is disabled, trailing dots are removed from all # built-in variables values who represent a FQDN, like `{zone_name}` # or `{record_fqdn}`. Optional. Default to `True`. trailing_dots: False # Any k/v present in context will be passed into the .format method and # thus be available as additional variables in the template. This is all # optional. context: key: value another: 42
Example Records:
foo: type: TXT value: The zone this record lives in is {zone_name}. There are {zone_num_records} record(s). bar: type: MX values: - preference: 1 exchange: mx1.{zone_name}.mail.mx. - preference: 1 exchange: mx2.{zone_name}.mail.mx.
Note that validations for some types reject values with {}. When encountered the best option is to use record level lenient: true https://github.com/octodns/octodns/blob/main/docs/records.md#lenience
Note that if you need to add dynamic context you can create a custom processor that inherits from Templating and passes them into the call to super, e.g.:
class MyTemplating(Templating): def __init__(self, *args, context={}, **kwargs): context['year'] = lambda desired, sources: datetime.now().strftime('%Y') super().__init__(*args, context, **kwargs)
See https://docs.python.org/3/library/string.html#custom-string-formatting for details on formatting options. Anything possible in an f-string or .format should work here.
- __init__(id, *args, trailing_dots=True, context={}, **kwargs)[source]
Initialize the processor.
- Parameters:
Note
The
nameparameter is deprecated and will be removed in version 2.0. Useidinstead.
- process_source_and_target_zones(desired, existing, provider, lenient=False)[source]
Process both desired and existing zones before computing changes.
Called just prior to computing changes for the target provider between
desiredandexisting. Provides an opportunity for the processor to modify either or both zones that will be used to compute the changes and create the initial plan.- Parameters:
desired (octodns.zone.Zone) – The desired zone state after all source processing.
existing (octodns.zone.Zone) – The existing zone state after all target processing.
target (octodns.provider.base.BaseProvider) – The target provider for which changes will be computed.
lenient (bool) – When True, relaxed validation rules should be applied when modifying zone records.
- Returns:
A tuple of (desired, existing) zones, typically the same objects passed in.
- Return type:
tuple[octodns.zone.Zone, octodns.zone.Zone]
Important
Will see
desiredafter any modifications done byProvider._process_desired_zoneand all processors viaProcessor.process_source_zone.Will see
existingafter any modifications done by all processors viaProcessor.process_target_zone.Will see both
desiredandexistingafter any modifications done by any processors configured to run before this one viaProcessor.process_source_and_target_zones.May modify
desireddirectly.Must return
desiredwhich will normally be thedesiredparam.May modify
existingdirectly.Must return
existingwhich will normally be theexistingparam.Must not modify records directly;
record.copyshould be called, the results of which can be modified, and thenZone.add_recordmay be used withreplace=True.May call
Zone.remove_recordto remove records fromdesired.May call
Zone.remove_recordto remove records fromexisting.Implementations should combine
self.lenient or lenientand pass the result to any record and zone calls that acceptlenientas a parameter, e.g.zone.add_record(..., lenient=lenient).