Skip to content

Commit

Permalink
feat: Added support for random password
Browse files Browse the repository at this point in the history
  • Loading branch information
13archit committed Jul 3, 2023
1 parent c88ef22 commit 39a1149
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 37 deletions.
8 changes: 6 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ updates:
schedule:
interval: "weekly"
- package-ecosystem: "terraform" # See documentation for possible values
directory: "examples/mysql-flexible-server" # Location of package manifests
directory: "_example/basic" # Location of package manifests
schedule:
interval: "weekly"
- package-ecosystem: "terraform" # See documentation for possible values
directory: "examples/mysql-flexible-server-replication" # Location of package manifests
directory: "_example/complete" # Location of package manifests
schedule:
interval: "weekly"
- package-ecosystem: "terraform" # See documentation for possible values
directory: "_example/mysql-flexible-server-replication" # Location of package manifests
schedule:
interval: "weekly"
2 changes: 1 addition & 1 deletion .github/workflows/semantic-releaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- main
paths:
- '**.tf'
- '!examples/**.tf'
- '!_example/**.tf'

jobs:
release:
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/static-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ jobs:
- ${{ needs.versionExtract.outputs.minVersion }}
- ${{ needs.versionExtract.outputs.maxVersion }}
directory:
- examples/mysql-flexible-server
- examples/mysql-flexible-server-replication
- _example/basic
- _example/complete
- _example/mysql-flexible-server-replication
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
29 changes: 29 additions & 0 deletions _example/basic/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
##-----------------------------------------------------------------------------
## Flexible Mysql server module call.
##-----------------------------------------------------------------------------
module "flexible-mysql" {
depends_on = [module.resource_group, module.vnet]
source = "../../"
name = "app"
environment = "test"
resource_group_name = "test-rg"
location = "Central India"
virtual_network_id = "/subscriptions/---------------<vnet_id>---------------"
delegated_subnet_id = "/subscriptions/---------------<delegated_subnet_id>---------------"
mysql_version = "8.0.21"
mysql_server_name = "testmysqlserver"
private_dns = true
zone = "1"
admin_username = "mysqlusername"
admin_password = "ba5yatgfgfhdsv6A3ns2lu4gqzzc"
sku_name = "GP_Standard_D8ds_v4"
db_name = "maindb"
charset = "utf8mb3"
collation = "utf8mb3_unicode_ci"
auto_grow_enabled = true
iops = 360
size_gb = "20"
##azurerm_mysql_flexible_server_configuration
server_configuration_names = ["interactive_timeout", "audit_log_enabled"]
values = ["600", "ON"]
}
17 changes: 17 additions & 0 deletions _example/basic/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
output "flexible-mysql_server_id" {
value = module.flexible-mysql.mysql_flexible_server_id
description = "The ID of the MySQL Flexible Server."
}
output "azurerm_private_dns_zone_virtual_network_link_id" {
value = module.flexible-mysql.azurerm_private_dns_zone_virtual_network_link_id
description = "The ID of the Private DNS Zone Virtual Network Link."
}
output "azurerm_flexible-mysql_server_configuration_id" {
value = module.flexible-mysql.azurerm_mysql_flexible_server_configuration_id
description = "The ID of the MySQL Flexible Server Configuration."
}

