Constellix / terraform-provider-constellix

Terraform Constellix provider
https://www.terraform.io/docs/providers/constellix/
Mozilla Public License 2.0
7 stars 21 forks source link

CNAME and A record geolocation properties not importing correctly #19

Open coxjonc opened 3 years ago

coxjonc commented 3 years ago

Description

constellix_a_record and constellix_cname_record fail to import geolocation properties correctly on the latest release. This bug affects the Import methods on both resource types. It may also affect other record types - I've only checked CNAME and A records.

Replicate (1)

On release v0.3.5 (commit 4e5aefac597661bfc6ab09a533d0f73a029ed4bf)

Use the following Terraform configuration to provision a domain and record:

provider constellix {
  apikey    = <API KEY HERE>
  secretkey = <SECRET KEY HERE> 
}

terraform {
  required_providers {
    constellix = {
      source = "Constellix/constellix"
      version = "0.3.5"
    }
  }
}

resource constellix_domain replicate1 {
  name = "domain1.dne.com"
  soa = {
    primary_nameserver = "ns41.constellix.com."
    ttl                = 1800
    refresh            = 48100
    retry              = 7200
    expire             = 1209
    negcache           = 8000
  }
}

resource constellix_cname_record replicate1 {
  name                          = "replicate"
  domain_id                     = constellix_domain.replicate1.id
  host                          = "test."
  source_type                   = "domains"
  ttl                           = 300

  geo_location = {
    drop = false
    geo_ip_failover = false
  }
}

After provisioning the resources, retrieve the CNAME record's ID and remove the resource from Terraform state:

$ terraform state rm constellix_cname_record.replicate1 <ID OF CNAME RECORD>

The resource is now untracked. Import it back into state.

$ terraform import constellix_cname_record.replicate1 domains:<DOMAIN ID>:<CNAME ID>

Run terraform show and note that the geolocation block is not populated. terraform plan will say that changes are required to the existing infrastructure to add geolocation properties.

Replicate (2)

Modify constellix/resource_constellix_cname_record_test.go and add a geo_location object to the HCL string in the testAccCheckConstellixCNameConfig_basic method. Pipe the following to git-apply

diff --git a/constellix/resource_constellix_cname_record_test.go b/constellix/resource_constellix_cname_record_test.go
index 9626737..d2abff8 100644
--- a/constellix/resource_constellix_cname_record_test.go
+++ b/constellix/resource_constellix_cname_record_test.go
@@ -80,6 +80,10 @@ func testAccCheckConstellixCNameConfig_basic(ttl int) string {
                ttl = "%d"
                note = "Practice record naptr"
                record_option = "failover"
+               geo_location = {
+                       drop = false
+                       geo_ip_failover = false
+               }
            record_failover_values  {
                             value = "a."
                             sort_order = 2

and run the TestAccCName_Basic test. You'll note the test fails, complaining of nonempty plan output for the newly created resource.

=== RUN   TestAccCName_Basic
--- FAIL: TestAccCName_Basic (23.85s)
    testing.go:684: Step 0 error: After applying this step, the plan was not empty:

Root cause

I think the root cause of this problem is that the schema for the geolocation block is defined incorrectly for both A and CNAME record types. It's declared as TypeMap, but uses the Elem block to define specific keys. According to the Terraform docs this is a misconfiguration. To enforce type constraints on predefined keys, use TypeSet. PR #14 fixes the import behavior by changing the geolocation field to TypeSet, but is backwards incompatible.