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

[RECUR-186] Deploy versioned API specs #137

Merged
merged 9 commits into from
Jul 25, 2024
57 changes: 43 additions & 14 deletions .github/workflows/update-azureapimanagement.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ on:
jobs:
update-api-management:
name: Update API Management Service
runs-on: ubuntu-latest
runs-on: boley
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down Expand Up @@ -111,20 +111,49 @@ jobs:
# Create a context object for API management operations
$Context = New-AzApiManagementContext -ResourceGroupName $ResourceGroup -ServiceName $ServiceName

# Construct the Swagger URL for the default API specification
$SwaggerURL = "https://$serviceFqdn${{ inputs.apiSpecificationPath }}"
Write-Output "Default API Swagger URL: $SwaggerURL"
# Retrieve the API Version Set
$apiVersionSet = Get-AzApiManagementApiVersionSet -Context $Context | Where-Object { $_.DisplayName -eq $ApiId }
if (-not $apiVersionSet) { # Corrected variable name here
$apiVersionSet = New-AzApiManagementApiVersionSet -Context $Context -Name $ApiId -Scheme Segment -Description "$ApiId API"
}

# Import the API using the Swagger URL
Import-AzApiManagementApi -Context $Context -SpecificationUrl $SwaggerURL -SpecificationFormat OpenApi -Path $ApiId -ApiId "$ApiId"
# Update API versions
1..3 | ForEach-Object {
$version = $_

# Retrieve the imported API object
$Api = Get-AzApiManagementApi -Context $Context -ApiId $ApiId
# Check if the versioned API exists
$apiDirectory = Get-ChildItem -Directory -Recurse -Filter "V${version}" -ErrorAction SilentlyContinue | Select-Object -First 1
if ($apiDirectory -and (Test-Path -Path $apiDirectory.FullName)) {
# Construct the Swagger URL for each version
$SwaggerURL = "https://$serviceFqdn/swagger/v$version/swagger.json"
Write-Output $SwaggerURL

# Update the API object with the service URL and subscription requirement
$Api.ServiceURL = $ServiceURL
$Api.SubscriptionRequired = $ApiSubscriptionRequired
# Generate a unique API ID for each version
$versionedApiId = "$ApiId-v$version"

# Update default API version
Set-AzApiManagementApi -InputObject $Api
Add-AzApiManagementApiToProduct -Context $Context -ApiId $ApiId -ProductId $ApiProductId
# Pull down Swagger JSON
$SwaggerJSON = (Invoke-WebRequest -Uri $SwaggerURL).Content

# Remove path prefix from Swagger JSON
$SwaggerJSON = $SwaggerJSON -replace "/v${version}", ""

# Save Swagger JSON to file
$SwaggerJSONFile = "swagger.json"
$SwaggerJSON | Out-File -FilePath $SwaggerJSONFile

# # Import the versioned API
Import-AzApiManagementApi -Context $Context -SpecificationPath $SwaggerJSONFile -SpecificationFormat OpenApi -Path $ApiId -ApiId "$versionedApiId" -ApiVersion "v$version" -ApiVersionSetId $ApiVersionSet.Id

# Retrieve and update the API object
$Api = Get-AzApiManagementApi -Context $Context -ApiId "$versionedApiId"
$Api.ServiceURL = "$ServiceURL/v${version}"
$Api.SubscriptionRequired = $ApiSubscriptionRequired

# Associate the versioned API with a product
Set-AzApiManagementApi -InputObject $Api
Add-AzApiManagementApiToProduct -Context $Context -ApiId "$versionedApiId" -ProductId $ApiProductId
}
else {
Write-Output "Version V$version does not exist in this repository."
}
}
Loading