block

This is used to create Terraform configuration blocks from within pretf_blocks() functions in *.tf.py files. Blocks must be yielded to be included in the generated JSON files.

Blocks are the most fundamental part of Terraform configuration. Read the documentation to learn more about blocks.

Signature:

def block(block_type: str, *labels: str, body: Optional[dict] = None) -> Block

block_type:
    block type such as "resource", "variable", "provider"
labels:
    labels for the block
body:
    the body of the block

returns:
    configuration block

Example:

from pretf.api import block


def pretf_blocks():

    # The group resource is defined in another file,
    # but we want to reference it here, so we can
    # create a block with an empty body. It is not
    # yielded so it won't be included in the JSON.
    group = block("resource", "aws_iam_group", "example", {})

    # Create and yield a block to include it in the JSON.
    user = yield block("resource", "aws_iam_user", "example", {
        "name": "example",
    })

    # Create and yield another block, this time demonstrating
    # how block attributes can be accessed. The resulting JSON
    # will contain Terraform references like:
    #   "users": "${aws_iam_user.example.name}",
    #   "groups": ["${aws_iam_group.example.name}"]
    yield block("resource", "aws_iam_user_group_membership", "example", {
        "user": user.name,
        "groups": [group.name]
    })

get_outputs

Runs pretf output in the specified directory and returns the values. If the path is not anchored (i.e. does not start with ./ or ../ or /) then it will check the current directory and all parent directories until found.

Signature:

def get_outputs(cwd: Union[Path, str], verbose: Optional[bool] = None) -> dict:

cwd:
    directory where Pretf/Terraform will run
verbose:
    whether to print information

returns:
    output values

Example:

from pretf.api import get_outputs


def pretf_variables():
    vpc_outputs = get_outputs("vpc")
    yield {
        "vpc_id": vpc_outputs["vpc_id"],
        "vpc_cidr_block": vpc_outputs["vpc_cidr_block"],
    }

log

log.accept

Prompts the user to enter "yes" or "no". Returns True if the response was "yes", otherwise False. Pressing Ctrl-C counts as "no".

Signature:

def accept(message: Any) -> bool:

message:
    the message to display

returns:
    whether the user entered "yes"

Example:

from pretf.api import log


def pretf_workflow():
    if log.accept("do you wish to continue?"):
        print("user accepted the prompt")
    else:
        print("user did not accept the prompt")

log.bad

Displays a message prefixed with [pref] in red. Can be raised as an exception to display the message and then exit.

Signature:

def bad(message: Any) -> None:

message:
    the message to display

returns:
    None

Example:

from pretf.api import log


def pretf_workflow():
    log.bad("something bad happened")

log.ok

Displays a message prefixed with [pref] in cyan. Can be raised as an exception to display the message and then exit.

Signature:

def ok(message: Any) -> None:

message:
    the message to display

returns:
    None

Example:

from pretf.api import log


def pretf_workflow():
    log.bad("something normal happened")