Skip to content

Commit

Permalink
Update scripts to use SMAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalvanderheiden committed Apr 25, 2024
1 parent 5cbb2b7 commit 2840357
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 139 deletions.
6 changes: 3 additions & 3 deletions infra/core/apic/apic-api.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ resource apicApi 'Microsoft.ApiCenter/services/workspaces/apis@2024-03-01' = {
}

resource apicApiVersion 'Microsoft.ApiCenter/services/workspaces/apis/versions@2024-03-01' = {
name: '${apicApi.name}-${versionName}'
name: versionName
parent: apicApi
properties: {
lifecycleStage: versionLifecycle
Expand All @@ -77,7 +77,7 @@ resource apicApiVersion 'Microsoft.ApiCenter/services/workspaces/apis/versions@2
}

resource apicApiDefinition 'Microsoft.ApiCenter/services/workspaces/apis/versions/definitions@2024-03-01' = {
name: '${apicApi.name}-${versionName}-${definitionName}'
name: definitionName
parent: apicApiVersion
properties: {
description: definitionDescription
Expand All @@ -87,7 +87,7 @@ resource apicApiDefinition 'Microsoft.ApiCenter/services/workspaces/apis/version
}

resource apicApiDeployment 'Microsoft.ApiCenter/services/workspaces/apis/deployments@2024-03-01' = {
name: '${apicApi.name}-${deploymentName}'
name: deploymentName
parent: apicApi
properties: {
definitionId: '/workspaces/${apicWorkspace.name}/apis/${apicApi.name}/versions/${apicApiVersion.name}/definitions/${apicApiDefinition.name}'
Expand Down
41 changes: 41 additions & 0 deletions infra/core/apic/openapi/API_BUSINESS_PARTNER.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"openapi": "3.0.1",
"info": {
"title": "SAP Business Partner API",
"version": "1.0"
},
"servers": [
{
"url": "http://apim-wvcnsuixn4fo4.azure-api.net/api/sapbp"
},
{
"url": "https://apim-wvcnsuixn4fo4.azure-api.net/api/sapbp"
}
],
"paths": {},
"components": {
"securitySchemes": {
"apiKeyHeader": {
"type": "apiKey",
"name": "Ocp-Apim-Subscription-Key",
"in": "header"
},
"apiKeyQuery": {
"type": "apiKey",
"name": "subscription-key",
"in": "query"
}
}
},
"security": [
{
"apiKeyHeader": []
},
{
"apiKeyQuery": []
}
],
"x-ms-export-notes": [
"The exported API contains schemas that aren't of content type 'application/vnd.oai.openapi.components+json', and are hence not exported as OpenAPI components."
]
}
2 changes: 1 addition & 1 deletion infra/core/gateway/apim-api.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ resource diagnosticsPolicy 'Microsoft.ApiManagement/service/apis/diagnostics@202
}
}
*/
output SERVICE_API_URI string = '${apimService.properties.gatewayUrl}/${apimSapApi.properties.path}'
output serviceApiUrl string = '${apimService.properties.gatewayUrl}/${apimSapApi.properties.path}'
29 changes: 17 additions & 12 deletions infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ var sapAppMetadataSchema = loadTextContent('./core/apic/metadata/sap-app-metadat
var rootUrl = !empty(sapApimDiscoveryUrl) ? split(sapApimDiscoveryUrl, '/apidiscovery')[0] : ''
var sapManagementPortalUrl = !empty(rootUrl) ? '${rootUrl}/shell/homepage' : ''
var sapDeveloperPortalUrl = !empty(rootUrl) ? '${rootUrl}/shell/configure' : ''

var apimOpenAPISpecFileLocation = 'infra/core/apic/openapi/API_BUSINESS_PARTNER.json'
// Organize resources in a resource group
resource rg 'Microsoft.Resources/resourceGroups@2021-04-01' = {
name: !empty(resourceGroupName) ? resourceGroupName : '${abbrs.resourcesResourceGroups}${environmentName}'
Expand Down Expand Up @@ -190,7 +190,7 @@ module sapApiService './core/gateway/apim-api.bicep' = if(deployAzureAPIMtoAPIC)
name: 'api-business-partner'
displayName: 'SAP Business Partner API'
path: 'api/sapbp'
SpecUrl: 'https://raw.githubusercontent.com/pascalvanderheiden/azd-apic-sap/main/infra/core/gateway/odata/API_BUSINESS_PARTNER.edmx'
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
Expand Down Expand Up @@ -287,6 +287,8 @@ module apimApicEnvironment './core/apic/apic-environment.bicep' = if(deployAzure
}
}

// Prepared for future use
/*
module sapApicMetadata './core/apic/apic-metadata.bicep' = {
name: 'sap-apic-metadata'
scope: rg
Expand All @@ -308,9 +310,7 @@ module sapApicMetadata './core/apic/apic-metadata.bicep' = {
apicServiceName: apic.outputs.apicServiceName
}
}

// Prepared for future use
/*
module sapApicServiceApi './core/apic/apic-api.bicep' = if(deploySapAPIMtoAPIC){
name: 'sap-apic-api'
scope: rg
Expand Down Expand Up @@ -342,6 +342,7 @@ module sapApicServiceApi './core/apic/apic-api.bicep' = if(deploySapAPIMtoAPIC){
apicServiceName: apic.outputs.apicServiceName
}
}
*/

module apimApicServiceApi './core/apic/apic-api.bicep' = if(deployAzureAPIMtoAPIC){
name: 'apim-apic-api'
Expand All @@ -359,13 +360,13 @@ module apimApicServiceApi './core/apic/apic-api.bicep' = if(deployAzureAPIMtoAPI
docsUrl: 'https://docs.microsoft.com/en-us/azure/api-management/'
licenseName: 'APIM License'
licenseUrl: 'https://azure.microsoft.com/en-us/pricing/details/api-management/'
versionName: '1-0'
versionTitle: '1.0'
versionName: 'v1'
versionTitle: 'v1'
versionLifecycle: 'Design'
definitionName: apimDefinitionName
definitionTitle: apimDefinitionTitle
definitionDescription: apimDefinitionDescription
runtimeUri: apim.outputs.apimGatewayUrl
definitionName: 'openapi'
definitionTitle: 'openapi'
definitionDescription: 'OpenAPI definition of the SAP Business Partner API'
runtimeUri: sapApiService.outputs.serviceApiUrl
deploymentName: 'v1-deployment'
deploymentTitle: 'v1 Deployment'
deploymentDescription: 'Initial deployment of the APIM API'
Expand All @@ -374,7 +375,6 @@ module apimApicServiceApi './core/apic/apic-api.bicep' = if(deployAzureAPIMtoAPI
apicEnvironmentName: apimApicEnvironment.outputs.apicEnvironmentName
}
}
*/

// Add outputs from the deployment here
output AZURE_LOCATION string = location
Expand All @@ -394,3 +394,8 @@ output SAP_CLIENTID_KV_SECRET_NAME string = deploySapAPIMtoAPIC ? sapClientIdSec
output SAP_SECRET_KV_SECRET_NAME string = deploySapAPIMtoAPIC ? sapSecretSecretName : ''
output DEPLOY_SAP_APIM_TO_APIC bool = deploySapAPIMtoAPIC
output DEPLOY_AZURE_APIM_TO_APIC bool = deployAzureAPIMtoAPIC

output APIM_SAP_OPENAPI_SPEC_FILE string = deployAzureAPIMtoAPIC ? apimOpenAPISpecFileLocation : ''
output APIM_SAP_API_NAME string = deployAzureAPIMtoAPIC ? apimApicServiceApi.outputs.apicApiName : ''
output APIM_SAP_VERSION_NAME string = deployAzureAPIMtoAPIC ? apimApicServiceApi.outputs.apicApiVersionName : ''
output APIM_SAP_DEFINITION_NAME string = deployAzureAPIMtoAPIC ? apimApicServiceApi.outputs.apicApiDefinitionName : ''
60 changes: 41 additions & 19 deletions scripts/azure-apim-discovery.ps1
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
#run az login and set correct subscription if needed
./scripts/set-az-currentsubscription.ps1
if ($? -eq $true) {
# Get environment variables
$azdenv = azd env get-values --output json | ConvertFrom-Json

# Import APIs from Azure API Management
Write-Host "Starting import APIs from Azure API Management..."
if (!$azdenv.APIM_RESOURCE_ID) {
Write-Host "Import skipped, Azure API Management not found"
}
else {
Write-Host "API found, importing..."
if (az apic service import-from-apim -g $azdenv.RESOURCE_GROUP_NAME -s $azdenv.APIC_SERVICE_NAME --source-resource-ids "$($azdenv.APIM_RESOURCE_ID)/apis/*")
{
Write-Host "Azure API Management APIs successfully imported"
}
else
{
Write-Host "Azure API Management APIs import failed"

# Get environment variables
$azdenv = azd env get-values --output json | ConvertFrom-Json

# Import APIs from Azure API Management
Write-Host "Starting import APIs from Azure API Management..."
if (!$azdenv.APIM_RESOURCE_ID) {
Write-Host "Import skipped, Azure API Management not found"
}
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"

# 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

# 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"
}
}
17 changes: 1 addition & 16 deletions scripts/deploy-apis.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ if ($? -eq $true) {

if($azdenv.DEPLOY_AZURE_APIM_TO_APIC) {
# Deploy Azure APIM APIs to APIC
try {
./scripts/azure-apim-discovery.ps1
}
catch {
Write-Host "Failed to deploy Azure APIM APIs to APIC"
Write-Host $_.Exception.Message
return
}
./scripts/azure-apim-discovery.ps1
}
else {
Expand All @@ -23,14 +15,7 @@ if ($? -eq $true) {

if($azdenv.DEPLOY_SAP_APIM_TO_APIC) {
# Deploy SAP APIM APIs to APIC
try {
./scripts/sap-apim-discovery.ps1
}
catch {
Write-Host "Failed to deploy SAP APIM APIs to APIC"
Write-Host $_.Exception.Message
return
}
./scripts/sap-apim-discovery.ps1
}
else {
Write-Host "Skipping SAP API Management APIs deployment to APIC"
Expand Down
Loading

0 comments on commit 2840357

Please sign in to comment.