hashicorp / consul-terraform-sync

Consul Terraform Sync is a service-oriented tool for managing network infrastructure near real-time.
Mozilla Public License 2.0
120 stars 27 forks source link

Fix plan output readability for TFC driver #425

Open lornasong opened 2 years ago

lornasong commented 2 years ago

Describe the bug

When a disabled task is re-enabled through the Enable CLI or CTS is executed in inspect mode, the CLI outputs information from a Terraform Plan to inform the user of the changes that CTS will make when the task becomes enabled.

For the TFC driver, the current CLI output looks different from when it was originally implemented if the task has a Terraform version of 0.15.2 or greater. This current output includes json blobs and is less readable and user-friendly.

Workaround: at the bottom of the outputted plan, there should be a URL e.g. "Inspection results can also be viewed at: <url>". This URL will bring you to the TFC UI that will show the plan in a much more readable way.

Sample

==> Inspecting changes to resource(s) if enabling 'task_serv'...

    Generating plan that Consul Terraform Sync will use Terraform to execute

    Terraform v1.0.8
on linux_amd64
Configuring remote state backend...
Initializing Terraform configuration...
{"@level":"info","@message":"Terraform 1.0.8","@module":"terraform.ui","@timestamp":"2021-10-07T17:56:16.701312Z","terraform":"1.0.8","type":"version","ui":"0.1.0"}
{"@level":"info","@message":"module.task_serv.local_file.address[\"consul.lornasong.dc1\"]: Plan to create","@module":"terraform.ui","@timestamp":"2021-10-07T17:56:18.960698Z","change":{"resource":{"addr":"module.task_serv.local_file.address[\"consul.lornasong.dc1\"]","module":"module.task_serv","resource":"local_file.address[\"consul.lornasong.dc1\"]","implied_provider":"local","resource_type":"local_file","resource_name":"address","resource_key":"consul.lornasong.dc1"},"action":"create"},"type":"planned_change"}
{"@level":"info","@message":"module.task_serv.local_file.address[\"web2.lornasong.dc1\"]: Plan to create","@module":"terraform.ui","@timestamp":"2021-10-07T17:56:18.960794Z","change":{"resource":{"addr":"module.task_serv.local_file.address[\"web2.lornasong.dc1\"]","module":"module.task_serv","resource":"local_file.address[\"web2.lornasong.dc1\"]","implied_provider":"local","resource_type":"local_file","resource_name":"address","resource_key":"web2.lornasong.dc1"},"action":"create"},"type":"planned_change"}
{"@level":"info","@message":"Plan: 2 to add, 0 to change, 0 to destroy.","@module":"terraform.ui","@timestamp":"2021-10-07T17:56:18.960820Z","changes":{"add":2,"change":0,"remove":0,"operation":"plan"},"type":"change_summary"}

    Inspection results can also be viewed at:
https://app.terraform.io/app/<redacted>

...

Versions

Consul Terraform Sync

consul-terraform-sync v0.3.0
consul-terraform-sync v0.4.0

Terraform Version

Terraform >=0.15.2

Configuration File(s)

Reminder to redact any sensitive information that may be present in this file

Click to toggle contents of config file ```hcl log_level = "TRACE" working_dir = "build" consul { address = "localhost:8500" } driver "terraform-cloud" { hostname = organization = token = } task { name = "task_serv" source = "lornasong/cts_file/local" condition "services" { regexp = ".*" } enabled = false } ```

Steps to Reproduce

  1. Start consul: consul agent -dev
  2. Start CTS with disabled task: consul-terraform-sync -config-file=config.hcl
  3. Register a service: consul services register -name=web -id=web1
  4. Enable the task: consul-terraform-sync task enable task_serv
  5. Observe outputted Terraform plan had difficult to read logs similar to description above

Expected Behavior

The outputted plan should be human readable so that a user can easily identify what resources are being creates, updated, deleted

Additional Context

Terraform Driver 's output needs to readable both for Terraform versions before and after Terraform v0.15.2.

mkam commented 2 years ago

Confirmed that this is also happening in v0.3.0-ent and also with the output of running inspect mode.

findkim commented 2 years ago

It looks like Terraform Cloud behavior differs based on the Terraform CLI version for the workspace. The output changes between Terraform 0.15.1 and 0.15.2.

task {
  name = "kngo_enable"
  services  = ["api", "web"]
  source  = "mkam/hello/cts"
  enabled = false
  terraform_version = "0.15.1"
}
$ consul-terraform-sync task enable kngo_enable
==> Inspecting changes to resource(s) if enabling 'kngo_enable'...

    Generating plan that Consul Terraform Sync will use Terraform to execute

    Terraform v0.15.1
on linux_amd64
Configuring remote state backend...
Initializing Terraform configuration...
module.kngo_enable.local_file.greeting_services: Refreshing state... [id=adc83b19e793491b1c6ea0fd8b46cd9f32e592fc]
module.kngo_enable.local_file.greeting: Refreshing state... [id=69342c5c39e5ae5f0077aecc32c0f81811fb8193]

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # module.kngo_enable.local_file.greeting will be created
...
$ consul-terraform-sync task enable kngo_enable
==> Inspecting changes to resource(s) if enabling 'kngo_enable'...

    Generating plan that Consul Terraform Sync will use Terraform to execute

    Terraform v0.15.2
on linux_amd64
Configuring remote state backend...
Initializing Terraform configuration...
{"@level":"info","@message":"Terraform 0.15.2","@module":"terraform.ui","@timestamp":"2021-10-12T10:59:32.803143Z","terraform":"0.15.2","type":"version","ui":"0.1.0"}
{"@level":"info","@message":"module.kngo_enable.local_file.greeting_services: Refreshing state... [id=adc83b19e793491b1c6ea0fd8b46cd9f32e592fc]","@module":"terraform.ui","@timestamp":"2021-10-12T10:59:34.302744Z","hook":{"resource":{"addr":"module.kngo_enable.local_file.greeting_services","module":"module.kngo_enable","resource":"local_file.greeting_services","implied_provider":"local","resource_type":"local_file","resource_name":"greeting_services","resource_key":null},"id_key":"id","id_value":"adc83b19e793491b1c6ea0fd8b46cd9f32e592fc"},"type":"refresh_start"}
mkam commented 2 years ago

Updated the issue description to reflect our findings: