Skip to content

doc: Adds initial structure for module examples cluster_to_advanced_cluster #3043

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
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Module Maintainer - `mongodbatlas_cluster` to `mongodbatlas_advanced_cluster`

The purpose of this example is to demonstrate how a Terraform module can help the migration from `mongodbatlas_cluster` to `mongodbatlas_advanced_cluster`.
The example contains three module versions:

<!-- Update Variable count -->
Version | Purpose | Variables | Resources
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[q] why is Variables count needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought it might be useful as a signal for complexity and the change between module versions

--- | --- | --- | ---
[v1](./v1) | Baseline | 5 | `mongodbatlas_cluster`
[v2](./v2) | Migrate to advanced_cluster with no change in variables or plan | 5 | `mongodbatlas_advanced_cluster`
[v3](./v3) | Use the latest features of advanced_cluster | 10 | `mongodbatlas_advanced_cluster`

<!-- TODO: Add highlights of module implementations -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
resource "mongodbatlas_cluster" "this" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to have a "simple" cluster on the example as it'd be overkill for some. We should talk though how to present the "complex" one

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean changing the module here? Or does the example usage in this comment look ok?

lifecycle {
precondition {
condition = !(var.auto_scaling_disk_gb_enabled && var.disk_size > 0)
error_message = "Must use either auto_scaling_disk_gb_enabled or disk_size, not both."
}
}

project_id = var.project_id
name = var.cluster_name

auto_scaling_disk_gb_enabled = var.auto_scaling_disk_gb_enabled
cluster_type = var.cluster_type
disk_size_gb = var.disk_size
mongo_db_major_version = var.mongo_db_major_version
provider_instance_size_name = var.instance_size
provider_name = var.provider_name

dynamic "tags" {
for_each = var.tags
content {
key = tags.key
value = tags.value
}
}

dynamic "replication_specs" {
for_each = var.replication_specs
content {
num_shards = replication_specs.value.num_shards
zone_name = replication_specs.value.zone_name

dynamic "regions_config" {
for_each = replication_specs.value.regions_config
content {
electable_nodes = regions_config.value.electable_nodes
priority = regions_config.value.priority
read_only_nodes = regions_config.value.read_only_nodes
region_name = regions_config.value.region_name
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
output "mongodb_raw_connection_strings" {
value = mongodbatlas_cluster.this.connection_strings
description = "This is the raw list of MongoDB Atlas connection strings. Note, these do not show the connection mechanism of the database details"
}

output "cluster_name" {
value = mongodbatlas_cluster.this.name
description = "MongoDB Atlas cluster name"
}

output "project_id" {
value = mongodbatlas_cluster.this.project_id
description = "MongoDB Atlas project id"
}

output "replication_specs" {
value = mongodbatlas_cluster.this.replication_specs
description = "Replication Specs for cluster"
}

output "mongodbatlas_cluster" {
value = mongodbatlas_cluster.this
description = "Full cluster configuration for mongodbatlas_cluster resource"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
variable "project_id" {
type = string
}

variable "cluster_name" {
type = string
}

variable "cluster_type" {
type = string

validation {
condition = contains(["REPLICASET", "SHARDED", "GEOSHARDED"], var.cluster_type)
error_message = "Valid supported cluster types are \"REPLICASET\", \"SHARDED\" or \"GEOSHARDED\"."
}
}

variable "instance_size" {
type = string
}

variable "mongo_db_major_version" {
type = string
}

variable "provider_name" {
type = string
}

# OPTIONAL VARIABLES

variable "disk_size" {
type = number
default = 0
}

variable "auto_scaling_disk_gb_enabled" {
type = bool
default = false
}

variable "tags" {
type = map(string)
default = {}
}

variable "replication_specs" {
type = list(object({
num_shards = number
zone_name = string
regions_config = set(object({
region_name = string
electable_nodes = number
priority = number
read_only_nodes = number
}))
}))
default = [{
num_shards = 1
zone_name = "Zone 1"
regions_config = [{
region_name = "US_EAST_1"
electable_nodes = 3
priority = 7
read_only_nodes = 0
}]
}]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
mongodbatlas = {
source = "mongodb/mongodbatlas"
version = "~> 1.22"
}
}
required_version = ">= 1.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = ">= 1.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = ">= 1.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Module User - `mongodbatlas_cluster` to `mongodbatlas_advanced_cluster`

The purpose of this example is to demonstrate the User experience when upgrading `mongodbatlas_cluster` to `mongodbatlas_advanced_cluster`.

Module Version | `mongodbatlas` version | Config Changes | Plan Changes
--- | --- | --- | ---
[v1](./v1) | <= PLACEHOLDER_TPF_RELEASE | Baseline configuration | -
[v2](./v2) | >= PLACEHOLDER_TPF_RELEASE | Only change to `v2` module version | No changes. Only moved.
[v3](./v3) | >= PLACEHOLDER_TPF_RELEASE | Usage of new variables to support multi-cloud and independent shard scaling | Yes (new features)


## Dependencies
<!-- TODO: Update XX versions -->
<!-- TODO: Update the `versions.tf` inside each vX -->
* Terraform CLI >= 1.X
* Terraform MongoDB Atlas Provider v1.XX.0
* A MongoDB Atlas account
* Configure the provider (can also be done with `variables.tfvars`)

```bash
export MONGODB_ATLAS_PUBLIC_KEY="xxxx"
export MONGODB_ATLAS_PRIVATE_KEY="xxxx"
```

## Usage

**Configure `variables.tfvars`:**
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative to using an inline call (see code snippet below)

The main advantage of this is to demonstrate that the exact same variable files can be used in both v1 and v2 and stay DRY
The main disadvantage is the duplicate variables.tf in both module_maintainer/{version}/variables.tf AND module_user/{version}/variables.tf

module "cluster" {
    source = "../../module_maintainer/v1"

    project_id             = "664619d870c247237f4b86a6"
    cluster_name           = "module-cluster"
    cluster_type           = "SHARDED"
    instance_size          = "M10"
    mongo_db_major_version = "8.0"
    provider_name          = "AWS"
    disk_size              = 40
    tags = {
    env    = "examples"
    module = "cluster_to_advanced_cluster"
    }
    replication_specs = [
    {
        num_shards = 2
        zone_name  = "Zone 1"
        regions_config = [
        {
            region_name     = "US_EAST_1"
            electable_nodes = 3
            priority        = 7
            read_only_nodes = 0
        }
        ]
    }
    ]
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a strong opinion, I'd probably go for the inline version as it's more frequently used, but I'm also ok with tfvars

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just got a customer example (unrelated to this) that are using tfvars. Of course it's only one, but I'd probably keep that as a reference


```tfvars
project_id = "664619d870c247237f4b86a6"
cluster_name = "module-cluster"
cluster_type = "SHARDED"
instance_size = "M10"
mongo_db_major_version = "8.0"
provider_name = "AWS"
disk_size = 40
tags = {
env = "examples"
module = "cluster_to_advanced_cluster"
}
replication_specs = [
{
num_shards = 2
zone_name = "Zone 1"
regions_config = [
{
region_name = "US_EAST_1"
electable_nodes = 3
priority = 7
read_only_nodes = 0
}
]
}
]
```


### `v1`

```bash
cd v1
terraform init
terraform apply -var-file=variables.tfvars
```

### `v2`

```bash
cd v2
export MONGODB_ATLAS_ADVANCED_CLUSTER_V2_SCHEMA=true # necessary for the `moved` block to work
terraform init -upgrade # in case your Atlas Provider version needs to be upgraded
terraform apply -var-file=variables.tfvars # notice the same variables used as in `v1`
```

### `v3`

**Configure `variables-updated.tfvars`:**

```tfvars
# TODO: Use the new variables for latest features
```

```bash
cd v3
export MONGODB_ATLAS_ADVANCED_CLUSTER_V2_SCHEMA=true # necessary for the `moved` block to work
terraform init -upgrade # in case your Atlas Provider version needs to be upgraded
terraform apply -var-file=variables-updated.tfvars # updated variables to enable latest mongodb_advanced_cluster features
```

### Cleanup with `terraform destroy`

```bash
terraform destroy
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
provider "mongodbatlas" {
public_key = var.public_key
private_key = var.private_key
}

module "cluster" {
source = "../../module_maintainer/v1"

auto_scaling_disk_gb_enabled = var.auto_scaling_disk_gb_enabled
cluster_name = var.cluster_name
cluster_type = var.cluster_type
disk_size = var.disk_size
instance_size = var.instance_size
mongo_db_major_version = var.mongo_db_major_version
project_id = var.project_id
provider_name = var.provider_name
replication_specs = var.replication_specs
tags = var.tags
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
variable "public_key" {
description = "Public API key to authenticate to Atlas"
type = string
default = ""
}
variable "private_key" {
description = "Private API key to authenticate to Atlas"
type = string
default = ""
}

# v1 & v2 variables
variable "project_id" {
type = string
}

variable "cluster_name" {
type = string
}

variable "cluster_type" {
type = string

validation {
condition = contains(["REPLICASET", "SHARDED", "GEOSHARDED"], var.cluster_type)
error_message = "Valid supported cluster types are \"REPLICASET\", \"SHARDED\" or \"GEOSHARDED\"."
}
}

variable "instance_size" {
type = string
}

variable "mongo_db_major_version" {
type = string
}

variable "provider_name" {
type = string
}

# OPTIONAL VARIABLES

variable "disk_size" {
type = number
default = 0
}

variable "auto_scaling_disk_gb_enabled" {
type = bool
default = false
}

variable "tags" {
type = map(string)
default = {}
}

variable "replication_specs" {
type = list(object({
num_shards = number
zone_name = string
regions_config = set(object({
region_name = string
electable_nodes = number
priority = number
read_only_nodes = number
}))
}))
default = [{
num_shards = 1
zone_name = "Zone 1"
regions_config = [{
region_name = "US_EAST_1"
electable_nodes = 3
priority = 7
read_only_nodes = 0
}]
}]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
mongodbatlas = {
source = "mongodb/mongodbatlas"
version = "~> 1.22"
}
}
required_version = ">= 1.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be >= 1.10 ? otherwise moved cannot work, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no moved inside of v1, so an earlier version of TF CLI is ok.

}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
mongodbatlas = {
source = "mongodb/mongodbatlas"
version = "~> 1.26" # todo: PLACEHOLDER_TPF_RELEASE
}
}
required_version = ">= 1.0" # todo: minimum moved block supported version
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
mongodbatlas = {
source = "mongodb/mongodbatlas"
version = "~> 1.26" # todo: PLACEHOLDER_TPF_RELEASE
}
}
required_version = ">= 1.0" # todo: minimum moved block supported version
}