diff --git a/.github/workflows/register-api.yml b/.github/workflows/register-api.yml new file mode 100644 index 0000000..fd2b5d1 --- /dev/null +++ b/.github/workflows/register-api.yml @@ -0,0 +1,62 @@ +name: Register API Definition to Azure API Center + +on: + workflow_dispatch: + inputs: + resource-id: + description: 'The resource ID of the API Center' + required: false + default: '' + resource-group: + description: 'The resource group name of the API Center' + required: false + default: '' + api-center-service: + description: 'The service name of the API Center' + required: false + default: '' + file-location: + description: 'The file path relative to the repository root' + required: false + default: '' + +permissions: + id-token: write + contents: read + +jobs: + register-api: + runs-on: ubuntu-latest + + env: + AZURE_CLIENT_ID: ${{ vars.AZURE_CLIENT_ID }} + AZURE_TENANT_ID: ${{ vars.AZURE_TENANT_ID }} + AZURE_SUBSCRIPTION_ID: ${{ vars.AZURE_SUBSCRIPTION_ID }} + AZURE_ENV_NAME: ${{ vars.AZURE_ENV_NAME }} + AZURE_LOCATION: ${{ vars.AZURE_LOCATION }} + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install APIC extension + shell: bash + run: | + # az extension add --name apic-extension --allow-preview true --yes + az extension add --source ./infra/scripts/apic_extension-1.0.0b4-py3-none-any.whl --allow-preview true --yes + + - name: Login to Azure + uses: Azure/login@v2 + with: + client-id: ${{ env.AZURE_CLIENT_ID }} + subscription-id: ${{ env.AZURE_SUBSCRIPTION_ID }} + tenant-id: ${{ env.AZURE_TENANT_ID }} + + - name: Register API + shell: pwsh + run: | + ./infra/scripts/New-ApiRegistration.ps1 ` + -ResourceId "${{ github.event.inputs.resource-id }}" ` + -ResourceGroup "${{ github.event.inputs.resource-group }}" ` + -ApiCenterService "${{ github.event.inputs.api-center-service }}" ` + -FileLocation "${{ github.event.inputs.file-location }}" diff --git a/infra/scripts/New-ApiRegistration.ps1 b/infra/scripts/New-ApiRegistration.ps1 new file mode 100644 index 0000000..bbd17f7 --- /dev/null +++ b/infra/scripts/New-ApiRegistration.ps1 @@ -0,0 +1,82 @@ +# Registers API to API Center. +Param( + [string] + [Parameter(Mandatory=$false)] + $ResourceId = "", + + [string] + [Parameter(Mandatory=$false)] + $ResourceGroup = "", + + [string] + [Parameter(Mandatory=$false)] + $ApiCenterService = "", + + [string] + [Parameter(Mandatory=$false)] + $FileLocation = "", + + [string] + [Parameter(Mandatory=$false)] + $ApiVersion = "2024-03-01", + + [switch] + [Parameter(Mandatory=$false)] + $Help +) + +function Show-Usage { + Write-Output " This registers API to API Center + + Usage: $(Split-Path $MyInvocation.ScriptName -Leaf) `` + [-ResourceId ] `` + [-ResourceGroup ] `` + [-ApiCenterService ] `` + [-FileLocation ] `` + [-ApiVersion ] `` + + [-Help] + + Options: + -ResourceId Resource ID. It must be provided unless `ResourceGroup` is provided. + -ResourceGroup Resource group. It must be provided unless `ResourceId` is provided. + -ApiCenterService API Center instance name. It must be provided unless `ResourceId` is provided. + -FileLocation File location to register. + -ApiVersion REST API version. Default is `2024-03-01`. + + -Help: Show this message. +" + + Exit 0 +} + +# Show usage +$needHelp = $Help -eq $true +if ($needHelp -eq $true) { + Show-Usage + Exit 0 +} + +if (($ResourceId -eq "") -and ($ResourceGroup -eq "" -or $ApiCenterService)) { + Write-Output "`ResourceId` must be provided, or both `ResourceGroup` and `ApiCenterService` must be provided" + Exit 0 +} +if ($FileLocation -eq "") { + Write-Output "`FileLocation` must be provided" + Exit 0 +} + +$segments = $ResourceId.Split("/", [System.StringSplitOptions]::RemoveEmptyEntries) +if ($ResourceGroup -eq "") { + $ResourceGroup = $segments[3] +} +if ($ApiCenterService -eq "") { + $ApiCenterService = $segments[7] +} + +$REPOSITORY_ROOT = git rev-parse --show-toplevel + +$registered = az apic api register ` + -g $ResourceGroup ` + -s $ApiCenterService ` + --api-location "$REPOSITORY_ROOT/$FileLocation" diff --git a/infra/scripts/Set-ApiMetadata.ps1 b/infra/scripts/Set-ApiMetadata.ps1 index 62cc6ac..778e4b5 100644 --- a/infra/scripts/Set-ApiMetadata.ps1 +++ b/infra/scripts/Set-ApiMetadata.ps1 @@ -56,7 +56,7 @@ function Show-Usage { -MetadataValue Metadata value. -ApiVersion REST API version. Default is `2024-03-01`. - -Help: Show this message. + -Help: Show this message. " Exit 0 diff --git a/infra/scripts/apic_extension-1.0.0b4-py3-none-any.whl b/infra/scripts/apic_extension-1.0.0b4-py3-none-any.whl new file mode 100644 index 0000000..f8a7f53 Binary files /dev/null and b/infra/scripts/apic_extension-1.0.0b4-py3-none-any.whl differ