Jinja2 Templating Overview

Certain actions within the product have the ability to leverage Jinja2 templating in the message body. This enables Bot authors to insert a variety of useful data about resources into the message. As of this writing, the following actions contain support for this capability:

  • Send Delayed Email (see example messages)
  • Post Request To URL
  • Send HipChat Message
  • Send Slack Message
  • Set Container Policy (see example policies)

When using these actions, you can access resource properties by using the following syntax:

{{resource.<attribute>}} or {{resource.<function>}}

Resource Attributes

Available attributes vary based on the resource type (E.g. instances). A listing of available attributes based on the resource type can be found by clicking the links below:

Resource Functions

A list of functions that you can use is listed below:

{{resource.get_resource_name()}}

This walks the resource to the database object and returns the name. For resources which do not have a name, the primary key value is returned. With public IP addresses, for example, the resource’s IP address will be returned.

{{resource.get_organization_service_name()}}

{{resource.get_organization_service().account_id}}

These walk the resource to the parent organization service (cloud account) to retrieve the name and account number assigned to the account (E.g. Acmecorp AWS Production and 123456789012)

{{resource.get_owner_name()}}

DivvyCloud has a concept of assigning ownership to one or more resources. Doing this not only grants full permission to that resource, but delegates a point of contact (POC) to reach out to for questions on the resource. This function will return the name of the POC if one is assigned, and None if one is not.

{{resource.get_daily_cost()}}

Calculate the daily cost for the resource in question. If the resource does not support cost calculation then this function will return None.

{{resource.get_monthly_cost()}}

Calculate the monthly cost for the resource in question. If the resource does not support cost calculation then this function will return None.

{{resource.get_tag_value(tag_key, case_insensitive=True, default_return_str=None)}}

Get the value of a particular tag key. Only the tag key is required; however, you can optionally set the case_insensitive and default_return_str kwargs to force case sensitivity and/or return a specific value if the tag key does not exist.

Putting It All Together

Let’s take a look at a couple examples. Let’s configure an the Send Delayed Email notification to send an Email to the admin team when a new instance is provisioned. This will provide line of sight on any new resource that can impact the monthly bill. The image below shows how to add a customized message body for each new instace.

../_images/send_email.png

Next let’s configure the Slack notification to send a customized message when new volumes are provisioned. The message will contain the account name and availability zone where the volume resides.

../_images/slack.png