Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update azd templates #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ module openAi2 'core/ai/cognitiveservices.bicep' = {
module apim 'core/gateway/apim.bicep' = {
name: 'apim'
scope: resourceGroup
dependsOn: [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not be needed, only the child API created on this APIM requires the dependencies

openAi1, openAi2
]
params: {
name: '${abbrs.apiManagementService}${resourceToken}-${apimName}'
location: apimLocation
Expand All @@ -123,6 +126,9 @@ module apim 'core/gateway/apim.bicep' = {
module api 'app/apim-api.bicep' = {
name: 'api'
scope: resourceGroup
dependsOn: [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed, as there are already params depending on both these resources. Bicep linter should report it

apim, openAi1, openAi2
]
params: {
name: apim.outputs.apimServiceName
applicationInsightsName: monitoring.outputs.applicationInsightsName
Expand Down Expand Up @@ -156,6 +162,9 @@ module monitoring 'core/monitor/monitoring.bicep' = {
module openAi1RoleApim 'core/security/role.bicep' = {
scope: resourceGroup
name: 'openai1-role-apim'
dependsOn: [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed, apim is automatically recognized as a dependency because of the param.
There's no dependency on openAi1 as the role is set at RG scope

apim, openAi1
]
params: {
principalId: apim.outputs.apimPrincipalId
// Cognitive Services OpenAI User
Expand All @@ -171,6 +180,9 @@ module openAi1RoleApim 'core/security/role.bicep' = {
module openAi2RoleApim 'core/security/role.bicep' = {
scope: resourceGroup
name: 'openai2-role-apim'
dependsOn: [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed, apim is automatically recognized as a dependency because of the param.
There's no dependency on openAi1 as the role is set at RG scope

Since the scope has change to RG, this second one role is redundant

apim, openAi2
]
params: {
principalId: apim.outputs.apimPrincipalId
// Cognitive Services OpenAI User
Expand Down
2 changes: 1 addition & 1 deletion infra/main.parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"value": "${AZURE_OPENAI_API_VERSION=2024-02-01}"
},
"apimLocation": {
"value": "${AZURE_APIM_LOCATION=southcentralus}"
"value": "${AZURE_APIM_LOCATION}"
}
}
}
Expand Down
164 changes: 164 additions & 0 deletions infra/scripts/Purge-ApiManagements.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# Purges the deleted the API Management instance.
Param(
[string]
[Parameter(Mandatory=$false)]
$ApiVersion = "2021-08-01",

[switch]
[Parameter(Mandatory=$false)]
$Help
)

function Show-Usage {
Write-Output " This permanently deletes the API Management instance

Usage: $(Split-Path $MyInvocation.ScriptName -Leaf) ``
[-ApiVersion <API version>] ``

[-Help]

Options:
-ApiVersion REST API version. Default is `2021-08-01`.

-Help: Show this message.
"

Exit 0
}

# Show usage
$needHelp = $Help -eq $true
if ($needHelp -eq $true) {
Show-Usage
Exit 0
}

# List soft-deleted API Management instances
function List-DeletedAPIMs {
param (
[string] $ApiVersion
)

$account = $(az account show | ConvertFrom-Json)

$url = "https://management.azure.com/subscriptions/$($account.id)/providers/Microsoft.ApiManagement/deletedservices?api-version=$($ApiVersion)"

# Uncomment to debug
# $url

$options = ""

$apims = $(az rest -m get -u $url --query "value" | ConvertFrom-Json)
if ($apims -eq $null) {
$options = "All soft-deleted API Management instances purged or no such instance found to purge"
$returnValue = @{ apims = $apims; options = $options }
return $returnValue
}

if ($apims.Count -eq 1) {
$name = $apims.name
$options += " 1: $name `n"
} else {
$apims | ForEach-Object {
$i = $apims.IndexOf($_)
$name = $_.name
$options += " $($i +1): $name `n"
}
}
$options += " a: Purge all`n"
$options += " q: Quit`n"

$returnValue = @{ apims = $apims; options = $options }
return $returnValue
}

# Purge all soft-deleted API Management instances at once.
function Purge-AllDeletedAPIMs {
param (
[string] $ApiVersion,
[object[]] $Instances
)

Process {
$Instances | ForEach-Object {
Write-Output "Purging $($_.name) ..."

$url = "https://management.azure.com$($_.id)?api-version=$($ApiVersion)"

$apim = $(az rest -m get -u $url)
if ($apim -ne $null) {
$deleted = $(az rest -m delete -u $url)
}

Write-Output "... $($_.name) purged"
}

Write-Output "All soft-deleted API Management instances purged"
}
}


# Purge soft-deleted API Management instances
function Purge-DeletedAPIMs {
param (
[string] $ApiVersion
)

$continue = $true
$result = List-DeletedAPIMs -ApiVersion $ApiVersion
if ($result.apims -eq $null) {
$continue = $false
}

while ($continue -eq $true) {
$options = $result.options

$input = Read-Host "Select the number to purge the soft-deleted API Management instance, 'a' to purge all or 'q' to quit: `n`n$options"
if ($input -eq "q") {
$continue = $false
break
}

if ($input -eq "a") {
Purge-AllDeletedAPIMs -ApiVersion $ApiVersion -Instances $result.apims
break
}

$parsed = $input -as [int]
if ($parsed -eq $null) {
Write-Output "Invalid input"
$continue = $false
break
}

$apims = $result.apims
if ($parsed -gt $apims.Count) {
Write-Output "Invalid input"
$continue = $false
break
}

$index = $parsed - 1

$url = "https://management.azure.com$($apims[$index].id)?api-version=$($ApiVersion)"

# Uncomment to debug
# $url

$apim = $(az rest -m get -u $url)
if ($apim -ne $null) {
$deleted = $(az rest -m delete -u $url)
}

$result = List-DeletedAPIMs -ApiVersion $ApiVersion
if ($result.apims -eq $null) {
$continue = $false
}
}

if ($continue -eq $false) {
return $result.options
}
}

Purge-DeletedAPIMs -ApiVersion $ApiVersion
163 changes: 163 additions & 0 deletions infra/scripts/Purge-CognitiveServices.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# Purges the deleted the Azure Cognitive Service instance.
Param(
[string]
[Parameter(Mandatory=$false)]
$ApiVersion = "2021-10-01",

[switch]
[Parameter(Mandatory=$false)]
$Help
)

function Show-Usage {
Write-Output " This permanently deletes the Azure Cognitive Service instance

Usage: $(Split-Path $MyInvocation.ScriptName -Leaf) ``
[-ApiVersion <API version>] ``

[-Help]

Options:
-ApiVersion REST API version. Default is `2021-10-01`.

-Help: Show this message.
"

Exit 0
}

# Show usage
$needHelp = $Help -eq $true
if ($needHelp -eq $true) {
Show-Usage
Exit 0
}

# List soft-deleted Azure Cognitive Service instances
function List-DeletedCognitiveServices {
param (
[string] $ApiVersion
)

$account = $(az account show | ConvertFrom-Json)

$url = "https://management.azure.com/subscriptions/$($account.id)/providers/Microsoft.CognitiveServices/deletedAccounts?api-version=$($ApiVersion)"

# Uncomment to debug
# $url

$options = ""

$aoais = $(az rest -m get -u $url --query "value" | ConvertFrom-Json)
if ($aoais -eq $null) {
$options = "All soft-deleted Azure Cognitive Service instances purged or no such instance found to purge"
$returnValue = @{ aoais = $aoais; options = $options }
return $returnValue
}

if ($aoais.Count -eq 1) {
$name = $aoais.name
$options += " 1: $name `n"
} else {
$aoais | ForEach-Object {
$i = $aoais.IndexOf($_)
$name = $_.name
$options += " $($i +1): $name `n"
}
}
$options += " a: Purge all`n"
$options += " q: Quit`n"

$returnValue = @{ aoais = $aoais; options = $options }
return $returnValue
}

# Purge all soft-deleted Azure Cognitive Service instances at once.
function Purge-AllDeletedCognitiveServices {
param (
[string] $ApiVersion,
[object[]] $Instances
)

Process {
$Instances | ForEach-Object {
Write-Output "Purging $($_.name) ..."

$url = "https://management.azure.com$($_.id)?api-version=$($ApiVersion)"

$aoai = $(az rest -m get -u $url)
if ($aoai -ne $null) {
$deleted = $(az rest -m delete -u $url)
}

Write-Output "... $($_.name) purged"
}

Write-Output "All soft-deleted Azure Cognitive Service instances purged"
}
}

# Purge soft-deleted Azure Cognitive Service instances
function Purge-DeletedCognitiveServices {
param (
[string] $ApiVersion
)

$continue = $true
$result = List-DeletedCognitiveServices -ApiVersion $ApiVersion
if ($result.aoais -eq $null) {
$continue = $false
}

while ($continue -eq $true) {
$options = $result.options

$input = Read-Host "Select the number to purge the soft-deleted Azure Cognitive Service instance, 'a' to purge all or 'q' to quit: `n`n$options"
if ($input -eq "q") {
$continue = $false
break
}

if ($input -eq "a") {
Purge-AllDeletedCognitiveServices -ApiVersion $ApiVersion -Instances $result.aoais
break
}

$parsed = $input -as [int]
if ($parsed -eq $null) {
Write-Output "Invalid input"
$continue = $false
break
}

$aoais = $result.aoais
if ($parsed -gt $aoais.Count) {
Write-Output "Invalid input"
$continue = $false
break
}

$index = $parsed - 1

$url = "https://management.azure.com$($aoais[$index].id)?api-version=$($ApiVersion)"

# Uncomment to debug
# $url

$apim = $(az rest -m get -u $url)
if ($apim -ne $null) {
$deleted = $(az rest -m delete -u $url)
}

$result = List-DeletedCognitiveServices -ApiVersion $ApiVersion
if ($result.aoais -eq $null) {
$continue = $false
}
}

if ($continue -eq $false) {
return $result.options
}
}

Purge-DeletedCognitiveServices -ApiVersion $ApiVersion