From 10adb7b6472022cec3abef59a56f29019c4945df Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Fri, 13 May 2022 17:36:56 +0100 Subject: [PATCH 01/27] terrabutler: click.py: Implement tf console command --- terrabutler/click.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index 657a2e7..2bb3eed 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -201,12 +201,22 @@ def tf_apply_cli(ctx, auto_approve, destroy, input, lock, lock_timeout, @tf_cli.command(name="console", help="Try Terraform expressions at an " "interactive command prompt") +@click.option("-state", help="Legacy option for the local backend only." + " See the local backend's documentation for more information.") @click.option("-var", multiple=True, help="Set a variable in the Terraform configuration. " "This flag can be set multiple times.") @click.pass_context -def tf_console_cli(ctx, var): - print(Fore.RED + "Function not implemented yet!") +def tf_console_cli(ctx, state, var): + args = [] + + if state: + args.append(f"-state={state}") + if var: + for name in var: + args.append(f"-var='{name}'") + + terraform_command_runner("console", args, "var", ctx.obj['SITE']) @tf_cli.command(name="destroy", help="Prepare your working directory for other" From 7b82a39daaae9c81fcf0e328fa27fdd4b8b22c00 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Fri, 13 May 2022 17:43:55 +0100 Subject: [PATCH 02/27] terrabutler: click.py: Finish terraform fmt --- terrabutler/click.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index 2bb3eed..5f39b0c 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -283,7 +283,8 @@ def tf_destroy_cli(ctx, auto_approve, input, lock, lock_timeout, no_color, @tf_cli.command(name="fmt", help="Reformat your configuration in the standard" "style") -@click.option("-diff", help="Display diffs of formatting changes") +@click.option("-diff", is_flag=True, + help="Display diffs of formatting changes") @click.option("-no-color", is_flag=True, help="If specified, output won't contain any color.") @click.option("-recursive", is_flag=True, @@ -291,7 +292,16 @@ def tf_destroy_cli(ctx, auto_approve, input, lock, lock_timeout, no_color, " given directory (or current directory) is processed.") @click.pass_context def tf_fmt_cli(ctx, diff, no_color, recursive): - print(Fore.RED + "Function not implemented yet!") + args = [] + + if diff: + args.append("-diff") + if no_color: + args.append("-no-color") + if recursive: + args.append("-recursive") + + terraform_command_runner("fmt", args, "none", ctx.obj['SITE']) @tf_cli.command(name="force-unlock", help="Release a stuck lock on the current" From 85735dad6d724fd6a6397c49ec377b15bf526dff Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Fri, 13 May 2022 17:50:15 +0100 Subject: [PATCH 03/27] terrabutler: click.py: Finish Terraform import command --- terrabutler/click.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index 5f39b0c..d9b32b5 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -351,7 +351,25 @@ def tf_generate_arguments_cli(ctx, command): @click.pass_context def tf_import_cli(ctx, addr, id, allow_missing_config, input, lock, no_color, var, ignore_remote_version): - print(Fore.RED + "Function not implemented yet!") + args = [] + + args.append(addr) + args.append(id) + if allow_missing_config: + args.append("-allow-missing-config") + if input is False: + args.append("-input=false") + if input is False: + args.append("-input=false") + if no_color: + args.append("-no-color") + if var: + for v in var: + args.append(f"-var={v}") + if ignore_remote_version: + args.append("-ignore-remote-version") + + terraform_command_runner("import", args, "var", ctx.obj['SITE']) @tf_cli.command(name="init", help="Prepare your working directory for other" From 47590c23c5f8fe42e77dc50486f427b287efb2ba Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Fri, 13 May 2022 17:53:03 +0100 Subject: [PATCH 04/27] terrabutler: click.py: Finish Terraform refresh command --- terrabutler/click.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index d9b32b5..bee59af 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -559,7 +559,22 @@ def tf_providers_cli(): "This flag can be set multiple times.") @click.pass_context def tf_refresh_cli(ctx, input, lock, no_color, target, var): - print(Fore.RED + "Function not implemented yet!") + args = [] + + if input is False: + args.append("-input=false") + if lock is False: + args.append("-lock=false") + if no_color: + args.append("-no-color") + if target: + for t in target: + args.append(f"-target={t}") + if var: + for v in var: + args.append(f"-var={v}") + + terraform_command_runner("refresh", args, "var", ctx.obj['SITE']) @tf_cli.command(name="show", help="Show the current state or a saved plan") From 107226cd5a53f498eda97ef118609a380ef9fcd5 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Fri, 13 May 2022 17:55:45 +0100 Subject: [PATCH 05/27] terrabutler: click.py: Finish terraform show command --- terrabutler/click.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index bee59af..6fb7bca 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -578,14 +578,24 @@ def tf_refresh_cli(ctx, input, lock, no_color, target, var): @tf_cli.command(name="show", help="Show the current state or a saved plan") +@click.argument("PATH", required=False) @click.option("-no-color", is_flag=True, help="If specified, output won't contain any color.") @click.option("-json", is_flag=True, help="If specified, machine readable output will be printed in" " JSON format.") @click.pass_context -def tf_show_cli(ctx, no_color, json): - print(Fore.RED + "Function not implemented yet!") +def tf_show_cli(ctx, path, no_color, json): + args = [] + + if path is not None: + args.append(path) + if no_color: + args.append("-no-color") + if json: + args.append("-json") + + terraform_command_runner("show", args, "var", ctx.obj['SITE']) @tf_cli.command(name="state", help="Advanced state management") From 627204b41c11d026fa3f76baf63bb6529444c161 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Fri, 13 May 2022 17:56:26 +0100 Subject: [PATCH 06/27] terrabutler: click.py: Small fix on terraform show --- terrabutler/click.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index 6fb7bca..184c219 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -595,7 +595,7 @@ def tf_show_cli(ctx, path, no_color, json): if json: args.append("-json") - terraform_command_runner("show", args, "var", ctx.obj['SITE']) + terraform_command_runner("show", args, "none", ctx.obj['SITE']) @tf_cli.command(name="state", help="Advanced state management") From 593df96fb61c0bcaf4e5a95deeae36633dbe44ec Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Fri, 13 May 2022 17:59:28 +0100 Subject: [PATCH 07/27] terrabutler: click.py: Finish terraform taint --- terrabutler/click.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index 184c219..985310f 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -605,6 +605,7 @@ def tf_state_cli(): @tf_cli.command(name="taint", help="Mark a resource instance as not fully" " functional") +@click.argument("address") @click.option("-allow-missing", is_flag=True, help="If specified, the command will succeed (exit code 0) even" " if the resource is missing.") @@ -617,9 +618,21 @@ def tf_state_cli(): help="A rare option used for the remote backend only. See the" " remote backend documentation for more information.") @click.pass_context -def tf_taint_cli(ctx, allow_missing, lock, lock_timeout, +def tf_taint_cli(ctx, address, allow_missing, lock, lock_timeout, ignore_remote_version): - print(Fore.RED + "Function not implemented yet!") + args = [] + + args.append(address) + if allow_missing: + args.append("-allow-missing") + if lock is False: + args.append("-lock=false") + if lock_timeout: + args.append(f"-lock-timeout={lock_timeout}") + if ignore_remote_version: + args.append("-ignore-remote-version") + + terraform_command_runner("taint", args, "none", ctx.obj['SITE']) @tf_cli.command(name="untaint", help="Remove the 'tainted' state from a" From 55a9c8b0bbe201ff51358457538c853cd9695b14 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Fri, 13 May 2022 18:00:21 +0100 Subject: [PATCH 08/27] terrabutler: click.py: Finish untaint --- terrabutler/click.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index 985310f..527a832 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -637,6 +637,7 @@ def tf_taint_cli(ctx, address, allow_missing, lock, lock_timeout, @tf_cli.command(name="untaint", help="Remove the 'tainted' state from a" " resource instance") +@click.argument("address") @click.option("-allow-missing", is_flag=True, help="If specified, the command will succeed (exit code 0) even" " if the resource is missing.") @@ -649,9 +650,21 @@ def tf_taint_cli(ctx, address, allow_missing, lock, lock_timeout, help="A rare option used for the remote backend only. See the" " remote backend documentation for more information.") @click.pass_context -def tf_untaint_cli(ctx, allow_missing, lock, lock_timeout, +def tf_untaint_cli(ctx, address, allow_missing, lock, lock_timeout, ignore_remote_version): - print(Fore.RED + "Function not implemented yet!") + args = [] + + args.append(address) + if allow_missing: + args.append("-allow-missing") + if lock is False: + args.append("-lock=false") + if lock_timeout: + args.append(f"-lock-timeout={lock_timeout}") + if ignore_remote_version: + args.append("-ignore-remote-version") + + terraform_command_runner("untaint", args, "none", ctx.obj['SITE']) @tf_cli.command(name="version", help="Show the current Terraform version") From 06e07a843e5370f620ddee0a3bf6bb3d4c9b4ab9 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Fri, 13 May 2022 18:04:26 +0100 Subject: [PATCH 09/27] terrabutler: click.py: Add validate function --- terrabutler/click.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/terrabutler/click.py b/terrabutler/click.py index 527a832..3dca6a6 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -677,3 +677,20 @@ def tf_version_cli(ctx, json): args.append("-json") terraform_command_runner("version", args, "", ctx.obj['SITE']) + + +@tf_cli.command(name="validate", help="Validate the configuration files") +@click.option("-no-color", is_flag=True, + help="If specified, output won't contain any color.") +@click.option("-json", is_flag=True, + help="Output the version information as a JSON object.") +@click.pass_context +def tf_validate_cli(ctx, no_color, json): + args = [] + + if no_color: + args.append("-no-color") + if json: + args.append("-json") + + terraform_command_runner("validate", args, "", ctx.obj['SITE']) From 0e94bfc5318042d91a9ecd4fa5bf287e766c3fb4 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 10:42:23 +0100 Subject: [PATCH 10/27] terrabutler: click.py: Add providers command --- terrabutler/click.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index 3dca6a6..62974b3 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -536,8 +536,8 @@ def tf_plan_cli(ctx, destroy, input, lock, lock_timeout, no_color, @tf_cli.command(name="providers", help="Show the providers required for this" " configuration") @click.pass_context -def tf_providers_cli(): - print(Fore.RED + "Function not implemented yet!") +def tf_providers_cli(ctx): + terraform_command_runner("providers", [], "none", ctx.obj['SITE']) @tf_cli.command(name="refresh", help="Update the state to match remote" From bf49860da8ec9f7cf765066ba8d3b4b005bf65e1 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 10:52:55 +0100 Subject: [PATCH 11/27] terrabutler: click.py: Add terraform providers lock --- terrabutler/click.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/terrabutler/click.py b/terrabutler/click.py index 62974b3..9b6c52e 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -540,6 +540,34 @@ def tf_providers_cli(ctx): terraform_command_runner("providers", [], "none", ctx.obj['SITE']) +@tf_providers_cli.command(name="lock", help="Write out dependency locks for" + " the configured providers") +@click.argument("providers", nargs=-1, required=True) +@click.option("-fs-mirror", help="Consult the given filesystem mirror" + " directory instead of the origin registry" + " for each of the given providers.") +@click.option("-net-mirror", help="Consult the given network mirror" + " (given as a base URL) instead of the" + " origin registry for each of the given" + " providers.") +@click.option("-platform", help="Choose a target platform to request package" + " checksums for.") +@click.pass_context +def tf_providers_lock_cli(ctx, providers, fs_mirror, net_mirror, platform): + args = [] + + args.append("lock") + args.append(providers) + if fs_mirror: + args.append(f"-fs-mirror={fs_mirror}") + if net_mirror: + args.append(f"-net-mirror={net_mirror}") + if platform: + args.append(f"-platform={platform}") + + terraform_command_runner("providers", args, "none", ctx.obj['SITE']) + + @tf_cli.command(name="refresh", help="Update the state to match remote" " systems") @click.option("-input", default=True, From 79e319c8694bb6316e00ac9805890ffa50dc8d1e Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 11:03:02 +0100 Subject: [PATCH 12/27] terrabutler: click.py: Add providers mirror command --- terrabutler/click.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/terrabutler/click.py b/terrabutler/click.py index 9b6c52e..ccc8da3 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -568,6 +568,23 @@ def tf_providers_lock_cli(ctx, providers, fs_mirror, net_mirror, platform): terraform_command_runner("providers", args, "none", ctx.obj['SITE']) +@tf_providers_cli.command(name="mirror", help="Save local copies of all" + " required provider plugins") +@click.argument("target-dir", required=True) +@click.option("-platform", help="Choose a target platform to request package" + " checksums for.") +@click.pass_context +def tf_providers_mirror_cli(ctx, target_dir, platform): + args = [] + + args.append("mirror") + args.append(target_dir) + if platform: + args.append(f"-platform={platform}") + + terraform_command_runner("providers", args, "none", ctx.obj['SITE']) + + @tf_cli.command(name="refresh", help="Update the state to match remote" " systems") @click.option("-input", default=True, From 240f1c3920df4674a1c9784cf1ca9d7ddcf79f7a Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 11:12:23 +0100 Subject: [PATCH 13/27] terrabutler: click.py: Add providers schema command --- terrabutler/click.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/terrabutler/click.py b/terrabutler/click.py index ccc8da3..1af1821 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -585,6 +585,21 @@ def tf_providers_mirror_cli(ctx, target_dir, platform): terraform_command_runner("providers", args, "none", ctx.obj['SITE']) +@tf_providers_cli.command(name="schema", help="Show schemas for the providers" + " used in the configuration") +@click.option("-json", help="Prints out a json representation of the schemas" + " for all providers used in the current" + " configuration.", is_flag=True) +@click.pass_context +def tf_providers_schema_cli(ctx, json): + args = [] + args.append(json) + if json: + args.append("-json") + + terraform_command_runner("providers", args, "none", ctx.obj['SITE']) + + @tf_cli.command(name="refresh", help="Update the state to match remote" " systems") @click.option("-input", default=True, From 07b2cbcc93551d7869d79aad94c0c9bc539aaddd Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 11:16:35 +0100 Subject: [PATCH 14/27] terrabutler: click.py: Add providers command as a group of terraform --- terrabutler/click.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index 1af1821..665b61e 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -533,8 +533,8 @@ def tf_plan_cli(ctx, destroy, input, lock, lock_timeout, no_color, terraform_command_runner("plan", args, "var", ctx.obj['SITE']) -@tf_cli.command(name="providers", help="Show the providers required for this" - " configuration") +@tf_cli.group(name="providers", help="Show the providers required for this" + " configuration") @click.pass_context def tf_providers_cli(ctx): terraform_command_runner("providers", [], "none", ctx.obj['SITE']) From a63c224268f160643b48611d626520356af186f0 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 11:25:17 +0100 Subject: [PATCH 15/27] terrabutler: click.py: Permit run only providers command without subcommand --- terrabutler/click.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index 665b61e..7556283 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -534,10 +534,12 @@ def tf_plan_cli(ctx, destroy, input, lock, lock_timeout, no_color, @tf_cli.group(name="providers", help="Show the providers required for this" - " configuration") + " configuration", + invoke_without_command=True) @click.pass_context def tf_providers_cli(ctx): - terraform_command_runner("providers", [], "none", ctx.obj['SITE']) + if ctx.invoked_subcommand is None: + terraform_command_runner("providers", [], "none", ctx.obj['SITE']) @tf_providers_cli.command(name="lock", help="Write out dependency locks for" From 8c22dc933357db9a476f1032dd01130105ea2c54 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 11:55:51 +0100 Subject: [PATCH 16/27] terrabutler: click.py: Create terraform state group --- terrabutler/click.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index 7556283..faa7981 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -660,9 +660,9 @@ def tf_show_cli(ctx, path, no_color, json): terraform_command_runner("show", args, "none", ctx.obj['SITE']) -@tf_cli.command(name="state", help="Advanced state management") +@tf_cli.group(name="state", help="Advanced state management") def tf_state_cli(): - print(Fore.RED + "Function not implemented yet!") + pass @tf_cli.command(name="taint", help="Mark a resource instance as not fully" From e35ab0571c0224076c64d18cf81e7bef03ce0cba Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 12:20:54 +0100 Subject: [PATCH 17/27] terrabutler: click.py: Add terraform state list command --- terrabutler/click.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index faa7981..4e597b7 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -661,10 +661,33 @@ def tf_show_cli(ctx, path, no_color, json): @tf_cli.group(name="state", help="Advanced state management") -def tf_state_cli(): +@click.pass_context +def tf_state_cli(ctx): pass +@tf_state_cli.command(name="list", help="List resources in the state") +@click.argument("address", nargs=-1, required=False) +@click.option("-state", help="Path to a Terraform state file to use to look up" + " Terraform-managed resources. By default," + " Terraform will consult the state of the" + " currently-selected workspace.") +@click.option("-id", help="Filters the results to include only instances" + " whoseresource types have an attribute named 'id'" + " whose value equals the given id string.") +@click.pass_context +def tf_state_list_cli(ctx, address, state, id): + args = [] + + args.append(address) + if state: + args.append(f"-state={state}") + if id: + args.append(f"-id={id}") + + terraform_command_runner("state list", args, "none", ctx.obj['SITE']) + + @tf_cli.command(name="taint", help="Mark a resource instance as not fully" " functional") @click.argument("address") From ea63b5781d7b0a38085fb9faa9911180819df942 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 12:44:34 +0100 Subject: [PATCH 18/27] terrabutler: click.py: Add terraform state mv --- terrabutler/click.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/terrabutler/click.py b/terrabutler/click.py index 4e597b7..f682802 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -688,6 +688,38 @@ def tf_state_list_cli(ctx, address, state, id): terraform_command_runner("state list", args, "none", ctx.obj['SITE']) +@tf_state_cli.command(name="mv", help="Move an item in the state") +@click.argument("source", nargs=-1, required=True) +@click.argument("destination", nargs=-1, required=True) +@click.option("-dry-run", is_flag=True, help="If set, prints out what would've" + " been moved but doesn't actually" + " move anything.") +@click.option("-lock", is_flag=True, default=True, + help="Don't hold a state lock during the operation. This is" + " dangerous if others might concurrently run commands" + " against the same workspace.") +@click.option("-lock-timeout", help="Duration to retry a state lock.") +@click.option("-ignore-remote-version", is_flag=True, + help="A rare option used for the remote backend only." + "See the remote backend documentation for more information.") +@click.pass_context +def tf_state_mv_cli(ctx, source, destination, dry_run, lock, lock_timeout, + ignore_remote_version): + args = [] + + if dry_run: + args.append("-dry-run") + if lock is False: + args.append("-lock=false") + if lock_timeout: + args.append(f"-lock-timeout={lock_timeout}") + if ignore_remote_version: + args.append("-ignore-remote-version") + + terraform_command_runner(f"state mv {source} {destination}", args, "none", + ctx.obj['SITE']) + + @tf_cli.command(name="taint", help="Mark a resource instance as not fully" " functional") @click.argument("address") From fdda5ccd9f0b6b90d70193b7c7e0806e58281400 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 12:53:27 +0100 Subject: [PATCH 19/27] terrabutler: click.py: Fix arguments of state mv --- terrabutler/click.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index f682802..aab0858 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -689,8 +689,8 @@ def tf_state_list_cli(ctx, address, state, id): @tf_state_cli.command(name="mv", help="Move an item in the state") -@click.argument("source", nargs=-1, required=True) -@click.argument("destination", nargs=-1, required=True) +@click.argument("source", required=True) +@click.argument("destination", required=True) @click.option("-dry-run", is_flag=True, help="If set, prints out what would've" " been moved but doesn't actually" " move anything.") From 070ae9b3704724393e044184a7b4667676637d5e Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 12:59:19 +0100 Subject: [PATCH 20/27] terrabutler: click.py: Add tf state pull --- terrabutler/click.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/terrabutler/click.py b/terrabutler/click.py index aab0858..cf0f6e4 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -720,6 +720,17 @@ def tf_state_mv_cli(ctx, source, destination, dry_run, lock, lock_timeout, ctx.obj['SITE']) +@tf_state_cli.command(name="pull", help="Pull current state and output to" + " stdout") +@click.pass_context +def tf_state_pull_cli(ctx): + args = [] + + args.append("pull") + + terraform_command_runner("state", args, "none", ctx.obj['SITE']) + + @tf_cli.command(name="taint", help="Mark a resource instance as not fully" " functional") @click.argument("address") From a78a715be9d9c37d21e3ba86ca24c13833919c81 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 15:04:36 +0100 Subject: [PATCH 21/27] terrabutler: click.py: Add state push command --- terrabutler/click.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/terrabutler/click.py b/terrabutler/click.py index cf0f6e4..2c2780d 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -731,6 +731,33 @@ def tf_state_pull_cli(ctx): terraform_command_runner("state", args, "none", ctx.obj['SITE']) +@tf_state_cli.command(name="push", help="Update remote state from a local" + " state file") +@click.argument("path", required=True) +@click.option("-force", is_flag=True, + help="Write the state even if lineages don't match or the remote" + " serial is higher.") +@click.option("-lock", is_flag=True, default=True, + help="Don't hold a state lock during the operation. This is" + " dangerous if others might concurrently run commands" + " against the same workspace.") +@click.option("-lock-timeout", help="Duration to retry a state lock.") +@click.pass_context +def tf_state_push_cli(ctx, path, force, lock, lock_timeout): + args = [] + + args.append("push") + args.append(path) + if force: + args.append("-force") + if lock is False: + args.append("-lock=false") + if lock_timeout: + args.append(f"-lock-timeout={lock_timeout}") + + terraform_command_runner("state", args, "none", ctx.obj['SITE']) + + @tf_cli.command(name="taint", help="Mark a resource instance as not fully" " functional") @click.argument("address") From 402d28a809d5f682193f8935d2b4095a5098c888 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 15:37:21 +0100 Subject: [PATCH 22/27] terrabutler: click.py: Add state replace-provider command --- terrabutler/click.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/terrabutler/click.py b/terrabutler/click.py index 2c2780d..bb9bfbd 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -758,6 +758,42 @@ def tf_state_push_cli(ctx, path, force, lock, lock_timeout): terraform_command_runner("state", args, "none", ctx.obj['SITE']) +@tf_state_cli.command(name="replace-provider", + help="Replace provider for resources in the Terraform" + " state.") +@click.argument("from_provider_fqdn", required=True) +@click.argument("to_provider_fqdn", required=True) +@click.option("-auto-approve", is_flag=True, + help="Skip interactive approval of plan before applying.") +@click.option("-lock", is_flag=True, default=True, + help="Don't hold a state lock during the operation. This is" + " dangerous if others might concurrently run commands" + " against the same workspace.") +@click.option("-lock-timeout", help="Duration to retry a state lock.") +@click.option("-ignore-remote-version", is_flag=True, + help="A rare option used for the remote backend only. See the" + " remote backend documentation for more information.") +@click.pass_context +def tf_state_replace_cli(ctx, from_provider_fqdn, to_provider_fqdn, + auto_approve, lock, lock_timeout, + ignore_remote_version): + args = [] + + args.append("replace-provider") + args.append(from_provider_fqdn) + args.append(to_provider_fqdn) + if auto_approve: + args.append("-auto-approve") + if lock is False: + args.append("-lock=false") + if lock_timeout: + args.append(f"-lock-timeout={lock_timeout}") + if ignore_remote_version: + args.append("-ignore-remote-version") + + terraform_command_runner("state", args, "none", ctx.obj['SITE']) + + @tf_cli.command(name="taint", help="Mark a resource instance as not fully" " functional") @click.argument("address") From 27353c5011d9477dd13ebe6c6a29e96a6fff6215 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 15:49:44 +0100 Subject: [PATCH 23/27] terrabutler: click.py: Add state rm --- terrabutler/click.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/terrabutler/click.py b/terrabutler/click.py index bb9bfbd..fd35ed7 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -794,6 +794,47 @@ def tf_state_replace_cli(ctx, from_provider_fqdn, to_provider_fqdn, terraform_command_runner("state", args, "none", ctx.obj['SITE']) +@tf_state_cli.command(name="rm", + help="Remove instances from the state") +@click.argument("address", nargs=-1, required=True) +@click.option("-dry-run", is_flag=True, help="If set, prints out what would've" + " been moved but doesn't actually" + " move anything.") +@click.option("-backup", help="Path where Terraform should write the backup" + " state.") +@click.option("-lock", is_flag=True, default=True, + help="Don't hold a state lock during the operation. This is" + " dangerous if others might concurrently run commands" + " against the same workspace.") +@click.option("-lock-timeout", help="Duration to retry a state lock.") +@click.option("-state", help="Path to the state file to update. Defaults to" + " the current workspace state.") +@click.option("-ignore-remote-version", is_flag=True, + help="A rare option used for the remote backend only. See the" + " remote backend documentation for more information.") +@click.pass_context +def tf_state_rm_cli(ctx, address, dry_run, backup, lock, lock_timeout, state, + ignore_remote_version): + args = [] + + args.append("rm") + args += address + if dry_run: + args.append("-dry-run") + if backup: + args.append(f"-backup={backup}") + if lock is False: + args.append("-lock=false") + if lock_timeout: + args.append(f"-lock-timeout={lock_timeout}") + if state: + args.append(f"-state={state}") + if ignore_remote_version: + args.append("-ignore-remote-version") + + terraform_command_runner("state", args, "none", ctx.obj['SITE']) + + @tf_cli.command(name="taint", help="Mark a resource instance as not fully" " functional") @click.argument("address") From 22b97ba69c12f521e964b06a5d56c323f697bcbd Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 15:52:12 +0100 Subject: [PATCH 24/27] terrabutler: click.py: Add state show command --- terrabutler/click.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/terrabutler/click.py b/terrabutler/click.py index fd35ed7..ed49059 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -835,6 +835,23 @@ def tf_state_rm_cli(ctx, address, dry_run, backup, lock, lock_timeout, state, terraform_command_runner("state", args, "none", ctx.obj['SITE']) +@tf_state_cli.command(name="show", + help="Show a resource in the state") +@click.argument("address", required=True) +@click.option("-state", help="Path to the state file to update. Defaults to" + " the current workspace state.") +@click.pass_context +def tf_state_show_cli(ctx, address, state): + args = [] + + args.append("show") + args.append(address) + if state: + args.append(f"-state={state}") + + terraform_command_runner("state", args, "none", ctx.obj['SITE']) + + @tf_cli.command(name="taint", help="Mark a resource instance as not fully" " functional") @click.argument("address") From ecc05f41b1dcdf5ab6372afaa85937409e9a1db0 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 15:56:05 +0100 Subject: [PATCH 25/27] terrabutler: click.py: Fix execution of state list command --- terrabutler/click.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index ed49059..e8abbee 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -679,13 +679,14 @@ def tf_state_cli(ctx): def tf_state_list_cli(ctx, address, state, id): args = [] - args.append(address) + args.append("list") + args += address if state: args.append(f"-state={state}") if id: args.append(f"-id={id}") - terraform_command_runner("state list", args, "none", ctx.obj['SITE']) + terraform_command_runner("state", args, "none", ctx.obj['SITE']) @tf_state_cli.command(name="mv", help="Move an item in the state") From 7a9bba75da91fd52a0031a55a142e0ac66762748 Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 15:57:41 +0100 Subject: [PATCH 26/27] terrabutler: clikc.py: Fix state mv command --- terrabutler/click.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index e8abbee..a37c6a4 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -708,6 +708,9 @@ def tf_state_mv_cli(ctx, source, destination, dry_run, lock, lock_timeout, ignore_remote_version): args = [] + args.append("mv") + args.append(source) + args.append(destination) if dry_run: args.append("-dry-run") if lock is False: @@ -717,8 +720,7 @@ def tf_state_mv_cli(ctx, source, destination, dry_run, lock, lock_timeout, if ignore_remote_version: args.append("-ignore-remote-version") - terraform_command_runner(f"state mv {source} {destination}", args, "none", - ctx.obj['SITE']) + terraform_command_runner("state", args, "none", ctx.obj['SITE']) @tf_state_cli.command(name="pull", help="Pull current state and output to" From 4d361c4f2c6f7f01a362e742dca7304b2179988f Mon Sep 17 00:00:00 2001 From: MiguelNdeCarvalho Date: Mon, 16 May 2022 15:59:48 +0100 Subject: [PATCH 27/27] terrabutler: click.py: Fix append of list of commands --- terrabutler/click.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terrabutler/click.py b/terrabutler/click.py index a37c6a4..c6e6ad4 100755 --- a/terrabutler/click.py +++ b/terrabutler/click.py @@ -559,7 +559,7 @@ def tf_providers_lock_cli(ctx, providers, fs_mirror, net_mirror, platform): args = [] args.append("lock") - args.append(providers) + args += providers if fs_mirror: args.append(f"-fs-mirror={fs_mirror}") if net_mirror: