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

cloudflare_custom_hostname import does not import anything #783

Closed
bryanchug opened this issue Sep 2, 2020 · 6 comments · Fixed by #788
Closed

cloudflare_custom_hostname import does not import anything #783

bryanchug opened this issue Sep 2, 2020 · 6 comments · Fixed by #788

Comments

@bryanchug
Copy link

bryanchug commented Sep 2, 2020

Terraform Version

Terraform v0.13.2
+ provider registry.terraform.io/cloudflare/cloudflare v2.10.1
+ provider registry.terraform.io/hashicorp/aws v3.1.0

Affected Resource(s)

  • cloudflare_custom_hostname

If this issue appears to affect multiple resources, it may be an issue with Terraform's core, so please mention this.

Terraform Configuration Files

resource "cloudflare_custom_hostname" "custom_hostname" {
  zone_id = "<ZONE_ID>"
  hostname = "xxxx.xxxx.com"
  custom_origin_server = "xxx.xxxx.com"
  ssl {
    method = "http"
    type = "dv"
    wildcard = "false"
    settings {
      min_tls_version = "1.0"
    }
  }
}

Debug Output

Skipped this to avoid showing secrets

Panic Output

No panic output

Expected Behavior

What should have happened?

terraform show should have shown info such as hostname after doing an import like so:
terraform import cloudflare_custom_hostname.custom_hostname <ZONE_ID>/<CUSTOM_HOSTNAME_ID>

Actual Behavior

What actually happened?

terraform show doesn't show anything, only the IDs previously inputted:

resource "cloudflare_custom_hostname" "custom_hostname" {
    id                          = "<CUSTOM_HOSTNAME_ID>"
    ownership_verification      = {
        "name"  = ""
        "type"  = ""
        "value" = ""
    }
    ownership_verification_http = {
        "http_body" = ""
        "http_url"  = ""
    }
    zone_id                     = "<ZONE_ID>"
}

Steps to Reproduce

  1. terraform import cloudflare_custom_hostname.custom_hostname <ZONE_ID>/<CUSTOM_HOSTNAME_ID>
  2. terraform show

Important Factoids

None.

References

#749 #769
#770 #761 #746

@jacobbednarz
Copy link
Member

@bryanchug Can you please provide the debug log? You can redact/clean up any sensitive information from it.

@bryanchug
Copy link
Author

@catemlitten
Copy link

Hi @jacobbednarz -- I was helping @bryanchug look into this and think I can clarify a few points:
When importing the cloudflare_custom_hostname the import is listed as successful however the problem that arises is that hostname is not set (instead it is viewed a new field requiring replacement) which means Terraform will try to delete/recreate the resource when you run a plan. A condensed log of what I'm seeing below:

terraform import cloudflare_custom_hostname.foo [zone_id]/[custom_hostname_id]
cloudflare_custom_hostname.foo: Importing from ID "[zone_id]/[custom_hostname_id]"...
cloudflare_custom_hostname.foo: Import prepared!
  Prepared cloudflare_custom_hostname for import
cloudflare_custom_hostname.foo: Refreshing state... [id=redacted]

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.

terraform plan 
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

 data.cloudflare_zones.bar: Refreshing state...
 cloudflare_custom_hostname.foo: Refreshing state... [id=redacted]
 
 ------------------------------------------------------------------------
 
 An execution plan has been generated and is shown below.
 Resource actions are indicated with the following symbols:
 -/+ destroy and then create replacement
 
 Terraform will perform the following actions:
 
    cloudflare_custom_hostname.foo must be replaced
 -/+ resource "cloudflare_custom_hostname" "foo" {
       + hostname                    = "foo.example.com" # forces replacement
       ~ id                          = "redacted" -> (known after apply)
       ~ ownership_verification      = {
           - "name"  = ""
           - "type"  = ""
           - "value" = ""
         } -> (known after apply)
       ~ ownership_verification_http = {
           - "http_body" = ""
           - "http_url"  = ""
         } -> (known after apply)
       + status                      = (known after apply)
         zone_id                     = "redacted"
       + ssl {
           + method = "http"
           + type   = "dv"
         }
     }
 
 Plan: 1 to add, 0 to change, 1 to destroy.

Looking at the state file for the imported resource I see this:

"resources": [
    {
      "mode": "managed",
      "type": "cloudflare_custom_hostname",
      "name": "foo",
      "provider": "provider.cloudflare",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "custom_origin_server": null,
            "hostname": null, <---- I think this is the problem child
            "id": "redacted",
            "ownership_verification": {
              "name": "",
              "type": "",
              "value": ""
            },
            "ownership_verification_http": {
              "http_body": "",
              "http_url": ""
            },
            "ssl": [],
            "status": null,
            "zone_id": "redacted"
          },
          "private": "redacted"
        }
      ]
    }
  ]

I hope this helps clarify the issue. :)

@bryanchug bryanchug changed the title cloudflare_custom_hostname import does not work cloudflare_custom_hostname import does not import anything Sep 4, 2020
@jacobbednarz
Copy link
Member

Thanks @bryanchug and @catemlitten, that all makes sense now. I'll take a pass at fixing this one shortly.

jacobbednarz added a commit to jacobbednarz/terraform-provider-cloudflare that referenced this issue Sep 6, 2020
Within the `Read` function, we weren't setting the hostname to the
imported value (assuming it was always provided) which has left it
missing in state and subsequently causing replacements unnecessarily.

Fixes cloudflare#783
@jacobbednarz
Copy link
Member

I've had a look at this one and pushed up #788 to address. You should be able to run this locally (after building the binary) and have it solve your issue.

patryk pushed a commit that referenced this issue Sep 7, 2020
Within the `Read` function, we weren't setting the hostname to the
imported value (assuming it was always provided) which has left it
missing in state and subsequently causing replacements unnecessarily.

Fixes #783
@bryanchug
Copy link
Author

We might just wait for the next version release. Thanks @jacobbednarz!

boekkooi-lengoo pushed a commit to boekkooi-lengoo/terraform-provider-cloudflare that referenced this issue Feb 28, 2022
fail CI if the struct size can be improved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants