Skip to content

Commit

Permalink
OPS-6673-mongodb (#30)
Browse files Browse the repository at this point in the history
Terraform module for IONOS MongoDB DBaaS
  • Loading branch information
YannickEvers authored Jul 10, 2024
1 parent eabbb30 commit 9247696
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 0 deletions.
54 changes: 54 additions & 0 deletions modules/ionos-mongodb-cluster/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!-- BEGIN_TF_DOCS -->

## Providers

| Name | Version |
|------|---------|
| <a name="provider_ionoscloud"></a> [ionoscloud](#provider\_ionoscloud) | 6.4.18 |
## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_cidr_workaround"></a> [cidr\_workaround](#module\_cidr\_workaround) | ../../modules/ionos-cidr-workaround | n/a |
## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_datacenter_id"></a> [datacenter\_id](#input\_datacenter\_id) | The datacenter to connect your cluster to. | `string` | n/a | yes |
| <a name="input_datacenter_location"></a> [datacenter\_location](#input\_datacenter\_location) | The physical location where the cluster will be created. This will be where all of your instances live. | `string` | n/a | yes |
| <a name="input_display_name"></a> [display\_name](#input\_display\_name) | Name of the MongoDB Cluster | `string` | n/a | yes |
| <a name="input_instances_count"></a> [instances\_count](#input\_instances\_count) | The total number of instances (1 master, n-1 fallbacks) in the cluster. Options(Business): 1, 3; Options(Enterprise): 3, 5, 7 | `number` | n/a | yes |
| <a name="input_lan_id"></a> [lan\_id](#input\_lan\_id) | The LAN to connect your cluster to. | `string` | n/a | yes |
| <a name="input_mongo_version"></a> [mongo\_version](#input\_mongo\_version) | The MongoDB version of your cluster. | `string` | n/a | yes |
| <a name="input_cores"></a> [cores](#input\_cores) | The number of CPU cores per node (Enterprise only) | `number` | `null` | no |
| <a name="input_ip_block_start"></a> [ip\_block\_start](#input\_ip\_block\_start) | The starting number to be set in the last ip block (Default: 101) | `number` | `101` | no |
| <a name="input_ip_prefix"></a> [ip\_prefix](#input\_ip\_prefix) | Prefix for the used IPs (e.g. 10.0.4.1/24, required if k8s\_cluster\_id is not defined) | `string` | `null` | no |
| <a name="input_k8s_cluster_id"></a> [k8s\_cluster\_id](#input\_k8s\_cluster\_id) | ID of the kubernetes cluster to connect to. (Used to generate matching IPs, alternativly you can set ip\_prefix directly) | `string` | `null` | no |
| <a name="input_maintenance_day"></a> [maintenance\_day](#input\_maintenance\_day) | The day of the week for the 4 hour-long maintenance window (Default: Sunday) | `string` | `"Sunday"` | no |
| <a name="input_maintenance_hour"></a> [maintenance\_hour](#input\_maintenance\_hour) | The time of the day when the 4 hour-long maintenance window may start. (Default: 3) | `number` | `3` | no |
| <a name="input_memory"></a> [memory](#input\_memory) | The amount of memory in megabytes per node (Enterprise only) | `number` | `null` | no |
| <a name="input_resource_template"></a> [resource\_template](#input\_resource\_template) | Name of the IONOS Template to use for resources (except for Enterprise) | `string` | `null` | no |
| <a name="input_shards_count"></a> [shards\_count](#input\_shards\_count) | Number of MongoDB shards (Enterprise only) | `number` | `null` | no |
| <a name="input_subnet_mask"></a> [subnet\_mask](#input\_subnet\_mask) | The subnet mask to use for allowing connections from the LAN (Default: 24) | `string` | `24` | no |
| <a name="input_users"></a> [users](#input\_users) | Map of users and their roles. Example: {admin: {password: "1234", roles: [{db: "admin", role: "dbAdminAnyDatabase"}]}} | <pre>map(<br> object({<br> password=string,<br> roles=set(<br> object({<br> db=string,<br> role=string<br> }))}))</pre> | `{}` | no |
| <a name="input_volume_disk_type"></a> [volume\_disk\_type](#input\_volume\_disk\_type) | The storage type used in your cluster (Enterprise only) | `string` | `null` | no |
| <a name="input_volume_size"></a> [volume\_size](#input\_volume\_size) | The amount of storage in MB per node (Enterprise only) | `number` | `null` | no |
## Outputs

| Name | Description |
|------|-------------|
| <a name="output_connection_string"></a> [connection\_string](#output\_connection\_string) | The URL pointing to your cluster. |
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
| <a name="requirement_ionoscloud"></a> [ionoscloud](#requirement\_ionoscloud) | 6.4.18 |
## Resources

| Name | Type |
|------|------|
| [ionoscloud_mongo_cluster.mongo_cluster](https://registry.terraform.io/providers/ionos-cloud/ionoscloud/6.4.18/docs/resources/mongo_cluster) | resource |
| [ionoscloud_mongo_user.initial_mongo_user](https://registry.terraform.io/providers/ionos-cloud/ionoscloud/6.4.18/docs/resources/mongo_user) | resource |
| [ionoscloud_mongo_template.mongo_template](https://registry.terraform.io/providers/ionos-cloud/ionoscloud/6.4.18/docs/data-sources/mongo_template) | data source |
<!-- END_TF_DOCS -->
12 changes: 12 additions & 0 deletions modules/ionos-mongodb-cluster/cidr-workaround.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module "cidr_workaround" {
count = var.k8s_cluster_id == null ? 0 : 1
source = "../../modules/ionos-cidr-workaround"
k8s_cluster_id = var.k8s_cluster_id
lan_id = var.lan_id
}

locals {
prefix = var.ip_prefix == null ? module.cidr_workaround[0].prefix : var.ip_prefix
full_ips = [for i in range(0, var.instances_count) : cidrhost(local.prefix, var.ip_block_start + i)]
cidrs = [for ip in local.full_ips : format("%s/%s", ip, var.subnet_mask)]
}
4 changes: 4 additions & 0 deletions modules/ionos-mongodb-cluster/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "ionoscloud_mongo_template" "mongo_template" {
count = var.resource_template == null ? 0 : 1
name = var.resource_template
}
40 changes: 40 additions & 0 deletions modules/ionos-mongodb-cluster/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
resource ionoscloud_mongo_cluster "mongo_cluster" {
mongodb_version = var.mongo_version
instances = var.instances_count
shards = var.shards_count
type = var.shards_count == null ? "replicaset" : "sharded-cluster"
cores = var.cores
ram = var.memory
storage_size = var.volume_size
storage_type = var.volume_disk_type
display_name = var.display_name
location = var.datacenter_location

connections {
datacenter_id = var.datacenter_id
lan_id = var.lan_id
cidr_list = local.cidrs
}

template_id = var.resource_template == null ? null : data.ionoscloud_mongo_template.mongo_template[0].id
edition = var.resource_template == null ? "enterprise" : null

maintenance_window {
day_of_the_week = var.maintenance_day
time = format("%02d:00:00", var.maintenance_hour)
}
}

resource "ionoscloud_mongo_user" "initial_mongo_user" {
for_each = var.users
cluster_id = ionoscloud_mongo_cluster.mongo_cluster.id
username = each.key
password = each.value.password
dynamic "roles" {
for_each = each.value.roles
content {
role = roles.value["role"]
database = roles.value["db"]
}
}
}
4 changes: 4 additions & 0 deletions modules/ionos-mongodb-cluster/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "connection_string" {
description = "The URL pointing to your cluster."
value = ionoscloud_mongo_cluster.mongo_cluster.connection_string
}
96 changes: 96 additions & 0 deletions modules/ionos-mongodb-cluster/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
variable "display_name" {
description = "Name of the MongoDB Cluster"
type = string
}
variable "mongo_version" {
description = "The MongoDB version of your cluster."
type = string
}
variable "instances_count" {
description = "The total number of instances (1 master, n-1 fallbacks) in the cluster. Options(Business): 1, 3; Options(Enterprise): 3, 5, 7"
type = number
}
variable "shards_count" {
description = "Number of MongoDB shards (Enterprise only)"
type = number
default = null
}
variable "resource_template" {
description = "Name of the IONOS Template to use for resources (except for Enterprise)"
type = string
default = null
}
variable "cores" {
description = "The number of CPU cores per node (Enterprise only)"
type = number
default = null
}
variable "memory" {
description = "The amount of memory in megabytes per node (Enterprise only)"
type = number
default = null
}
variable "volume_size" {
description = "The amount of storage in MB per node (Enterprise only)"
type = number
default = null
}
variable "volume_disk_type" {
description = "The storage type used in your cluster (Enterprise only)"
type = string
default = null
}
variable "datacenter_id" {
description = "The datacenter to connect your cluster to."
type = string
}
variable "datacenter_location" {
description = "The physical location where the cluster will be created. This will be where all of your instances live."
type = string
}
variable "k8s_cluster_id" {
description = "ID of the kubernetes cluster to connect to. (Used to generate matching IPs, alternativly you can set ip_prefix directly)"
type = string
default = null
}
variable "lan_id" {
description = "The LAN to connect your cluster to."
type = string
}
variable "ip_prefix" {
description = "Prefix for the used IPs (e.g. 10.0.4.1/24, required if k8s_cluster_id is not defined)"
type = string
default = null
}
variable "subnet_mask" {
description = "The subnet mask to use for allowing connections from the LAN (Default: 24)"
type = string
default = 24
}
variable "ip_block_start" {
description = "The starting number to be set in the last ip block (Default: 101)"
type = number
default = 101
}
variable "users" {
description = "Map of users and their roles. Example: {admin: {password: \"1234\", roles: [{db: \"admin\", role: \"dbAdminAnyDatabase\"}]}}"
default = {}
type = map(
object({
password=string,
roles=set(
object({
db=string,
role=string
}))}))
}
variable "maintenance_day" {
description = "The day of the week for the 4 hour-long maintenance window (Default: Sunday)"
type = string
default = "Sunday"
}
variable "maintenance_hour" {
description = "The time of the day when the 4 hour-long maintenance window may start. (Default: 3)"
type = number
default = 3
}
9 changes: 9 additions & 0 deletions modules/ionos-mongodb-cluster/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
ionoscloud = {
source = "ionos-cloud/ionoscloud"
version = "6.4.18"
}
}
required_version = ">= 0.13"
}

0 comments on commit 9247696

Please sign in to comment.