output "azurerm_private_dns_zone_id" {
value = module.flexible-mysql.azurerm_private_dns_zone_id
description = "The Private DNS Zone ID."
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,50 @@ provider "azurerm" {
features {}
}

module "resource_group" {
source = "clouddrove/resource-group/azure"
version = "1.0.2"

locals {
name = "app"
environment = "test"
label_order = ["name", "environment"]
location = "Canada Central"
}

##-----------------------------------------------------------------------------
## Resource Group module call
## Resource group in which all resources will be deployed.
##-----------------------------------------------------------------------------
module "resource_group" {
source = "clouddrove/resource-group/azure"
version = "1.0.2"
name = local.name
environment = local.environment
label_order = local.label_order
location = "Central India"
}

##-----------------------------------------------------------------------------
## Virtual Network module call.
##-----------------------------------------------------------------------------
module "vnet" {
source = "clouddrove/vnet/azure"
version = "1.0.2"
name = "app"
environment = "test"
name = local.name
environment = local.environment
resource_group_name = module.resource_group.resource_group_name
location = module.resource_group.resource_group_location
address_space = "10.0.0.0/16"
}

##-----------------------------------------------------------------------------
## Subnet module call.
## Delegated subnet for mysql.
##-----------------------------------------------------------------------------
module "subnet" {
source = "clouddrove/subnet/azure"
version = "1.0.2"
name = "app"
environment = "test"
name = local.name
environment = local.environment
resource_group_name = module.resource_group.resource_group_name
location = module.resource_group.resource_group_location
virtual_network_name = join("", module.vnet.vnet_name)

#subnet
subnet_names = ["default"]
subnet_prefixes = ["10.0.1.0/24"]
Expand All @@ -45,13 +60,14 @@ module "subnet" {
}
}



##-----------------------------------------------------------------------------
## Flexible Mysql server module call.
##-----------------------------------------------------------------------------
module "flexible-mysql" {
depends_on = [module.resource_group, module.vnet]
source = "clouddrove/flexible-mysql/azure"
name = "app"
environment = "test"
source = "../../"
name = local.name
environment = local.environment
resource_group_name = module.resource_group.resource_group_name
location = module.resource_group.resource_group_location
virtual_network_id = module.vnet.vnet_id[0]
Expand All @@ -69,9 +85,7 @@ module "flexible-mysql" {
auto_grow_enabled = true
iops = 360
size_gb = "20"

##azurerm_mysql_flexible_server_configuration
server_configuration_names = ["interactive_timeout", "audit_log_enabled"]
values = ["600", "ON"]

}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,47 @@ provider "azurerm" {
features {}
}

module "resource_group" {
source = "clouddrove/resource-group/azure"
version = "1.0.2"

name = "app-mysqll2"
locals {
name = "app"
environment = "test"
label_order = ["name", "environment"]
}

##-----------------------------------------------------------------------------
## Resource Group module call
## Resource group in which all resources will be deployed.
##-----------------------------------------------------------------------------
module "resource_group" {
source = "clouddrove/resource-group/azure"
version = "1.0.2"
name = local.name
environment = local.environment
label_order = local.label_order
location = "Canada Central"
}

##-----------------------------------------------------------------------------
## Virtual Network module call.
##-----------------------------------------------------------------------------
module "vnet" {
source = "clouddrove/vnet/azure"
version = "1.0.3"
name = "app"
environment = "test"
name = local.name
environment = local.environment
resource_group_name = module.resource_group.resource_group_name
location = module.resource_group.resource_group_location
address_space = "10.0.0.0/16"
}

##-----------------------------------------------------------------------------
## Subnet module call.
## Delegated subnet for mysql.
##-----------------------------------------------------------------------------
module "subnet" {
source = "clouddrove/subnet/azure"
version = "1.0.2"
name = "app"
environment = "test"
name = local.name
environment = local.environment
resource_group_name = module.resource_group.resource_group_name
location = module.resource_group.resource_group_location
virtual_network_name = join("", module.vnet.vnet_name)
Expand All @@ -45,22 +61,31 @@ module "subnet" {
}
}

#existing resource group where dns zone created
##-----------------------------------------------------------------------------
## Existing resource group where dns zone created
##-----------------------------------------------------------------------------
data "azurerm_resource_group" "main" {
name = "app-mysqll-test-resource-group"
}

##-----------------------------------------------------------------------------
## Data block for existing private dns zone.
## Required because for replication both flexible mysql servers must be in same private dns zone.
##-----------------------------------------------------------------------------
data "azurerm_private_dns_zone" "main" {
depends_on = [data.azurerm_resource_group.main]
name = "privatelink.mysql.database.azure.com"
resource_group_name = data.azurerm_resource_group.main.name
}

##-----------------------------------------------------------------------------
## Flexible Mysql server module call.
##-----------------------------------------------------------------------------
module "flexible-mysql" {
depends_on = [module.resource_group, module.vnet, data.azurerm_resource_group.main]
source = "clouddrove/flexible-mysql/azure"
name = "app"
environment = "test"
name = local.name
environment = local.environment
main_rg_name = data.azurerm_resource_group.main.name
resource_group_name = module.resource_group.resource_group_name
location = module.resource_group.resource_group_location
Expand Down
46 changes: 44 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
## Vritual Network and Subnet Creation
##-----------------------------------------------------------------------------
## Vritual Network and Subnet Creation
##-----------------------------------------------------------------------------
data "azurerm_client_config" "current" {}

##-----------------------------------------------------------------------------
## Locals Declaration
##-----------------------------------------------------------------------------
locals {
resource_group_name = var.resource_group_name
location = var.location
}

##-----------------------------------------------------------------------------
## Labels module callled that will be used for naming and tags.
##-----------------------------------------------------------------------------
module "labels" {
source = "clouddrove/labels/azure"
version = "1.0.0"
Expand All @@ -16,14 +24,31 @@ module "labels" {
repository = var.repository
}

##-----------------------------------------------------------------------------
## Random Password Resource.
## Will be passed as admin password of mysql server when admin password is not passed manually as variable.
##-----------------------------------------------------------------------------

resource "random_password" "main" {
count = var.admin_password == null ? 1 : 0
length = var.admin_password_length
min_upper = 4
min_lower = 2
min_numeric = 4
special = false
}

##-----------------------------------------------------------------------------
## Below resource will create flexible mysql server in Azure environment.
##-----------------------------------------------------------------------------

resource "azurerm_mysql_flexible_server" "main" {
count = var.enabled ? 1 : 0
name = format("%s-mysql-flexible-server", module.labels.id)
resource_group_name = local.resource_group_name
location = var.location
administrator_login = var.admin_username
administrator_password = var.admin_password
administrator_password = var.admin_password == null ? random_password.main[0].result : var.admin_password
backup_retention_days = var.backup_retention_days
delegated_subnet_id = var.delegated_subnet_id
private_dns_zone_id = var.private_dns ? join("", azurerm_private_dns_zone.main.*.id) : var.existing_private_dns_zone_id
Expand Down Expand Up @@ -55,6 +80,10 @@ resource "azurerm_mysql_flexible_server" "main" {
depends_on = [azurerm_private_dns_zone_virtual_network_link.main, azurerm_private_dns_zone_virtual_network_link.main2]
}

##-----------------------------------------------------------------------------
## Below resource will create mysql flexible database.
##-----------------------------------------------------------------------------

resource "azurerm_mysql_flexible_database" "main" {
count = var.enabled ? 1 : 0
name = var.db_name
Expand All @@ -65,6 +94,10 @@ resource "azurerm_mysql_flexible_database" "main" {
depends_on = [azurerm_mysql_flexible_server.main]
}

##-----------------------------------------------------------------------------
## Below resource will create flexible mysql server configuration.
##-----------------------------------------------------------------------------

resource "azurerm_mysql_flexible_server_configuration" "main" {
count = var.enabled ? length(var.server_configuration_names) : 0
name = element(var.server_configuration_names, count.index)
Expand All @@ -82,13 +115,19 @@ resource "azurerm_mysql_server_key" "main" {
key_vault_key_id = var.key_vault_key_id
}

##-----------------------------------------------------------------------------
## Below resource will deploy private dns for flexible mysql server.
##-----------------------------------------------------------------------------
resource "azurerm_private_dns_zone" "main" {
count = var.enabled && var.private_dns ? 1 : 0
name = "privatelink.mysql.database.azure.com"
resource_group_name = local.resource_group_name
tags = module.labels.tags
}

##-----------------------------------------------------------------------------
## Below resource will create vnet link in above created mysql private dns resource.
##-----------------------------------------------------------------------------
resource "azurerm_private_dns_zone_virtual_network_link" "main" {
count = var.enabled && var.private_dns ? 1 : 0
name = format("mysql-endpoint-link-%s", module.labels.id)
Expand All @@ -99,6 +138,9 @@ resource "azurerm_private_dns_zone_virtual_network_link" "main" {
tags = module.labels.tags
}

##-----------------------------------------------------------------------------
## Below resource will create vnet link in previously existing mysql private dns zone.
##-----------------------------------------------------------------------------
resource "azurerm_private_dns_zone_virtual_network_link" "main2" {
count = var.enabled && var.existing_private_dns_zone ? 1 : 0
name = format("mysql-endpoint-link-%s", module.labels.id)
Expand Down
8 changes: 7 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ variable "admin_password" {
default = null
}

variable "admin_password_length" {
type = number
default = 16
description = "Length of random password generated."
}

variable "backup_retention_days" {
type = number
default = 7
Expand Down Expand Up @@ -243,4 +249,4 @@ variable "high_availability" {
mode = "SameZone"
standby_availability_zone = 1
}
}
}

0 comments on commit 39a1149

Please sign in to comment.