charmhelpers.core.services.helpers

HttpRelation Relation context for the http interface.
MysqlRelation Relation context for the mysql interface.
RelationContext Base class for a context generator that gets relation data from juju.
RequiredConfig Data context that loads config options with one or more mandatory options.
StoredContext A data context that always returns the data that it was first created with.
TemplateCallback Callback class that will render a Jinja2 template, for use as a ready action.
render_template alias of charmhelpers.core.services.helpers.TemplateCallback
template alias of charmhelpers.core.services.helpers.TemplateCallback
class charmhelpers.core.services.helpers.RelationContext(name=None, additional_required_keys=None)

Bases: dict

Base class for a context generator that gets relation data from juju.

Subclasses must provide the attributes name, which is the name of the interface of interest, interface, which is the type of the interface of interest, and required_keys, which is the set of keys required for the relation to be considered complete. The data for all interfaces matching the name attribute that are complete will used to populate the dictionary values (see get_data, below).

The generated context will be namespaced under the relation name, to prevent potential naming conflicts.

Parameters:
  • name (str) – Override the relation name, since it can vary from charm to charm
  • additional_required_keys (list) – Extend the list of required_keys
get_data()

Retrieve the relation data for each unit involved in a relation and, if complete, store it in a list under self[self.name]. This is automatically called when the RelationContext is instantiated.

The units are sorted lexographically first by the service ID, then by the unit ID. Thus, if an interface has two other services, ‘db:1’ and ‘db:2’, with ‘db:1’ having two units, ‘wordpress/0’ and ‘wordpress/1’, and ‘db:2’ having one unit, ‘mediawiki/0’, all of which have a complete set of data, the relation data for the units will be stored in the order: ‘wordpress/0’, ‘wordpress/1’, ‘mediawiki/0’.

If you only care about a single unit on the relation, you can just access it as {{ interface[0][‘key’] }}. However, if you can at all support multiple units on a relation, you should iterate over the list, like:

{% for unit in interface -%}
    {{ unit['key'] }}{% if not loop.last %},{% endif %}
{%- endfor %}

Note that since all sets of relation data from all related services and units are in a single list, if you need to know which service or unit a set of data came from, you’ll need to extend this class to preserve that information.

interface = None
is_ready()

Returns True if all of the required_keys are available from any units.

name = None
provide_data()

Return data to be relation_set for this interface.

class charmhelpers.core.services.helpers.TemplateCallback(source, target, owner='root', group='root', perms=292, on_change_action=None, template_loader=None)

Bases: charmhelpers.core.services.base.ManagerCallback

Callback class that will render a Jinja2 template, for use as a ready action.

Parameters:
  • source (str) – The template source file, relative to $CHARM_DIR/templates
  • target (str) – The target to write the rendered template to (or None)
  • owner (str) – The owner of the rendered file
  • group (str) – The group of the rendered file
  • perms (int) – The permissions of the rendered file
  • on_change_action (partial) – functools partial to be executed when rendered file changes
  • loader template_loader (jinja2) – A jinja2 template loader
Return str:

The rendered template

charmhelpers.core.services.helpers.render_template

alias of charmhelpers.core.services.helpers.TemplateCallback

charmhelpers.core.services.helpers.template

alias of charmhelpers.core.services.helpers.TemplateCallback