Skip to content

Commit

Permalink
Fixed SAP APIM deploy only
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalvanderheiden committed Apr 26, 2024
1 parent f472f13 commit 6f547da
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 41 deletions.
30 changes: 15 additions & 15 deletions infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ module apim './core/gateway/apim.bicep' = if(deployAzureAPIMtoAPIC) {
sku: apimSku
skuCount: apimSkuCount
applicationInsightsName: monitoring.outputs.applicationInsightsName
managedIdentityName: managedIdentityApim.outputs.managedIdentityName
managedIdentityName: deployAzureAPIMtoAPIC ? managedIdentityApim.outputs.managedIdentityName : ''
apicManagedIdentityName: managedIdentityApic.outputs.managedIdentityName
}
}
Expand All @@ -191,11 +191,11 @@ module sapApiService './core/gateway/apim-api.bicep' = if(deployAzureAPIMtoAPIC)
displayName: 'SAP Business Partner API'
path: 'api/sapbp'
SpecUrl: 'https://raw.githubusercontent.com/azure-samples/azd-apic-sap/main/infra/core/gateway/odata/API_BUSINESS_PARTNER.edmx'
apimServiceName: apim.outputs.apimServiceName
apimLoggerName: apim.outputs.apimLoggerName
keyVaultEndpoint: keyVault.outputs.keyVaultEndpoint
apimServiceName: deployAzureAPIMtoAPIC ? apim.outputs.apimServiceName : ''
apimLoggerName: deployAzureAPIMtoAPIC ? apim.outputs.apimLoggerName : ''
keyVaultEndpoint: deployAzureAPIMtoAPIC ? keyVault.outputs.keyVaultEndpoint :''
sapKeyVaultSecretName: sapApiKeySecretName
managedIdentityName: managedIdentityApim.outputs.managedIdentityName
managedIdentityName: deployAzureAPIMtoAPIC ? managedIdentityApim.outputs.managedIdentityName : ''
sapUri: sapBackendEndpoint
}
}
Expand Down Expand Up @@ -267,8 +267,8 @@ module sapApicEnvironment './core/apic/apic-environment.bicep' = if(deploySapAPI
developerPortalUri: sapDeveloperPortalUrl
managementPortalUri: sapManagementPortalUrl
environmentType: 'ApiGee API Management'
apicWorkspaceName: apic.outputs.apicWorkspaceName
apicServiceName: apic.outputs.apicServiceName
apicWorkspaceName: deploySapAPIMtoAPIC ? apic.outputs.apicWorkspaceName : ''
apicServiceName: deploySapAPIMtoAPIC ? apic.outputs.apicServiceName : ''
}
}

Expand All @@ -280,11 +280,11 @@ module apimApicEnvironment './core/apic/apic-environment.bicep' = if(deployAzure
title: 'Azure API Management'
description: 'Azure API Management instance (${apimSku})'
kind: 'Development'
developerPortalUri: apim.outputs.apimDeveloperPortalUrl
managementPortalUri: apim.outputs.apimManagementPortalUrl
developerPortalUri: deployAzureAPIMtoAPIC ? apim.outputs.apimDeveloperPortalUrl : ''
managementPortalUri: deployAzureAPIMtoAPIC ? apim.outputs.apimManagementPortalUrl : ''
environmentType: 'Azure API Management'
apicServiceName: apic.outputs.apicServiceName
apicWorkspaceName: apic.outputs.apicWorkspaceName
apicServiceName: deployAzureAPIMtoAPIC ? apic.outputs.apicServiceName : ''
apicWorkspaceName: deployAzureAPIMtoAPIC ? apic.outputs.apicWorkspaceName : ''
}
}

Expand Down Expand Up @@ -367,13 +367,13 @@ module apimApicServiceApi './core/apic/apic-api.bicep' = if(deployAzureAPIMtoAPI
definitionName: 'openapi'
definitionTitle: 'openapi'
definitionDescription: 'OpenAPI definition of the SAP Business Partner API'
runtimeUri: sapApiService.outputs.serviceApiUrl
runtimeUri: deployAzureAPIMtoAPIC ? sapApiService.outputs.serviceApiUrl : ''
deploymentName: 'v1-deployment'
deploymentTitle: 'v1 Deployment'
deploymentDescription: 'Initial deployment of the APIM API'
apicServiceName: apic.outputs.apicServiceName
apicWorkspaceName: apic.outputs.apicWorkspaceName
apicEnvironmentName: apimApicEnvironment.outputs.apicEnvironmentName
apicServiceName: deployAzureAPIMtoAPIC ? apic.outputs.apicServiceName : ''
apicWorkspaceName: deployAzureAPIMtoAPIC ? apic.outputs.apicWorkspaceName : ''
apicEnvironmentName: deployAzureAPIMtoAPIC ? apimApicEnvironment.outputs.apicEnvironmentName : ''
}
}

