Is there a way to automatically generate Swagger tags from precomipled C# Azure Functions?

て烟熏妆下的殇ゞ 提交于 2019-12-04 21:03:10

Unfortunately, it seems that it is not supported currently. I also find the similar issue in the github. You also could give your feedback to Azure function team.

Found this article:

I've impletemented the steps using powershell.
The script assumes that you have a configured host.json file:

{
  "swagger": {
    "enabled": true
  }
}

This script only work for Azure Function runtime v1, swagger generation is not yet implemented for runtime v2

function Get-KuduApiAuthorisationHeaderValueAzure {
    Param
    (
        [Parameter(Mandatory = $true)]
        [string]$resourceGroupName,
        [Parameter(Mandatory = $true)]
        [string]$functionappName
    )
    Begin {               
        $resourceType = "Microsoft.Web/sites/config"
        $resourceName = "$functionappName/publishingcredentials"
        $publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force

        $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword)))
        return $base64AuthInfo
    }
}

function Get-MasterAPIKey {
    Param
    (
        [Parameter(Mandatory = $true)]
        [string]$resourceGroupName,
        [Parameter(Mandatory = $true)]
        [string]$functionappName
    )
    Begin {               
        $kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValueAzure -resourceGroupName $resourceGroupName -functionappName $functionappName
        $apiUrl = "https://$functionappName.scm.azurewebsites.net/api/functions/admin/masterkey"
        $response = Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization = ("Basic {0}" -f $kuduApiAuthorisationToken);"If-Match"="*"}
        return $response.masterKey
    }
}

function Get-FunctionsApiJwt {
    Param
    (
        [Parameter(Mandatory = $true)]
        [string]$resourceGroupName,
        [Parameter(Mandatory = $true)]
        [string]$functionappName
    )
    Begin {               
        $base64AuthInfo = Get-KuduApiAuthorisationHeaderValueAzure -resourceGroupName $resourceGroupName -functionappName $functionappName
        $apiUrl = "https://$functionappName.scm.azurewebsites.net/api/functions/admin/token"
        $jwt = Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)} -Method GET

        return $jwt
    }
}

function Generate-SwaggerDefinition {
    Param
    (
        [Parameter(Mandatory = $true)]
        [string]$resourceGroupName,
        [Parameter(Mandatory = $true)]
        [string]$functionappName
    )
    Begin {   

        # Get the swagger documentation key
        $masterKey = Get-MasterAPIKey -resourceGroupName $resourceGroupName -functionappName $functionappName
        $apiUrl = "https://$functionappName.azurewebsites.net/admin/host/systemkeys/swaggerdocumentationkey"
        $swaggerdocumentationkey = (Invoke-RestMethod -Uri $apiUrl -Headers @{"x-functions-key"=$masterKey} -Method Post).value

        # Update the resource
        $config = Get-AzureRmResource -ResourceGroupName $resourceGroupName -ResourceType "microsoft.web/sites/config" -Name "$functionappName/web" -ApiVersion "2015-08-01"
        $config.Properties.apiDefinition = @{"url"="https://$functionappName.azurewebsites.net/admin/host/swagger?code=$swaggerdocumentationkey"}
        $config | Set-AzureRmResource -Force -ApiVersion "2015-08-01"

        # Generate the swagger definition
        $jwt = Get-FunctionsApiJwt -resourceGroupName $resourceGroupName -functionappName $functionappName
        $swaggerDefinition = (Invoke-RestMethod -Uri "https://$functionappName.azurewebsites.net/admin/host/swagger/default?code=$swaggerdocumentationkey" -Headers @{Authorization=("Bearer {0}" -f $jwt)} -Method Get)

        # Save the swagger definition
        Invoke-RestMethod -Uri "https://$functionappName.azurewebsites.net/admin/host/swagger?code=$swaggerdocumentationkey" -ContentType 'application/json' -Headers @{Authorization=("Bearer {0}" -f $jwt)} -Method Post -Body (ConvertTo-Json $swaggerDefinition -Depth 100)
    }
}

you can call this script like that:

Generate-SwaggerDefinition -resourceGroupName "myresourcegroupname" -functionappName "myfunctionappname"

EDIT

Newly created function apps use TLS 1.2 by default so you need to add this line at the top of the Powershell script:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!