Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
toni-moreno opened this issue Apr 13, 2023 · 4 comments
Assignees
Labels
bug Used to mark issues with provider's incorrect behavior category:resource resource:resource_monitor Issue connected to the snowflake_resource_monitor resource

Comments

@toni-moreno
Copy link

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 toni-moreno added the bug Used to mark issues with provider's incorrect behavior label Apr 13, 2023
@toni-moreno
Copy link
Author

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:

func ListResourceMonitors(db *sql.DB) ([]ResourceMonitor, error) {
stmt := "SHOW RESOURCE MONITORS"
rows, err := Query(db, stmt)
if err != nil {
return nil, err
}
defer rows.Close()
dbs := []ResourceMonitor{}
if err := sqlx.StructScan(rows, &dbs); err != nil {
if errors.Is(err, sql.ErrNoRows) {
log.Println("[DEBUG] no resource monitors found")
return nil, nil
}
return nil, fmt.Errorf("unable to scan row for %s err = %w", stmt, err)
}
return dbs, nil
}

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'

  • There is any way to change the terraform session behavior to handle resource monitor ?

@sfc-gh-jcieslak sfc-gh-jcieslak added category:resource resource:resource_monitor Issue connected to the snowflake_resource_monitor resource labels May 20, 2024
@sfc-gh-jcieslak
Copy link
Collaborator

Hello 👋
Please refer to this response - #1175 (comment). I'll let you know whenever the updated resource is available. I think altering the session in the provider currently wouldn't make sense because we are recreating the Snowflake client a few times during the terraform apply, which could make altering session parameters useless. I guess the only way (at least in the current version of the provider) is to use the values proposed by the Snowflake, so they will match during the Read. In the later versions any timestamp will be possible to define.

@sfc-gh-jcieslak sfc-gh-jcieslak self-assigned this Sep 5, 2024
sfc-gh-jcieslak added a commit that referenced this issue Sep 11, 2024
## Changes
- Add ValuePresent assert to our custom assertions
- Add ToConfigValues function for every model
- Update Resource Monitor SDK + unit and integration tests
- Update Resource Monitor Resource + acc tests
- Handle issues connected to the resource monitor (mostly timestamp
format difference causing infinite plan or only trigger updates causing
SQL compilation error):
  -  #1500 
  - #1624 
  - #1716 
  - #1754 
  - #1821 
  - #1832 
  - #1990

## Next pr
- Adjust examples and update migration notes
- Data source (impl, tests, examples, migration notes)

## References
* [CREATE RESOURCE
MONITOR](https://docs.snowflake.com/en/sql-reference/sql/create-resource-monitor)
sfc-gh-fbudzynski pushed a commit that referenced this issue Sep 19, 2024
## Changes
- Add ValuePresent assert to our custom assertions
- Add ToConfigValues function for every model
- Update Resource Monitor SDK + unit and integration tests
- Update Resource Monitor Resource + acc tests
- Handle issues connected to the resource monitor (mostly timestamp
format difference causing infinite plan or only trigger updates causing
SQL compilation error):
  -  #1500 
  - #1624 
  - #1716 
  - #1754 
  - #1821 
  - #1832 
  - #1990

## Next pr
- Adjust examples and update migration notes
- Data source (impl, tests, examples, migration notes)

## References
* [CREATE RESOURCE
MONITOR](https://docs.snowflake.com/en/sql-reference/sql/create-resource-monitor)
@sfc-gh-jcieslak
Copy link
Collaborator

Hey 👋
The new and refactored resource monitor was released yesterday in version 0.96.0 of the provider. Please migrate with migration guide and let me know if the issue has been resolved so we could close the ticket. Thanks in advance 🙏.

@sfc-gh-jcieslak
Copy link
Collaborator

Closing due to long inactivity. Please, create another issue if you think the problem is still not resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Used to mark issues with provider's incorrect behavior category:resource resource:resource_monitor Issue connected to the snowflake_resource_monitor resource
Projects
None yet
Development

No branches or pull requests

2 participants