Expand Down
61 changes: 35 additions & 26 deletions scripts/azure-apim-discovery.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,45 @@ if (!$azdenv.APIM_RESOURCE_ID) {
}
else
{
Write-Host "APIs found, importing..."
# AZ CLI METHOD to import all APIs from APIM
# $allApis = "$($azdenv.APIM_RESOURCE_ID)/apis/*"
# az apic service import-from-apim -g $azdenv.RESOURCE_GROUP_NAME -s $azdenv.APIC_SERVICE_NAME --source-resource-ids $allApis
# Write-Host "Importing APIs from Azure API Management completed"
Write-Host "API Management found, checking if APIs need to be imported..."
$api = az apic api list -g $azdenv.RESOURCE_GROUP_NAME -s $azdenv.APIC_SERVICE_NAME --output json | ConvertFrom-Json
$matchingApi = $api | Where-Object { $_.name -eq $azdenv.APIM_SAP_API_NAME) }
if (!$matchingApi) {
Write-Host "API not found, importing..."
# AZ CLI METHOD to import all APIs from APIM
# $allApis = "$($azdenv.APIM_RESOURCE_ID)/apis/*"
# az apic service import-from-apim -g $azdenv.RESOURCE_GROUP_NAME -s $azdenv.APIC_SERVICE_NAME --source-resource-ids $allApis
# Write-Host "Importing APIs from Azure API Management completed"

# SMAPI METHOD to import only the SAP API Sepc, as the rest is created via Bicep
# Obtain an access token
$accessToken = az account get-access-token --query accessToken -o tsv
# SMAPI METHOD to import only the SAP API Sepc, as the rest is created via Bicep
# Obtain an access token
$accessToken = az account get-access-token --query accessToken -o tsv

$jsonString = Get-Content -Path "$($azdenv.APIM_SAP_OPENAPI_SPEC_FILE)" -Raw
$jsonString = Get-Content -Path "$($azdenv.APIM_SAP_OPENAPI_SPEC_FILE)" -Raw

# Import Spec
$specUrl = "https://management.azure.com/subscriptions/$($azdenv.AZURE_SUBSCRIPTION_ID)/resourceGroups/$($azdenv.RESOURCE_GROUP_NAME)/providers/Microsoft.ApiCenter/services/$($azdenv.APIC_SERVICE_NAME)/workspaces/$($azdenv.APIC_WORKSPACE_NAME)/apis/$($azdenv.APIM_SAP_API_NAME)/versions/$($azdenv.APIM_SAP_VERSION_NAME)/definitions/$($azdenv.APIM_SAP_DEFINITION_NAME)/importSpecification?api-version=2024-03-01"
$specBody = @{
format = "inline"
value = "$jsonString"
specification = @{
name = "openapi"
version = "3.0.1"
# Import Spec
$specUrl = "https://management.azure.com/subscriptions/$($azdenv.AZURE_SUBSCRIPTION_ID)/resourceGroups/$($azdenv.RESOURCE_GROUP_NAME)/providers/Microsoft.ApiCenter/services/$($azdenv.APIC_SERVICE_NAME)/workspaces/$($azdenv.APIC_WORKSPACE_NAME)/apis/$($azdenv.APIM_SAP_API_NAME)/versions/$($azdenv.APIM_SAP_VERSION_NAME)/definitions/$($azdenv.APIM_SAP_DEFINITION_NAME)/importSpecification?api-version=2024-03-01"
$specBody = @{
format = "inline"
value = "$jsonString"
specification = @{
name = "openapi"
version = "3.0.1"
}
} | ConvertTo-Json -Depth 5
$responseSpec = Invoke-RestMethod -Uri $specUrl -Method Post -Headers @{Authorization="Bearer $accessToken"} -Body $specBody -ContentType "application/json"
if ($responseSpec) {
Write-Host "OpenAPI Spec imported successfully"
}
else
{
# Note: The import returns failed, but is successful.
Write-Host "OpenAPI Spec imported successfully"
## Write-Host "Failed to import OpenAPI Spec"
}
} | ConvertTo-Json -Depth 5
$responseSpec = Invoke-RestMethod -Uri $specUrl -Method Post -Headers @{Authorization="Bearer $accessToken"} -Body $specBody -ContentType "application/json"
if ($responseSpec) {
Write-Host "OpenAPI Spec imported successfully"
}
else
}
else
{
# Note: The import returns failed, but is successful.
Write-Host "OpenAPI Spec imported successfully"
## Write-Host "Failed to import OpenAPI Spec"
Write-Host "API already exists, skipping import"
}
}

0 comments on commit 6f547da

Please sign in to comment.