diff --git a/main.tf b/main.tf index a18150c..0a26058 100644 --- a/main.tf +++ b/main.tf @@ -1,13 +1,18 @@ +provider "azurerm" { + version = ">=1.40.0" +} + resource "azurerm_resource_group" "mysql" { + count = var.resource_group_create ? 1 : 0 name = var.resource_group_name location = var.location tags = var.tags } -resource "azurerm_mysql_server" "mysql" { +resource "azurerm_mysql_server" "server" { name = "${var.name}-mysqlsvr" location = var.location - resource_group_name = azurerm_resource_group.mysql.name + resource_group_name = var.resource_group_name sku_name = var.sku_name @@ -25,18 +30,40 @@ resource "azurerm_mysql_server" "mysql" { tags = var.tags } -resource "azurerm_mysql_database" "mysql" { - name = local.db_name - resource_group_name = azurerm_resource_group.mysql.name - server_name = azurerm_mysql_server.mysql.name - charset = var.charset - collation = var.collation +resource "azurerm_mysql_database" "database" { + for_each = var.dbs + + name = each.value.name + charset = lookup(each.value, "charset", var.db_charset) + collation = lookup(each.value, "collation", var.db_collation) + resource_group_name = var.resource_group_name + server_name = azurerm_mysql_server.server.name } -resource "azurerm_mysql_firewall_rule" "mysql" { - name = "${var.name}-fwrules" - resource_group_name = azurerm_resource_group.mysql.name - server_name = azurerm_mysql_server.mysql.name - start_ip_address = var.start_ip_address - end_ip_address = var.end_ip_address +resource "azurerm_mysql_firewall_rule" "firewall_rule" { + for_each = var.firewall_rules + + name = each.key + start_ip_address = each.value.start_ip + end_ip_address = each.value.end_ip + resource_group_name = var.resource_group_name + server_name = azurerm_mysql_server.server.name } + +resource "azurerm_mysql_virtual_network_rule" "vnet_rule" { + for_each = var.vnet_rules + + name = each.key + subnet_id = each.value + resource_group_name = var.resource_group_name + server_name = azurerm_mysql_server.server.name +} + +resource "azurerm_mysql_configuration" "config" { + for_each = var.mysql_configurations + + name = each.key + value = each.value + resource_group_name = var.resource_group_name + server_name = azurerm_mysql_server.server.name +} \ No newline at end of file diff --git a/outputs.tf b/outputs.tf index 876b122..5d5f446 100644 --- a/outputs.tf +++ b/outputs.tf @@ -8,11 +8,6 @@ output "server_name" { value = azurerm_mysql_server.mysql.name } -output "database_name" { - description = "The database name of MySQL Server." - value = azurerm_mysql_database.mysql.name -} - output "admin_username" { description = "The administrator username of MySQL Server." value = var.admin_username diff --git a/variables.tf b/variables.tf index 0e7969d..8f945e5 100644 --- a/variables.tf +++ b/variables.tf @@ -4,15 +4,15 @@ variable "resource_group_name" { default = "mysqlResourceGroup" } -variable "location" { - description = "Specifies the supported Azure location where the resource exists." - type = string +variable "resource_group_create" { + description = "Set to false if the resource group already exists" + type = bool + default = true } -variable "auto_grow" { - description = "(Optional) Defines whether autogrow is enabled or disabled for the storage. Valid values are Enabled or Disabled." +variable "location" { + description = "Specifies the supported Azure location where the resource exists." type = string - default = "Enabled" } variable "name" { @@ -20,12 +20,6 @@ variable "name" { type = string } -variable "db_name" { - description = "The name of the database to be created." - type = string - default = "" -} - variable "admin_username" { description = "The administrator username of MySQL Server." type = string @@ -36,18 +30,6 @@ variable "password" { type = string } -variable "start_ip_address" { - description = "Defines the start IP address used in your database firewall rule." - type = string - default = "0.0.0.0" -} - -variable "end_ip_address" { - description = "Defines the end IP address used in your database firewall rule." - type = string - default = "255.255.255.255" -} - variable "db_version" { description = "Specifies the version of MySQL to use. Valid values are 5.6 and 5.7." type = string @@ -66,24 +48,6 @@ variable "sku_name" { default = "B_Gen4_2" } -variable "sku_capacity" { - description = "The scale up/out capacity, representing server's compute units." - type = number - default = 2 -} - -variable "sku_tier" { - description = "The tier of the particular SKU. Possible values are Basic, GeneralPurpose, and MemoryOptimized." - type = string - default = "Basic" -} - -variable "sku_family" { - description = "The family of hardware Gen4 or Gen5, before selecting your family check the product documentation for availability in your region." - type = string - default = "Gen4" -} - variable "storage_mb" { description = "Max storage allowed for a server. Possible values are between 5120 MB(5GB) and 1048576 MB(1TB) for the Basic SKU and between 5120 MB(5GB) and 4194304 MB(4TB) for General Purpose/Memory Optimized SKUs." type = number @@ -102,6 +66,13 @@ variable "geo_redundant_backup" { default = "Disabled" } +variable "auto_grow" { + description = "(Optional) Defines whether autogrow is enabled or disabled for the storage. Valid values are Enabled or Disabled." + type = string + default = "Enabled" +} + + variable "charset" { description = "Specifies the Charset for the MySQL Database, which needs to be a valid MySQL Charset." type = string @@ -114,12 +85,26 @@ variable "collation" { default = "utf8_unicode_ci" } -variable "tags" { - description = "Resource Tags." +variable "dbs" { + description = "Map of databases to create, values supported: name, charset, collation" type = map(string) default = {} } -locals { - db_name = var.db_name == "" ? var.name : var.db_name +variable "firewall_rules" { + description = "Map of firewall rules to create. Key is rule name, values are start_ip, end_ip" + type = map(string) + default = {} +} + +variable "vnet_rules" { + description = "Map of vnet rules to create. Key is name, value is vnet id" + type = string + default = {} +} + +variable "tags" { + description = "Resource Tags." + type = map(string) + default = {} }