Created
July 31, 2020 10:14
-
-
Save krishrocks1904/57ee9ab92c63a5688ff5b0ace2937eeb to your computer and use it in GitHub Desktop.
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
################################### Method to Add Product to Group 'Add-ProductToGroup' ################################ | |
function Add-NewAPI { | |
param ($ApiMgmtContext, $product, $api) | |
Write-Host "**************************** Add-NewAPI*******************************" | |
# Powershell returns API name value in API_Id field | |
$reverionSchemaFiles = (Get-ChildItem ../**/**.* -Recurse -filter $api.api_path) | |
if($reverionSchemaFiles.Count -gt 0) | |
{ | |
$apiSpecificationPath = $reverionSchemaFiles[0].FullName; | |
} | |
else | |
{ | |
Write-Error "[NEW API] Open API json (swagger) file $($api.api_path) not found " | |
} | |
write-host "[NEW API] Working on API json (swagger) file '$($apiSpecificationPath)' " | |
if ((Test-Path $apiSpecificationPath) -and $reverionSchemaFiles[0].Name.Contains($api.api_path)) | |
{ | |
$apiSpecification = Get-API -ApiMgmtContext $ApiMgmtContext -api $api #($ApiMgmtContext, $api); | |
# API Path is the sufix value which will be used | |
$requireToCreateNewApi = ($null -eq $apiSpecification)? $true : $false | |
$apiName = $api.api_name | |
$path = $api.sufix | |
$apiVersion = $api.api_version# "{ API Version}" | |
$currentApiVersion = $apiSpecification.ApiVersion | |
$currentApiRevision = $apiSpecification.ApiRevision | |
$apiRevision = $api.api_revision | |
$ApiId = $apiSpecification.ApiId | |
$displayName =$api.display_name | |
$etpmp = Get-AzApiManagementApiVersionSet -Context $ApiMgmtContext | |
foreach ($item in $etpmp) { | |
Remove-AzApiManagementApiVersionSet -Context $ApiMgmtContext ` | |
-ApiVersionSetId $item.ApiVersionSetId ` | |
-ErrorAction SilentlyContinue ` | |
} | |
if($true -eq $requireToCreateNewApi) | |
{ | |
$importedApi = Add-NewApiVersion -ApiMgmtContext $ApiMgmtContext -apiName $apiName -displayName $displayName -path $path -apiVersion $currentApiVersion -apiSpecificationPath $apiSpecificationPath -serviceUrl $serviceUrl -revision $apiRevision | |
} | |
if( $null -ne $apiVersion -and ($currentApiVersion -ne $apiVersion)) | |
{ | |
$importedApi = Add-NewApiVersion -ApiMgmtContext $ApiMgmtContext -apiName $apiName -displayName $displayName -path $path -apiVersion $apiVersion -apiSpecificationPath $apiSpecificationPath -serviceUrl $serviceUrl -revision $apiRevision | |
} | |
if($null -ne $importedApi) | |
{ | |
#Reset the variables only if a new API is imported of a Version of API is Imported | |
Write-Host "[SET API VARIABLE] Re-Setting the variable beacuse a new API is imported of a Version of API is Imported" | |
$ApiId = $importedApi.ApiId | |
} | |
if(($null -ne $apiRevision -and $apiRevision -ne "") -and ($currentApiRevision -ne $apiRevision)) | |
{ | |
Write-Host "[REVISION SET] Set API revision for API :: '$($apiSpecification.Name)' ...." | |
$newRevision = $apiRevision # [int]$apiSpecification.ApiRevision + 1; | |
# now lets create an api revision | |
$newApiRevisionParams = @{ | |
Context = $ApiMgmtContext | |
ApiId = $ApiId | |
ApiRevision = $newRevision | |
SourceApiRevision = $apiSpecification.ApiRevision | |
} | |
Write-Host "[REVISION SET] create new API Revision for api: '$($apiSpecification.Name)' ...." | |
$newApiRevision = New-AzApiManagementApiRevision @newApiRevisionParams | |
# we make a new schema | |
$newApiManagementSchemaParams = @{ | |
Context = $ApiMgmtContext | |
ApiId = $ApiId | |
SchemaDocumentContentType = "application/json" | |
SchemaDocumentFilePath = $apiSpecificationPath # this value is coming from input | |
} | |
Write-Host "[REVISION SET] create new API Schema for API: '$($apiSpecification.Name)' with new revision $newRevision" | |
New-AzApiManagementApiSchema @newApiManagementSchemaParams | |
# now lets create an api release for this revision | |
# without releasing the revision, no external | |
# consumer will be able to see the set revision | |
$newApiManagementApiReleaseParams = @{ | |
Context = $ApiMgmtContext | |
ApiId = $ApiId | |
ApiRevision = $newApiRevision.ApiRevision | |
} | |
Write-Host "[REVISION SET] Release API: '$($apiSpecification.Name)' with new revision $newRevision" | |
New-AzApiManagementApiRelease @newApiManagementApiReleaseParams | |
Write-Host "[REVISION SET] API '$($apiSpecification.Name)' has revision $newRevision online now" | |
} | |
Write-Host "[API-PRODUCT-LINK] Associate API '$($apiSpecification.Name)' with product :: '$($product.product_title)'" | |
$addApiToProductParams = @{ | |
Context = $ApiMgmtContext | |
ApiId = $ApiId | |
ProductId = $product.product_id | |
} | |
Write-Host "[API-PRODUCT-LINK] Adding API $($ApiId) to product: $($product.product_id) " | |
Add-AzApiManagementApiToProduct @addApiToProductParams | |
Write-Host "[API-PRODUCT-LINK] API $($ApiId) added to product: $($product.product_id) " | |
} | |
else | |
{ | |
write-host "[NEW API] Api swagger file $($apiSpecificationPath) NOT FOUND " | |
} | |
return $apiSpecification | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment