Skip to content

Commit b999f96

Browse files
authored
Add support for setting provisioner and connection blocks (#30)
Signed-off-by: Yoriyasu Yano <[email protected]> Signed-off-by: tflibsonnet-ci <[email protected]> Co-authored-by: yorinasub17 <[email protected]>
1 parent 4ff9055 commit b999f96

File tree

3 files changed

+792
-6
lines changed

3 files changed

+792
-6
lines changed

docs/README.md

Lines changed: 173 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@ local tf = import "github.com/tf-libsonnet/core/main.libsonnet"
2929
* [`fn withResource(type, label, attrs, _meta={})`](#fn-withresource)
3030
* [`fn withVariable(name, isRequired=true, type='null', description='null', default='null')`](#fn-withvariable)
3131
* [`obj meta`](#obj-meta)
32-
* [`fn new(count='null', depends_on='null', for_each='null', provider='null', lifecycle='null')`](#fn-metanew)
32+
* [`fn new(count='null', depends_on='null', for_each='null', provider='null', lifecycle='null', connection='null', provisioner='null')`](#fn-metanew)
3333
* [`fn newForModule(count='null', depends_on='null', for_each='null', providers='null')`](#fn-metanewformodule)
3434
* [`obj meta.lifecycle`](#obj-metalifecycle)
3535
* [`fn new(create_before_destroy='null', prevent_destroy='null', ignore_changes='null', replace_triggered_by='null', precondition='null', postcondition='null')`](#fn-metalifecyclenew)
3636
* [`obj meta.lifecycle.condition`](#obj-metalifecyclecondition)
3737
* [`fn new(condition, error_message)`](#fn-metalifecycleconditionnew)
38+
* [`obj meta.provisioner`](#obj-metaprovisioner)
39+
* [`fn newFile(destination, source='null', content='null', connection='null', when='null', on_failure='null')`](#fn-metaprovisionernewfile)
40+
* [`fn newLocalExec(command, working_dir='null', interpreter='null', environment='null', when='null', on_failure='null')`](#fn-metaprovisionernewlocalexec)
41+
* [`fn newRemoteExec(inline='null', script='null', scripts='null', connection='null', when='null', on_failure='null')`](#fn-metaprovisionernewremoteexec)
42+
* [`obj meta.provisioner.connection`](#obj-metaprovisionerconnection)
43+
* [`fn 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')`](#fn-metaprovisionerconnectionnew)
3844

3945
## Fields
4046

@@ -366,7 +372,7 @@ withVariable(name, isRequired=true, type='null', description='null', default='nu
366372
### fn meta.new
367373

368374
```ts
369-
new(count='null', depends_on='null', for_each='null', provider='null', lifecycle='null')
375+
new(count='null', depends_on='null', for_each='null', provider='null', lifecycle='null', connection='null', provisioner='null')
370376
```
371377

372378
`tf.meta.new` will generate an object that can be mixed into any resource or data source to set the Terraform meta
@@ -393,6 +399,12 @@ reference](https://developer.hashicorp.com/terraform/language) for more informat
393399
- `lifecycle` (`obj`): Set the `lifecycle` meta-argument block on the resulting block. When `null`, the
394400
`lifecycle` block is omitted. It is recommended to generate this using
395401
[tf.meta.lifecycle.new](#fn-metalifecyclenew).
402+
- `connection` (`obj`): Set the `connection` meta-argument blocks on the resulting block. When
403+
`null`, there will be no `provisioner` blocks added. It is recommended to generate
404+
this using in [tf.meta.provisioner.connection.new](#obj-metaprovisionerconnectionnew).
405+
- `provisioner` (`list[obj]`): Set the list of `provisioner` meta-argument blocks on the resulting block. When
406+
`null`, there will be no `provisioner` blocks added. It is recommended to generate
407+
this using functions in [tf.meta.provisioner](#obj-metaprovisioner).
396408

397409
**Returns**:
398410
- A mixin that can be merged with a resource or data source object to set meta-arguments.
@@ -484,4 +496,162 @@ or `postcondition` in the `lifecycle` block.
484496
- `error_message` (`string`): Set the `error_message` attribute on the block.
485497

486498
**Returns**:
487-
- A mixin that can be used as a `precondition` or `postcondition` subblock for a `lifecycle` block.
499+
- An object that can be used as a `precondition` or `postcondition` subblock for a `lifecycle` block.
500+
501+
502+
## obj meta.provisioner
503+
504+
505+
506+
### fn meta.provisioner.newFile
507+
508+
```ts
509+
newFile(destination, source='null', content='null', connection='null', when='null', on_failure='null')
510+
```
511+
512+
`tf.meta.provisioner.newFile` will generate a new `file` provisioner block that can be used as part of
513+
the `provisioner` meta-argument block.
514+
515+
**Args**:
516+
- `source` (`str`): The source file or directory to copy. Exactly one of `source` or `content` must be provided.
517+
- `content` (`str`): The direct content to copy to the destination. Exactly one of `source` or `content` must be provied.
518+
- `destination` (`str`): The destination path to write on the remote system.
519+
- `connection` (`obj`): Set the `connection` meta-argument blocks on the resulting block. When
520+
`null`, there will be no `provisioner` blocks added. It is recommended to generate
521+
this using in [tf.meta.provisioner.connection.new](#fn-metaprovisionerconnectionnew).
522+
- `when` (`str`): Specifies when Terraform should run the command. Can only be set to `"destroy"`. When `null`,
523+
the attribute is omitted from the resulting block.
524+
- `on_failure` (`str`): Modify how Terraform handles errors in the underlying command. Must be one of `"continue"`
525+
or `"fail"`. When `null`, the attribute is omitted from the resulting block.
526+
527+
**Returns**:
528+
- An object that can be used as a `provisioner` meta-argument block in the `provisioner` block list of `tf.meta.new`.
529+
530+
531+
### fn meta.provisioner.newLocalExec
532+
533+
```ts
534+
newLocalExec(command, working_dir='null', interpreter='null', environment='null', when='null', on_failure='null')
535+
```
536+
537+
`tf.meta.provisioner.newLocalExec` will generate a new `local-exec` provisioner block that can be used as part of
538+
the `provisioner` meta-argument block.
539+
540+
**Args**:
541+
- `command` (`str`): The command to execute on the operator machine as part of the resource lifecycle.
542+
- `working_dir` (`str`): The working directory where `command` will be executed. When `null`, the attribute is
543+
omitted from the resulting block.
544+
- `interpreter` (`list[str]`): The list of interpreter arguments used to execute the command. When `null`, the
545+
attribute is omitted from the resulting block.
546+
- `environment` (`map[str, str]`): Map of key-value pairs representing the environment variables that should be
547+
set. When `null`, the attribute is omitted from the resulting block.
548+
- `when` (`str`): Specifies when Terraform should run the command. Can only be set to `"destroy"`. When `null`,
549+
the attribute is omitted from the resulting block.
550+
- `on_failure` (`str`): Modify how Terraform handles errors in the underlying command. Must be one of `"continue"`
551+
or `"fail"`. When `null`, the attribute is omitted from the resulting block.
552+
553+
**Returns**:
554+
- An object that can be used as a `provisioner` meta-argument block in the `provisioner` block list of `tf.meta.new`.
555+
556+
557+
### fn meta.provisioner.newRemoteExec
558+
559+
```ts
560+
newRemoteExec(inline='null', script='null', scripts='null', connection='null', when='null', on_failure='null')
561+
```
562+
563+
`tf.meta.provisioner.newRemoteExec` will generate a new `remote-exec` provisioner block that can be used as part of
564+
the `provisioner` meta-argument block.
565+
566+
**Args**:
567+
- `inline` (`list[str]`): The list of commands to execute on the remote machine as part of the resource lifecycle.
568+
Exactly one of `inline`, `script`, or `scripts` must be provied.
569+
- `script` (`str`): The path to a local script that will be copied to the remote machine and then executed.
570+
Exactly one of `inline`, `script`, or `scripts` must be provied.
571+
- `scripts` (`list[str]`): The list of paths to local scripts that will be copied to the remote machine and then
572+
executed. Exactly one of `inline`, `script`, or `scripts` must be provied.
573+
- `connection` (`obj`): Set the `connection` meta-argument blocks on the resulting block. When
574+
`null`, there will be no `provisioner` blocks added. It is recommended to generate
575+
this using in [tf.meta.provisioner.connection.new](#fn-metaprovisionerconnectionnew).
576+
- `when` (`str`): Specifies when Terraform should run the command. Can only be set to `"destroy"`. When `null`,
577+
the attribute is omitted from the resulting block.
578+
- `on_failure` (`str`): Modify how Terraform handles errors in the underlying command. Must be one of `"continue"`
579+
or `"fail"`. When `null`, the attribute is omitted from the resulting block.
580+
581+
**Returns**:
582+
- An object that can be used as a `provisioner` meta-argument block in the `provisioner` block list of `tf.meta.new`.
583+
584+
585+
## obj meta.provisioner.connection
586+
587+
588+
589+
### fn meta.provisioner.connection.new
590+
591+
```ts
592+
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')
593+
```
594+
595+
`tf.meta.provisioner.connection.new` will generate a new `connection` block that can be used as part of
596+
the `provisioner` meta-argument block.
597+
598+
**Args**:
599+
- `host` (`str`): The address of the remote resource to connect to.
600+
- `type` (`str`): The connection type to use when connecting to the instance. Must be one of `"ssh"` or `"winrm"`.
601+
When `null`, the attribute is omitted from the resulting block and defaults to what Terraform
602+
has set internally (`"ssh"`).
603+
- `user` (`str`): The user to use for the connection. When `null`, the attribute is omitted from the resulting
604+
block and defaults to what Terraform has set internally.
605+
- `password` (`str`): The password to use for the connection. When `null`, the attribute is omitted from the
606+
resulting block.
607+
- `port` (`number`): The port to use for the connection. When `null`, the attribute is omitted from the resulting
608+
block and defaults to what Terraform has set internally.
609+
- `timeout` (`str`): The timeout to wait for the connection. When `null`, the attribute is omitted from the
610+
resulting block and defaults to what Terraform has set internally (`"5m"`).
611+
- `script_path` (`str`): The path used to copy scripts meant for remote execution. When `null`, the attribute is
612+
omitted from the resulting block.
613+
- `private_key` (`str`): The contents of an SSH key to use for the connection. When `null`, the attribute is
614+
omitted from the resulting block. Can only be set when `type` is `"ssh"` or `null`.
615+
- `certificate` (`str`): The contents of a signed CA certificate to be used in conjunction with the `private_key`
616+
arg. When `null`, the attribute is omitted from the resulting block. Can only be set when
617+
`type` is `"ssh"` or `null`.
618+
- `agent` (`bool`): Whether to use the `ssh-agent` for authenticating. When `null`, the attribute is omitted from
619+
the resulting block. Can only be set when `type` is `"ssh"` or `null`.
620+
- `agent_identity` (`str`): The preferred identity from the ssh agent to use for authentication. When `null`, the
621+
attribute is omitted from the resulting block. Can only be set when `type` is `"ssh"`
622+
or `null`.
623+
- `host_key` (`str`): The public key from the remote host or the signing CA. This is used to verify the
624+
connection. When `null`, the attribute is omitted from the resulting block. Can only be set
625+
when `type` is `"ssh"` or `null`.
626+
- `target_platform` (`str`): The target platform to connect to. Must be one of `"unix"` or `"windows"`. When
627+
`null`, the attribute is omitted from the resulting block and defaults to what
628+
Terraform has set internally (`"unix"`). Can only be set when `type` is `"ssh"` or
629+
`null`.
630+
- `bastion_host` (`str`): The address of a bastion host to hop the connection through. When `null`, the attribute
631+
is omitted from the resulting block. Can only be set when `type` is `"ssh"` or `null`.
632+
- `bastion_host_key` (`str`): The public key from the bastion host or the signing CA. This is used to verify the
633+
connection. When `null`, the attribute is omitted from the resulting block. Can
634+
only be set when `type` is `"ssh"` or `null`.
635+
- `bastion_port` (`number`): The port to use for the bastion connection. When `null`, the attribute is omitted
636+
from the resulting block. Can only be set when `type` is `"ssh"` or `null`.
637+
- `bastion_user` (`str`): The user to use for the bastion connection. When `null`, the attribute is omitted
638+
from the resulting block. Can only be set when `type` is `"ssh"` or `null`.
639+
- `bastion_password` (`str`): The password to use for the bastion connection. When `null`, the attribute is
640+
omitted from the resulting block. Can only be set when `type` is `"ssh"` or `null`.
641+
- `bastion_private_key` (`str`): The contents of an SSH key file to use for the bastion connection. When `null`,
642+
the attribute is omitted from the resulting block. Can only be set when `type` is
643+
`"ssh"` or `null`.
644+
- `bastion_certificate` (`str`): The contents of a signed CA certificate to be used in conjunction with the
645+
`bastion_private_key` arg. When `null`, the attribute is omitted from the
646+
resulting block. Can only be set when `type` is `"ssh"` or `null`.
647+
- `https` (`bool`): Whether to connect using HTTPS as opposed to HTTP. When `null`, the attribute is omitted from
648+
the resulting block. Can only be set when `type` is `"winrm"`.
649+
- `insecure` (`bool`): Whether to skip validation of the HTTPS certificate chain. When `null`, the attribute is
650+
omitted from the resulting block. Can only be set when `type` is `"winrm"`.
651+
- `use_ntlm` (`bool`): Whether to use NTLM authentication. When `null`, the attribute is omitted from the
652+
resulting block. Can only be set when `type` is `"winrm"`.
653+
- `cacert` (`str`): The CA certificate to validate against. When `null`, the attribute is omitted from the
654+
resulting block. Can only be set when `type` is `"winrm"`.
655+
656+
**Returns**:
657+
- An object that can be used as a `connection` sub block on any `remote-exec` or `file` `provisioner` meta-argument block.

src/_custom/meta.libsonnet

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ local newMetaDoc =
3535
- `lifecycle` (`obj`): Set the `lifecycle` meta-argument block on the resulting block. When `null`, the
3636
`lifecycle` block is omitted. It is recommended to generate this using
3737
[tf.meta.lifecycle.new](#fn-metalifecyclenew).
38+
- `connection` (`obj`): Set the `connection` meta-argument blocks on the resulting block. When
39+
`null`, there will be no `provisioner` blocks added. It is recommended to generate
40+
this using in [tf.meta.provisioner.connection.new](#obj-metaprovisionerconnectionnew).
41+
- `provisioner` (`list[obj]`): Set the list of `provisioner` meta-argument blocks on the resulting block. When
42+
`null`, there will be no `provisioner` blocks added. It is recommended to generate
43+
this using functions in [tf.meta.provisioner](#obj-metaprovisioner).
3844
3945
**Returns**:
4046
- A mixin that can be merged with a resource or data source object to set meta-arguments.
@@ -45,9 +51,11 @@ local newMetaDoc =
4551
d.arg('for_each', d.T.array, d.T.nil),
4652
d.arg('provider', d.T.string, d.T.nil),
4753
d.arg('lifecycle', d.T.object, d.T.nil),
54+
d.arg('connection', d.T.object, d.T.nil),
55+
d.arg('provisioner', d.T.array, d.T.nil),
4856
],
4957
);
50-
local newMeta(count=null, depends_on=null, for_each=null, provider=null, lifecycle=null) =
58+
local newMeta(count=null, depends_on=null, for_each=null, provider=null, lifecycle=null, connection=null, provisioner=null) =
5159
local maybeCount =
5260
if count != null then
5361
{ count: count }
@@ -78,11 +86,25 @@ local newMeta(count=null, depends_on=null, for_each=null, provider=null, lifecyc
7886
else
7987
{};
8088

89+
local maybeConnection =
90+
if connection != null then
91+
{ connection: connection }
92+
else
93+
{};
94+
95+
local maybeProvisioner =
96+
if provisioner != null then
97+
{ provisioner: provisioner }
98+
else
99+
{};
100+
81101
maybeCount
82102
+ maybeDependsOn
83103
+ maybeForEach
84104
+ maybeProvider
85-
+ maybeLifecycle;
105+
+ maybeLifecycle
106+
+ maybeConnection
107+
+ maybeProvisioner;
86108

87109

88110
local newModuleMetaDoc =
@@ -272,7 +294,7 @@ local newConditionDoc =
272294
- `error_message` (`string`): Set the `error_message` attribute on the block.
273295
274296
**Returns**:
275-
- A mixin that can be used as a `precondition` or `postcondition` subblock for a `lifecycle` block.
297+
- An object that can be used as a `precondition` or `postcondition` subblock for a `lifecycle` block.
276298
|||,
277299
[
278300
d.arg('condition', d.T.string),
@@ -301,5 +323,6 @@ local newCondition(condition, error_message) =
301323
new:: newCondition,
302324
},
303325
},
326+
provisioner:: (import './meta_provisioner.libsonnet'),
304327
},
305328
}

0 commit comments

Comments
 (0)