Developing a custom postprocessor (nonpersistent backend)¶
Postprocessors in PyTerraBackTYL allow you to perform additional actions based on lock and state information from Terraform. An example of this is the SlackNotifyPostProcessor which pushes notications into a Slack chatroom.
Note
All exceptions generated by postprocessors are logged, but ignored as they cannot be allowed to interfere with the functionality of the persistent backend. For debugging purposes, the json data from /state
endpoint includes a count of the number of errors logged for the postprocessor since startup, as well as the most recently logged error (useful for searching the logs later).
Import TYLNonpersistent
and define your subclass:
from abc_tylstore import TYLNonpersistent
class MyNonpersistentBackend(TYLNonpersistent):
def __init__(self, environment, constants, **kwargs):
.
.
.
Implement the following functions in your class:
__init__(self, environment, constants, **kwargs):
environment
every environment will get a separate instance of your class and as a result, this value should be treated as a constant.constants
a python class containing constant values. These values are partly populated byconfig.yaml
as key:value pairs. For example, if you setMY_BACKEND_FOO: ‘Hello, World!’
inconfig.yaml
, you can access the string value with constants.MY_BACKEND_FOO. This allows you to configure your postprocessor module without the need of a separate configuration file.**kwargs
It is recommended to add this to help ensure forward compatibility of your module with future versions of PyTerraBackTYL.
def on_locked(self, state_obj, **kwargs):
state_obj
Unpacked JSON (dict
) with the specifics on who is putting a lock on the environment.**kwargs
includesraw
which contains the original JSON string (str
) value.
def on_unlocked(self, state_obj, **kwargs):
state_obj
Unpacked JSON (dict
) with the specifics on who is unlocking the environment.**kwargs
includesraw
which contains the original JSON string (str
) value.
def process_tfstate(self, tfstate_obj, **kwargs):
tfstate_obj
Unpacked JSON (dict
) which specifies the current terraform state (terraform.tfstate
)**kwargs
includesraw
which contains the original JSON string (str
) value.
def post_processor_status(self):
OPTIONAL- RETURNS: A JSON compatible object containing health and status information about the postprocessor.