coder / terraform-provider-coderd

Manage a Coder deployment using Terraform
https://registry.terraform.io/providers/coder/coderd/latest/docs
Mozilla Public License 2.0
10 stars 2 forks source link

Improve error handling #92

Closed michvllni closed 1 month ago

michvllni commented 2 months ago

When deploying a template via the coderd_template resource, the errors from the internal commands do not bubble up.

All I get is for example this:

╷
│ Error: Client Error
│ 
│   with coderd_template.templates["az-ps-function"],
│   on templates.tf line 2, in resource "coderd_template" "templates":
│    2: resource "coderd_template" "templates" {
│ 
│ failed to wait for job: provisioner job did not succeed: failed (template
│ import provision for start: terraform plan: exit status 1)
╵

I have to use debug mode in terraform to get the actual error, which is very cumbersome: 2024-09-11T14:48:56.832Z [INFO] provider.terraform-provider-coderd_v0.0.4: A data resource "coder_parameter" "ado_projects" has not been declared in the root module.: job_stage="Detecting persistent resources" log_source=provisioner tf_req_id=6f199328-b1d4-3746-8d48-3cb8af035cfe job_id=10626 tf_provider_addr=registry.terraform.io/coder/coderd tf_resource_type=coderd_template @caller=github.com/coder/terraform-provider-coderd/internal/provider/template_resource.go:954 @module=coderd created_at=2024-09-11T14:48:56.783Z level=error tf_rpc=ApplyResourceChange timestamp=2024-09-11T14:48:56.832Z

It would be great if errors from the executed commands would be visible in the output.

matifali commented 2 months ago

This is a design decision, and to surface these errors. You need TF_LOG=INFO or higher. We may look into revisiting this.

This is documented here: https://registry.terraform.io/providers/coder/coderd/latest/docs/resources/template

Logs from building template versions are streamed from the provisioner when the TF_LOG environment variable is INFO or higher.

cc: @ethanndickson

michvllni commented 2 months ago

Sorry, must have missed that disclaimer when I read the documentation.

In my opinion an error does not belong into info but into error, but you must have had your reasons.

Isn't it possible to separate the outputs and print errors in the build to error?

I agree that it is not necessary to print the whole plan all the time

ethanndickson commented 2 months ago

Unfortunately, this is a Terraform provider restriction. Our output options are either the aforementioned logging (where none of the available logging levels show by default, afaik) or a Terraform warning, which always show, but don't seem suitable for the Coder provisioner output due to their size and the fact they always contain Warning at the top.

e.g:

│ Warning: Log Output
│ 
│   with coderd_template.sample,
│   on main.tf line 24, in resource "coderd_template" "sample":
│   24: resource "coderd_template" "sample" {
│ 
│ module.git-clone.coder_script.git_clone: Plan to create

As I write this message, I realise we could just wait for the build to finish/fail and then include them all as a single warning/error respectively (or just omit the logs if successful). That way they'd always show, and I wouldn't want to stream them as individual warnings, as that seems pretty horrid. I'll experiment, thanks for the inspiration! :)

Update: This is much nicer, will implement