Skip to content

Commit

Permalink
Add mssql server
Browse files Browse the repository at this point in the history
  • Loading branch information
CDA0 committed Jan 14, 2024
1 parent 04608ac commit 8ff7cbd
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/mssql-server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Changelog
32 changes: 32 additions & 0 deletions modules/mssql-server/README.md
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/gofrontier-com/azurerm-terraform-modules/blob/main/README.rst#contributing>.
1 change: 1 addition & 0 deletions modules/mssql-server/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0
14 changes: 14 additions & 0 deletions modules/mssql-server/src/locals.tf
Original file line number Diff line number Diff line change
@@ -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
}
}
54 changes: 54 additions & 0 deletions modules/mssql-server/src/main.tf
Original file line number Diff line number Diff line change
@@ -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
}
}
}
11 changes: 11 additions & 0 deletions modules/mssql-server/src/outputs.tf
Original file line number Diff line number Diff line change
@@ -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
}
76 changes: 76 additions & 0 deletions modules/mssql-server/src/variables.tf
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions modules/mssql-server/test/main.tf
Original file line number Diff line number Diff line change
@@ -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"
}
}
10 changes: 10 additions & 0 deletions modules/mssql-server/test/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = "~> 1.5"

required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.85"
}
}
}

0 comments on commit 8ff7cbd

Please sign in to comment.