-
Notifications
You must be signed in to change notification settings - Fork 50
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
Resources are not sensitive to provider's default project ID changes #742
Comments
@sylviamoss Does the configured credential have access to both old and new project? Also, can you give example of kind of error shown? |
@manish-hashicorp There are several possible scenarios. For this problem to be silent the credential has to have access to both projects, but if it doesn't, the resource using the old project will fail to read instead of destroying and creating. I ran an example to get the output for different use cases. In the following examples I am using the configuration: terraform {
required_providers {
hcp = {
source = "hashicorp/hcp"
version = ">=0.65.0"
}
}
}
variable "project_id" {
description = "The project id."
type = string
}
provider "hcp" {
project_id = var.project_id
}
resource "hcp_service_principal" "example" {
name = "example-sp"
}
tf-help git:(main) ✗ terraform apply -var="project_id=ac8e2f32-37de-492f-9a2d-51c3b9a03089"
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# hcp_service_principal.example will be created
+ resource "hcp_service_principal" "example" {
+ name = "example-sp"
+ parent = (known after apply)
+ resource_id = (known after apply)
+ resource_name = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
hcp_service_principal.example: Creating...
hcp_service_principal.example: Creation complete after 0s [name=example-sp]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Then re-apply changing the provider's default project. ➜ tf-help git:(main) ✗ terraform apply -var="project_id=82950878-75b8-464a-bb02-8d07f6083085"
hcp_service_principal.example: Refreshing state... [name=example-sp]
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
➜ tf-help git:(main) ✗ The service principal continues in the old project and is not created in the new one. |
Still using org credentials, now I added provider "hcp" {
project_id = var.project_id
}
resource "hcp_service_principal" "sp" {
name = "example-sp-new-name"
}
resource "hcp_project_iam_binding" "example" {
principal_id = hcp_service_principal.sp.resource_id
role = "roles/contributor"
} Creating them using provider's default project tf-help git:(main) ✗ terraform apply -var="project_id=ac8e2f32-37de-492f-9a2d-51c3b9a03089"
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# hcp_project_iam_binding.example will be created
+ resource "hcp_project_iam_binding" "example" {
+ principal_id = (known after apply)
+ role = "roles/contributor"
}
# hcp_service_principal.sp will be created
+ resource "hcp_service_principal" "sp" {
+ name = "example-sp-new-name"
+ parent = (known after apply)
+ resource_id = (known after apply)
+ resource_name = (known after apply)
}
Plan: 2 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
hcp_service_principal.sp: Creating...
hcp_service_principal.sp: Creation complete after 0s [name=example-sp-new-name]
hcp_project_iam_binding.example: Creating...
hcp_project_iam_binding.example: Creation complete after 1s Re-applying without changing the configuration. tf-help git:(main) ✗ terraform apply -var="project_id=82950878-75b8-464a-bb02-8d07f6083085"
hcp_service_principal.sp: Refreshing state... [name=example-sp-new-name]
hcp_project_iam_binding.example: Refreshing state...
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# hcp_project_iam_binding.example will be created
+ resource "hcp_project_iam_binding" "example" {
+ principal_id = "example-sp-new-name-023501@ac8e2f32-37de-492f-9a2d-51c3b9a03089"
+ role = "roles/contributor"
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
hcp_project_iam_binding.example: Creating...
╷
│ Error: failed to retrieve organization IAM policy
│
│ with hcp_project_iam_binding.example,
│ on main.tf line 98, in resource "hcp_project_iam_binding" "example":
│ 98: resource "hcp_project_iam_binding" "example" {
│
│ [PUT /resource-manager/2019-12-10/projects/{id}/iam-policy][400] ProjectService_SetIamPolicy default &{Code:9 Details:[] Message:failed to set IAM policy for project: not all service principals
│ in the policy are members of the org or the project} |
Closed with #808. |
Hey @manish-hashicorp, PR #808 doesn't fix all the affected resources but only some of them. I will reopen the issue. Let me know if you think otherwise. |
Description
Resources are not sensitive to the provider's default project ID changes. A plan modifier is already implemented and used by the
hcp_notifications_webhook
resource.https://github.com/hashicorp/terraform-provider-hcp/blob/main/internal/provider/modifiers/project.go#L26
https://github.com/hashicorp/terraform-provider-hcp/blob/main/internal/provider/webhook/resource_notifications_webhook.go#L195
Affected Resource(s)
The majority of resources in the provider do not implement a modifier to identify default project ID changes.
Terraform Configuration Files
Steps to Reproduce
terraform apply
terraform plan/apply
Expected Behavior
After the provider's default project ID is changed, any resource inheriting the ID should be recreated under the new project.
Actual Behavior
After the provider's default project ID is changed, the plan/apply will fail to read the resource or depending on the resource not identify the changes.
Community Note
The text was updated successfully, but these errors were encountered: