PrefectHQ / terraform-provider-prefect

Terraform Provider for Prefect Cloud
https://registry.terraform.io/providers/PrefectHQ/prefect/latest/docs
Apache License 2.0
33 stars 16 forks source link

fix(blocks): retain data field from config on Update #248

Closed mitchnielsen closed 1 month ago

mitchnielsen commented 1 month ago

Summary

When calling Update on a block, we call Get to retrieve the latest copy of the block object from the API. This response does not include the $ref expression if it was originally provided. This was causing "inconsistent result after apply" errors.

So for now, we'll use the user-defined data field when updating the state instead of the (incomplete) data field from the Get request.

Closes https://github.com/PrefectHQ/terraform-provider-prefect/issues/240

Testing

Aside from the acceptance tests passing, I also validated manually to confirm the profile field showed the link to the profile as expected:

resource "prefect_block" "my_dbt_cli_profile" {
  name      = "my-dbt-cli-profile"
  type_slug = "dbt-cli-profile"

  data = jsonencode({
    "name"   = "mine"
    "target" = "prefect-dbt-profile"
  })
}

resource "prefect_block" "my_dbt_run_operation_block" {
  name      = "my-dbt-operations"
  type_slug = "dbt-core-operation"

  # apply this first, without the `$ref` field (incorrect configuration)
  data = jsonencode({
    "commands"        = ["dbt deps", "dbt seed", "dbt run"]
    "dbt_cli_profile" = prefect_block.my_dbt_cli_profile.id
  })

  # apply this next, to trigger the Update logic (correct configuration)
  data = jsonencode({
    "commands"        = ["dbt deps", "dbt seed", "dbt run"]
    "dbt_cli_profile" = { "$ref" : { "block_document_id" : prefect_block.my_dbt_cli_profile.id } }
  })
}
image