From 01e326f2d869817b757199ed7de5b90882aa33b3 Mon Sep 17 00:00:00 2001 From: Vladimir Vshivkov Date: Fri, 9 Jun 2023 16:30:07 +0200 Subject: [PATCH 1/7] rds --- .gitignore | 2 ++ modules/rds/main.tf | 16 ++++++++++ modules/rds/outputs.tf | 24 ++++++++++++++ modules/rds/variables.tf | 68 ++++++++++++++++++++++++++++++++++++++++ modules/rds/versions.tf | 10 ++++++ 5 files changed, 120 insertions(+) create mode 100644 modules/rds/main.tf create mode 100644 modules/rds/outputs.tf create mode 100644 modules/rds/variables.tf create mode 100644 modules/rds/versions.tf diff --git a/.gitignore b/.gitignore index dd601bd..c94d547 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ override.tf.json # Ignore CLI configuration files .terraformrc terraform.rc + +.idea/** \ No newline at end of file diff --git a/modules/rds/main.tf b/modules/rds/main.tf new file mode 100644 index 0000000..47d63d0 --- /dev/null +++ b/modules/rds/main.tf @@ -0,0 +1,16 @@ +locals { + +} + + + +resource "opentelekomcloud_rds_instance_v3" "this" { + name = each.value["name"] + flavor = each.value["flavor"] + volume_size = each.value["storage"] + availability_zone = var.db_high_availability || var.db_flavor != "" ? local.db_availability_zones : [local.db_availability_zones[0]] + vpc_id = each.value["network_id"] + subnet_id = each.value["subnet_id"] + password = each.value["password"] +} + diff --git a/modules/rds/outputs.tf b/modules/rds/outputs.tf new file mode 100644 index 0000000..7f876f0 --- /dev/null +++ b/modules/rds/outputs.tf @@ -0,0 +1,24 @@ +output "db_private_ip" { + value = opentelekomcloud_rds_instance_v3.db_instance.private_ips[0] +} + +output "db_public_ip" { + value = try(opentelekomcloud_vpc_eip_v1.db_eip[0].publicip[0].ip_address, "") +} + +output "db_root_password" { + value = random_password.db_root_password.result + sensitive = true +} + +output "db_root_username" { + value = opentelekomcloud_rds_instance_v3.db_instance.db[0].user_name +} + +output "sg_secgroup_id" { + value = opentelekomcloud_rds_instance_v3.db_instance.security_group_id +} + +output "db_instance_ids" { + value = opentelekomcloud_rds_instance_v3.db_instance.nodes[*].id +} \ No newline at end of file diff --git a/modules/rds/variables.tf b/modules/rds/variables.tf new file mode 100644 index 0000000..9009f45 --- /dev/null +++ b/modules/rds/variables.tf @@ -0,0 +1,68 @@ +variable "name" { + description = "The name of the RDS instance" + type = string +} + +variable "flavor" { + description = "The flavor (size) of the RDS instance" + type = string +} + +variable "storage" { + description = "The storage size in GB for the RDS instance" + type = number +} + +variable "network_id" { + description = "The ID of the network where the RDS instance should be deployed" + type = string +} + +variable "subnet_id" { + description = "The ID of the subnet where the RDS instance should be deployed" + type = string +} + +variable "password" { + description = "The password for the RDS instance" + type = string +} + +variable "availability_zone" { + description = "The availability zone where the RDS instance should be deployed" + type = string +} + +variable "db_availability_zones" { + type = set(string) + description = "Availability zones for the RDS instance. One or two zones are supported for single and primary/standby instances respectively." + default = [] +} + +locals { + valid_availability_zones = { + eu-de = toset([ + "eu-de-01", + "eu-de-02", + "eu-de-03", + ]) + eu-nl = toset([ + "eu-nl-01", + "eu-nl-02", + "eu-nl-03", + ]) + eu-ch2 = toset([ + "eu-ch2a", + "eu-ch2b", + ]) + } + + region = data.opentelekomcloud_identity_project_v3.current.region + default_zones = { + eu-de = formatlist("${local.region}%s", ["-01", "-02"]) + eu-nl = formatlist("${local.region}%s", ["-01", "-02"]) + eu-ch2 = formatlist("${local.region}%s", ["a", "b"]) + } + + db_availability_zones = length(var.db_availability_zones) == 0 ? local.default_zones[local.region] : var.db_availability_zones +} \ No newline at end of file diff --git a/modules/rds/versions.tf b/modules/rds/versions.tf new file mode 100644 index 0000000..d2b49a5 --- /dev/null +++ b/modules/rds/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = ">= 1.0" + + required_providers { + opentelekomcloud = { + source = "opentelekomcloud/opentelekomcloud" + version = ">=1.34.4" + } + } +} \ No newline at end of file From 40cc18c590e77179bc679b5bf0128f4601eeb1d6 Mon Sep 17 00:00:00 2001 From: Vladimir Vshivkov Date: Tue, 13 Jun 2023 10:11:09 +0200 Subject: [PATCH 2/7] rds --- modules/rds/main.tf | 17 +++++++++++------ modules/rds/outputs.tf | 26 +++++++++++++++++++++----- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/modules/rds/main.tf b/modules/rds/main.tf index 47d63d0..f242c72 100644 --- a/modules/rds/main.tf +++ b/modules/rds/main.tf @@ -1,9 +1,3 @@ -locals { - -} - - - resource "opentelekomcloud_rds_instance_v3" "this" { name = each.value["name"] flavor = each.value["flavor"] @@ -14,3 +8,14 @@ resource "opentelekomcloud_rds_instance_v3" "this" { password = each.value["password"] } +resource "opentelekomcloud_rds_read_replica_v3" "this" { + count = var.db_flavor != "" ? 1 : 0 + name = each.value["name"] + flavor = each.value["flavor"] + volume_size = each.value["storage"] + availability_zone = var.db_high_availability || var.db_flavor != "" ? local.db_availability_zones : [local.db_availability_zones[0]] + vpc_id = each.value["network_id"] + subnet_id = each.value["subnet_id"] + password = each.value["password"] + replica_of_id = opentelekomcloud_rds_instance_v3.this[0].id +} diff --git a/modules/rds/outputs.tf b/modules/rds/outputs.tf index 7f876f0..1f10efe 100644 --- a/modules/rds/outputs.tf +++ b/modules/rds/outputs.tf @@ -1,9 +1,9 @@ output "db_private_ip" { - value = opentelekomcloud_rds_instance_v3.db_instance.private_ips[0] + value = opentelekomcloud_rds_instance_v3.this[*].private_ip } output "db_public_ip" { - value = try(opentelekomcloud_vpc_eip_v1.db_eip[0].publicip[0].ip_address, "") + value = try(opentelekomcloud_vpc_eip_v1.this[0].publicip[0].ip_address, "") } output "db_root_password" { @@ -12,13 +12,29 @@ output "db_root_password" { } output "db_root_username" { - value = opentelekomcloud_rds_instance_v3.db_instance.db[0].user_name + value = opentelekomcloud_rds_instance_v3.this.db[0].user_name } output "sg_secgroup_id" { - value = opentelekomcloud_rds_instance_v3.db_instance.security_group_id + value = opentelekomcloud_rds_instance_v3.this.security_group_id } output "db_instance_ids" { - value = opentelekomcloud_rds_instance_v3.db_instance.nodes[*].id + value = opentelekomcloud_rds_instance_v3.this.nodes[*].id +} + +output "db_read_replica_ids" { + value = opentelekomcloud_rds_read_replica_v3.this[*].id +} + +output "db_read_replica_private_ips" { + value = opentelekomcloud_rds_read_replica_v3.this[*].private_ip +} + +output "db_instance_private_ips" { + value = opentelekomcloud_rds_instance_v3.this[*].private_ip +} + +output "db_instance_public_ips" { + value = opentelekomcloud_rds_instance_v3.this[*].public_ip } \ No newline at end of file From 178853db16c2490fcf9e46c2f11c15949926cdd4 Mon Sep 17 00:00:00 2001 From: Vladimir Vshivkov Date: Fri, 16 Jun 2023 11:28:08 +0200 Subject: [PATCH 3/7] nl --- .gitignore | 2 +- modules/rds/outputs.tf | 2 +- modules/rds/variables.tf | 2 +- modules/rds/versions.tf | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index c94d547..58781b9 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,4 @@ override.tf.json .terraformrc terraform.rc -.idea/** \ No newline at end of file +.idea/** diff --git a/modules/rds/outputs.tf b/modules/rds/outputs.tf index 1f10efe..9b71171 100644 --- a/modules/rds/outputs.tf +++ b/modules/rds/outputs.tf @@ -37,4 +37,4 @@ output "db_instance_private_ips" { output "db_instance_public_ips" { value = opentelekomcloud_rds_instance_v3.this[*].public_ip -} \ No newline at end of file +} diff --git a/modules/rds/variables.tf b/modules/rds/variables.tf index 9009f45..d88ddcf 100644 --- a/modules/rds/variables.tf +++ b/modules/rds/variables.tf @@ -65,4 +65,4 @@ locals { } db_availability_zones = length(var.db_availability_zones) == 0 ? local.default_zones[local.region] : var.db_availability_zones -} \ No newline at end of file +} diff --git a/modules/rds/versions.tf b/modules/rds/versions.tf index d2b49a5..47f30ed 100644 --- a/modules/rds/versions.tf +++ b/modules/rds/versions.tf @@ -7,4 +7,4 @@ terraform { version = ">=1.34.4" } } -} \ No newline at end of file +} From 8e4a3e146bb1ede97133d149e0ac4405305b9fe7 Mon Sep 17 00:00:00 2001 From: Vladimir Vshivkov Date: Fri, 16 Jun 2023 12:28:01 +0200 Subject: [PATCH 4/7] rds --- modules/rds/main.tf | 21 -- modules/rds/outputs.tf | 40 ---- modules/rds/rds_backup/main.tf | 4 + modules/rds/rds_instance/main.tf | 31 +++ modules/rds/rds_instance/variables.tf | 245 ++++++++++++++++++++ modules/rds/{ => rds_instance}/versions.tf | 2 +- modules/rds/rds_parametergroup/main.tf | 11 + modules/rds/rds_parametergroup/variables.tf | 13 ++ modules/rds/rds_read_replicas/main.tf | 15 ++ modules/rds/rds_read_replicas/variables.tf | 17 ++ modules/rds/rds_read_replicas/versions.tf | 13 ++ modules/rds/variables.tf | 68 ------ 12 files changed, 350 insertions(+), 130 deletions(-) delete mode 100644 modules/rds/main.tf delete mode 100644 modules/rds/outputs.tf create mode 100644 modules/rds/rds_backup/main.tf create mode 100644 modules/rds/rds_instance/main.tf create mode 100644 modules/rds/rds_instance/variables.tf rename modules/rds/{ => rds_instance}/versions.tf (98%) create mode 100644 modules/rds/rds_parametergroup/main.tf create mode 100644 modules/rds/rds_parametergroup/variables.tf create mode 100644 modules/rds/rds_read_replicas/main.tf create mode 100644 modules/rds/rds_read_replicas/variables.tf create mode 100644 modules/rds/rds_read_replicas/versions.tf delete mode 100644 modules/rds/variables.tf diff --git a/modules/rds/main.tf b/modules/rds/main.tf deleted file mode 100644 index f242c72..0000000 --- a/modules/rds/main.tf +++ /dev/null @@ -1,21 +0,0 @@ -resource "opentelekomcloud_rds_instance_v3" "this" { - name = each.value["name"] - flavor = each.value["flavor"] - volume_size = each.value["storage"] - availability_zone = var.db_high_availability || var.db_flavor != "" ? local.db_availability_zones : [local.db_availability_zones[0]] - vpc_id = each.value["network_id"] - subnet_id = each.value["subnet_id"] - password = each.value["password"] -} - -resource "opentelekomcloud_rds_read_replica_v3" "this" { - count = var.db_flavor != "" ? 1 : 0 - name = each.value["name"] - flavor = each.value["flavor"] - volume_size = each.value["storage"] - availability_zone = var.db_high_availability || var.db_flavor != "" ? local.db_availability_zones : [local.db_availability_zones[0]] - vpc_id = each.value["network_id"] - subnet_id = each.value["subnet_id"] - password = each.value["password"] - replica_of_id = opentelekomcloud_rds_instance_v3.this[0].id -} diff --git a/modules/rds/outputs.tf b/modules/rds/outputs.tf deleted file mode 100644 index 9b71171..0000000 --- a/modules/rds/outputs.tf +++ /dev/null @@ -1,40 +0,0 @@ -output "db_private_ip" { - value = opentelekomcloud_rds_instance_v3.this[*].private_ip -} - -output "db_public_ip" { - value = try(opentelekomcloud_vpc_eip_v1.this[0].publicip[0].ip_address, "") -} - -output "db_root_password" { - value = random_password.db_root_password.result - sensitive = true -} - -output "db_root_username" { - value = opentelekomcloud_rds_instance_v3.this.db[0].user_name -} - -output "sg_secgroup_id" { - value = opentelekomcloud_rds_instance_v3.this.security_group_id -} - -output "db_instance_ids" { - value = opentelekomcloud_rds_instance_v3.this.nodes[*].id -} - -output "db_read_replica_ids" { - value = opentelekomcloud_rds_read_replica_v3.this[*].id -} - -output "db_read_replica_private_ips" { - value = opentelekomcloud_rds_read_replica_v3.this[*].private_ip -} - -output "db_instance_private_ips" { - value = opentelekomcloud_rds_instance_v3.this[*].private_ip -} - -output "db_instance_public_ips" { - value = opentelekomcloud_rds_instance_v3.this[*].public_ip -} diff --git a/modules/rds/rds_backup/main.tf b/modules/rds/rds_backup/main.tf new file mode 100644 index 0000000..361e7c3 --- /dev/null +++ b/modules/rds/rds_backup/main.tf @@ -0,0 +1,4 @@ +resource "opentelekomcloud_rds_backup_v3" "test" { + instance_id = opentelekomcloud_rds_instance_v3.this.id + name = "backup-test" +} diff --git a/modules/rds/rds_instance/main.tf b/modules/rds/rds_instance/main.tf new file mode 100644 index 0000000..55e73c7 --- /dev/null +++ b/modules/rds/rds_instance/main.tf @@ -0,0 +1,31 @@ +resource "opentelekomcloud_rds_instance_v3" "this" { + count = var.create_rds ? 1 : 0 + + availability_zone = var.availability_zone + db { + password = var.db_password + type = var.db_type + version = var.db_version + port = var.db_port + } + name = "${var.prefix}_rds_instance" + security_group_id = var.secgroup_id + subnet_id = var.network_id + vpc_id = var.vpc_id + volume { + type = var.volume_type + size = var.volume_size + disk_encryption_id = var.volume_encryption_id + } + flavor = var.db_flavor + ha_replication_mode = var.ha_replication_mode + + backup_strategy { + start_time = var.backup_start_time + keep_days = var.backup_keep_days + } + + tags = var.tags + + param_group_id = length(var.parametergroup_values) > 0 ? concat(opentelekomcloud_rds_parametergroup_v3.this.*.id)[0] : null +} diff --git a/modules/rds/rds_instance/variables.tf b/modules/rds/rds_instance/variables.tf new file mode 100644 index 0000000..c70e4d2 --- /dev/null +++ b/modules/rds/rds_instance/variables.tf @@ -0,0 +1,245 @@ +### mandatories +data "opentelekomcloud_identity_project_v3" "current" {} + +variable "name" { + type = string + description = "Name of the RDS instance." +} + +variable "tags" { + type = map(string) + description = "Common tag set for project resources" + default = {} +} + +variable "vpc_id" { + type = string + description = "Id of the VPC to create database cluster in." +} + +variable "subnet_id" { + type = string + description = "Id of the subnet to create database cluster in." +} + +variable "db_availability_zones" { + type = set(string) + description = "Availability zones for the RDS instance. One or two zones are supported for single and primary/standby instances respectively." + default = [] +} + +locals { + valid_availability_zones = { + eu-de = toset([ + "eu-de-01", + "eu-de-02", + "eu-de-03", + ]) + eu-nl = toset([ + "eu-nl-01", + "eu-nl-02", + "eu-nl-03", + ]) + eu-ch2 = toset([ + "eu-ch2a", + "eu-ch2b", + ]) + } + + region = data.opentelekomcloud_identity_project_v3.current.region + default_zones = { + eu-de = formatlist("${local.region}%s", ["-01", "-02"]) + eu-nl = formatlist("${local.region}%s", ["-01", "-02"]) + eu-ch2 = formatlist("${local.region}%s", ["a", "b"]) + } + + db_availability_zones = length(var.db_availability_zones) == 0 ? local.default_zones[local.region] : var.db_availability_zones +} + +resource "errorcheck_is_valid" "db_availability_zones" { + name = "Check if db_availability_zones is set up correctly." + test = { + assert = length(setsubtract(local.db_availability_zones, local.valid_availability_zones[local.region])) == 0 + error_message = "Please check your availability zones. For ${local.region} the valid az's are ${jsonencode(local.valid_availability_zones[local.region])}" + } +} + +variable "db_type" { + type = string + description = "RDS database product type. (MySQL, PostgreSQL or SQLServer)" + validation { + condition = contains(["MySQL", "PostgreSQL", "SQLServer"], var.db_type) + error_message = "Parameter db_type must be MySQL, PostgreSQL or SQLServer." + } +} + +variable "db_version" { + type = string + description = "RDS database product version." +} + +variable "db_port" { + type = string + description = "Port number for accessing the database. Default ports are: 3306(MySQL), 5432(PostgreSQL) and 1433(SQLServer)" + default = "default" +} + +locals { + db_port_defaults = { + MySQL = "3306" + PostgreSQL = "5432" + SQLServer = "1433" + } + db_port = var.db_port == "default" ? local.db_port_defaults[var.db_type] : var.db_port +} + +variable "db_cpus" { + type = string + description = "Number of CPU cores desired for database nodes. (default: 2)" + default = "2" +} + +variable "db_memory" { + type = number + description = "Amount of memory desired for database nodes in GB. (default: 4)" + default = 4 +} + +variable "db_high_availability" { + type = bool + description = "Whether a single db instance or a high available (primary/standby) db instance is desired. (default: false)" + default = false +} + +variable "db_ha_replication_mode" { + type = string + description = "RDS data replication mode for instances with high availability (primary/standby) enabled. Defaults are async(MySQL), async(PostgreSQL) and sync(SQLServer)" + default = "" +} + +locals { + supported_ha_replication_modes = { + MySQL = ["async", "semisync"] + PostgreSQL = ["async", "sync"] + SQLServer = ["sync"] + } + db_ha_replication_mode = var.db_high_availability ? var.db_ha_replication_mode == "" ? local.supported_ha_replication_modes[var.db_type][0] : var.db_ha_replication_mode : null +} + +resource "errorcheck_is_valid" "db_ha_replication_mode_constraint" { + name = "Check if a selected HA replication mode is supported on OTC." + test = { + assert = contains(local.supported_ha_replication_modes[var.db_type], var.db_ha_replication_mode) || var.db_ha_replication_mode == "" || !var.db_high_availability + error_message = "ERROR! Supported db_ha_replication_mode values for ${var.db_type} are [${join(", ", local.supported_ha_replication_modes[var.db_type])}]." + } +} + +variable "db_flavor" { + type = string + description = "RDS Flavor string override. This parameter will override parameters for db_cpu, db_memory and db_high_availability." + default = "" +} + +data "opentelekomcloud_rds_flavors_v3" "db_flavor" { + count = var.db_flavor == "" ? 1 : 0 + db_type = var.db_type + db_version = var.db_version + instance_mode = var.db_high_availability ? "ha" : "single" +} + +locals { + db_flavor = var.db_flavor == "" ? try([for f in data.opentelekomcloud_rds_flavors_v3.db_flavor[0].flavors : f.name if f.vcpus == var.db_cpus && f.memory == var.db_memory][0], var.db_flavor) : var.db_flavor +} + +resource "errorcheck_is_valid" "db_flavor_constraint" { + name = "Check if a flavor is found in OTC." + test = { + assert = local.db_flavor != "" + error_message = "ERROR! No RDS Flavor is found for ${var.db_type} with ${var.db_cpus} cores and ${var.db_memory} GB memory." + } + depends_on = [data.opentelekomcloud_rds_flavors_v3.db_flavor] +} + +variable "db_size" { + type = number + description = "Amount of storage desired for the database in GB. (default: 10)" + default = 100 +} + +variable "db_storage_type" { + type = string + description = "Type of storage desired for the database. Allowed values are COMMON (SATA) or ULTRAHIGH (SSD) (default: ULTRAHIGH)" + default = "ULTRAHIGH" + validation { + condition = contains(["COMMON", "ULTRAHIGH"], var.db_storage_type) + error_message = "Parameter db_storage_type must be one of COMMON or ULTRAHIGH." + } +} + +variable "db_backup_days" { + type = number + description = "Retain time for automated backups in days. (default: 7)" + default = "7" +} + +variable "db_backup_interval" { + type = string + description = "UTC time window for automated database backups in \"HH:MM-HH:MM\" format. Must be at least 1 hour (default: 03:00-04:00)" + default = "03:00-04:00" +} + +variable "db_parameters" { + type = map(string) + description = "A map of additional parameters for the database instance. Check the DB Engine's documentation." + default = {} +} + +variable "db_volume_encryption" { + type = bool + description = "Enable OTC KMS volume encryption for the database volumes. (default: true)" + default = true +} + +variable "db_volume_encryption_key_name" { + type = string + description = "If KMS volume encryption is enabled for the database volumes, use this kms key name instead of creating a new one. (default: null)" + default = null +} + +variable "sg_allowed_cidr" { + type = set(string) + description = "CIDR ranges that are allowed to connect to the database. (default: )" + default = [] +} + +data "opentelekomcloud_vpc_subnet_v1" "db_subnet" { + id = var.subnet_id +} + +variable "sg_allowed_secgroups" { + type = set(string) + description = "Security groups that are allowed to connect to the database. (default: [])" + default = [] +} + +variable "sg_secgroup_id" { + type = string + description = "Security group override to allow user defined security group definitions." + default = "" +} + +variable "db_storage_alarm_threshold" { + type = number + description = "CES alarm threshold (in percent) for database storage capacity. Can be disabled by setting to 0. (default: 75)" + default = 75 + validation { + condition = var.db_storage_alarm_threshold < 100 && var.db_storage_alarm_threshold >= 0 + error_message = "Parameter db_storage_alarm_threshold is in percent and must be between 0 and 100!" + } +} + +variable "db_eip_bandwidth" { + type = number + description = "Bandwidth of the EIP of RDS instance, can be disabled by setting to 0. (default: 0)" + default = 0 +} \ No newline at end of file diff --git a/modules/rds/versions.tf b/modules/rds/rds_instance/versions.tf similarity index 98% rename from modules/rds/versions.tf rename to modules/rds/rds_instance/versions.tf index 47f30ed..d2b49a5 100644 --- a/modules/rds/versions.tf +++ b/modules/rds/rds_instance/versions.tf @@ -7,4 +7,4 @@ terraform { version = ">=1.34.4" } } -} +} \ No newline at end of file diff --git a/modules/rds/rds_parametergroup/main.tf b/modules/rds/rds_parametergroup/main.tf new file mode 100644 index 0000000..bd16650 --- /dev/null +++ b/modules/rds/rds_parametergroup/main.tf @@ -0,0 +1,11 @@ +resource "opentelekomcloud_rds_parametergroup_v3" "this" { + count = length(var.parametergroup_values) > 0 ? 1 : 0 + + name = "${var.prefix}_parametergroup" + description = var.parametergroup_description + values = var.parametergroup_values + datastore { + type = lower(var.db_type) + version = var.db_version + } +} diff --git a/modules/rds/rds_parametergroup/variables.tf b/modules/rds/rds_parametergroup/variables.tf new file mode 100644 index 0000000..9d78174 --- /dev/null +++ b/modules/rds/rds_parametergroup/variables.tf @@ -0,0 +1,13 @@ +variable "parametergroup_values" { + type = map(string) + description = "(optional) Map of the values of the RDSv3 db parameter group" + + default = {} +} + +variable "parametergroup_description" { + type = string + description = "(optional) Description of the RDSv3 parameter group to create" + + default = "" +} \ No newline at end of file diff --git a/modules/rds/rds_read_replicas/main.tf b/modules/rds/rds_read_replicas/main.tf new file mode 100644 index 0000000..12fa2c1 --- /dev/null +++ b/modules/rds/rds_read_replicas/main.tf @@ -0,0 +1,15 @@ +resource "opentelekomcloud_rds_read_replica_v3" "this" { + count = length(var.read_replica_config) + + name = var.read_replica_config[count.index]["name"] + flavor_ref = var.read_replica_config[count.index]["flavor"] + availability_zone = var.read_replica_config[count.index]["availability_zone"] + replica_of_id = local.rds_instance_id + + public_ips = var.read_replica_config[count.index]["public_ips"] + + volume { + type = var.read_replica_config[count.index]["volume_type"] + disk_encryption_id = var.read_replica_config[count.index]["volume_encryption_id"] + } +} \ No newline at end of file diff --git a/modules/rds/rds_read_replicas/variables.tf b/modules/rds/rds_read_replicas/variables.tf new file mode 100644 index 0000000..b9635f9 --- /dev/null +++ b/modules/rds/rds_read_replicas/variables.tf @@ -0,0 +1,17 @@ +variable "read_replica_config" { + type = list( + object( + { + name = string, + flavor = string, + availability_zone = string, + public_ips = list(string), + volume_type = string, + volume_encryption_id = string + } + ) + ) + description = "(optional) The configuration of RDSv3 db read replica instances" + + default = [] +} diff --git a/modules/rds/rds_read_replicas/versions.tf b/modules/rds/rds_read_replicas/versions.tf new file mode 100644 index 0000000..5ca3bdb --- /dev/null +++ b/modules/rds/rds_read_replicas/versions.tf @@ -0,0 +1,13 @@ +terraform { + required_providers { + opentelekomcloud = { + source = "opentelekomcloud/opentelekomcloud" + version = ">=1.34.4" + } + } +} + +# Configure the OpenTelekomCloud Provider +provider "opentelekomcloud" { + cloud = "terraform" +} diff --git a/modules/rds/variables.tf b/modules/rds/variables.tf deleted file mode 100644 index d88ddcf..0000000 --- a/modules/rds/variables.tf +++ /dev/null @@ -1,68 +0,0 @@ -variable "name" { - description = "The name of the RDS instance" - type = string -} - -variable "flavor" { - description = "The flavor (size) of the RDS instance" - type = string -} - -variable "storage" { - description = "The storage size in GB for the RDS instance" - type = number -} - -variable "network_id" { - description = "The ID of the network where the RDS instance should be deployed" - type = string -} - -variable "subnet_id" { - description = "The ID of the subnet where the RDS instance should be deployed" - type = string -} - -variable "password" { - description = "The password for the RDS instance" - type = string -} - -variable "availability_zone" { - description = "The availability zone where the RDS instance should be deployed" - type = string -} - -variable "db_availability_zones" { - type = set(string) - description = "Availability zones for the RDS instance. One or two zones are supported for single and primary/standby instances respectively." - default = [] -} - -locals { - valid_availability_zones = { - eu-de = toset([ - "eu-de-01", - "eu-de-02", - "eu-de-03", - ]) - eu-nl = toset([ - "eu-nl-01", - "eu-nl-02", - "eu-nl-03", - ]) - eu-ch2 = toset([ - "eu-ch2a", - "eu-ch2b", - ]) - } - - region = data.opentelekomcloud_identity_project_v3.current.region - default_zones = { - eu-de = formatlist("${local.region}%s", ["-01", "-02"]) - eu-nl = formatlist("${local.region}%s", ["-01", "-02"]) - eu-ch2 = formatlist("${local.region}%s", ["a", "b"]) - } - - db_availability_zones = length(var.db_availability_zones) == 0 ? local.default_zones[local.region] : var.db_availability_zones -} From 44addf5763007c3ad31c2ec253ec7005d2587f30 Mon Sep 17 00:00:00 2001 From: Vladimir Vshivkov Date: Tue, 20 Jun 2023 09:42:20 +0200 Subject: [PATCH 5/7] rds --- modules/dns/README.md | 8 ++++---- modules/rds/rds_instance/variables.tf | 2 +- modules/rds/rds_instance/versions.tf | 2 +- modules/rds/rds_parametergroup/variables.tf | 2 +- modules/rds/rds_read_replicas/main.tf | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/dns/README.md b/modules/dns/README.md index 74429a1..26affac 100644 --- a/modules/dns/README.md +++ b/modules/dns/README.md @@ -32,14 +32,14 @@ dns_settings = { vpc_id = "REWRITE_IN_LOCALS" region = "REWRITE_IN_LOCALS" tags = {} - recordsets = [ + recordsets = [ { subdomain = "cname" type = "cname" description = "cname record set" ttl = 3600 records = ["server1.example.com."] - tags = { + tags = { "Recordset" = "private_cname" } }, @@ -49,7 +49,7 @@ dns_settings = { data "opentelekomcloud_identity_project_v3" "current" {} -data "opentelekomcloud_vpc_subnet_v1" "subnet" { +data "opentelekomcloud_vpc_subnet_v1" "subnet" { name = "my-subnet" } @@ -72,7 +72,7 @@ locals { module "dns" { source = "../../modules/dns" - + dns_zone_settings = local.local_dns_settings default_tags_set = var.default_tags_set } diff --git a/modules/rds/rds_instance/variables.tf b/modules/rds/rds_instance/variables.tf index c70e4d2..85b2f56 100644 --- a/modules/rds/rds_instance/variables.tf +++ b/modules/rds/rds_instance/variables.tf @@ -242,4 +242,4 @@ variable "db_eip_bandwidth" { type = number description = "Bandwidth of the EIP of RDS instance, can be disabled by setting to 0. (default: 0)" default = 0 -} \ No newline at end of file +} diff --git a/modules/rds/rds_instance/versions.tf b/modules/rds/rds_instance/versions.tf index d2b49a5..47f30ed 100644 --- a/modules/rds/rds_instance/versions.tf +++ b/modules/rds/rds_instance/versions.tf @@ -7,4 +7,4 @@ terraform { version = ">=1.34.4" } } -} \ No newline at end of file +} diff --git a/modules/rds/rds_parametergroup/variables.tf b/modules/rds/rds_parametergroup/variables.tf index 9d78174..486c3ea 100644 --- a/modules/rds/rds_parametergroup/variables.tf +++ b/modules/rds/rds_parametergroup/variables.tf @@ -10,4 +10,4 @@ variable "parametergroup_description" { description = "(optional) Description of the RDSv3 parameter group to create" default = "" -} \ No newline at end of file +} diff --git a/modules/rds/rds_read_replicas/main.tf b/modules/rds/rds_read_replicas/main.tf index 12fa2c1..6ce7a85 100644 --- a/modules/rds/rds_read_replicas/main.tf +++ b/modules/rds/rds_read_replicas/main.tf @@ -12,4 +12,4 @@ resource "opentelekomcloud_rds_read_replica_v3" "this" { type = var.read_replica_config[count.index]["volume_type"] disk_encryption_id = var.read_replica_config[count.index]["volume_encryption_id"] } -} \ No newline at end of file +} From 228b360716ab6db5b80cdb6731f71498f8a999da Mon Sep 17 00:00:00 2001 From: Vladimir Vshivkov Date: Wed, 21 Jun 2023 18:49:43 +0200 Subject: [PATCH 6/7] outputs --- modules/rds/rds_instance/outputs.tf | 52 +++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 modules/rds/rds_instance/outputs.tf diff --git a/modules/rds/rds_instance/outputs.tf b/modules/rds/rds_instance/outputs.tf new file mode 100644 index 0000000..6c745c5 --- /dev/null +++ b/modules/rds/rds_instance/outputs.tf @@ -0,0 +1,52 @@ +output "rds_instance_id" { + value = opentelekomcloud_rds_instance_v3.this.*.id +} + +output "rds_instance_name" { + value = opentelekomcloud_rds_instance_v3.this.*.name +} + +output "rds_instance_status" { + value = opentelekomcloud_rds_instance_v3.this.*.status +} + +output "rds_instance_availability_zone" { + value = opentelekomcloud_rds_instance_v3.this.*.availability_zone +} + +output "rds_instance_volume_size" { + value = opentelekomcloud_rds_instance_v3.this.*.volume.0.size +} + +output "rds_instance_volume_type" { + value = opentelekomcloud_rds_instance_v3.this.*.volume.0.type +} + +output "rds_instance_volume_encryption_id" { + value = opentelekomcloud_rds_instance_v3.this.*.volume.0.disk_encryption_id +} + +output "rds_private_ip" { + value = opentelekomcloud_rds_instance_v3.this.*.private_ip +} + +output "rds_instance_port" { + value = opentelekomcloud_rds_instance_v3.this.*.db.0.port +} + +output "rds_public_ip" { + value = try(opentelekomcloud_rds_instance_v3.this.*.public_ips, "") +} + +output "rds_instance_nodes" { + value = opentelekomcloud_rds_instance_v3.this.*.nodes +} + +output "rds_root_username" { + value = opentelekomcloud_rds_instance_v3.db_instance.db[0].user_name +} + +output "rds_root_password" { + value = try(opentelekomcloud_rds_instance_v3.db_instance.db[0].password, "") + sensitive = true +} From f0125d8f4ffeb5831c14db365feb32c56b9b309c Mon Sep 17 00:00:00 2001 From: Vladimir Vshivkov Date: Thu, 22 Jun 2023 11:04:54 +0200 Subject: [PATCH 7/7] rds --- modules/rds/rds_backup/main.tf | 8 +++++-- modules/rds/rds_backup/output.tf | 27 ++++++++++++++++++++++ modules/rds/rds_backup/variables.tf | 17 ++++++++++++++ modules/rds/rds_backup/versions.tf | 10 ++++++++ modules/rds/rds_parametergroup/versions.tf | 10 ++++++++ 5 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 modules/rds/rds_backup/output.tf create mode 100644 modules/rds/rds_backup/variables.tf create mode 100644 modules/rds/rds_backup/versions.tf create mode 100644 modules/rds/rds_parametergroup/versions.tf diff --git a/modules/rds/rds_backup/main.tf b/modules/rds/rds_backup/main.tf index 361e7c3..6ac90a0 100644 --- a/modules/rds/rds_backup/main.tf +++ b/modules/rds/rds_backup/main.tf @@ -1,4 +1,8 @@ resource "opentelekomcloud_rds_backup_v3" "test" { - instance_id = opentelekomcloud_rds_instance_v3.this.id - name = "backup-test" + for_each = var.rds_backup_settings + + name = each.key + description = each.value["description"] + databases = each.value["databases"] + instance_id = each.value["instance_id"] } diff --git a/modules/rds/rds_backup/output.tf b/modules/rds/rds_backup/output.tf new file mode 100644 index 0000000..2082831 --- /dev/null +++ b/modules/rds/rds_backup/output.tf @@ -0,0 +1,27 @@ +output "rds_backup_id" { + value = try(opentelekomcloud_rds_backup_v3.this.*.id, "") +} + +output "rds_backup_name" { + value = try(opentelekomcloud_rds_backup_v3.this.*.name, "") +} + +output "rds_backup_description" { + value = try(opentelekomcloud_rds_backup_v3.this.*.description, "") +} + +output "rds_backup_databases" { + value = try(opentelekomcloud_rds_backup_v3.this.*.databases, "") +} + +output "rds_backup_status" { + value = try(opentelekomcloud_rds_backup_v3.this.*.status, "") +} + +output "rds_backup_type" { + value = try(opentelekomcloud_rds_backup_v3.this.*.type, "") +} + +output "rds_backup_begin_time" { + value = try(opentelekomcloud_rds_backup_v3.this.*.begin_time, "") +} diff --git a/modules/rds/rds_backup/variables.tf b/modules/rds/rds_backup/variables.tf new file mode 100644 index 0000000..f960e28 --- /dev/null +++ b/modules/rds/rds_backup/variables.tf @@ -0,0 +1,17 @@ +variable "rds_backup_settings" { + default = { + /*Example: + #Here you set rds_backup name as key + rds_backup_name = { + description = "desc" + databases = ["db1", "db2"] + instance_id = "instance_id" + } + */ + } + type = map(object({ + description = optional(string) + databases = optional(list(string)) + instance_id = optional(string) + })) +} diff --git a/modules/rds/rds_backup/versions.tf b/modules/rds/rds_backup/versions.tf new file mode 100644 index 0000000..47f30ed --- /dev/null +++ b/modules/rds/rds_backup/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = ">= 1.0" + + required_providers { + opentelekomcloud = { + source = "opentelekomcloud/opentelekomcloud" + version = ">=1.34.4" + } + } +} diff --git a/modules/rds/rds_parametergroup/versions.tf b/modules/rds/rds_parametergroup/versions.tf new file mode 100644 index 0000000..47f30ed --- /dev/null +++ b/modules/rds/rds_parametergroup/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = ">= 1.0" + + required_providers { + opentelekomcloud = { + source = "opentelekomcloud/opentelekomcloud" + version = ">=1.34.4" + } + } +}