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

Assigning database cluster resource's port to an SRV record's port does not seem to work. #670

Closed
aphistic opened this issue Aug 11, 2021 · 2 comments · Fixed by #671
Closed
Labels

Comments

@aphistic
Copy link

Bug Report


Describe the bug

I'm trying to create a digitalocean_record SRV record that references one of my newly provisioned database clusters (done in the same plan/apply) by pointing the digitalocean_database_cluster's port output to the digitalocean_record's port field but when I try to do a terraform plan it tells me "'port' is required for when type is 'SRV'".

Affected Resource(s)

  • digitalocean_database_cluster
  • digitalocean_record

Expected Behavior

I'd expect the SRV record to have the database cluster's port value after the cluster has been provisioned.

Actual Behavior

Terraform gives me the following error:

│ Error: `port` is required for when type is `SRV`
│ 
│   with digitalocean_record.pgsql_default_pub_srv,
│   on pgsql.tf line 21, in resource "digitalocean_record" "pgsql_default_pub_srv":
│   21: resource "digitalocean_record" "pgsql_default_pub_srv" {

Steps to Reproduce

  1. Create a digitalocean_database_cluster resource.
  2. Create a digitalocean_record resource with type SRV and the port referencing the database cluster's port.
  3. Run terraform plan

Terraform Configuration Files

data "terraform_remote_state" "core" {
    backend = "remote"

    config = {
        organization = var.tf_core_org
        workspaces = {
            name = var.tf_core_workspace
        }
    }
}

resource "digitalocean_database_cluster" "pgsql_default" {
    name = "${var.do_project}-pgsql"
    engine = "pg"
    version = "11"
    size = "db-s-1vcpu-1gb"
    region = var.do_region
    node_count = 1

    private_network_uuid = data.terraform_remote_state.core.outputs.vpc.id

    tags = ["default"]
}

resource "digitalocean_record" "pgsql_default_pub" {
    domain = data.terraform_remote_state.core.outputs.domain.name

    type = "A"
    name = "pub.default.pg.${data.terraform_remote_state.core.outputs.domain.env}"
    value = digitalocean_database_cluster.pgsql_default.host
}
resource "digitalocean_record" "pgsql_default_pub_srv" {
    domain = data.terraform_remote_state.core.outputs.domain.name

    type = "SRV"
    name = "_postgresql_.tcp.${digitalocean_record.pgsql_default_pub.fqdn}"
    
    priority = 0
    weight = 0

    port = digitalocean_database_cluster.pgsql_default.port
    value = digitalocean_record.pgsql_default_pub.fqdn
}
Terraform v1.0.4
on linux_amd64
+ provider registry.terraform.io/digitalocean/digitalocean v2.10.1

Debug Output

https://gist.github.com/aphistic/f6c6f36e9d699d6c3649e8e85efb9526

Additional context

When troubleshooting I also tried to set the digitalocean_record's to a static value of 1234 and that worked fine. When I changed it back to use the port field from the database cluster it failed with the same error message again.

@aphistic aphistic added the bug label Aug 11, 2021
@andrewsomething
Copy link
Member

Thanks for reporting this @aphistic!

It looks like we're doing validation inside of a CustomizeDiff function. The interpolated values are not available there on plan. This is similar to hashicorp/terraform-provider-consul#260 Looks like we'll need to move that validation to later in the process.

CustomizeDiff: func(ctx context.Context, diff *schema.ResourceDiff, v interface{}) error {
recordType := diff.Get("type").(string)
_, hasPriority := diff.GetOkExists("priority")
if recordType == "MX" {
if !hasPriority {
return fmt.Errorf("`priority` is required for when type is `MX`")
}
}
_, hasPort := diff.GetOkExists("port")
_, hasWeight := diff.GetOkExists("weight")
if recordType == "SRV" {
if !hasPriority {
return fmt.Errorf("`priority` is required for when type is `SRV`")
}
if !hasPort {
return fmt.Errorf("`port` is required for when type is `SRV`")
}
if !hasWeight {
return fmt.Errorf("`weight` is required for when type is `SRV`")
}
}
_, hasFlags := diff.GetOkExists("flags")
_, hasTag := diff.GetOk("tag")
if recordType == "CAA" {
if !hasFlags {
return fmt.Errorf("`flags` is required for when type is `CAA`")
}
if !hasTag {
return fmt.Errorf("`tag` is required for when type is `CAA`")
}
}
return nil
},

@aphistic
Copy link
Author

Ahh, interesting! I guess I'm glad there's actually a bug and that you already know what's happening and it's not just something I did wrong. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants