From 8ff7cbdf999896f5ca2cf94852e345b63ebf66bc Mon Sep 17 00:00:00 2001 From: Craig Anderson <1877372+CDA0@users.noreply.github.com> Date: Sun, 14 Jan 2024 16:24:55 +0000 Subject: [PATCH] Add mssql server --- modules/mssql-server/CHANGELOG.md | 1 + modules/mssql-server/README.md | 32 +++++++++++ modules/mssql-server/VERSION | 1 + modules/mssql-server/src/locals.tf | 14 +++++ modules/mssql-server/src/main.tf | 54 ++++++++++++++++++ modules/mssql-server/src/outputs.tf | 11 ++++ modules/mssql-server/src/variables.tf | 76 ++++++++++++++++++++++++++ modules/mssql-server/test/main.tf | 20 +++++++ modules/mssql-server/test/terraform.tf | 10 ++++ 9 files changed, 219 insertions(+) create mode 100644 modules/mssql-server/CHANGELOG.md create mode 100644 modules/mssql-server/README.md create mode 100644 modules/mssql-server/VERSION create mode 100644 modules/mssql-server/src/locals.tf create mode 100644 modules/mssql-server/src/main.tf create mode 100644 modules/mssql-server/src/outputs.tf create mode 100644 modules/mssql-server/src/variables.tf create mode 100644 modules/mssql-server/test/main.tf create mode 100644 modules/mssql-server/test/terraform.tf diff --git a/modules/mssql-server/CHANGELOG.md b/modules/mssql-server/CHANGELOG.md new file mode 100644 index 0000000..825c32f --- /dev/null +++ b/modules/mssql-server/CHANGELOG.md @@ -0,0 +1 @@ +# Changelog diff --git a/modules/mssql-server/README.md b/modules/mssql-server/README.md new file mode 100644 index 0000000..e4cd9ea --- /dev/null +++ b/modules/mssql-server/README.md @@ -0,0 +1,32 @@ +# MS SQL Server + +This module creates a [MS SQL Server](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mssql_database) and associated [Diagnostic Setting](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_diagnostic_setting). + +## Usage + +```hcl +module "mssql_server" { + source = "https://github.com/gofrontier-com/azurerm-terraform-modules/releases/download/mssql-server/[VERSION]/module.tar.gz//src" + + administrator_username = "mysqladmin" + administrator_password = "P@ssw0rd1234!" + environment = "dev" + identifier = "mortgages" + location = "uksouth" + log_analytics_workspace_id = data.azurerm_log_analytics_workspace.main.id + resource_group_name = module.resource_group.name + zone = "mtg" + + tags = { + WorkloadType = "MortgagesLZ/data-platform" + } +} +``` + +## Known issues + +_None._ + +## Contributing + +See . diff --git a/modules/mssql-server/VERSION b/modules/mssql-server/VERSION new file mode 100644 index 0000000..d3827e7 --- /dev/null +++ b/modules/mssql-server/VERSION @@ -0,0 +1 @@ +1.0 diff --git a/modules/mssql-server/src/locals.tf b/modules/mssql-server/src/locals.tf new file mode 100644 index 0000000..6a78776 --- /dev/null +++ b/modules/mssql-server/src/locals.tf @@ -0,0 +1,14 @@ +locals { + identifier = replace(lower(var.identifier), "/[^a-z1-9]/", "") + + short_locations = { + "uksouth" = "uks" + "ukwest" = "ukw" + } + + tags = { + Environment = var.environment + Location = var.location + Zone = var.zone + } +} diff --git a/modules/mssql-server/src/main.tf b/modules/mssql-server/src/main.tf new file mode 100644 index 0000000..2765e70 --- /dev/null +++ b/modules/mssql-server/src/main.tf @@ -0,0 +1,54 @@ +resource "azurerm_mssql_server" "main" { + minimum_tls_version = var.minimum_tls_version + location = var.location + name = "mssql-${var.zone}-${var.environment}-${lookup(local.short_locations, var.location)}-${local.identifier}" + resource_group_name = var.resource_group_name + version = var.sql_server_version + administrator_login = var.azuread_administrator.azuread_authentication_only ? null : var.administrator_username + administrator_login_password = var.azuread_administrator.azuread_authentication_only ? null : var.administrator_password + tags = merge(var.tags, local.tags) + identity { + type = "SystemAssigned" + } + dynamic "azuread_administrator" { + for_each = var.azuread_administrator != null ? [{}] : [] + content { + azuread_authentication_only = var.azuread_administrator.azuread_authentication_only + login_username = var.azuread_administrator.login_username + object_id = var.azuread_administrator.object_id + } + } +} + + + +resource "azurerm_monitor_diagnostic_setting" "main" { + name = "log-analytics" + target_resource_id = azurerm_mssql_server.main.id + log_analytics_workspace_id = var.log_analytics_workspace_id + log_analytics_destination_type = "AzureDiagnostics" + + dynamic "enabled_log" { + for_each = var.log_categories + + content { + category = enabled_log.value + } + } + + dynamic "enabled_log" { + for_each = var.log_category_groups + + content { + category_group = enabled_log.value + } + } + + dynamic "metric" { + for_each = var.metric_categories + + content { + category = metric.value + } + } +} diff --git a/modules/mssql-server/src/outputs.tf b/modules/mssql-server/src/outputs.tf new file mode 100644 index 0000000..1863f2d --- /dev/null +++ b/modules/mssql-server/src/outputs.tf @@ -0,0 +1,11 @@ +output "id" { + value = azurerm_mssql_server.main.id +} + +output "name" { + value = azurerm_mssql_server.main.name +} + +output "fqdn" { + value = azurerm_mssql_server.main.fully_qualified_domain_name +} diff --git a/modules/mssql-server/src/variables.tf b/modules/mssql-server/src/variables.tf new file mode 100644 index 0000000..d868662 --- /dev/null +++ b/modules/mssql-server/src/variables.tf @@ -0,0 +1,76 @@ +variable "administrator_password" { + type = string + default = null +} + +variable "administrator_username" { + type = string + default = "sqladmin" +} + +variable "azuread_administrator" { + type = object({ + azuread_authentication_only = bool + login_username = string + object_id = string + }) + default = { + azuread_authentication_only = true + login_username = null + object_id = null + } +} + +variable "environment" { + type = string +} + +variable "identifier" { + type = string +} + +variable "location" { + type = string +} + +variable "log_analytics_workspace_id" { + type = string +} + +variable "log_categories" { + type = list(string) + default = [] +} + +variable "log_category_groups" { + type = list(string) + default = [] +} + +variable "metric_categories" { + type = list(string) + default = [] +} + +variable "minimum_tls_version" { + type = string + default = "1.2" +} + +variable "resource_group_name" { + type = string +} + +variable "sql_server_version" { + type = string + default = "12.0" +} + +variable "tags" { + type = map(string) + default = {} +} + +variable "zone" { + type = string +} diff --git a/modules/mssql-server/test/main.tf b/modules/mssql-server/test/main.tf new file mode 100644 index 0000000..de4798a --- /dev/null +++ b/modules/mssql-server/test/main.tf @@ -0,0 +1,20 @@ +provider "azurerm" { + features {} +} + +module "mssql-server" { + source = "../src" + + administrator_username = "sqladmin" + administrator_password = "P@ssw0rd1234!" + environment = "baz" + identifier = "qux" + location = "uksouth" + log_analytics_workspace_id = "quux" + resource_group_name = "grault" + zone = "waldo" + + tags = { + Foo = "Bar" + } +} diff --git a/modules/mssql-server/test/terraform.tf b/modules/mssql-server/test/terraform.tf new file mode 100644 index 0000000..762ef2e --- /dev/null +++ b/modules/mssql-server/test/terraform.tf @@ -0,0 +1,10 @@ +terraform { + required_version = "~> 1.5" + + required_providers { + azurerm = { + source = "hashicorp/azurerm" + version = "~> 3.85" + } + } +}