permalink |
---|
/ |
local tf = import "github.com/tf-libsonnet/core/main.libsonnet"
tf
implements core utility functions for generating Terraform code.
fn isStringArray(v)
fn mergeAll(objs)
fn objItems(obj)
fn objItemsAll(obj)
fn withData(type, label, attrs, _meta={})
fn withLocal(name, value)
fn withLocalList(locals)
fn withLocalMap(map)
fn withModule(name, source, inpuuts, version='null', _meta={})
fn withOutput(name, value, description='null', sensitive='null')
fn withOutputList(outputs)
fn withOutputMap(map)
fn withProvider(name, attrs, alias='null', src='null', version='null')
fn withResource(type, label, attrs, _meta={})
fn withSensitiveOutputMap(map)
fn withVariable(name, isRequired=true, type='null', description='null', default='null')
obj meta
fn new(count='null', depends_on='null', for_each='null', provider='null', lifecycle='null', connection='null', provisioner='null')
fn newForModule(count='null', depends_on='null', for_each='null', providers='null')
obj meta.lifecycle
obj meta.provisioner
fn newFile(destination, source='null', content='null', connection='null', when='null', on_failure='null')
fn newLocalExec(command, working_dir='null', interpreter='null', environment='null', when='null', on_failure='null')
fn newRemoteExec(inline='null', script='null', scripts='null', connection='null', when='null', on_failure='null')
obj meta.provisioner.connection
isStringArray(v)
tf.isStringArray
returns true
if the given value is an array with all elements as string.
Args:
v
(any
): The value being evaluated.
Returns:
- A boolean indicating whether the given arg is a string array.
mergeAll(objs)
tf.mergeAll
takes a list of objects and merges them together into a single object, processing the objects from left
to right. This is useful when you want to create many Terraform resources using list comprehension.
Args:
objs
(list[obj]
): List of objects to be merged into one.
Returns:
- A single object that is the result of merging all the input objects together.
objItems(obj)
tf.objItems
takes an object and returns a list of objects with two attributes:
k
: The object key being iterated.v
: The object value being iterated.
This is useful when iterating the keys and values of an object. For example, if you had the object:
{
one: 1,
two: 2,
three: 3,
}
You can create iterate the fields like so:
[i.k + ' = ' + i.v for i in objItems(obj)]
Args:
obj
(obj
): The object whose fields and values to iterate.
Returns:
- A list of objects with attributes k and v to denote the object keys and values.
objItemsAll(obj)
tf.objItemsAll
is like objItems
, but also includes hidden fields.
Args:
obj
(obj
): The object whose fields and values to iterate.
Returns:
- A list of objects with attributes k and v to denote the object keys and values.
withData(type, label, attrs, _meta={})
tf.withData
injects a new Terraform data
source block into the root configuration.
Additionally, this inserts a private function into the _ref attribute that generates references to attributes of the data source. For example, if you added a new data source using:
withData('azurerm_virtual_network', 'foo', {})
You can get the reference to the id
field of the azurerm_virtual_network
using the reference:
$._ref.azurerm_virtual_network.foo.get('id')
NOTE: When chaining and merging multiple calls to withResource, withData, and
withModule, you may not be able to use super
, self
, or $
to get the reference to _ref
.
Instead, make an explicit binding to the outer object using local
.
Args:
type
(string
): The data source type to create (e.g.,aws_instance
,local_file
, etc).label
(string
): The label to apply to the instance of the data source.attrs
(obj
): The attributes for the instance of the data source being created._meta
(obj
): An optional meta-argument object that (seemeta.libsonnet
). Note that while technically you can set the meta-arguments on theattrs
object, it is recommended to use the_meta
arg to highlight the meta-arguments.
Returns:
- A mixin object that injects the new data source into the root Terraform configuration.
withLocal(name, value)
tf.withLocal
injects a new Terraform local
definition into the root configuration.
Args:
name
(string
): The name of thelocal
to define.value
(any
): The value to bind to thelocal
.
Returns:
- A mixin object that injects the new local into the root Terraform configuration.
withLocalList(locals)
tf.withLocalList
injects the list of local configurations as Terraform local
definitions in the root
configuration.
Args:
locals
(list[obj]
): List of local configurations, where each element describes alocal
. Each element should have the keysn
(forname
) andv
(forvalue
).
Returns:
- A mixin object that injects all the locals into the Terraform config.
withLocalMap(map)
tf.withLocalMap
injects all the key value pairs of the input map as Terraform local
definitions in the root
configuration.
Args:
map
(map[str, str]
): Map of local keys to local values.
Returns:
- A mixin object that injects all the key value pairs as locals.
withModule(name, source, inpuuts, version='null', _meta={})
tf.withModule
injects a new module
block into the root configuration.
Additionally, this inserts a private function into the _ref attribute that generates references to attributes of the module call. For example, if you added a new module call using:
withModule('foo', 'some-source', {})
You can get the reference to the id
output using the reference:
$._ref.module.foo.get('id')
NOTE: When chaining and merging multiple calls to withResource, withData, and
withModule, you may not be able to use super
, self
, or $
to get the reference to _ref
.
Instead, make an explicit binding to the outer object using local
.
Args:
name
(string
): The name of the module block.source
(string
): The source for the module block.inputs
(obj
): The input values to pass into the module block.version
(string
): The version of the module source to pull in, if the module source references a registry. Whennull
, theversion
field is omitted from the resulting module block._meta
(obj
): An optional meta-argument object that (seemeta.libsonnet
). Note that while technically you can set the meta-arguments on theinputs
object, it is recommended to use the_meta
arg to highlight the meta-arguments.
Returns:
- A mixin object that injects the new module block into the root Terraform configuration.
withOutput(name, value, description='null', sensitive='null')
tf.withOutput
injects a new Terraform output
block into the root configuration.
Args:
name
(string
): The name of the output.value
(string
): The expression to bind to the output name.description
(string
): The description of the output. Whennull
, thedescription
field is omitted from the object.sensitive
(bool
): Whether the output contains sensitive information. Whennull
, thesensitive
field is omitted from the object.
Returns:
- A mixin object that injects the new output into the root Terraform configuration.
withOutputList(outputs)
withOutputList
injects the list of output configurations as Terraform output
blocks into the root
configuration.
Args:
outputs
(list[obj]
): List of output configurations, where each element describes anoutput
block. Each element should have the keysn
(forname
),v
(forvalue
),d
(fordescription
), ands
(forsensitive
).
Returns:
- A mixin object that injects all the outputs as output blocks.
withOutputMap(map)
tf.withOutputMap
injects all the key value pairs of the input map as Terraform output
blocks into the root
configuration.
Args:
map
(map[str, str]
): Map of output keys to output values.
Returns:
- A mixin object that injects all the key value pairs as output blocks.
withProvider(name, attrs, alias='null', src='null', version='null')
tf.withProvider
injects a new Terraform provider
block into the root configuration
Args:
name
(string
): The name of the provider to inject.attrs
(obj
): The attributes to apply to the provider block being injected.alias
(string
): Thealias
to bind to the provider block. Whennull
, thealias
attribute is omitted from the provider attributes.src
(string
): Where to source the provider. If specified, an entry torequired_providers
will be added specifying the source. If bothsrc
andversion
isnull
, the required_providers entry is omitted.version
(string
): Whatversion
of the provider to use. If specified, an entry torequired_providers
will be added specifying the version. If bothsrc
andversion
isnull
, therequired_providers
entry is omitted.
Returns:
- A mixin object that injects the new provider block into the root Terraform configuration.
withResource(type, label, attrs, _meta={})
tf.withResource
injects a new Terraform resource
block into the root configuration.
Additionally, this inserts a private function into the _ref attribute that generates references to attributes of the resource. For example, if you added a new resource using:
withResource('null_resource', 'foo', {})
You can get the reference to the id
field of the null_resource
using the reference:
$._ref.null_resource.foo.get('id')
NOTE: When chaining and merging multiple calls to withResource, withData, and
withModule, you may not be able to use super
, self
, or $
to get the reference to _ref
.
Instead, make an explicit binding to the outer object using local
.
Args:
type
(string
): The resource type to create (e.g.,aws_instance
,null_resource
, etc).label
(string
): The label to apply to the instance of the resource.attrs
(obj
): The attributes for the instance of the resource being created._meta
(obj
): An optional meta-argument object that (seemeta.libsonnet
). Note that while technically you can set the meta-arguments on theattrs
object, it is recommended to use the_meta
arg to highlight the meta-arguments.
Returns:
- A mixin object that injects the new resource into the root Terraform configuration.
withSensitiveOutputMap(map)
tf.withSensitiveOutputMap
injects all the key value pairs of the input map as Terraform output
blocks with
sensitive
set to true
into the root configuration.
Args:
map
(map[str, str]
): Map of output keys to output values.
Returns:
- A mixin object that injects all the key value pairs as output blocks.
withVariable(name, isRequired=true, type='null', description='null', default='null')
tf.withVariable
injects a new Terraform variable
block into the root configuration.
Args:
name
(string
): The name of the variable.isRequired
(bool
): Whether the variable is required. Whentrue
, thedefault
value is omitted from the object.type
(string
): The type of the variable. Whennull
, thetype
field is omitted from the object.description
(string
): The description of the variable. Whennull
, thedescription
field is omitted from the object.default
(any
): The default value of the variable. Omitted whenisRequired
istrue
.
Returns:
- A mixin object that injects the new variable into the root Terraform configuration.
new(count='null', depends_on='null', for_each='null', provider='null', lifecycle='null', connection='null', provisioner='null')
tf.meta.new
will generate an object that can be mixed into any resource or data source to set the Terraform meta
arguments. This can be passed in as the _meta
parameter for any call to [tf.withResource](#fn-withresource] or
tf.withData.
Note that this is for resources and data sources. If you wish to generate meta-arguments for a module block, use the tf.meta.newForModule function.
Refer to the meta-arguments tab on the Terraform language reference for more information.
Args:
count
(number
): Set thecount
meta-argument on the block. Whennull
, thecount
attribute is omitted.depends_on
(list[str]
orstring
): Set thedepends_on
meta-argument on the block. Whennull
, thedepends_on
attribute is omitted. This can be a string if using interpolation in the resulting Terraform.for_each
(map[str, any]
,list[any]
orstring
): Set thefor_each
meta-argument on the block. Whennull
, thefor_each
attribute is omitted. This can be a string if using interpolation in the resulting Terraform.provider
(string
): Set theprovider
meta-argument on the block. Whennull
, theprovider
attribute is omitted.lifecycle
(obj
): Set thelifecycle
meta-argument block on the resulting block. Whennull
, thelifecycle
block is omitted. It is recommended to generate this using tf.meta.lifecycle.new.connection
(obj
): Set theconnection
meta-argument blocks on the resulting block. Whennull
, there will be noprovisioner
blocks added. It is recommended to generate this using in tf.meta.provisioner.connection.new.provisioner
(list[obj]
): Set the list ofprovisioner
meta-argument blocks on the resulting block. Whennull
, there will be noprovisioner
blocks added. It is recommended to generate this using functions in tf.meta.provisioner.
Returns:
- A mixin that can be merged with a resource or data source object to set meta-arguments.
newForModule(count='null', depends_on='null', for_each='null', providers='null')
tf.meta.newForModule
will generate an object that can be mixed into any module block to set the Terraform meta
arguments. This can be passed in as the _meta
parameter for any call to [tf.withModule](#fn-withmodule].
Note that this is for module calls. If you wish to generate meta-arguments for a resource or data source block, use the tf.meta.new function.
Refer to the meta-arguments tab on the Terraform language reference for more information.
Args:
count
(number
): Set thecount
meta-argument on the block. Whennull
, thecount
attribute is omitted.depends_on
(list[str]
orstring
): Set thedepends_on
meta-argument on the block. Whennull
, thedepends_on
attribute is omitted. This can be a string if using interpolation in the resulting Terraform.for_each
(map[str, any]
,list[any]
orstring
): Set thefor_each
meta-argument on the block. Whennull
, thefor_each
attribute is omitted. This can be a string if using interpolation in the resulting Terraform.providers
(map[str, str]
): Set theproviders
meta-argument on the block. Whennull
, theproviders
attribute is omitted.
Returns:
- A mixin that can be merged with a resource or data source object to set meta-arguments.
new(create_before_destroy='null', prevent_destroy='null', ignore_changes='null', replace_triggered_by='null', precondition='null', postcondition='null')
tf.meta.lifecycle.new
will generate a new lifecycle
block.
Note that unlike the other functions, this includes type checking due to the Terraform requirement that the lifecycle block only supports literal values only. As such, it is easier to do a type check on the args since there is no possibility to use complex Terraform expressions (which will reduce to a string type in jsonnet).
Args:
create_before_destroy
(bool
): Setcreate_before_destroy
on the block. Whennull
, thecreate_before_destroy
attribute is omitted.prevent_destroy
(bool
): Setprevent_destroy
on the block. Whennull
, theprevent_destroy
attribute is omitted.ignore_changes
(list[str]
): Setignore_changes
on the block. Whennull
, theignore_changes
attribute is omitted.replace_triggered_by
(list[str]
): Setreplace_triggered_by
on the block. Whennull
, thereplace_triggered_by
attribute is omitted.precondition
(list[object]
): Setprecondition
subblocks on the block. Whennull
, noprecondition
subblocks will be rendered. It is recommended to construct this using tf.meta.lifecycle.condition.new.postcondition
(list[object]
): Setpostcondition
subblocks on the block. Whennull
, nopostcondition
subblocks will be rendered. It is recommended to construct this using tf.meta.lifecycle.condition.new.
Returns:
- A mixin that can be merged with a meta-argument block to set the
lifecycle
block.
new(condition, error_message)
tf.meta.lifecycle.condition.new
will generate a new condition
block that can be used as part of precondition
or postcondition
in the lifecycle
block.
Args:
condition
(string
): Set thecondition
attribute on the block. This should be a Terraform expression escaped with${}
.error_message
(string
): Set theerror_message
attribute on the block.
Returns:
- An object that can be used as a
precondition
orpostcondition
subblock for alifecycle
block.
newFile(destination, source='null', content='null', connection='null', when='null', on_failure='null')
tf.meta.provisioner.newFile
will generate a new file
provisioner block that can be used as part of
the provisioner
meta-argument block.
Args:
source
(str
): The source file or directory to copy. Exactly one ofsource
orcontent
must be provided.content
(str
): The direct content to copy to the destination. Exactly one ofsource
orcontent
must be provied.destination
(str
): The destination path to write on the remote system.connection
(obj
): Set theconnection
meta-argument blocks on the resulting block. Whennull
, there will be noprovisioner
blocks added. It is recommended to generate this using in tf.meta.provisioner.connection.new.when
(str
): Specifies when Terraform should run the command. Can only be set to"destroy"
. Whennull
, the attribute is omitted from the resulting block.on_failure
(str
): Modify how Terraform handles errors in the underlying command. Must be one of"continue"
or"fail"
. Whennull
, the attribute is omitted from the resulting block.
Returns:
- An object that can be used as a
provisioner
meta-argument block in theprovisioner
block list oftf.meta.new
.
newLocalExec(command, working_dir='null', interpreter='null', environment='null', when='null', on_failure='null')
tf.meta.provisioner.newLocalExec
will generate a new local-exec
provisioner block that can be used as part of
the provisioner
meta-argument block.
Args:
command
(str
): The command to execute on the operator machine as part of the resource lifecycle.working_dir
(str
): The working directory wherecommand
will be executed. Whennull
, the attribute is omitted from the resulting block.interpreter
(list[str]
): The list of interpreter arguments used to execute the command. Whennull
, the attribute is omitted from the resulting block.environment
(map[str, str]
): Map of key-value pairs representing the environment variables that should be set. Whennull
, the attribute is omitted from the resulting block.when
(str
): Specifies when Terraform should run the command. Can only be set to"destroy"
. Whennull
, the attribute is omitted from the resulting block.on_failure
(str
): Modify how Terraform handles errors in the underlying command. Must be one of"continue"
or"fail"
. Whennull
, the attribute is omitted from the resulting block.
Returns:
- An object that can be used as a
provisioner
meta-argument block in theprovisioner
block list oftf.meta.new
.
newRemoteExec(inline='null', script='null', scripts='null', connection='null', when='null', on_failure='null')
tf.meta.provisioner.newRemoteExec
will generate a new remote-exec
provisioner block that can be used as part of
the provisioner
meta-argument block.
Args:
inline
(list[str]
): The list of commands to execute on the remote machine as part of the resource lifecycle. Exactly one ofinline
,script
, orscripts
must be provied.script
(str
): The path to a local script that will be copied to the remote machine and then executed. Exactly one ofinline
,script
, orscripts
must be provied.scripts
(list[str]
): The list of paths to local scripts that will be copied to the remote machine and then executed. Exactly one ofinline
,script
, orscripts
must be provied.connection
(obj
): Set theconnection
meta-argument blocks on the resulting block. Whennull
, there will be noprovisioner
blocks added. It is recommended to generate this using in tf.meta.provisioner.connection.new.when
(str
): Specifies when Terraform should run the command. Can only be set to"destroy"
. Whennull
, the attribute is omitted from the resulting block.on_failure
(str
): Modify how Terraform handles errors in the underlying command. Must be one of"continue"
or"fail"
. Whennull
, the attribute is omitted from the resulting block.
Returns:
- An object that can be used as a
provisioner
meta-argument block in theprovisioner
block list oftf.meta.new
.
new(host, type='null', user='null', password='null', port='null', timeout='null', script_path='null', private_key='null', certificate='null', agent='null', agent_identity='null', host_key='null', target_platform='null', bastion_host='null', bastion_host_key='null', bastion_port='null', bastion_user='null', bastion_password='null', bastion_private_key='null', bastion_certificate='null', https='null', insecure='null', use_ntlm='null', cacert='null')
tf.meta.provisioner.connection.new
will generate a new connection
block that can be used as part of
the provisioner
meta-argument block.
Args:
host
(str
): The address of the remote resource to connect to.type
(str
): The connection type to use when connecting to the instance. Must be one of"ssh"
or"winrm"
. Whennull
, the attribute is omitted from the resulting block and defaults to what Terraform has set internally ("ssh"
).user
(str
): The user to use for the connection. Whennull
, the attribute is omitted from the resulting block and defaults to what Terraform has set internally.password
(str
): The password to use for the connection. Whennull
, the attribute is omitted from the resulting block.port
(number
): The port to use for the connection. Whennull
, the attribute is omitted from the resulting block and defaults to what Terraform has set internally.timeout
(str
): The timeout to wait for the connection. Whennull
, the attribute is omitted from the resulting block and defaults to what Terraform has set internally ("5m"
).script_path
(str
): The path used to copy scripts meant for remote execution. Whennull
, the attribute is omitted from the resulting block.private_key
(str
): The contents of an SSH key to use for the connection. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"ssh"
ornull
.certificate
(str
): The contents of a signed CA certificate to be used in conjunction with theprivate_key
arg. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"ssh"
ornull
.agent
(bool
): Whether to use thessh-agent
for authenticating. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"ssh"
ornull
.agent_identity
(str
): The preferred identity from the ssh agent to use for authentication. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"ssh"
ornull
.host_key
(str
): The public key from the remote host or the signing CA. This is used to verify the connection. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"ssh"
ornull
.target_platform
(str
): The target platform to connect to. Must be one of"unix"
or"windows"
. Whennull
, the attribute is omitted from the resulting block and defaults to what Terraform has set internally ("unix"
). Can only be set whentype
is"ssh"
ornull
.bastion_host
(str
): The address of a bastion host to hop the connection through. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"ssh"
ornull
.bastion_host_key
(str
): The public key from the bastion host or the signing CA. This is used to verify the connection. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"ssh"
ornull
.bastion_port
(number
): The port to use for the bastion connection. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"ssh"
ornull
.bastion_user
(str
): The user to use for the bastion connection. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"ssh"
ornull
.bastion_password
(str
): The password to use for the bastion connection. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"ssh"
ornull
.bastion_private_key
(str
): The contents of an SSH key file to use for the bastion connection. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"ssh"
ornull
.bastion_certificate
(str
): The contents of a signed CA certificate to be used in conjunction with thebastion_private_key
arg. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"ssh"
ornull
.https
(bool
): Whether to connect using HTTPS as opposed to HTTP. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"winrm"
.insecure
(bool
): Whether to skip validation of the HTTPS certificate chain. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"winrm"
.use_ntlm
(bool
): Whether to use NTLM authentication. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"winrm"
.cacert
(str
): The CA certificate to validate against. Whennull
, the attribute is omitted from the resulting block. Can only be set whentype
is"winrm"
.
Returns:
- An object that can be used as a
connection
sub block on anyremote-exec
orfile
provisioner
meta-argument block.