Skip to content

INTMDB-434 - Fixed private_endpoint output documentation #907

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

Merged
merged 12 commits into from
Nov 29, 2022
Merged
20 changes: 20 additions & 0 deletions examples/aws-atlas-privatelink-regionalized/atlas-pl.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,23 @@ resource "mongodbatlas_privatelink_endpoint_service" "atlaseplink_east" {
private_link_id = mongodbatlas_privatelink_endpoint.atlaspl_east.id
provider_name = "AWS"
}

locals {
private_endpoints = flatten([for cs in mongodbatlas_cluster.cluster-atlas.connection_strings : cs.private_endpoint])
connection_strings_west = [
for pe in local.private_endpoints : pe.srv_connection_string
if contains([for e in pe.endpoints : e.endpoint_id], aws_vpc_endpoint.ptfe_service_west.id)
]
connection_strings_east = [
for pe in local.private_endpoints : pe.srv_connection_string
if contains([for e in pe.endpoints : e.endpoint_id], aws_vpc_endpoint.ptfe_service_east.id)
]
}

output "connection_string_east" {
value = length(local.connection_strings_east) > 0 ? local.connection_strings_east[0] : ""
}

output "connection_string_west" {
value = length(local.connection_strings_west) > 0 ? local.connection_strings_west[0] : ""
}
11 changes: 7 additions & 4 deletions examples/azure-atlas-privatelink/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ This project aims to provide an example of using Azure and MongoDB Atlas togethe
## Dependencies

* Terraform v0.13
* Microsoft Azure account
* A MongoDB Atlas account
* Microsoft Azure account
* MongoDB Atlas account

```
Terraform v0.13.0
Expand Down Expand Up @@ -51,7 +51,10 @@ XXXXX

Now create **terraform.tfvars** file with all the variable values and make sure **not to commit it**.

**3\. Review the Terraform plan. **
An existing cluster on the project can optionally be linked via the `cluster_name` variable.
If included, the azure connection string to the cluster will be output.

**3\. Review the Terraform plan.**

Execute the below command and ensure you are happy with the plan.

Expand All @@ -66,7 +69,7 @@ This project currently does the below deployments:

**4\. Execute the Terraform apply.**

Now execute the plan to provision the AWS resources.
Now execute the plan to provision the Azure resources.

``` bash
$ terraform apply
Expand Down
18 changes: 18 additions & 0 deletions examples/azure-atlas-privatelink/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,21 @@ resource "mongodbatlas_privatelink_endpoint_service" "test" {
private_endpoint_ip_address = azurerm_private_endpoint.test.private_service_connection[0].private_ip_address
provider_name = "AZURE"
}

data "mongodbatlas_advanced_cluster" "cluster" {
count = var.cluster_name == "" ? 0 : 1
project_id = var.project_id
name = var.cluster_name
}

locals {
private_endpoints = try(flatten([for cs in data.mongodbatlas_advanced_cluster.cluster[0].connection_strings : cs.private_endpoint]), [])
connection_strings = [
for pe in local.private_endpoints : pe.srv_connection_string
if contains([for e in pe.endpoints : e.endpoint_id], azurerm_private_endpoint.test.id)
]
}

output "connection_string" {
value = length(local.connection_strings) > 0 ? local.connection_strings[0] : ""
}
5 changes: 4 additions & 1 deletion examples/azure-atlas-privatelink/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
variable "project_id" {
default = "PROJECT-ID"
}

