Skip to content

Commit

Permalink
AzureBingoFunction Add
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthebrit committed May 27, 2020
1 parent 7229893 commit 8fdb001
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 0 deletions.
1 change: 1 addition & 0 deletions AzureBingoPowerShellFunction/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Get-AzureBingoResponse.ps1
105 changes: 105 additions & 0 deletions AzureBingoPowerShellFunction/Get-BingoResponse.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
function Get-BingoResponse
{
<# John Savill 5/27/2020
Simple local function to return a data entry not already returned
from data file in blob tracking returned entries in Azure Table
Assumes already connected to Azure
#>
Param (
[Parameter(Mandatory=$false,
ValueFromPipeline=$false)]
[switch] $ResetGame
)

#Import AzTable (used for the cloud table interactions)

$statusGood = $true

$sessionID = '2Infinity'

$resourceGroupName = $env:FUNC_STOR_RGName #='RG-USSC-AzureBingoFunction'
$storageAccountName = $env:FUNC_STOR_ActName #='sasavusscbingodata'
$tableName = $env:FUNC_STOR_TblName #='bingostatedata'
$blobContainer = $env:FUNC_STOR_BlobContainer #='bingodata'
$blobDataFile = $env:FUNC_STOR_BlobDataFile #= 'bingodata.txt'
try {
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName `
-Name $storageAccountName
$storageContext = $storageAccount.Context
$cloudTable = (Get-AzStorageTable –Name $tableName –Context $storageContext).CloudTable

#Read the items for the sessionID
$records = Get-AzTableRow `
-table $cloudTable `
-PartitionKey $sessionID
}
catch {
$statusGood = $false
$body = "Failure connecting to table for state data, $_"
}

if($ResetGame)
{
try {
#Delete all for the sessionID
$records | Remove-AzTableRow -table $cloudTable
$records = $null
}
catch {
$statusGood = $false
$body = "Failure removing existing records, i.e. reset failed, $_"
}
}

if($records) #if there are some
{
$bingoPreCalled = $records | select-object -ExpandProperty RowKey
}
else {
$bingoPreCalled = $null
}

$tempFile = "$((New-Guid).Guid).data"
try {
Get-AzStorageBlobContent -Context $storageContext `
-Container $blobContainer -Blob $blobDataFile `
-Destination "$($env:temp)\$tempFile"
}
catch {
$statusGood = $false
$body = "Failure getting data file from blob, $_"
}

#Read in the data file from temp storage
$bingoSourceData = Get-Content "$($env:temp)\$tempFile" -ErrorAction:SilentlyContinue
Remove-Item "$($env:temp)\$tempFile"

if($bingoPreCalled -ne $null)
{
$bingoData = Compare-Object -ReferenceObject $bingoSourceData -DifferenceObject $bingoPreCalled -PassThru
}
else
{
$bingoData = $bingoSourceData
}

#Number of items
$bingoCount = $bingoData.Count
Write-Output "$bingoCount items left"

#Generate a random number based on the entries available
$dataItem = Get-Random -Maximum $bingoCount

#Selected data
$returnData = $bingoData[$dataItem]
#Add entry and returned
try {
Add-AzTableRow -table $cloudTable -partitionKey $sessionID -rowKey $returnData
}
catch {
$statusGood = $false
$body = "Failure adding item to state table, $_"
}

return $returnData
}
50 changes: 50 additions & 0 deletions AzureBingoPowerShellFunction/bingodata.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Availability Set
Virtual Machine
VM Scale Set
Public IP
Managed Disk
Image
Gallery
Disk Encryption Set
Proximity Placement Group
Availability Set
Availability Zone
Region
Virtual Network
Subnet
ExpressRoute
S2S VPN
Gateway
Peering
Private Link
Load Balancer
App Gateway
Function
LogicApp
Azure Firewall
App Service Plan
ASE
Cosmos DB
Azure AD
Azure SQL Database
Azure Synapse
Azure Database
PostgreSQL
MariaDB
MySQL
Redis
Azure Storage
Azure Files
Ultra Disk
Azure DevOps
GitHub Actions
AKS
Containers
ACR
ADH
WVD
Batch
Windows
B2B
B2C
AI
118 changes: 118 additions & 0 deletions AzureBingoPowerShellFunction/run.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "Azure GetBingoEntry function processed a request."

# Interact with query parameters or the body of the request.
$ResetGame = $Request.Query.ResetGame
if (-not $ResetGame) {
$ResetGame= $Request.Body.ResetGame
}

$statusGood = $true

$sessionID = $Request.Headers."x-forwarded-for".Split(":")[0]

$resourceGroupName = $env:FUNC_STOR_RGName #='RG-USSC-AzureBingoFunction'
$storageAccountName = $env:FUNC_STOR_ActName #='sasavusscbingodata'
$tableName = $env:FUNC_STOR_TblName #='bingostatedata'
$blobContainer = $env:FUNC_STOR_BlobContainer #='bingodata'
$blobDataFile = $env:FUNC_STOR_BlobDataFile #= 'bingodata.txt'

try {
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName `
-Name $storageAccountName
$storageContext = $storageAccount.Context
$cloudTable = (Get-AzStorageTable –Name $tableName –Context $storageContext).CloudTable

#Read the items for the sessionID
$records = Get-AzTableRow `
-table $cloudTable `
-PartitionKey $sessionID
}
catch {
$statusGood = $false
$body = "Failure connecting to table for state data, $_"
}

if ($ResetGame) {
write-output "Received Game Reset"
try {
#Delete all for the sessionID
$records | Remove-AzTableRow -table $cloudTable
$records = $null
}
catch {
$statusGood = $false
$body = "Failure removing existing records, i.e. reset failed, $_"
}
}

if($records) #if there are some
{
$bingoPreCalled = $records | select-object -ExpandProperty RowKey
}
else {
$bingoPreCalled = $null
}

$tempFile = "$((New-Guid).Guid).data"
try {
Get-AzStorageBlobContent -Context $storageContext `
-Container $blobContainer -Blob $blobDataFile `
-Destination "$($env:temp)\$tempFile"
}
catch {
$statusGood = $false
$body = "Failure getting data file from blob, $_"
}

#Read in the data file from temp storage
$bingoSourceData = Get-Content "$($env:temp)\$tempFile" -ErrorAction:SilentlyContinue
Remove-Item "$($env:temp)\$tempFile"

if($bingoPreCalled -ne $null)
{
$bingoData = Compare-Object -ReferenceObject $bingoSourceData -DifferenceObject $bingoPreCalled -PassThru
}
else
{
$bingoData = $bingoSourceData
}

#Number of items
$bingoCount = $bingoData.Count
Write-Output "$bingoCount items left"

#Generate a random number based on the entries available
$dataItem = Get-Random -Maximum $bingoCount

#Selected data
$returnData = $bingoData[$dataItem]
#Add entry and returned
try {
Add-AzTableRow -table $cloudTable -partitionKey $sessionID -rowKey $returnData
}
catch {
$statusGood = $false
$body = "Failure adding item to state table, $_"
}

if(!$statusGood)
{
$status = [HttpStatusCode]::BadRequest
}
else
{
$status = [HttpStatusCode]::OK
$body = $returnData
}

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = $status
Body = $body
})
13 changes: 13 additions & 0 deletions PowerShellStuff/profile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function prompt {
#put this in $profile, e.g. code $profile
$location = Get-Location
$pathparts = $location.path.split("\")
if($pathparts.Count -gt 2)
{
"PS $($pathparts[0])\..\$($pathparts[$pathparts.Count-1])> "
}
else
{
"PS $location> "
}
}

0 comments on commit 8fdb001

Please sign in to comment.