mongodb / terraform-provider-mongodbatlas

Terraform MongoDB Atlas Provider: Deploy, update, and manage MongoDB Atlas infrastructure as code through HashiCorp Terraform
https://registry.terraform.io/providers/mongodb/mongodbatlas
Mozilla Public License 2.0
240 stars 168 forks source link

Can't get PrivateLink-aware mongodb+srv address when using privatelink #147

Closed jlforester closed 4 years ago

jlforester commented 4 years ago

In 0.4.0 of the provider, there doesn't appear to be a way to retrieve the privatelink aware mongodb+srv address for my cluster. The srv_address attribute of the mongodbatlas_cluster resource returns the same URI as before. When using the Atlas console 'connect' option on the cluster, you can see that there are two different URIs depending on whether you want to use the Privatelink address or the standard address.

Here are two URIs for a cluster when PrivateLink is used:

mongodb+srv://angua-dj2pl.mongodb.net
mongodb+srv://angua-pl-0-dj2pl.mongodb.net

The first is the standard connection address, and the second is the PrivateLink-aware address.

My workaround is to take the value returned by srv_address and do a replace() to get the right address, but this seems fragile.

themantissa commented 4 years ago

@jlforester Thank you for the report. The returned private link connection string was not yet available for this version, 0.4.0. We'll ensure we add in support as soon as we can get it on the schedule.

themantissa commented 4 years ago

@jlforester our documentation is now updated with how to get the correct connection strings: https://docs.atlas.mongodb.com/reference/api/clusters-get-all/ and https://docs.atlas.mongodb.com/reference/api/clusters-get-one/ This means we can now update the provider to support this. We have this on our plan and we'll update when completed.

roobert commented 4 years ago

For anyone else wanting a workaround until this support is added, it's not pretty but I ended up with this:

locals {
  db_srv_records = {
    for address in split(",", split("/", mongodbatlas_cluster.cluster.mongo_uri_with_options)[2]) :
    replace(split(":", address)[0], "/(cluster[0-9]-shard-[0-9]+-[0-9]+)./", "$1-pri.") => split(":", address)[1]
  }

  db_txt_record = trimprefix(split("/", mongodbatlas_cluster.cluster.mongo_uri_with_options)[3], "?ssl=true&")
}

This gives you a map in the form of: { server => port, ... } which you can then iterate over or use keys(db_srv_records)[...] or values(db_srv_records)[...] to fetch a specific records hostname or port.

Also related to #183.

themantissa commented 4 years ago

@roobert appreciate the share for others! Also note the work is currently in a PR in this repo. We have a bit more to knock out in the next point version but new connections types will be in there.

roobert commented 4 years ago

@themantissa - it would be really useful to expose a map of the cluster shards and a value for the connection settings for those of us that want or need to configure custom DNS, that way we could avoid the string manipulation!

themantissa commented 4 years ago

0.5.0 released today now supports all new connection strings as returned attributes: https://www.terraform.io/docs/providers/mongodbatlas/r/cluster.html#connection_strings