Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save thehunmonkgroup/253d691f14b3d57f4ac8b6bfd14f9d12 to your computer and use it in GitHub Desktop.

Select an option

Save thehunmonkgroup/253d691f14b3d57f4ac8b6bfd14f9d12 to your computer and use it in GitHub Desktop.
Error on Terraform Apply: Bool Required

USER

Getting this error on terraform apply:

│ Error: a bool is required
│
│   with module.provider-pre.constellix_txt_record.records["@"],
│   on ../../../../modules/providers/constellix/provider-pre/main.tf line 117, in resource "constellix_txt_record" "records":
│  117: resource "constellix_txt_record" "records" {

Here's the relevant resource config:

resource "constellix_txt_record" "records" {
  for_each = try(var.dns_records.txt, {})
  domain_id = constellix_domain.base.id
  source_type = "domains"
  type = "TXT"
  name  = each.key == "@" ? "" : each.key
  dynamic "roundrobin" {
    for_each = each.value.values
    content {
      value = roundrobin.value.value
    }
  }
  ttl = lookup(each.value, "ttl", 300)
  lifecycle {
    prevent_destroy = true
  }
}

Here's the doc for the constellix_txt_record resource:

constellix_txt_record
Manages records of type TXT for a specific domain.

Example Usage
resource "constellix_txt_record" "txtrecord1" {
  domain_id   = "${constellix_domain.domain1.id}"
  ttl         = 1800
  name        = "txtrecord"
  noanswer    = false
  note        = ""
  gtd_region  = 1
  type        = "TXT"
  source_type = "domains"
  roundrobin {
    value        = "\"{\\\"cfg\\\":[{\\\"useAS\\\":0}]}\""
    disable_flag = false
  }
}
Copy
Argument Reference
domain_id - (Required) Record id of TXT record
ttl - (Required) TTL must be in between 0 and 2147483647
source_type - (Required) "domains" for Domain records and "template" for Template records
roundrobin - (Required) Set
roundrobin.value - (Required) Free form text data of any type which may be no longer than 255 characters unless divided into multiple strings with sets of quotation marks..
roundrobin.disable_flag - (Optional) Disable flag. Default is false
name - (Optional) Name of record. Name should be unique.
noanswer - (Optional) Shows if record is enabled or disabled. Default is false (Active)
note - (Optional) Record note
gtd_region - (Optional) Shows id of GTD region in which record is to be created, 1 for World (Default), 2 for Europe, 3 for US East, 4 for US West, 5 for Asia Pacific, 6 for Oceania, note: "gtdRegion" from 2 to 6 will be applied only when GTD region is enabled on domain.
type - (Optional) Record type TXT

I don't see any required bool arguments there... What might be the issue, and how can I get better debug data for WHAT bool is required??


ASSISTANT

The error message you're seeing indicates that Terraform is expecting a boolean value somewhere in your constellix_txt_record resource definition, but it's not clear from the message exactly where that boolean is required.

From the documentation snippet you provided, the only optional boolean arguments for the constellix_txt_record resource are noanswer and roundrobin.disable_flag. Since these are optional and have default values, they should not be causing the issue if they are omitted.

However, the error message suggests that the problem is with the roundrobin block. The roundrobin.disable_flag is an optional boolean field, and if it's not specified, it defaults to false. If you are not setting this field, it should not be the source of the error.

To debug this further, you can add explicit values for the optional boolean fields to see if that resolves the issue. Here's an example of how you might modify your roundrobin block to include the disable_flag explicitly:

resource "constellix_txt_record" "records" {
  for_each = try(var.dns_records.txt, {})
  domain_id = constellix_domain.base.id
  source_type = "domains"
  type = "TXT"
  name  = each.key == "@" ? "" : each.key
  dynamic "roundrobin" {
    for_each = each.value.values
    content {
      value = roundrobin.value.value
      disable_flag = false  # Explicitly setting the optional boolean field
    }
  }
  ttl = lookup(each.value, "ttl", 300)
  lifecycle {
    prevent_destroy = true
  }
}

Additionally, you can enable more verbose logging in Terraform to get better debug data. Set the TF_LOG environment variable to DEBUG or TRACE before running terraform apply:

export TF_LOG=DEBUG
terraform apply

This will provide more detailed logs that might help pinpoint the exact location and cause of the error.

If the issue persists, double-check the var.dns_records.txt variable structure to ensure it matches the expected format for the roundrobin block. The error could be due to a mismatch between the expected data structure and the actual data being provided.


USER

No, fix it so it tries to lookup the disable_flag value, and set it to false if not found in roundrobin.value


ASSISTANT

To fix the issue by trying to look up the disable_flag value and setting it to false if not found within roundrobin.value, you can use the lookup function within the roundrobin block. Here's how you can modify your roundrobin block to include this logic:

resource "constellix_txt_record" "records" {
  for_each = try(var.dns_records.txt, {})
  domain_id = constellix_domain.base.id
  source_type = "domains"
  type = "TXT"
  name  = each.key == "@" ? "" : each.key
  dynamic "roundrobin" {
    for_each = each.value.values
    content {
      value = roundrobin.value.value
      disable_flag = lookup(roundrobin.value, "disable_flag", false)
    }
  }
  ttl = lookup(each.value, "ttl", 300)
  lifecycle {
    prevent_destroy = true
  }
}

In this code, lookup is used to search for the disable_flag key within each roundrobin.value. If the disable_flag key is not found, it defaults to false. This ensures that the disable_flag is always set to a boolean value, as required by the constellix_txt_record resource.


USER

This appears to be a bug in the constellix provider plugin, as my resource works fine in v0.3.9, but is broken in v0.3.10.

Here is a diff of the relevant .go file between those two versions, are you able to diagnose where the bug is?

diff --git a/constellix/resource_constellix_txt_record.go b/constellix/resource_constellix_txt_record.go
index 1e09b84..f9e55d5 100644
--- a/constellix/resource_constellix_txt_record.go
+++ b/constellix/resource_constellix_txt_record.go
@@ -96,7 +96,7 @@ func resourceConstellixTxt() *schema.Resource {
                                                },

                                                "disable_flag": &schema.Schema{
-                                                   Type:     schema.TypeString,
+                                                 Type:     schema.TypeBool,
                                                        Optional: true,
                                                        Computed: true,
                                                },
@@ -137,13 +137,14 @@ func resourceConstellixTxtImport(d *schema.ResourceData, m interface{}) ([]*sche
        d.Set("parentid", data["parentId"])
        d.Set("parent", data["parent"])
        d.Set("source", data["source"])
+
        resrr := (data["roundRobin"]).([]interface{})
        mapListRR := make([]interface{}, 0, 1)
        for _, val := range resrr {
                tpMap := make(map[string]interface{})
                inner := val.(map[string]interface{})
-           tpMap["value"] = fmt.Sprintf("%v", inner["value"])
-           tpMap["disableFlag"] = fmt.Sprintf("%v", inner["disableFlag"])
+         tpMap["value"] = stripQuotes(inner["value"].(string)) // removing the quotes added by the server during the GET call
+         tpMap["disable_flag"] = inner["disableFlag"].(bool)
                mapListRR = append(mapListRR, tpMap)
        }

@@ -330,8 +331,8 @@ func resourceConstellixTxtRead(d *schema.ResourceData, m interface{}) error {
        for _, val := range resrr {
                tpMap := make(map[string]interface{})
                inner := val.(map[string]interface{})
-           tpMap["value"] = fmt.Sprintf("%v", inner["value"])
-           tpMap["disableFlag"] = fmt.Sprintf("%v", inner["disableFlag"])
+         tpMap["value"] = stripQuotes(inner["value"].(string)) // removing the quotes added by the server during the GET call
+         tpMap["disable_flag"] = inner["disableFlag"].(bool)
                mapListRR = append(mapListRR, tpMap)
        }

When reviewing this change, reflect on how a Terraform provider is implemented, and what may cause this change to throw the error I provided for roundrobin.disable_flag


ASSISTANT

The diff you've provided shows changes in the constellix/resource_constellix_txt_record.go file between two versions of the Constellix Terraform provider. The key change that stands out is the modification of the disable_flag field's type from schema.TypeString to schema.TypeBool.

Here's the specific change:

"disable_flag": &schema.Schema{
-   Type:     schema.TypeString,
+   Type:     schema.TypeBool,
    Optional: true,
    Computed: true,
},

This change in the schema definition means that the disable_flag is now expected to be a boolean (true or false) rather than a string ("true" or "false"). This could potentially cause issues if the Terraform configuration or state file still contains string representations of boolean values for this field.

Additionally, the import and read functions have been updated to directly cast the disableFlag value to a boolean:

- tpMap["disableFlag"] = fmt.Sprintf("%v", inner["disableFlag"])
+ tpMap["disable_flag"] = inner["disableFlag"].(bool)

This change assumes that the disableFlag value coming from the API response is already a boolean. If the API returns a non-boolean value (like a string), this type assertion will fail, and Terraform will throw an error.

Given the error you're encountering, it's possible that the state file or the Terraform configuration still contains string values for the disable_flag, which are now incompatible with the updated provider schema expecting a boolean.

To resolve this issue, you can do the following:

  1. Ensure that your Terraform configuration passes a boolean value for disable_flag rather than a string.
  2. If you have existing state files, you may need to manually update them to reflect the new boolean type for disable_flag, or re-import the resources to update the state file automatically.

If the issue is indeed a bug in the provider, you may want to report it to the maintainers of the Constellix Terraform provider, providing them with the details of the error and the changes that seem to have introduced the issue. They may need to handle the type conversion more gracefully or ensure backward compatibility with existing configurations and state files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment