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

Importing google_compute_project_metadata_item fails with "Error: project: required field is not set" #5742

Closed
pdecat opened this issue Feb 24, 2020 · 8 comments
Assignees
Labels

Comments

@pdecat
Copy link
Contributor

pdecat commented Feb 24, 2020

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request.
  • Please do not leave +1 or me too comments, they generate extra noise for issue followers and do not help prioritize the request.
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment.
  • If an issue is assigned to the modular-magician user, it is either in the process of being autogenerated, or is planned to be autogenerated soon. If an issue is assigned to a user, that user is claiming responsibility for the issue. If an issue is assigned to hashibot, a community member has claimed the issue already.

Terraform Version

# terraform version
Terraform v0.12.21
+ provider.google v3.9.0

Note: same issue happens with provider version 2.20.2.

Affected Resource(s)

  • google_compute_project_metadata_item

Terraform Configuration Files

provider "google" {
  version = "3.9.0"

  project = "myproject"
  region  = "europe-west1"
}

resource "google_project" "project" {
  name            = "myproject"
  project_id      = "myproject"
  billing_account = "123456-123456-123456"
}

resource "google_compute_project_metadata_item" "project_disable_legacy_endpoints" {
  project = google_project.project.project_id

  key   = "disable-legacy-endpoints"
  value = "TRUE"
}

Debug Output

https://gist.github.com/pdecat/95b65fcc68f834bc42837a3d79d62b7c

Panic Output

N/A

Expected Behavior

Import should be successful.

Actual Behavior

Import fails:

# terraform import google_compute_project_metadata_item.project_disable_legacy_endpoints disable-legacy-endpoints
google_compute_project_metadata_item.project_disable_legacy_endpoints: Importing from ID "disable-legacy-endpoints"...
google_compute_project_metadata_item.project_disable_legacy_endpoints: Import prepared!
  Prepared google_compute_project_metadata_item for import
google_compute_project_metadata_item.project_disable_legacy_endpoints: Refreshing state... [id=disable-legacy-endpoints]

Error: project: required field is not set

Steps to Reproduce

  1. terraform import google_compute_project_metadata_item.project_disable_legacy_endpoints disable-legacy-endpoints

Important Factoids

References

@ghost ghost added the bug label Feb 24, 2020
@pdecat
Copy link
Contributor Author

pdecat commented Feb 24, 2020

Hmm, this issue does not happen with an new empty state:

# terraform import google_compute_project_metadata_item.project_disable_legacy_endpoints disable-legacy-endpoints
google_compute_project_metadata_item.project_disable_legacy_endpoints: Importing from ID "disable-legacy-endpoints"...
google_compute_project_metadata_item.project_disable_legacy_endpoints: Import prepared!
  Prepared google_compute_project_metadata_item for import
google_compute_project_metadata_item.project_disable_legacy_endpoints: Refreshing state... [id=disable-legacy-endpoints]

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.

Mine was just upgraded from 2.20.2 to 3.9.0.
Also, in my actual use case, the project is in a module.

@pdecat
Copy link
Contributor Author

pdecat commented Feb 24, 2020

Import also works with an empty state when resources are in a module:

main.tf:

provider "google" {
  version = "3.9.0"

  project = "myproject"
  region  = "europe-west1"
}

module "root_state" {
  source = "./root_state"
}

root_state/main.tf:

resource "google_project" "project" {
  name            = "myproject"
  project_id      = "myproject"
  billing_account = "123456-123456-123456"
}

resource "google_compute_project_metadata_item" "project_disable_legacy_endpoints" {
  project = google_project.project.project_id

  key   = "disable-legacy-endpoints"
  value = "TRUE"
}
# terraform import module.root_state.google_compute_project_metadata_item.project_disable_legacy_endpoints disable-legacy-endpoints
module.root_state.google_compute_project_metadata_item.project_disable_legacy_endpoints: Importing from ID "disable-legacy-endpoints"...
module.root_state.google_compute_project_metadata_item.project_disable_legacy_endpoints: Import prepared!
  Prepared google_compute_project_metadata_item for import
module.root_state.google_compute_project_metadata_item.project_disable_legacy_endpoints: Refreshing state... [id=disable-legacy-endpoints]

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.

@edwardmedia edwardmedia self-assigned this Feb 24, 2020
@edwardmedia
Copy link
Contributor

@pdecat you provided two cases in which import works for empty state. That is expected. If I leave the state file in place, I got below error, which is not the same as you got. What is the scenario for NON empty state? Can you specify the steps how you hit your error and what exact config do you have then?

google_compute_project_metadata_item.project_disable_legacy_endpoints: Importing from ID "my_metadata"...
google_compute_project_metadata_item.project_disable_legacy_endpoints: Import prepared!
  Prepared google_compute_project_metadata_item for import

Error: Resource already managed by Terraform

Terraform is already managing a remote object for
google_compute_project_metadata_item.project_disable_legacy_endpoints. To
import to this address you must first remove the existing object from the
state.

@pdecat
Copy link
Contributor Author

pdecat commented Feb 24, 2020

I just added that google_compute_project_metadata_item part this morning to catch up with out of band changes I noticed:

resource "google_compute_project_metadata_item" "project_disable_legacy_endpoints" {
  project = google_project.project.project_id

  key   = "disable-legacy-endpoints"
  value = "TRUE"
}

The first version of the state for that stack was created in 2016 (terraform 0.7.4 at the time, before the big provider split) and was upgraded to almost all versions of terraform and the google provider since then.

I wanted to import the project metadata item resource to avoid updating the Google Compute Metadata and faced the issue described here.

@ghost ghost removed the waiting-response label Feb 24, 2020
@edwardmedia
Copy link
Contributor

@pdecat can you try to import if you have the config like below, then manually add the key / value in the config accordingly?

resource "google_compute_project_metadata_item" "project_disable_legacy_endpoints" {
 project = google_project.project.project_id
}

@pdecat
Copy link
Contributor Author

pdecat commented Feb 24, 2020

@edwardmedia, thanks for the help on this issue!

I've tried commenting these fields but the same issue still happened.

Doing further tests, I've managed to locate the cause.
I had this in my root_state module that came from pre-0.11 configuration (we fixed this in our other modules a long time ago):

provider google {
  version = "~> 3.9.0"
}

The underlying issue is described here: hashicorp/terraform#16835

I replaced it with the following and the import worked:

terraform {
  required_providers {
    google = "~> 3.9.0"
  }
}

I guess we can close this issue.

Best regards,
Patrick.

@ghost ghost removed the waiting-response label Feb 24, 2020
@edwardmedia
Copy link
Contributor

@pdecat Great. I am glad you have identified the problem. I am closing this issue now. Thank you for using the tool

@ghost
Copy link

ghost commented Mar 26, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

@ghost ghost locked and limited conversation to collaborators Mar 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants