scripts/Copy-PSBicepApiManagementApiWithGUI.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<#
    .Synopsis
        Copy an Api from a source Azure Api Management to a target Azure Api Management interactivelyà
 
    .Description
        Copy an Api from a source Azure Api Management to a target Azure Api Management. It shows
        a menu to help the user to select the source Api and the target Api Management. Ensure to be already logged
        to an Azure subscription using Connect-AzAccount
 
    .Example
        # Copy an Api from a source Api Management to a target one
        Copy-PSBicepApiManagementApiWithGUI
#>

function Copy-PSBicepApiManagementApiWithGUI()
{
    $ErrorActionPreference = 'Stop'

    try{
        Get-AzContext|Out-Null
    }
    catch{
        connect-azAccount|Out-Null
    }

    function Get-ApiManagement($env){
        Get-AzSubscription | Out-ConsoleGridView -Title "Select $env subscription" -OutputMode Single | Set-AzContext | Out-Null

        $Apim = Get-AzApiManagement | ForEach-Object{
            $obj = "" |Select-Object Name,ResourceGroup, Apim
            $obj.Name = $_.Name
            $obj.ResourceGroup = $_.ResourceGroupName
            $obj.apim = $_
            $obj
        } | Out-ConsoleGridView -Title "Select $env Api Management" -OutputMode Single
        if($null -eq $Apim)
        {
            throw "No Api management service selected"
        }
        $Apim.Apim
    }
    $SubscriptionIdFinder = [regex]'^/subscriptions/(.*?)/.*$'

    $sourceApiManagement = Get-ApiManagement -env 'source'
    $sourceSubscriptionId = $SubscriptionIdFinder.Replace($sourceApiManagement.Id,'$1')

    $sourceVersionSet = $null
    $Response = Read-Host -Prompt 'Do you want to export a single Api version or all versions of an Api? [ONE/all]'
    if($response -ne 'all')
    {
        $sourceApimcontext = New-AzApiManagementContext -ResourceId $sourceApiManagement.Id
        $sourceApiobj = Get-AzApiManagementApi -Context $sourceApimcontext | ForEach-Object{
            $obj = "" |Select-Object Name,Path,ApiVersion,ApiId,Api
            $obj.Name = $_.Name
            $obj.Path = $_.Path
            $obj.ApiVersion = $_.ApiVersion
            $obj.ApiId = $_.ApiId
            $obj.Api = $_
            $obj
        } | Out-ConsoleGridView -Title "Select source Api" -OutputMode Single

        if($null -eq $sourceApiobj)
        {
            throw "No Api selected"
        }

        $sourceApi = $sourceApiobj.Api
        $sourceelement = $sourceApi.ApiId
    }
    else{
        
        $sourceApimcontext = New-AzApiManagementContext -ResourceId $sourceApiManagement.Id
        $apis = Get-AzApiManagementApi -Context $sourceApimcontext 
        $sourceVersionSetObj = Get-AzApiManagementApiVersionSet -Context $sourceApimcontext | ForEach-Object{
            $obj = "" |Select-Object Name,VersionSetId
            $obj.Name = $_.DisplayName
            $obj.VersionSetId = $_.ApiVersionSetId
            $ApiVersionSetId=$_.Id
            $hasAssociatedApi = -not ($null -eq ($apis|Where-Object{$_.ApiVersionSetId -eq $ApiVersionSetId}))
            if($hasAssociatedApi)
            {
                $obj
            }
            
        } | Out-ConsoleGridView -Title "Select source Api Version set" -OutputMode Single

        if($null -eq $sourceVersionSetObj)
        {
            throw "No Api version selected"
        }

        $sourceVersionSet = $sourceVersionSetObj
        $sourceelement = $sourceVersionSet.VersionSetId

    }

    $targetFile = "$Env:temp\$($sourceelement).bicep"

    write-host "Source"
    write-host " Api Mamanagement"
    write-host " SubscriptionId: $sourceSubscriptionId"
    write-host " Resource Group: $($sourceApiManagement.ResourceGroupName)"
    write-host " Name: $($sourceApiManagement.Name)"

    if($null -ne $sourceVersionSet){
        write-host " Api version set"
        write-host " Name: $($sourceVersionSet.Name)"
        write-host " Id: $($sourceVersionSet.VersionSetId)"
    }
    else{
        write-host " Api"
        write-host " Name: $($sourceApi.Name)"
        write-host " Version: $($sourceApi.ApiVersion)"
        write-host " Id: $($sourceApi.ApiId)"
    }
    write-host ""
    write-host "Temporary target file: $targetFile"
    write-host ""
    if($null -ne $sourceVersionSet){
        write-host "Executing 'Export-PSBicepApiManagementApiVersionSet -SubscriptionId '$sourceSubscriptionId' -ResourceGroupName '$($sourceApiManagement.ResourceGroupName)' -ApiManagementName '$($sourceApiManagement.Name)' -ApiVersionSetId $($sourceVersionSet.VersionSetId) -TargetFile '$($targetFile)''"
        Export-PSBicepApiManagementApiVersionSet -SubscriptionId $sourceSubscriptionId -ResourceGroupName $sourceApiManagement.ResourceGroupName -ApiManagementName $sourceApiManagement.Name -ApiVersionSetId $sourceVersionSet.VersionSetId -TargetFile $targetFile
    }
    else{
        write-host "Executing 'Export-PSBicepApiManagementApi -SubscriptionId '$sourceSubscriptionId' -ResourceGroupName '$($sourceApiManagement.ResourceGroupName)' -ApiManagementName '$($sourceApiManagement.Name)' -ApiId '$($sourceApi.ApiId)' -TargetFile '$($targetFile)''"
        Export-PSBicepApiManagementApi -SubscriptionId $sourceSubscriptionId -ResourceGroupName $sourceApiManagement.ResourceGroupName -ApiManagementName $sourceApiManagement.Name -ApiId $sourceApi.ApiId -TargetFile $targetFile
    }
    $targetApiManagement = Get-ApiManagement -env 'target'
    $TargetSubscriptionId = $SubscriptionIdFinder.Replace($targetApiManagement.Id,'$1')

    $bicepParameters=@{}
    $BicepDocument =Get-Content $targetFile -raw |ConvertFrom-PSBicepDocument
    foreach($FileParam in $BicepDocument.Params){
        if($FileParam.DefaultValue -ne '''#TargetApiManagement#'''){
            $valueRead=$false
            while(-not $valueRead){
                $message = "Enter value for parameter '$($FileParam.Identifier)'"
                if($null -ne $FileParam.DefaultValue){
                    $message+= " [$($FileParam.DefaultValue)]"
                }
                $Value = Read-Host -Prompt $message
                if([String]::IsNullOrWhiteSpace($Value)){
                    if($null -ne $FileParam.DefaultValue){
                        $value = $FileParam.DefaultValue.Replace("'","")
                    }
                    else{
                        write-host "Please provide a value"
                        continue;
                    }
                }
                $valueRead=$true
                $bicepParameters[$FileParam.Identifier]= $Value
            }
        }
    }

    write-host "Target"
    write-host " Api Mamanagement"
    write-host " SubscriptionId: $TargetSubscriptionId"
    write-host " Resource Group: $($targetApiManagement.ResourceGroupName)"
    write-host " Name: $($targetApiManagement.Name)"
    write-host " Parameters:"
    foreach($key in $bicepParameters.Keys)
    {
        write-host " Key: $key"
        write-host " Value: $($bicepParameters[$key])"
    }
    write-host ""
    write-host "Executing 'Import-PSBicepApiManagementApi -SubscriptionId '$TargetSubscriptionId' -ResourceGroupName '$($targetApiManagement.ResourceGroupName)' -ApiManagementName '$($targetApiManagement.Name)' -TargetFile '$($targetFile)'' -Parameters $('$bicepParameters')"
    Import-PSBicepApiManagementApi -SubscriptionId $TargetSubscriptionId -ResourceGroupName $targetApiManagement.ResourceGroupName -ApiManagementName $targetApiManagement.Name -TargetFile $targetFile -Parameters $bicepParameters

}