Snowflake-Labs / terraform-provider-snowflake

Terraform provider for managing Snowflake accounts
https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest
MIT License
545 stars 419 forks source link

resource_monitor crashes when removing a list of users #2336

Closed MattDelac closed 9 months ago

MattDelac commented 9 months ago

Terraform CLI and Provider Versions

Snowflake-Labs/snowflake = 0.73.0 Terraform v1.5.5

Terraform Configuration

terraform {
  required_version = ">= 0.13"
  cloud {
    organization = "xxx"

    workspaces {
      name = "xxx"
    }
  }

  required_providers {
    snowflake = {
      source  = "Snowflake-Labs/snowflake"
      version = "~> 0.73, <= 1.0.0"
    }
  }
}

provider "snowflake" {
  alias       = "db_ops"
  account     = format("%s.%s", local.snowflake_locator, local.snowflake_region)
  username    = "xxx"
  private_key = var.snowflake_private_key
  role        = "xxx"
}
provider "snowflake" {
  alias       = "role_mgmt"
  account     = format("%s.%s", local.snowflake_locator, local.snowflake_region)
  username    = "xxx"
  private_key = var.snowflake_private_key
  role        = "xxx"
}
provider "snowflake" {
  alias       = "account_admin"
  account     = format("%s.%s", local.snowflake_locator, local.snowflake_region)
  username    = "xxx"
  private_key = var.snowflake_private_key
  role        = "xxx"
}

Expected Behavior

terraform apply should work when modifying partially the resource snowflake_resource_monitor

Actual Behavior

With the following piece of code

resource "snowflake_resource_monitor" "warehouses" {
  for_each                  = local.warehouse_monthly_credit_quota
  provider                  = snowflake.account_admin
  name                      = "warehouse_${each.key}"
  credit_quota              = each.value
  frequency                 = "MONTHLY"
  start_timestamp           = "2024-01-11 00:00"
  notify_triggers           = [format("%.0f", each.value / 2), format("%.0f", each.value * 0.75)]
  suspend_trigger           = format("%.0f", (each.value / 5 * 4))
  suspend_immediate_trigger = format("%.0f", (each.value / 5 * 4)) + 1
  warehouses                = [each.key]
}

When I removed the "notify_users" parameters, terraform plan was good but terraform apply crashed

Logs

snowflake_resource_monitor.warehouses["XXX"]: Refreshing state... [id=warehouse_XXX]
╷
│ Warning: Resource targeting is in effect
│
│ You are creating a plan with the -target option, which means that the
│ result of this plan may not represent all of the changes requested by the
│ current configuration.
│
│ The -target option is not for routine use, and is provided only for
│ exceptional situations such as recovering from errors or mistakes, or when
│ Terraform specifically suggests to use it as part of an error message.
╵

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # snowflake_resource_monitor.warehouses["XXX"] will be updated in-place
  ~ resource "snowflake_resource_monitor" "warehouses" {
        id                        = "warehouse_XXX"
        name                      = "warehouse_XXX"
      ~ notify_users              = [
          - "XXX",
          - "XXX",
        ]
      ~ start_timestamp           = "2024-01-10T10:03:00Z" -> (known after apply)
        # (7 unchanged attributes hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

------------------------------------------------------------------------

Cost Estimation:

Not available for this plan, because it was created with the -target option.

------------------------------------------------------------------------

Do you want to perform these actions in workspace "xxx"?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

snowflake_resource_monitor.warehouses["XXX"]: Modifying... [id=warehouse_XXX]
╷
│ Error: Plugin did not respond
│
│   with snowflake_resource_monitor.warehouses["XXX"],
│   on warehouses.tf line 263, in resource "snowflake_resource_monitor" "warehouses":
│  263: resource "snowflake_resource_monitor" "warehouses" {
│
│ The plugin encountered an error, and failed to respond to the
│ plugin.(*GRPCProvider).ApplyResourceChange call. The plugin logs may
│ contain more details.
╵
╷
│ Warning: Applied changes may be incomplete
│
│ The plan was created with the -target option in effect, so some changes
│ requested in the configuration may have been ignored and the output values
│ may not be fully updated. Run the following command to verify that no other
│ changes are pending:
│     terraform plan
│
│ Note that the -target option is not suitable for routine use, and is
│ provided only for exceptional situations such as recovering from errors or
│ mistakes, or when Terraform specifically suggests to use it as part of an
│ error message.
╵

Stack trace from the terraform-provider-snowflake_v0.73.0 plugin:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x153b971]

goroutine 87 [running]:
github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources.UpdateResourceMonitor(0xc000bc7200, {0x1b6e6a0?, 0xc0005fed00?})
    github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources/resource_monitor.go:346 +0x1831
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).update(0x1ea1900?, {0x1ea1900?, 0xc000c3cf90?}, 0xd?, {0x1b6e6a0?, 0xc0005fed00?})
    github.com/hashicorp/terraform-plugin-sdk/v2@v2.29.0/helper/schema/resource.go:800 +0x178
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*Resource).Apply(0xc000652d20, {0x1ea1900, 0xc000c3cf90}, 0xc000ab4340, 0xc000bc7080, {0x1b6e6a0, 0xc0005fed00})
    github.com/hashicorp/terraform-plugin-sdk/v2@v2.29.0/helper/schema/resource.go:919 +0x845
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.(*GRPCProviderServer).ApplyResourceChange(0xc0009bfdd0, {0x1ea1900?, 0xc000c3ce70?}, 0xc000bee460)
    github.com/hashicorp/terraform-plugin-sdk/v2@v2.29.0/helper/schema/grpc_provider.go:1060 +0xe8d
