heroku / terraform-provider-heroku

Terraform Heroku provider
https://registry.terraform.io/providers/heroku/heroku/latest
Mozilla Public License 2.0
99 stars 75 forks source link

Importing postgres-db doesn't import config version #176

Open chdsbd opened 5 years ago

chdsbd commented 5 years ago

Terraform Version

Terraform v0.11.11

Heroku Provider Version

provider.heroku v1.4.0

Affected Resource(s)

Terraform Configuration Files

resource "heroku_addon" "postgres-db" {
  app  = "example-app"
  plan = "heroku-postgresql:hobby-basic"
  config = {
    version = "10"
  }
}

Debug Output

N/A

Panic Output

N/A

Expected Behavior

Importing a postgres-db resource should update the config state to match the version config definition on Heroku.

If I have a postgres database on Heroku that is configured to be version 10, when I import that resource, the Terraform state should include config.version = "10".

So in my example, while my Terraform configuration matches the Heroku add-on. When I import the Heroku resource, the config version of "10" is not imported. So Terraform thinks it needs to recreate the resource to update that state.

Actual Behavior

The config version was not updated and terraform wants to recreate the resource from scratch

Steps to Reproduce

  1. create a postgres database addon
  2. create a terraform resource for the database
  3. import the database into that resource
  4. terraform apply and observe that a new resource is going to be created

Important Factoids

N/A

References

It seems like the code for importing addons is incorrect.

When creating resources, we set config but when importing, we do not.

mars commented 5 years ago

Hi @chdsbd 😄

Importing a postgres-db resource should update the config to match the version

This is not how terraform import works. The HCL config must already match the pre-existing resource's state.

See Terraform docs about import, "It does not generate configuration."

chdsbd commented 5 years ago

I think you misunderstand. I created a resource that matches the Heroku config, but when I import, the heroku provider doesn't import the config version.

Edit: by "config", I literally mean the "config" field in the Terraform state. The "config" state is not pulled down from Heroku when importing.

chdsbd commented 5 years ago

@mars I've updated my expected behavior a bit. Please reopen this PR.

chdsbd commented 5 years ago

I think I found the bug. Please see the updated references section of this issue.

davidji99 commented 5 years ago

This is a limitation of the Heroku Platform API. I'm not seeing an API endpoint that would allow the provider to GET the config of the addon. This endpoint only returns the full URL of the postgres DB and that's it. This endpoint doesn't return the config either; only the config vars for the addon, which are different. Without the ability to get GET the config of the postgres addon, it would be hard to set it during a terraform import.

chdsbd commented 5 years ago

Thanks for looking that up @davidji99. It seems like this should be labeled "Heroku API Support".

chdsbd commented 5 years ago

Actually, on second pass, I think this endpoint might work: https://devcenter.heroku.com/articles/platform-api-reference#add-on-config-list

EDIT: This is the endpoint for fetching config_vars, not config.

crisp2u commented 4 years ago

Hello, I recently ran into this after Heroku updated Postgres to v12. Since we have now way to get the initial config from the API, my workaround for existing DB addons is to specify the desired version but ignore all changes in the attributes. Which I should have done in the first place since I don't want TF to manage changes to the database

resource "heroku_addon" "database" {
  app    = "myapp"
  plan   = "heroku-postgresql:standard-0"
  config = {
    version = "11"
  }
  lifecycle {
    ignore_changes = all
  }
}