variable "subscription_id" {
default = "AZURE SUBSCRIPTION ID"
}
Expand All @@ -18,3 +17,7 @@ variable "tenant_id" {
variable "resource_group_name" {
default = "AZURE RESOURCE GROUP NAME"
}
variable "cluster_name" {
description = "(Optional) Cluster whose connection string to output"
default = ""
}
17 changes: 10 additions & 7 deletions examples/gcp-atlas-privatelink/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ This project aims to provide an example of setting up GCP Private Service Connec
## Dependencies

* Terraform v0.13+
* Google account
* A MongoDB Atlas account
* Google Cloud account
* MongoDB Atlas account

```
Terraform v0.13.0
+ provider registry.terraform.io/hashicorp/google
+ provider registry.terraform.io/terraform-providers/mongodbatlas
+ provider registry.terraform.io/hashicorp/google
+ provider registry.terraform.io/terraform-providers/mongodbatlas
```

## Usage
Expand All @@ -26,7 +26,7 @@ Terraform v0.13.0
⇒ gcloud init
You are logged in as: [[email protected]].

Pick cloud project to use:
Pick cloud project to use:
[1] project1
[2] project2
...
Expand All @@ -40,7 +40,10 @@ Your Google Cloud SDK is configured and ready to use!

Now create **terraform.tfvars** file with all the variable values and make sure **not to commit it**.

**3\. Review the Terraform plan. **
An existing cluster on the project can optionally be linked via the `cluster_name` variable.
If included, the gcp connection string to the cluster will be output.

**3\. Review the Terraform plan.**

Execute the below command and ensure you are happy with the plan.

Expand All @@ -55,7 +58,7 @@ This project currently does the below deployments:

**4\. Execute the Terraform apply.**

Now execute the plan to provision the AWS resources.
Now execute the plan to provision the GCP resources.

``` bash
$ terraform apply
Expand Down
19 changes: 19 additions & 0 deletions examples/gcp-atlas-privatelink/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,22 @@ resource "mongodbatlas_privatelink_endpoint_service" "test" {

depends_on = [google_compute_forwarding_rule.default]
}

data "mongodbatlas_advanced_cluster" "cluster" {
count = var.cluster_name == "" ? 0 : 1
# Use endpoint service as source of project_id to gather cluster data after endpoint changes are applied
project_id = mongodbatlas_privatelink_endpoint_service.test.project_id
name = var.cluster_name
}

locals {
endpoint_service_id = google_compute_network.default.name
private_endpoints = try(flatten([for cs in data.mongodbatlas_advanced_cluster.cluster[0].connection_strings : cs.private_endpoint]), [])
connection_strings = [
for pe in local.private_endpoints : pe.srv_connection_string
if contains([for e in pe.endpoints : e.endpoint_id], local.endpoint_service_id)
]
}
output "connection_string" {
value = length(local.connection_strings) > 0 ? local.connection_strings[0] : ""
}
4 changes: 4 additions & 0 deletions examples/gcp-atlas-privatelink/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ variable "public_key" {
variable "private_key" {
description = "Private API key to authenticate to Atlas"
}
variable "cluster_name" {
description = "(Optional) Cluster whose connection string to output"
default = ""
}
59 changes: 55 additions & 4 deletions website/docs/r/advanced_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,62 @@ resource "mongodbatlas_advanced_cluster" "test" {
}
```

### Example - Return a Connection String
Standard
```terraform
output "standard" {
value = mongodbatlas_cluster.cluster-test.connection_strings[0].standard
}
# Example return string: standard = "mongodb://cluster-atlas-shard-00-00.ygo1m.mongodb.net:27017,cluster-atlas-shard-00-01.ygo1m.mongodb.net:27017,cluster-atlas-shard-00-02.ygo1m.mongodb.net:27017/?ssl=true&authSource=admin&replicaSet=atlas-12diht-shard-0"
```
Standard srv
```terraform
output "standard_srv" {
value = mongodbatlas_cluster.cluster-test.connection_strings[0].standard_srv
}
# Example return string: standard_srv = "mongodb+srv://cluster-atlas.ygo1m.mongodb.net"
```
Private with Network peering and Custom DNS AWS enabled
```terraform
output "private" {
value = mongodbatlas_cluster.cluster-test.connection_strings[0].private
}
# Example return string: private = "mongodb://cluster-atlas-shard-00-00-pri.ygo1m.mongodb.net:27017,cluster-atlas-shard-00-01-pri.ygo1m.mongodb.net:27017,cluster-atlas-shard-00-02-pri.ygo1m.mongodb.net:27017/?ssl=true&authSource=admin&replicaSet=atlas-12diht-shard-0"
private = "mongodb+srv://cluster-atlas-pri.ygo1m.mongodb.net"
```
Private srv with Network peering and Custom DNS AWS enabled
```terraform
output "private_srv" {
value = mongodbatlas_cluster.cluster-test.connection_strings[0].private_srv
}
# Example return string: private_srv = "mongodb+srv://cluster-atlas-pri.ygo1m.mongodb.net"
```

By endpoint_service_id
```terraform
locals {
endpoint_service_id = google_compute_network.default.name
private_endpoints = try(flatten([for cs in data.mongodbatlas_advanced_cluster.cluster[0].connection_strings : cs.private_endpoint]), [])
connection_strings = [
for pe in local.private_endpoints : pe.srv_connection_string
if contains([for e in pe.endpoints : e.endpoint_id], local.endpoint_service_id)
]
}
output "endpoint_service_connection_string" {
value = length(local.connection_strings) > 0 ? local.connection_strings[0] : ""
}
# Example return string: connection_string = "mongodb+srv://cluster-atlas-pl-0.ygo1m.mongodb.net"
```
Refer to the following for full endpoint service connection string examples:
* [AWS, Regionalized Private Endpoints](https://github.com/mongodb/terraform-provider-mongodbatlas/tree/master/examples/aws-atlas-privatelink-regionalized)
* [GCP Private Endpoint](https://github.com/mongodb/terraform-provider-mongodbatlas/tree/master/examples/gcp-atlas-privatelink)
* [Azure Private Endpoint](https://github.com/mongodb/terraform-provider-mongodbatlas/tree/master/examples/azure-atlas-privatelink)

## Argument Reference

* `project_id` - (Required) Unique ID for the project to create the database user.
* `name` - (Required) Name of the cluster as it appears in Atlas. Once the cluster is created, its name cannot be changed. **WARNING** Changing the name will result in destruction of the existing cluster and the creation of a new cluster.
* `cluster_type` - (Required) Atlas provides different instance sizes, each with a default storage capacity and RAM size. The instance size you select is used for all the data-bearing servers in your cluster. See [Create a Cluster](https://docs.atlas.mongodb.com/reference/api/clusters-create-one/) `providerSettings.instanceSizeName` for valid values and default resources.
* `cluster_type` - (Required) Atlas provides different instance sizes, each with a default storage capacity and RAM size. The instance size you select is used for all the data-bearing servers in your cluster. See [Create a Cluster](https://docs.atlas.mongodb.com/reference/api/clusters-create-one/) `providerSettings.instanceSizeName` for valid values and default resources.

* `backup_enabled` - (Optional) Flag that indicates whether the cluster can perform backups.
If `true`, the cluster can perform backups. You must set this value to `true` for NVMe clusters.
Expand Down Expand Up @@ -166,7 +217,7 @@ This parameter defaults to false.
`lifecycle {
ignore_changes = [paused]
}`


### bi_connector

Expand Down Expand Up @@ -243,7 +294,7 @@ Key-value pairs that tag and categorize the cluster. Each key and value has a ma
* `value` - The value that you want to write.


### replication_specs
### replication_specs

```terraform
//Example Multicloud
Expand Down Expand Up @@ -336,7 +387,7 @@ In addition to all arguments above, the following attributes are exported:

**NOTE** Connection strings must be returned as a list, therefore to refer to a specific attribute value add index notation. Example: mongodbatlas_advanced_cluster.cluster-test.connection_strings.0.standard_srv

Private connection strings may not be available immediately as the reciprocal connections may not have finalized by end of the Terraform run. If the expected connection string(s) do not contain a value a terraform refresh may need to be performed to obtain the value. One can also view the status of the peered connection in the [Atlas UI](https://docs.atlas.mongodb.com/security-vpc-peering/).
Private connection strings may not be available immediately as the reciprocal connections may not have finalized by end of the Terraform run. If the expected connection string(s) do not contain a value a terraform refresh may need to be performed to obtain the value. One can also view the status of the peered connection in the [Atlas UI](https://docs.atlas.mongodb.com/security-vpc-peering/).

- `connection_strings.standard` - Public mongodb:// connection string for this cluster.
- `connection_strings.standard_srv` - Public mongodb+srv:// connection string for this cluster. The mongodb+srv protocol tells the driver to look up the seed list of hosts in DNS. Atlas synchronizes this list with the nodes in a cluster. If the connection string uses this URI format, you don’t need to append the seed list or change the URI if the nodes change. Use this URI format if your driver supports it. If it doesn’t , use connectionStrings.standard.
Expand Down
Loading