-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Hekku2/feat/existing-analyzer
Add support for using existing Cognitive Services Account
- Loading branch information
Showing
17 changed files
with
267 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
{ | ||
"ResourceGroup": "", | ||
"ApplicationName": "", | ||
"Location": "North Europe", | ||
"DiscordToken": "", | ||
"DiscordGuildId": 0, | ||
"DiscordChannelId": 0, | ||
"Tags": [] | ||
"ResourceGroup": "", | ||
"ApplicationName": "", | ||
"Location": "North Europe", | ||
"DiscordToken": "", | ||
"DiscordGuildId": 0, | ||
"DiscordChannelId": 0, | ||
"ExistingCognitiveServicesAccountName": "existing-account-name", | ||
"ExistingCognitiveServicesResourceGroup": "existing-resource-group-name", | ||
"Tags": [] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Create a role assignment to assign the Cognitive Services User role to the given User Assigned Identities. | ||
* This should be deployed to the resource group where Cognitive Services Account is. Identities can be in | ||
* a different resource group. | ||
*/ | ||
|
||
@description('Name of the Cognitive Services account.') | ||
param cognitiveServiceName string | ||
|
||
@description('Identities of which are assigned with Blob Data Reader to the containers.') | ||
param cognitiveServiceUserIdentityName string | ||
param cognitiveServiceUserIdentityResourceGroup string | ||
|
||
var cognitiveServicesUserRoleDefinitionId = subscriptionResourceId( | ||
'Microsoft.Authorization/roleDefinitions', | ||
'a97b65f3-24c7-4388-baec-2e87135dc908' | ||
) | ||
|
||
resource identity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-07-31-preview' existing = { | ||
name: cognitiveServiceUserIdentityName | ||
scope: resourceGroup(cognitiveServiceUserIdentityResourceGroup) | ||
} | ||
|
||
resource cognitiveServiceAccount 'Microsoft.CognitiveServices/accounts@2023-05-01' existing = { | ||
name: cognitiveServiceName | ||
scope: resourceGroup() | ||
} | ||
|
||
resource cognitiveServiceAssignments 'Microsoft.Authorization/roleAssignments@2022-04-01' = { | ||
scope: cognitiveServiceAccount | ||
name: guid(identity.id, cognitiveServicesUserRoleDefinitionId, cognitiveServiceAccount.id) | ||
properties: { | ||
principalId: identity.properties.principalId | ||
principalType: 'ServicePrincipal' | ||
roleDefinitionId: cognitiveServicesUserRoleDefinitionId | ||
} | ||
} | ||
|
||
output cognitiveServiceAccountName string = cognitiveServiceAccount.name |
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,51 @@ | ||
import { CognitiveServiceSettings } from 'types.bicep' | ||
|
||
/* | ||
* Either creates a new CognitiveService account or uses an existing one. | ||
* and adds the necessary permissions to the user identity. | ||
* This is seprate module because the existing CognitiveService account | ||
* may be in a different resource group (scope). | ||
*/ | ||
|
||
@minLength(5) | ||
param baseName string | ||
|
||
@description('Location for all resources.') | ||
param location string = resourceGroup().location | ||
|
||
@description('Settings for deciding which CognitiveService resource is used.') | ||
param cognitiveService CognitiveServiceSettings | ||
|
||
@description('Identities of which are assigned with Blob Data Reader to the containers.') | ||
param cognitiveServiceUserIdentityName string | ||
|
||
// NOTE: ?? '' - is used to circument type warnings. | ||
var existingServiceResourceGroupName = cognitiveService.existingServiceResourceGroup ?? '' | ||
var getExisting = cognitiveService.existingServiceName != null | ||
|
||
resource old 'Microsoft.CognitiveServices/accounts@2023-05-01' existing = if (getExisting) { | ||
name: cognitiveService.existingServiceName ?? '' | ||
scope: resourceGroup(existingServiceResourceGroupName) | ||
} | ||
|
||
module rbacAssingments 'image-analyzer-permissions.bicep' = if (getExisting) { | ||
scope: resourceGroup(existingServiceResourceGroupName) | ||
name: 'cognitiveServiceUser-old' | ||
params: { | ||
cognitiveServiceName: old.name | ||
cognitiveServiceUserIdentityName: cognitiveServiceUserIdentityName | ||
cognitiveServiceUserIdentityResourceGroup: resourceGroup().name | ||
} | ||
} | ||
|
||
module new 'image-analyzer.bicep' = if (!getExisting) { | ||
name: 'newAnalyzer' | ||
params: { | ||
baseName: baseName | ||
location: location | ||
cognitiveServiceUserIdentityNames: [cognitiveServiceUserIdentityName] | ||
} | ||
} | ||
|
||
output cognitiveServiceAccountName string = getExisting ? old.name : new.outputs.cognitiveServiceAccountName | ||
output cognitiveServiceResourceGroup string = getExisting ? existingServiceResourceGroupName : resourceGroup().name |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<# | ||
.SYNOPSIS | ||
This script builds and publishes measurement listener to Azure | ||
.DESCRIPTION | ||
This assumes that dotnet sdk and Azure Powershell are installed | ||
.PARAMETER SettinsFile | ||
Settings file that contains environment settings. Defaults to 'developer-settings.json' | ||
#> | ||
param( | ||
[Parameter()][string]$SettingsFile = 'developer-settings.json' | ||
) | ||
$ErrorActionPreference = "Stop" | ||
Set-StrictMode -Version Latest | ||
|
||
.$PSScriptRoot/FunctionUtil.ps1 | ||
|
||
$settingsJson = Get-DeveloperSettings -SettingsFile $SettingsFile | ||
$appName = "func-$($settingsJson.ResourceGroup)" | ||
|
||
$webApp = Get-AzWebApp -ResourceGroupName $settingsJson.ResourceGroup -Name $appName | ||
$url = Get-FunctionBaseUrl -FunctionApp $webApp | ||
$code = Get-FunctionCode -FunctionApp $webApp | ||
|
||
$functionUrl = "$($url)GetImageIndex?code=$code" | ||
Invoke-RestMethod -Method Get -Uri $functionUrl -ContentType 'application/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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace DiscordImagePoster.Common.ImageAnalysis; | ||
|
||
public class NoOpImageAnalysisService : IImageAnalysisService | ||
{ | ||
public NoOpImageAnalysisService(ILogger<NoOpImageAnalysisService> logger) | ||
{ | ||
} | ||
|
||
public Task<ImageAnalysisResults?> AnalyzeImageAsync(BinaryData binaryData) | ||
{ | ||
return Task.FromResult<ImageAnalysisResults?>(null); | ||
} | ||
} |
Oops, something went wrong.