generated from arcus-azure/arcus.github.template
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added "Set Entities" script for Azure Table storage (#222)
Co-authored-by: Stijn Moreels <[email protected]> Co-authored-by: Pim Simons <[email protected]> Co-authored-by: Pim Simons <[email protected]>
- Loading branch information
1 parent
12902f5
commit 1cb37f2
Showing
15 changed files
with
482 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+82 Bytes
(100%)
src/Arcus.Scripting.Storage.Table/Arcus.Scripting.Storage.Table.psd1
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/Arcus.Scripting.Storage.Table/Scripts/Set-AzTableStorageEntities.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
param( | ||
[Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Name of resource group is required"), | ||
[Parameter(Mandatory = $true)][string] $StorageAccountName = $(throw "Name of Azure storage account is required"), | ||
[Parameter(Mandatory = $true)][string] $TableName = $(throw "Name of Azure table is required"), | ||
[Parameter(Mandatory = $true)][string] $ConfigurationFile = $(throw "Path to the configuration file is required") | ||
) | ||
|
||
if (-not (Test-Path -Path $ConfigurationFile)) { | ||
throw "Cannot re-create entities based on JSON configuration file because no file was found at: '$ConfigurationFile'" | ||
} | ||
if ((Get-Content -Path $ConfigurationFile -Raw) -eq $null) { | ||
throw "Cannot re-create entities based on JSON configuration file because the file is empty." | ||
} | ||
|
||
$schema = @' | ||
{ | ||
"definitions": {}, | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"$id": "https://scripting.arcus-azure.net/Features/powershell/azure-storage/azure-storage-table/config.json", | ||
"type": "array", | ||
"title": "The configuration JSON schema", | ||
"items": [{ | ||
"type": "object", | ||
"patternProperties": { | ||
"^.*$": { | ||
"anyOf": [{ | ||
"type": "string" | ||
}, { | ||
"type": "null" | ||
} | ||
] | ||
} | ||
}, | ||
"additionalProperties": false | ||
} | ||
] | ||
} | ||
'@ | ||
|
||
if (-not (Get-Content -Path $ConfigurationFile -Raw | Test-Json -Schema $schema -ErrorAction SilentlyContinue)) { | ||
throw "Cannot re-create entities based on JSON configuration file because the file does not contain a valid JSON configuration file." | ||
} | ||
|
||
Write-Verbose "Retrieving Azure storage account context for Azure storage account '$StorageAccountName' in resource group '$ResourceGroupName'..." | ||
$storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName | ||
if ($storageAccount -eq $null) { | ||
throw "Retrieving Azure storage account context for Azure storage account '$StorageAccountName' in resource group '$ResourceGroupName' failed." | ||
} | ||
$ctx = $storageAccount.Context | ||
Write-Verbose "Azure storage account context has been retrieved for Azure storage account '$StorageAccountName' in resource group '$ResourceGroupName'" | ||
|
||
Write-Verbose "Retrieving Azure storage table '$TableName' for Azure storage account '$StorageAccountName' in resource group '$ResourceGroupName'..." | ||
$storageTable = Get-AzStorageTable -Name $TableName -Context $ctx | ||
if ($storageTable -eq $null) { | ||
throw "Retrieving Azure storage table '$TableName' for Azure storage account '$StorageAccountName' in resource group '$ResourceGroupName' failed." | ||
} | ||
$cloudTable = ($storageTable).CloudTable | ||
Write-Verbose "Azure storage table '$TableName' has been retrieved for Azure storage account '$StorageAccountName' in resource group '$ResourceGroupName'" | ||
|
||
Write-Host "Deleting all existing entities in Azure storage table '$TableName' for Azure storage account '$StorageAccountName' in resource group '$ResourceGroupName'..." | ||
$entitiesToDelete = Get-AzTableRow -table $cloudTable | ||
$deletedEntities = $entitiesToDelete | Remove-AzTableRow -table $cloudTable | ||
Write-Host "Successfully deleted all existing entities in Azure storage table '$TableName' for Azure storage account '$StorageAccountName' in resource group '$ResourceGroupName'" | ||
|
||
$configFile = Get-Content -Path $ConfigurationFile | ConvertFrom-Json | ||
foreach ($entityToAdd in $configFile) { | ||
if ($entityToAdd.PartitionKey) { | ||
$partitionKey = $entityToAdd.PartitionKey | ||
$entityToAdd.PSObject.Properties.Remove('PartitionKey') | ||
} else { | ||
$partitionKey = New-Guid | ||
} | ||
|
||
if ($entityToAdd.RowKey) { | ||
$rowKey = $entityToAdd.RowKey | ||
$entityToAdd.PSObject.Properties.Remove('RowKey') | ||
} else { | ||
$rowKey = New-Guid | ||
} | ||
|
||
$entityHash = @{} | ||
$entityToAdd.PSObject.Properties | foreach { $entityHash[$_.Name] = $_.Value } | ||
|
||
$addedRow = Add-AzTableRow ` | ||
-table $cloudTable ` | ||
-partitionKey $partitionKey ` | ||
-rowKey $rowKey ` | ||
-property $entityHash | ||
|
||
Write-Verbose "Successfully added row with PartitionKey '$partitionKey' and RowKey '$rowKey' to Azure storage table '$TableName' for Azure storage account '$StorageAccountName' in resource group '$ResourceGroupName'" | ||
} | ||
|
||
Write-Host "Successfully added all entities in Azure storage table '$TableName' for Azure storage account '$StorageAccountName' in resource group '$ResourceGroupName'" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...ipting.Tests.Integration/Files/TableStorage/set-aztablestorageentities-config-nokeys.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[ | ||
{ | ||
"ReadPath": "/home/in", | ||
"ReadIntervalInSeconds": "30" | ||
}, | ||
{ | ||
"ReadPath": "/data/in", | ||
"ReadIntervalInSeconds": "10", | ||
"HasSubdirectories": "true" | ||
} | ||
] |
15 changes: 15 additions & 0 deletions
15
...cus.Scripting.Tests.Integration/Files/TableStorage/set-aztablestorageentities-config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[ | ||
{ | ||
"PartitionKey": "SystemA", | ||
"RowKey": "100", | ||
"ReadPath": "/home/in", | ||
"ReadIntervalInSeconds": "30" | ||
}, | ||
{ | ||
"PartitionKey": "SystemA", | ||
"RowKey": "200", | ||
"ReadPath": "/data/in", | ||
"ReadIntervalInSeconds": "10", | ||
"HasSubdirectories": "true" | ||
} | ||
] |
Oops, something went wrong.