github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server.(*server).ApplyResourceChange(0xc000723900, {0x1ea1900?, 0xc000c3c240?}, 0xc000704230)
    github.com/hashicorp/terraform-plugin-go@v0.19.0/tfprotov5/tf5server/server.go:859 +0x574
github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5._Provider_ApplyResourceChange_Handler({0x1b1fda0?, 0xc000723900}, {0x1ea1900, 0xc000c3c240}, 0xc000704070, 0x0)
    github.com/hashicorp/terraform-plugin-go@v0.19.0/tfprotov5/internal/tfplugin5/tfplugin5_grpc.pb.go:467 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0xc00082cd20, {0x1eb1480, 0xc000007860}, 0xc00104a000, 0xc0006cba40, 0x2b61258, 0x0)
    google.golang.org/grpc@v1.57.0/server.go:1360 +0xe23
google.golang.org/grpc.(*Server).handleStream(0xc00082cd20, {0x1eb1480, 0xc000007860}, 0xc00104a000, 0x0)
    google.golang.org/grpc@v1.57.0/server.go:1737 +0xa36
google.golang.org/grpc.(*Server).serveStreams.func1.1()
    google.golang.org/grpc@v1.57.0/server.go:982 +0x98
created by google.golang.org/grpc.(*Server).serveStreams.func1
    google.golang.org/grpc@v1.57.0/server.go:980 +0x18c

Error: The terraform-provider-snowflake_v0.73.0 plugin crashed!

This is always indicative of a bug within the plugin. It would be immensely
helpful if you could report the crash with the plugin's maintainers so that it
can be fixed. The output above should help diagnose the issue.

Operation failed: failed running terraform apply (exit 1)

I had a similar behaviour when I only changed the start_timestamp even if the frequency parameter remains the same

╷
│ Error: error updating resource monitor warehouse_XXX
│ must specify frequency and start time together
│ 
│   with snowflake_resource_monitor.warehouses["XXX"],
│   on warehouses.tf line 262, in resource "snowflake_resource_monitor" "warehouses":
│  262: resource "snowflake_resource_monitor" "warehouses" {
│ 
╵
Operation failed: failed running terraform apply (exit 1)
Error: Terraform exited with code 1.
Error: Process completed with exit code 1.

Steps to Reproduce

  1. Create a resource snowflake_resource_monitor block
  2. Change the start_timestamp while keeping the same value for the frequency
  3. Remove the user_notify parameters (or put an empty list)
  4. terraform apply

How much impact is this issue causing?

High

Logs

No response

Additional Information

Impact is high as the only workaround is to destroy and create again the resources which is a real time sink

sfc-gh-asawicki commented 9 months ago

Hey @MattDelac. Thanks for reporting the issue.

This error was fixed in #2287 (available in v0.81.0). There are some other problems with this resource solved with #2319; these will be released tomorrow as a part of v0.83.0.

Please bump to v0.81.0 or wait for v0.83.0 and bump then; this should solve your problem.

MattDelac commented 9 months ago

Hey @MattDelac. Thanks for reporting the issue.

This error was fixed in #2287 (available in v0.81.0). There are some other problems with this resource solved with #2319; these will be released tomorrow as a part of v0.83.0.

Please bump to v0.81.0 or wait for v0.83.0 and bump then; this should solve your problem.

That's great. I will wait for 0.83.0 then

Thanks