Skip to content

Commit 312cf8c

Browse files
authored
Merge pull request #5 from kolosovpetro/AZ400-314
AZ400-314. Update Terraform template
2 parents 6c4a4b8 + 3e11fba commit 312cf8c

File tree

12 files changed

+285
-115
lines changed

12 files changed

+285
-115
lines changed

.github/workflows/terraform-ci.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@ jobs:
4848

4949
- name: Terraform Version
5050
run: terraform -v
51-
51+
52+
- name: Mock Public key file
53+
run: echo test > id_rsa.pub
54+
55+
- name: Mock Password file
56+
run: echo test > password.txt
57+
5258
- name: Verify encoding
5359
shell: pwsh
54-
run: ./scripts/verify-encoding.ps1
60+
run: ./scripts/Test-Encoding.ps1
5561

5662
- name: Terraform Init
5763
run: terraform init -backend=false
@@ -60,4 +66,4 @@ jobs:
6066
run: terraform validate
6167

6268
- name: Terraform Format
63-
run: terraform fmt -check
69+
run: terraform fmt -check

.terraform.lock.hcl

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ and this project adheres to [Semantic Versioning v2.0.0](https://semver.org/spec
1919
- Azure pipelines for terraform
2020
- Add editorconfig
2121
- Remove example sas token
22+
- Remove json variables file
23+
- Add default values to tf variables
24+
- Fix encoding script
25+
- Add public key and password files mocks CI

azure-pipelines/terraform-ci.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,22 @@ stages:
2929
steps:
3030
- checkout: self
3131
fetchDepth: 0
32-
32+
3333
- task: PowerShell@2
3434
displayName: 'Fix Encoding'
3535
inputs:
3636
targetType: 'filePath'
37-
filePath: $(System.DefaultWorkingDirectory)/scripts/verify-encoding.ps1
37+
filePath: $(System.DefaultWorkingDirectory)/scripts/Test-Encoding.ps1
3838
arguments: '-Autofix'
3939
pwsh: true
4040
workingDirectory: $(System.DefaultWorkingDirectory)
4141

42+
- script: echo test > id_rsa.pub
43+
displayName: 'Public key mock'
44+
45+
- script: echo test > password.txt
46+
displayName: 'Password mock'
47+
4248
- task: TerraformInstaller@1
4349
displayName: 'Terraform Installer'
4450
inputs:

main.tf

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,65 @@
11
data "azurerm_client_config" "current" {}
22
data "azurerm_subscription" "current" {}
33

4+
#################################################################################################################
5+
# LOCALS
6+
#################################################################################################################
7+
8+
locals {
9+
vnet_cidr = ["10.10.0.0/24"]
10+
vm_subnet_cidr = ["10.10.0.0/26"]
11+
fw_subnet_cidr = ["10.10.0.64/26"]
12+
bastion_subnet_cidr = ["10.10.0.128/26"]
13+
}
14+
15+
#################################################################################################################
16+
# RESOURCE GROUP
17+
#################################################################################################################
18+
419
resource "azurerm_resource_group" "public" {
520
location = var.location
6-
name = "${var.resource_group_name}-${var.prefix}"
21+
name = "rg-tf-template-${var.prefix}"
722
tags = var.tags
823
}
924

25+
#################################################################################################################
26+
# VNET AND SUBNET
27+
#################################################################################################################
28+
29+
resource "azurerm_virtual_network" "public" {
30+
name = "vnet-${var.prefix}"
31+
address_space = local.vnet_cidr
32+
location = azurerm_resource_group.public.location
33+
resource_group_name = azurerm_resource_group.public.name
34+
}
35+
36+
resource "azurerm_subnet" "vm" {
37+
name = "snet-vm-${var.prefix}"
38+
resource_group_name = azurerm_resource_group.public.name
39+
virtual_network_name = azurerm_virtual_network.public.name
40+
address_prefixes = local.vm_subnet_cidr
41+
}
42+
43+
resource "azurerm_subnet" "fw" {
44+
name = "AzureFirewallSubnet"
45+
resource_group_name = azurerm_resource_group.public.name
46+
virtual_network_name = azurerm_virtual_network.public.name
47+
address_prefixes = local.fw_subnet_cidr
48+
}
49+
50+
resource "azurerm_subnet" "bastion_snet" {
51+
name = "AzureBastionSubnet"
52+
resource_group_name = azurerm_resource_group.public.name
53+
virtual_network_name = azurerm_virtual_network.public.name
54+
address_prefixes = local.bastion_subnet_cidr
55+
}
56+
57+
#################################################################################################################
58+
# MODULE CALL EXAMPLE
59+
#################################################################################################################
60+
1061
module "resource_group" {
11-
source = "./modules/example_submodule"
62+
source = "./modules/module"
1263
resource_group_location = var.location
1364
resource_group_name = "rg-from-module-${var.prefix}"
1465
}

scripts/Test-Encoding.ps1

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# SPDX-FileCopyrightText: 2020-2025 VerifyEncoding contributors <https://github.com/ForNeVeR/VerifyEncoding>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
<#
6+
.SYNOPSIS
7+
This function will verify that there's no UTF-8 BOM or CRLF line endings in the files inside of the project.
8+
#>
9+
10+
param (
11+
[string] $SourceRoot,
12+
[switch] $Autofix,
13+
[string[]] $ExcludeExtensions = @(
14+
'.dotsettings'
15+
)
16+
)
17+
18+
function Test-Encoding
19+
{
20+
param (
21+
# Path to the repository root. All text files under the root will be checked for UTF-8 BOM and CRLF.
22+
#
23+
# By default (if nothing's passed), the script will try auto-detecting the nearest Git root.
24+
[string] $SourceRoot,
25+
26+
# Makes the script to perform file modifications to bring them to the standard.
27+
[switch] $Autofix,
28+
29+
# List of file extensions (with leading dots) to ignore. Case-insensitive.
30+
[string[]] $ExcludeExtensions = @(
31+
'.dotsettings'
32+
)
33+
)
34+
35+
Set-StrictMode -Version Latest
36+
$ErrorActionPreference = 'Stop'
37+
38+
if (!$SourceRoot)
39+
{
40+
$SourceRoot = git rev-parse --show-toplevel
41+
42+
if (!$?)
43+
{
44+
throw "Cannot call `"git rev-parse`": exit code $LASTEXITCODE."
45+
}
46+
}
47+
48+
# For PowerShell to properly process the UTF-8 output from git ls-tree we need to set up the output encoding:
49+
[Console]::OutputEncoding = [Text.Encoding]::UTF8
50+
51+
try
52+
{
53+
Push-Location $SourceRoot
54+
55+
# Step 1: Get all file paths from git
56+
$gitFiles = git -c core.quotepath=off ls-tree -r HEAD --name-only
57+
58+
# Step 2: Filter out deleted files
59+
$existingFiles = $gitFiles | Where-Object { Test-Path -LiteralPath $_ }
60+
61+
# Step 3: Filter out directories (keep only files)
62+
$allFiles = @($existingFiles | Where-Object { -not (Get-Item -Force -LiteralPath $_).PSIsContainer })
63+
64+
if (!$?)
65+
{
66+
throw "Cannot call `"git ls-tree`": exit code $LASTEXITCODE."
67+
}
68+
69+
Write-Output "Total files in the repository: $($allFiles.Length)"
70+
71+
$counter = [pscustomobject]@{ Value = 0 }
72+
73+
$groupSize = 50
74+
75+
$chunks = @($allFiles | Group-Object -Property { [math]::Floor($counter.Value++ / $groupSize) })
76+
77+
Write-Output "Split into $( $chunks.Count ) chunks."
78+
79+
# https://stackoverflow.com/questions/6119956/how-to-determine-if-git-handles-a-file-as-binary-or-as-text#comment15281840_6134127
80+
$nullHash = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
81+
82+
$textFiles = @($chunks | ForEach-Object {
83+
$chunk = $_.Group
84+
$filePaths = git -c core.quotepath=off diff --numstat $nullHash HEAD -- @chunk
85+
if (!$?)
86+
{
87+
throw "Cannot call `"git diff`": exit code $LASTEXITCODE."
88+
}
89+
$filePaths |
90+
Where-Object { -not $_.StartsWith('-') } |
91+
ForEach-Object { [Regex]::Unescape($_.Split("`t", 3)[2]) }
92+
})
93+
94+
Write-Output "Text files in the repository: $( $textFiles.Length )"
95+
96+
$bom = @(0xEF, 0xBB, 0xBF)
97+
$bomErrors = @()
98+
$lineEndingErrors = @()
99+
100+
foreach ($file in $textFiles)
101+
{
102+
if ($ExcludeExtensions -contains [IO.Path]::GetExtension($file).ToLowerInvariant())
103+
{
104+
continue
105+
}
106+
107+
$fullPath = Resolve-Path -LiteralPath $file
108+
109+
$bytes = [IO.File]::ReadAllBytes($fullPath) | Select-Object -First $bom.Length
110+
111+
if (!$bytes)
112+
{
113+
continue
114+
} # filter empty files
115+
116+
$bytesEqualsBom = @(Compare-Object $bytes $bom -SyncWindow 0).Length -eq 0
117+
118+
if ($bytesEqualsBom -and $Autofix)
119+
{
120+
$fullContent = [IO.File]::ReadAllBytes($fullPath)
121+
$newContent = $fullContent | Select-Object -Skip $bom.Length
122+
[IO.File]::WriteAllBytes($fullPath, $newContent)
123+
Write-Output "Removed UTF-8 BOM from file $file"
124+
}
125+
elseif ($bytesEqualsBom)
126+
{
127+
$bomErrors += @($file)
128+
}
129+
130+
$text = [IO.File]::ReadAllText($fullPath)
131+
132+
$crlf = "`r`n"
133+
$lf = "`n"
134+
$cr = "`r"
135+
136+
$hasWrongLineEndings = $text.Contains($crlf) -or $text.Contains($cr)
137+
138+
if ($hasWrongLineEndings -and $Autofix)
139+
{
140+
$newText = $text -replace $crlf, $lf -replace $cr, $lf
141+
[IO.File]::WriteAllText($fullPath, $newText)
142+
Write-Output "Fixed the line endings for file $file"
143+
}
144+
elseif ($hasWrongLineEndings)
145+
{
146+
$lineEndingErrors += @($file)
147+
}
148+
}
149+
150+
if ($bomErrors.Length)
151+
{
152+
throw "The following $( $bomErrors.Length ) files have UTF-8 BOM:`n" + ($bomErrors -join "`n")
153+
}
154+
if ($lineEndingErrors.Length)
155+
{
156+
throw "The following $( $lineEndingErrors.Length ) files have CRLF instead of LF:`n" + ($lineEndingErrors -join "`n")
157+
}
158+
}
159+
finally
160+
{
161+
Pop-Location
162+
}
163+
}
164+
165+
# Convenience launch mode when not invoked as part of a module:
166+
if (!$MyInvocation.PSCommandPath -or !$MyInvocation.PSCommandPath.EndsWith('.psm1')) {
167+
Write-Output "Direct script launcher mode.$(if ($MyInvocation.PSCommandPath) {
168+
' Launched from "' + $MyInvocation.PSCommandPath + '".'
169+
})"
170+
Test-Encoding -SourceRoot:$SourceRoot -Autofix:$Autofix -ExcludedExtensions:$ExcludeExtensions
171+
}

0 commit comments

Comments
 (0)