Snowflake-Labs / terraform-provider-snowflake

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

Resource Monitor marked as changed but didn't change, and fails when re-apply it second time #1716

Open toni-moreno opened 1 year ago

toni-moreno commented 1 year ago

Provider & terraform Version

Terraform v1.4.4
on linux_amd64
+ provider registry.terraform.io/aidanmelen/snowsql v1.3.3
+ provider registry.terraform.io/snowflake-labs/snowflake v0.61.0

Describe the bug

Terraform is marking as changed a snowflake_resource_monitor resource but it didn't change.

Expected behavior

When re-plan without changes it marks resource as changed , when re-apply it fails

Code samples and commands

resource_monitors = {
  "RM_ETL" = {
    quota           = 554
    frequency       = "NEVER"
    start_timestamp = "2023-04-14 12:00"
    end_timestamp   = "2023-11-01 12:00"
    notify_triggers = [75, 100]
    warehouses      = ["ETL"]
  }
}

variable "resource_monitors" {
  description = "lista de resource monitors"
  type = map(object({
    for_account               = optional(bool, false)
    quota                     = number 
    frequency                 = optional(string)
    start_timestamp           = string
    end_timestamp             = string
    notify_triggers           = list(number)
    suspend_trigger           = optional(number) 
    suspend_immediate_trigger = optional(number) 
    notify_users              = optional(list(string))
    warehouses                = optional(set(string))
  }))
 #....some validations here
}

resource "snowflake_resource_monitor" "RM" {

  for_each = var.resource_monitors

  set_for_account = each.value.for_account

  name         = each.key
  credit_quota = each.value.quota

  frequency       = each.value.frequency
  start_timestamp = each.value.start_timestamp
  end_timestamp   = each.value.end_timestamp

  notify_triggers           = each.value.notify_triggers
  suspend_trigger           = each.value.suspend_trigger
  suspend_immediate_trigger = each.value.suspend_immediate_trigger

  notify_users = each.value.notify_users
  warehouses   = each.value.warehouses

  depends_on = [snowflake_warehouse.warehouse]

}

When executing first terraform apply everything seems ok but in snowflake ui the time is not the same than expected form my terraform code

image

My Snowflake account is in switzerland-north.azure and my timezone is Europe/Madrid.

When re-plan without changes the provider is showing me the resource as changed.

  # snowflake_resource_monitor.RM["RM_ETL"] will be updated in-place
  ~ resource "snowflake_resource_monitor" "RM" {
      ~ end_timestamp             = "2023-11-01T05:00:00-07:00" -> "2023-11-01 12:00"
        id                        = "RM_ETL"
        name                      = "RM_ETL"
      ~ start_timestamp           = "2023-04-14T05:00:00-07:00" -> "2023-04-14 12:00"
        # (7 unchanged attributes hidden)
    }

When re-apply ( force it ) it fails

╷
│ Error: error updating resource monitor RM_ETL
│ 090259 (42601): Must specify frequency and start time together.
│ 
│   with snowflake_resource_monitor.RM["RM_ETL"],
│   on 001_resource_monitors.tf line 5, in resource "snowflake_resource_monitor" "RM":
│    5: resource "snowflake_resource_monitor" "RM" {
│ 
╵

Here there is 3 things to review .

1) what timezone is taking the provider by default 2) how can I change in the end_timestamp and start_timestamp parameters ? 3) ( the bug) why is talking about changed parameters that indeed didn't change 4) (the bug) why is failing when force re-apply.

toni-moreno commented 1 year ago

reviewing the error, seems like TIMESTAMP_OUTPUT_FORMAT is the default YYYY-MM-DD HH24:MI:SS.FF3 TZHTZM and when the provider runs a SHOW RESOURCE MONITORS here:

https://github.com/Snowflake-Labs/terraform-provider-snowflake/blob/d9f634a1fe1911a9f2f5ad44d39b9ff5e21ff602/pkg/snowflake/resource_monitor.go#L261-L278

It gets the following timestamp

image

that is different than used in the resource this is becouse terraform thinks it has changed

 # snowflake_resource_monitor.RM["RM_ETL"] will be updated in-place
  ~ resource "snowflake_resource_monitor" "RM" {
      ~ end_timestamp             = "2023-11-01T05:00:00-07:00" -> "2023-11-01 12:00"
        id                        = "RM_ETL"
        name                      = "RM_ETL"
      ~ start_timestamp           = "2023-04-14T05:00:00-07:00" -> "2023-04-14 12:00"
        # (7 unchanged attributes hidden)
    }

But the resource doesn't accept other format than "YYYY-MM-DD HH:MM" as input. when trying to change timestamp to the default output format

"RM_ETL" = {
    quota     = 100
    frequency = "NEVER"
    start_timestamp = "2023-29-03T05:00:00-07:00"
    end_timestamp   = "2023-11-01T05:00:00-07:00"
    notify_triggers = [75, 100]
    warehouses      = ["ETL"]
  }

this error happens.

Invalid date/time format string '2023-29-03T05:00:00-07:00': Invalid format: "2023-29-03T05:00:00-07:00" is malformed at "T05:00:00-07:00"

the solution seems to change TIMESTAMP_OUTPUT_FORMAT to the same format than supported in input before the SHOW RESOURCES MONITOR in the current terraform opened session.

alter session set TIMESTAMP_OUTPUT_FORMAT = 'YYYY-MM-DD HH24:MI'