Resource Functions

Jumppad configuration files can use built in functions which are interpolated at runtime. For example the env([environment variable name]) function could be used to return the value of an environment variable.

env

The env function can be used to interpolate a system set environment variable inside your configuration.

Parameters

  • Name
    name
    Type
    (string: "")
    Required
    required
    Readonly
    Description

    The name of the environment variable to return.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "./mydestionation.txt"

  variables = {
    path = env("PATH")
  }
}

len

The len function returns the lenght of a string or an array.

Parameters

  • Name
    value
    Type
    (interface: )
    Required
    required
    Readonly
    Description

    The string or array to return the length of

variable "myarray" {
  value = [1,2,3]
}

variable "mystring" {
  value = "string"
}

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "./mydestionation.txt"

  variables = {
    array_length = len(variable.myarray) // 3
    string_length = len(variable.mystring) // 6
  }
}

home

The home function returns the location of the current users HOME folder.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "./mydestionation.txt"

  variables = {
    home = home()
  }
}

file

The file function returns the contents of a file at the given path.

Parameters

  • Name
    path
    Type
    (interface: )
    Required
    required
    Readonly
    Description

    The path of the file to read.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "./mydestionation.txt"

  variables = {
    contents = file("myfile.txt")
  }
}

dir

The dir function returns the directory of the file containing the current resource.

resource "template" "mine" {
  source = "${dir()}/mysource.txt"
  destination = "./mydestination.txt"
}

trim

The trim function removes whitespace from the given string.

Parameters

  • Name
    value
    Type
    (string: "")
    Required
    required
    Readonly
    Description

    The string to trim whitespace from.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "./mydestionation.txt"

  variables = {
    contents = trim(file("myfile.txt"))
  }
}

jumppad

The jumppad function returns the location of the .jumppad folder.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "./mydestination.txt"

  variables = {
    location = jumppad()
  }
}

docker_ip

The docker_ip function returns the ip address of the configured docker host.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "./mydestination.txt"

  variables = {
    url = "${docker_ip()}:8080"
  }
}

docker_host

The docker_host function returns the socket or TCP address for the current Docker host.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "./mydestination.txt"

  variables = {
    DOCKER_HOST = docker_host
  }
}

data

The data function creates a temporary folder that exists until jumppad down is executed. Data folders are created with the permissions 755.

First use of data creates the folder inside of .jumppad/data, subsequent uses return the path.

Parameters

  • Name
    name
    Type
    (string: "")
    Required
    required
    Readonly
    Description

    Name of the data folder to create, or path to return.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "${data("temp")}/file.txt"

  variables = {
    DOCKER_HOST = docker_host
  }
}

data_with_permissions

The data_with_permissions function creates a temporary folder that exists until jumppad down is executed. Unlike data, data_with_permissions allows you to specify the directory permissions as a parameter.

First use of data_with_permissions creates the folder inside of .jumppad/data, subsequent uses return the path.

Parameters

  • Name
    name
    Type
    (string: "")
    Required
    required
    Readonly
    Description

    Name of the data folder to create, or path to return.

  • Name
    permissions
    Type
    (int: )
    Required
    required
    Readonly
    Description

    Name of the data folder to create, or path to return.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "${data_with_permissions("temp", 644)}/file.txt"

  variables = {
    DOCKER_HOST = docker_host
  }
}

template_file

Returns the rendered contents of a template file at the given path with the given input variables.

Templates can leverage the Handlebars templating language, more details on Handlebars can be found at the following link:

https://handlebarsjs.com/

Parameters

  • Name
    path
    Type
    (string: "")
    Required
    required
    Readonly
    Description

    Name of the data folder to create, or path to return.

  • Name
    variables
    Type
    (map[string]interface: map[]{})
    Required
    required
    Readonly
    Description

    Name of the data folder to create, or path to return.

#given a file "./mytemplate.tmpl" with the contents "hello {{name}}"

mytype "test" {
  // my_file = "foobar"
  my_file = template_file("./mytemplate.tmpl", {
    name = "world"
  })
}

Template Helpers

The template_file function provides helpers that can be used inside your templates as shown in the example below.

resource "template" "consul_config" {

  source = <<-EOF

  file_content = "{{ file "./myfile.txt" }}"
  quote = {{quote something}} 
  trim = {{quote (trim with_whitespace)}}

  EOF

  destination = "./consul_config/consul.hcl"
}

quote [string]

Returns the original string wrapped in quotations, quote can be used with the Go template pipe modifier.

// given the string abc

quote "abc" // would return the value "abc"

trim [string]

Removes whitespace such as carrige returns and spaces from the begining and the end of the string, can be used with the Go template pipe modifier.

// given the string abc

trim " abc " // would return the value "abc"