Skip to content
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

Refactor for multiple dbs, firewall, vnet rules #6

Merged
merged 2 commits into from Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 41 additions & 14 deletions main.tf
@@ -1,13 +1,18 @@
provider "azurerm" {
version = ">=1.40.0"
Copy link

Choose a reason for hiding this comment

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

This will cause problems if we're using a different provider version

Copy link
Member Author

Choose a reason for hiding this comment

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

I can knock it down to a lesser version, but I haven't tested it on those.

}

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

Expand All @@ -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
}
5 changes: 0 additions & 5 deletions outputs.tf
Expand Up @@ -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
}
Copy link

Choose a reason for hiding this comment

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

why take this out?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because I'm using a map now to create multiple databases, thus the dbs var.

Copy link

Choose a reason for hiding this comment

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

ohh yeah that'll mess up the output that I don't know if we need. I think that makes this a major release?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I was tagging this 3.0.0


output "admin_username" {
description = "The administrator username of MySQL Server."
value = var.admin_username
Expand Down
77 changes: 31 additions & 46 deletions variables.tf
Expand Up @@ -4,28 +4,22 @@ 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" {
description = "The name of the server and resources to be created."
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 = {}
}