Repository navigation
Expand file tree
/
Copy pathdeploy-sql-database.ps1
More file actions
294 lines (233 loc) · 7.5 KB
/
Copy pathdeploy-sql-database.ps1
File metadata and controls
294 lines (233 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# SQL Database Module Deployment Script
# This script provides automation for deploying, testing, and managing the SQL Database module
param(
[Parameter(Mandatory=$false)]
[string]$Action = "deploy",
[Parameter(Mandatory=$false)]
[string]$Environment = "test",
[Parameter(Mandatory=$false)]
[string]$Location = "East US",
[Parameter(Mandatory=$false)]
[string]$ResourceGroupName = "",
[Parameter(Mandatory=$false)]
[string]$SQLServerName = "",
[Parameter(Mandatory=$false)]
[switch]$SkipTests,
[Parameter(Mandatory=$false)]
[switch]$Cleanup
)
# Configuration
$ModulePath = Join-Path $PSScriptRoot "..\modules\database\sql-database"
$TestPath = Join-Path $ModulePath "test"
$ExamplesPath = Join-Path $ModulePath "examples"
# Colors for output
$Green = "Green"
$Yellow = "Yellow"
$Red = "Red"
$Cyan = "Cyan"
function Write-ColorOutput {
param(
[string]$Message,
[string]$Color = "White"
)
Write-Host $Message -ForegroundColor $Color
}
function Test-Prerequisites {
Write-ColorOutput "Checking prerequisites..." $Cyan
# Check Terraform
try {
$tfVersion = terraform version
Write-ColorOutput "✓ Terraform installed: $tfVersion" $Green
} catch {
Write-ColorOutput "✗ Terraform not found. Please install Terraform >= 1.5.0" $Red
exit 1
}
# Check Go
try {
$goVersion = go version
Write-ColorOutput "✓ Go installed: $goVersion" $Green
} catch {
Write-ColorOutput "✗ Go not found. Please install Go >= 1.21" $Red
exit 1
}
# Check Azure CLI
try {
$azVersion = az version --query '"azure-cli"' -o tsv
Write-ColorOutput "✓ Azure CLI installed: $azVersion" $Green
} catch {
Write-ColorOutput "✗ Azure CLI not found. Please install Azure CLI" $Red
exit 1
}
Write-ColorOutput "Prerequisites check completed." $Green
}
function Initialize-Module {
Write-ColorOutput "Initializing SQL Database module..." $Cyan
Push-Location $ModulePath
try {
Write-ColorOutput "Running terraform init..." $Yellow
terraform init -backend=false
Write-ColorOutput "Running terraform validate..." $Yellow
terraform validate
Write-ColorOutput "✓ Module initialized successfully" $Green
} catch {
Write-ColorOutput "✗ Module initialization failed: $_" $Red
Pop-Location
exit 1
}
Pop-Location
}
function Invoke-Tests {
if ($SkipTests) {
Write-ColorOutput "Skipping tests as requested." $Yellow
return
}
Write-ColorOutput "Running Terratest suite..." $Cyan
Push-Location $TestPath
try {
Write-ColorOutput "Installing Go dependencies..." $Yellow
go mod download
Write-ColorOutput "Running Go vet..." $Yellow
go vet ./...
Write-ColorOutput "Running Go fmt check..." $Yellow
$fmtResult = gofmt -s -l .
if ($fmtResult) {
Write-ColorOutput "✗ Go code formatting issues found:" $Red
Write-ColorOutput $fmtResult $Red
exit 1
}
Write-ColorOutput "Compiling tests..." $Yellow
go test -c -o sql_database_test
Write-ColorOutput "✓ Tests compiled successfully" $Green
Write-ColorOutput "Note: Actual test execution requires Azure authentication and resources" $Yellow
} catch {
Write-ColorOutput "✗ Test execution failed: $_" $Red
Pop-Location
exit 1
}
Pop-Location
}
function Deploy-Example {
param(
[string]$ExampleName = "basic"
)
$examplePath = Join-Path $ExamplesPath $ExampleName
if (!(Test-Path $examplePath)) {
Write-ColorOutput "✗ Example '$ExampleName' not found" $Red
return
}
Write-ColorOutput "Deploying example: $ExampleName" $Cyan
Push-Location $examplePath
try {
Write-ColorOutput "Initializing Terraform..." $Yellow
terraform init -backend=false
Write-ColorOutput "Planning deployment..." $Yellow
terraform plan -out=tfplan
if (!$WhatIf) {
$confirmation = Read-Host "Do you want to apply this plan? (y/N)"
if ($confirmation -eq 'y' -or $confirmation -eq 'Y') {
Write-ColorOutput "Applying deployment..." $Yellow
terraform apply tfplan
Write-ColorOutput "✓ Example deployed successfully" $Green
} else {
Write-ColorOutput "Deployment cancelled by user" $Yellow
}
}
} catch {
Write-ColorOutput "✗ Deployment failed: $_" $Red
Pop-Location
exit 1
}
Pop-Location
}
function Remove-Resources {
param(
[string]$ExampleName = "basic"
)
$examplePath = Join-Path $ExamplesPath $ExampleName
if (!(Test-Path $examplePath)) {
Write-ColorOutput "✗ Example '$ExampleName' not found" $Red
return
}
Write-ColorOutput "Cleaning up example: $ExampleName" $Cyan
Push-Location $examplePath
try {
Write-ColorOutput "Planning destruction..." $Yellow
terraform plan -destroy -out=tfplan-destroy
$confirmation = Read-Host "Do you want to destroy these resources? (y/N)"
if ($confirmation -eq 'y' -or $confirmation -eq 'Y') {
Write-ColorOutput "Destroying resources..." $Yellow
terraform apply tfplan-destroy
Write-ColorOutput "✓ Resources destroyed successfully" $Green
} else {
Write-ColorOutput "Cleanup cancelled by user" $Yellow
}
} catch {
Write-ColorOutput "✗ Cleanup failed: $_" $Red
}
Pop-Location
}
function Show-Usage {
Write-ColorOutput @"
SQL Database Module Automation Script
USAGE:
.\sql-database.ps1 [options]
ACTIONS:
-Action <action> Action to perform (deploy, test, validate, cleanup)
Default: deploy
OPTIONS:
-Environment <env> Environment name (test, dev, staging, prod)
Default: test
-Location <location> Azure region
Default: East US
-ResourceGroupName <rg> Resource group name (optional)
-SQLServerName <server> SQL Server name (optional)
-SkipTests Skip running tests
-Cleanup Clean up resources after deployment
EXAMPLES:
# Deploy basic example
.\sql-database.ps1 -Action deploy
# Run tests only
.\sql-database.ps1 -Action test
# Validate module
.\sql-database.ps1 -Action validate
# Deploy high availability example
.\sql-database.ps1 -Action deploy -Example high-availability
# Clean up resources
.\sql-database.ps1 -Action cleanup
"@ -Color White
}
# Main execution
switch ($Action.ToLower()) {
"deploy" {
Test-Prerequisites
Initialize-Module
Invoke-Tests
Deploy-Example -ExampleName "basic"
}
"test" {
Test-Prerequisites
Invoke-Tests
}
"validate" {
Test-Prerequisites
Initialize-Module
}
"cleanup" {
Remove-Resources -ExampleName "basic"
}
"deploy-ha" {
Test-Prerequisites
Initialize-Module
Invoke-Tests
Deploy-Example -ExampleName "high-availability"
}
"cleanup-ha" {
Remove-Resources -ExampleName "high-availability"
}
default {
Write-ColorOutput "Unknown action: $Action" $Red
Show-Usage
exit 1
}
}
Write-ColorOutput "Script execution completed." $Green