Skip to content

Commit fd95f37

Browse files
Feature/sqlami (#55)
1 parent fef8bbd commit fd95f37

File tree

11 files changed

+524
-2
lines changed

11 files changed

+524
-2
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: Module:sql-managed-instance
2+
on:
3+
workflow_dispatch:
4+
pull_request:
5+
branches:
6+
- main
7+
paths:
8+
- '.github/workflows/sql-managed-instance.yml'
9+
- 'terraform/sql-managed-instance/**'
10+
- '.github/actions/**'
11+
12+
env:
13+
terraform_workingdir: "terraform/sql-managed-instance"
14+
GH_TOKEN: ${{ secrets.GH_TOKEN }}
15+
ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }}
16+
ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }}
17+
ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}
18+
ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}
19+
20+
jobs:
21+
terraform-lint:
22+
name: Run Terraform lint
23+
runs-on: ubuntu-latest
24+
defaults:
25+
run:
26+
working-directory: "${{ env.terraform_workingdir }}"
27+
28+
steps:
29+
- uses: actions/checkout@v2
30+
- uses: hashicorp/setup-terraform@v2
31+
32+
- name: Terraform fmt
33+
id: fmt
34+
run: terraform fmt -check
35+
continue-on-error: false
36+
37+
terraform-sec:
38+
name: Run Terraform tfsec
39+
needs:
40+
- terraform-lint
41+
runs-on: ubuntu-latest
42+
43+
steps:
44+
- name: Check out code
45+
uses: actions/checkout@main
46+
47+
- name: Run tfsec with reviewdog output on the PR
48+
uses: ./.github/actions/run-terraform-sec
49+
50+
terratest:
51+
name: Run Terratest
52+
needs:
53+
- terraform-sec
54+
runs-on: ubuntu-latest
55+
56+
defaults:
57+
run:
58+
working-directory: "${{ env.terraform_workingdir }}/test"
59+
60+
steps:
61+
- name: Check out code
62+
uses: actions/checkout@v3
63+
64+
- name: Set up Go
65+
uses: actions/setup-go@v2
66+
with:
67+
go-version: 1.18.2
68+
69+
- name: Setup Dependencies
70+
run: go mod init test && go mod tidy
71+
env:
72+
GOPATH: "/home/runner/work/azure-labs-modules/azure-labs-modules/${{ env.terraform_workingdir }}"
73+
74+
- name: Unit-test
75+
run: go test -v -timeout 120m
76+
env:
77+
GOPATH: "/home/runner/work/azure-labs-modules/azure-labs-modules/${{ env.terraform_workingdir }}"

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ terraform.tfvars
1010

