akshaykarle / terraform-provider-mongodbatlas

Terraform provider for MongoDB Atlas
Mozilla Public License 2.0
123 stars 54 forks source link

mongodbatlas_vpc_peering_connection import doesn't import properly #94

Closed xM8WVqaG closed 5 years ago

xM8WVqaG commented 5 years ago

PR #80 added support for GCP, which added a new required argument provider_name on mongodbatlas_vpc_peering_connection. I've updated to the documentation to include this argument here: #93 but there doesn't appear to be a way to upgrade an AWS peering_connection to the latest version of the provider without recreating the connection.

Even if you remove the provider from state, and import it again, it still doesn't pick up which provider the connection should be associated with and will attempt to recreate it.

This information should be inferable in the response from the MongoDBAtlas API: https://docs.atlas.mongodb.com/reference/api/vpc-get-connection/.

Version

Tested on both TF 11.13 and TF 12.2 with version 1.1.0 of the MongoDBAtlas provider.

Example

Sample config:

resource "mongodbatlas_vpc_peering_connection" "example" {
  group                  = "${mongodbatlas_project.example.id}"
  provider_name          = "AWS"
  aws_account_id         = "${var.aws_account_id}"
  vpc_id                 = "${module.vpc.vpc_id}"
  route_table_cidr_block = "10.0.0.0/16"
  container_id           = "${mongodbatlas_container.example.id}"
}

resource "aws_vpc_peering_connection_accepter" "example" {
  vpc_peering_connection_id = "${mongodbatlas_vpc_peering_connection.example.connection_id}"
  auto_accept               = true
}

If you attempt to run tf plan without the new provider_name argument:

$ tf plan

Error: mongodbatlas_vpc_peering_connection.example: "provider_name": required field is not set

If you add the provider_name argument, you then have to recreate the resource regardless.

$ tf plan

<snip>
-/+ aws_vpc_peering_connection_accepter.example (new resource required)
      id:                         "pcx-REDACTED" => <computed> (forces new resource)
      accept_status:              "active" => <computed>
      accepter.#:                 "1" => <computed>
      auto_accept:                "true" => "true"
      peer_owner_id:              "REDACTED" => <computed>
      peer_region:                "eu-west-1" => <computed>
      peer_vpc_id:                "vpc-REDACTED" => <computed>
      requester.#:                "0" => <computed>
      vpc_id:                     "vpc-REDACTED" => <computed>
      vpc_peering_connection_id:  "pcx-REDACTED" => "${mongodbatlas_vpc_peering_connection.example.connection_id}" (forces new resource)

-/+ mongodbatlas_vpc_peering_connection.example (new resource required)
      id:                         "REDACTED" => <computed> (forces new resource)
      aws_account_id:             "" => "REDACTED" (forces new resource)
      connection_id:              "" => <computed>
      container_id:               "REDACTED1" => "REDACTED1"
      error_message:              "" => <computed>
      error_state_name:           "" => <computed>
      group:                      "REDACTED2" => "REDACTED2"
      identifier:                 "REDACTED" => <computed>
      provider_name:              "" => "AWS" (forces new resource)
      route_table_cidr_block:     "" => "10.0.0.0/16"
      status:                     "" => <computed>
      status_name:                "" => <computed>
      vpc_id:                     "" => "vpc-REDACTED" (forces new resource)

<snip>

Removing the existing state:

$ tf state rm mongodbatlas_vpc_peering_connection.example

1 items removed.
Item removal successful.

Reimporting:

$ tf import mongodbatlas_vpc_peering_connection.example REDACTEDA-REDACTEDB
mongodbatlas_vpc_peering_connection.example: Importing from ID "REDACTEDA-REDACTEDB"...
mongodbatlas_vpc_peering_connection.example: Import complete!
  Imported mongodbatlas_vpc_peering_connection (ID: REDACTEDB)
mongodbatlas_vpc_peering_connection.example: Refreshing state... (ID: REDACTEDB)

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

Then running terraform plan will still attempt to recreate the resource even though it's fine:

$ tf plan

<snip>
-/+ aws_vpc_peering_connection_accepter.example (new resource required)
      id:                         "pcx-REDACTED" => <computed> (forces new resource)
      accept_status:              "active" => <computed>
      accepter.#:                 "1" => <computed>
      auto_accept:                "true" => "true"
      peer_owner_id:              "REDACTED" => <computed>
      peer_region:                "eu-west-1" => <computed>
      peer_vpc_id:                "vpc-REDACTED" => <computed>
      requester.#:                "0" => <computed>
      vpc_id:                     "vpc-REDACTED" => <computed>
      vpc_peering_connection_id:  "pcx-REDACTED" => "${mongodbatlas_vpc_peering_connection.example.connection_id}" (forces new resource)

-/+ mongodbatlas_vpc_peering_connection.example (new resource required)
      id:                         "REDACTED" => <computed> (forces new resource)
      aws_account_id:             "" => "REDACTED" (forces new resource)
      connection_id:              "" => <computed>
      container_id:               "REDACTED1" => "REDACTED1"
      error_message:              "" => <computed>
      error_state_name:           "" => <computed>
      group:                      "REDACTED2" => "REDACTED2"
      identifier:                 "REDACTED" => <computed>
      provider_name:              "" => "AWS" (forces new resource)
      route_table_cidr_block:     "" => "10.0.0.0/16"
      status:                     "" => <computed>
      status_name:                "" => <computed>
      vpc_id:                     "" => "vpc-REDACTED" (forces new resource)

<snip>

Expectation

I shouldn't have to recreate a connection (or manually hack the state) after import because of the provider_name block.

Reality

You cannot currently import an AWS mongodbatlas_vpc_peering_connection.

gordonbondon commented 5 years ago

I’ve submitted a pr that should fix all of this #95

akshaykarle commented 5 years ago

closed by https://github.com/akshaykarle/terraform-provider-mongodbatlas/pull/95