hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.25k stars 1.7k forks source link

Terraform plan with 'google_compute_security_policy' not show the creation of existing resources #8335

Open skymoraa opened 3 years ago

skymoraa commented 3 years ago

Community Note

Terraform Version

Terraform v0.14.4
+ provider registry.terraform.io/hashicorp/google v3.54.0

Affected Resource(s)

Terraform Configuration Files

resource "google_compute_security_policy" "policy" {
  name = "my-policy"

  rule {
    action   = "allow"
    priority = "100"
    match {
      versioned_expr = "SRC_IPS_V1"
      config {
        src_ip_ranges = ["1.1.1.1/32"]
      }
    }
    description = "test"
  }

  rule {
    action   = "allow"
    priority = "101"
    match {
      versioned_expr = "SRC_IPS_V1"
      config {
        src_ip_ranges = ["2.2.2.2/32"]
      }
    }
    description = "test1"
  }

  rule {
    action   = "deny(404)"
    priority = "2147483647"
    match {
      versioned_expr = "SRC_IPS_V1"
      config {
        src_ip_ranges = ["*"]
      }
    }
    description = "Deny access to all IPs"
  }
}

Expected Behavior

I expect that running a terraform plan, if a resource exists but it is not present in the state on bucket, the command want to add the resource until i import it in state. For examples: rule with priority=101 created by gcp console, then add configuration into .tf file and run a plan.

Actual Behavior

Instead, terraform plan shows "No changes. Infrastructure is up-to-date.". Moreover then doing a terraform apply puts it on the state. But the plan before didn't show it.

Steps to Reproduce

  1. Create policy manualy with console
  2. Run terraform plan with the same resources (nothing is displayed)
  3. Run terraform apply on "No changes. Infrastructure is up-to-date" and the state will be refreshed

References

venkykuberan commented 3 years ago

@skymoraa if you want to bring a resource created in google console to a terraform managed resource, you need to use terraform import command. Hope that helps.

MoraAndrea commented 3 years ago

Yes I agree, but without the import the plan shouldn't show me the resource to add, because it is not present in the state? As it happens for other resources

Because terraform plan doesn't show it and if I run terraform apply the resource is still added to the state even if the plan did not say so.

venkykuberan commented 3 years ago

Can you add debug output of your tf plan command, just want to check the calls we are making there.

skymoraa commented 3 years ago

Debug Output Log: terraform_log_debug.log

Rule with priority=100 and default create with terraform. Rule priority=101 create manualy and then add configuration on tf. Terraform plan make a rest call and find resource. But doesn't it make a difference with the state?

State: stete.txt