1111
tests/
1212
app-service/
13-
stream-analytics-cluster/
14-
sql-managed-instance/
13+
stream-analytics-cluster/
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Associate subnet and the security group
2+
resource "azurerm_subnet_network_security_group_association" "adl_sqlmi" {
3+
subnet_id = var.subnet_id
4+
network_security_group_id = var.network_security_group_id
5+
count = var.module_enabled ? 1 : 0
6+
}
7+
8+
# Associate subnet and the route table
9+
resource "azurerm_subnet_route_table_association" "adl_sqlmi" {
10+
subnet_id = var.subnet_id
11+
route_table_id = var.route_table_id
12+
count = var.module_enabled ? 1 : 0
13+
}
14+
15+
# Associate subnet and the security group
16+
resource "azurerm_subnet_network_security_group_association" "adl_sqlmi_pe" {
17+
subnet_id = var.subnet_private_enpoint_id
18+
network_security_group_id = var.network_security_group_id
19+
count = var.is_sec_module && var.module_enabled ? 1 : 0
20+
}
21+
22+
# Associate subnet and the route table
23+
resource "azurerm_subnet_route_table_association" "adl_sqlmi_pe" {
24+
subnet_id = var.subnet_private_enpoint_id
25+
route_table_id = var.route_table_id
26+
count = var.is_sec_module && var.module_enabled ? 1 : 0
27+
}
28+
29+
30+
resource "azurerm_mssql_managed_instance" "adl_sqlmi" {
31+
name = "sqlmi-${var.basename}"
32+
resource_group_name = var.rg_name
33+
location = var.location
34+
35+
license_type = var.license_type
36+
sku_name = var.sku_name
37+
storage_size_in_gb = var.storage_size_in_gb
38+
storage_account_type = var.storage_account_type
39+
subnet_id = var.subnet_id
40+
vcores = var.vcores
41+
maintenance_configuration_name = var.maintenance_configuration_name
42+
dns_zone_partner_id = var.dns_zone_partner_id == "" ? null : var.dns_zone_partner_id
43+
44+
identity {
45+
type = "SystemAssigned"
46+
}
47+
48+
collation = var.collation
49+
proxy_override = var.proxy_override
50+
public_data_endpoint_enabled = var.public_data_endpoint_enabled
51+
timezone_id = var.timezone_id
52+
53+
administrator_login = var.administrator_login
54+
administrator_login_password = var.administrator_login_password
55+
56+
count = var.module_enabled ? 1 : 0
57+
tags = var.tags
58+
59+
depends_on = [
60+
azurerm_subnet_network_security_group_association.adl_sqlmi,
61+
azurerm_subnet_route_table_association.adl_sqlmi
62+
]
63+
}
64+
65+
# Private Endpoint configuration
66+
67+
resource "azurerm_private_endpoint" "sqlmi_pe_server" {
68+
name = "pe-${azurerm_mssql_managed_instance.adl_sqlmi[0].name}-sqlmi"
69+
location = var.location
70+
resource_group_name = var.rg_name
71+
subnet_id = var.subnet_private_enpoint_id
72+
73+
private_service_connection {
74+
name = "psc-sqlmi-${var.basename}"
75+
private_connection_resource_id = azurerm_mssql_managed_instance.adl_sqlmi[0].id
76+
subresource_names = ["managedInstance"]
77+
is_manual_connection = false
78+
}
79+
80+
private_dns_zone_group {
81+
name = "private-dns-zone-group-server"
82+
private_dns_zone_ids = var.private_dns_zone_ids
83+
}
84+
count = var.is_sec_module && var.module_enabled ? 1 : 0
85+
86+
tags = var.tags
87+
}
88+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
output "id" {
2+
value = (
3+
length(azurerm_mssql_managed_instance.adl_sqlmi) > 0 ?
4+
azurerm_mssql_managed_instance.adl_sqlmi[0].id : ""
5+
)
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
locals {
2+
tags = {
3+
Project = "Azure/azure-data-labs-modules"
4+
Module = "sql-managed-instance"
5+
Toolkit = "Terraform"
6+
}
7+
8+
dns_sql_server = "privatelink.database.windows.net"
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "id" {
2+
value = module.sql_managed_instance.id
3+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
terraform {
2+
backend "azurerm" {
3+
resource_group_name = "rg-adl-terraform-state"
4+
storage_account_name = "stadltfstate"
5+
container_name = "default"
6+
key = "sqlami.terraform.tfstate"
7+
8+
9+
}
10+
11+
required_providers {
12+
azurerm = {
13+
source = "hashicorp/azurerm"
14+
version = "= 3.30.0"
15+
}
16+
}
17+
18+
}
19+
20+
provider "azurerm" {
21+
features {
22+
resource_group {
23+
prevent_deletion_if_contains_resources = false
24+
}
25+
}
26+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
# Modules dependencies
3+
module "local_rg" {
4+
source = "../../resource-group"
5+
basename = random_string.postfix.result
6+
location = var.location
7+
tags = local.tags
8+
}
9+
10+
module "local_vnet" {
11+
source = "../../virtual-network"
12+
rg_name = module.local_rg.name
13+
basename = "vnet-${random_string.postfix.result}-sql-default"
14+
location = var.location
15+
address_space = ["10.0.0.0/16"]
16+
}
17+
18+
module "local_snet_default" {
19+
source = "../../subnet"
20+
rg_name = module.local_rg.name
21+
name = "snet-${random_string.postfix.result}-sqlmi-default"
22+
vnet_name = module.local_vnet.name
23+
address_prefixes = ["10.0.6.0/24"]
24+
subnet_delegation = {
25+
delegation = [{
26+
name = "Microsoft.Sql/managedInstances"
27+
actions = ["Microsoft.Network/virtualNetworks/subnets/join/action", "Microsoft.Network/virtualNetworks/subnets/prepareNetworkPolicies/action", "Microsoft.Network/virtualNetworks/subnets/unprepareNetworkPolicies/action"]
28+
}] }
29+
}
30+
31+
module "local_snet_private_enpoint" {
32+
source = "../../subnet"
33+
rg_name = module.local_rg.name
34+
name = "snet-${random_string.postfix.result}-sqlmi-private-endpoint"
35+
vnet_name = module.local_vnet.name
36+
address_prefixes = ["10.0.5.0/24"]
37+
}
38+
39+
module "network_security_group" {
40+
source = "../../network-security-group"
41+
basename = "nsg-${random_string.postfix.result}"
42+
rg_name = module.local_rg.name
43+
location = var.location
44+
tags = {}
45+
}
46+
47+
module "route_table" {
48+
source = "../../route-table"
49+
basename = "route-${random_string.postfix.result}"
50+
rg_name = module.local_rg.name
51+
location = var.location
52+
tags = {}
53+
}
54+
55+
// sql_managed_instance module
56+
module "sql_managed_instance" {
57+
source = "git::https://github.com/Azure/azure-data-labs-modules.git//terraform/sql-managed-instance?ref=feature/sqlami"
58+
basename = "sqlmi-${random_string.postfix.result}"
59+
rg_name = module.local_rg.name
60+
location = var.location
61+
subnet_id = module.local_snet_default.id
62+
subnet_private_enpoint_id = module.local_snet_private_enpoint.id
63+
route_table_id = module.route_table.id
64+
network_security_group_id = module.network_security_group.id
65+
administrator_login = "sqladminuser"
66+
administrator_login_password = "ThisIsNotVerySecure!"
67+
module_enabled = true
68+
is_sec_module = var.is_sec_module
69+
tags = {}
70+
license_type = "BasePrice"
71+
sku_name = "GP_Gen5"
72+
storage_size_in_gb = 32
73+
vcores = 4
74+
maintenance_configuration_name = "SQL_Default"
75+
dns_zone_partner_id = ""
76+
collation = "SQL_Latin1_General_CP1_CI_AS"
77+
minimum_tls_version = "1.2"
78+
proxy_override = "Default"
79+
public_data_endpoint_enabled = false
80+
storage_account_type = "GRS"
81+
timezone_id = "UTC"
82+
83+
}
84+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
"github.com/gruntwork-io/terratest/modules/terraform"
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestModule(t *testing.T) {
10+
t.Parallel()
11+
12+
terraformOptions := &terraform.Options{
13+
TerraformDir: "./",
14+
// VarFiles: []string{"terraform_unitest.tfvars"},
15+
}
16+
17+
// At the end of the test, run `terraform destroy` to clean up any resources that were created
18+
defer terraform.Destroy(t, terraformOptions)
19+
20+
// Is used mainly for debugging, fail early if plan is not possible
21+
terraform.InitAndPlan(t, terraformOptions)
22+
23+
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
24+
terraform.InitAndApply(t, terraformOptions)
25+
26+
// Check if the outputs exist
27+
assert := assert.New(t)
28+
id := terraform.Output(t, terraformOptions, "id")
29+
assert.NotNil(id)
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
resource "random_string" "postfix" {
2+
length = 8
3+
special = false
4+
upper = false
5+
}
6+
7+
variable "location" {
8+
type = string
9+
default = "North Europe"
10+
}
11+
12+
variable "rg_name_dns" {
13+
type = string
14+
default = "rg-adl-modules-test-01-global-dns"
15+
}
16+
17+
variable "is_sec_module" {
18+
type = bool
19+
description = "Is secure module?"
20+
default = true
21+
}

0 commit comments

Comments
 (0)