functions/Get-jtAzTemplate.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
function Get-jtAzTemplate {
<#
.SYNOPSIS
    Returns a template that you can use for reference when deploying resources to Azure.

.DESCRIPTION
    Get-jtAzTemplate is a function that returns the template for the given resource found at https://docs.microsoft.com/en-us/azure/templates/.
    You can return the json-, the bicep template or both.

.PARAMETER providerNamespace
    The provider namespace

.PARAMETER resourceType
    The resource type. If no resource type is given this function will return all available resource types and api versions for the given provider namespace.

.PARAMETER apiVersion
    The API version. Default is 'Latest'. Multiple versions are allowed.
    For a list of all api versions for a given provider namespace run Get-jtAzTemplate -providerNamespace <providerNamespace>

.EXAMPLE
     Get-jtAzTemplate -providerNamespace 'microsoft.sql' -resourceType 'servers/databases'

.EXAMPLE
     Get-jtAzTemplate -providerNamespace 'microsoft.sql' -resourceType 'servers/databases' -templateStructure Bicep

.EXAMPLE
     Get-jtAzTemplate -providerNamespace microsoft.sql -resourceType servers/databases -apiVersion '2020-08-01-preview'

.INPUTS
    String

.OUTPUTS
    # todo: PSCustomObject

.NOTES
    Author: Jeroen Trimbach
    Website: https://jeroentrimbach.com
#>


    [CmdletBinding()]
    param (
        [Parameter(Position=0,Mandatory=$true)]
        [string]$providerNamespace, # = 'microsoft.resources'
        
        [Parameter(Position=1,Mandatory=$false)]
        [string]$resourceType, # = 'deployments'

        [Parameter(Position=1,Mandatory=$false)]
        [string[]]$apiVersion = 'Latest',

        [Parameter(Position=3,Mandatory=$false)]
        [ValidateSet("ARM","Bicep","Both")]
        [string]$templateStructure
    )
    BEGIN {
        Write-Verbose "Starting: $($MyInvocation.MyCommand)"
    } #begin
    PROCESS {
        if (!$PSBoundParameters.ContainsKey('resourceType')) {
            Write-Verbose 'resourceType not given. Calling function Get-jtAzResourceTypes.'
            Get-jtAzResourceTypes -providerNamespace $providerNamespace
        } else {
            $baseUrl = 'https://docs.microsoft.com/azure/templates/'
            foreach ($version in $apiVersion) {
                try {
                    if ($apiVersion -eq 'Latest') {
                        $Uri = $baseUrl + $providerNamespace + '/' + $resourceType
                        Write-Verbose "apiVersion: $version. Resource URL: $Uri"
                    } else {
                        $Uri = $baseUrl + $providerNamespace + '/' + $version + '/' + $resourceType
                        Write-Verbose "apiVersion: $version. Resource URL: $Uri"
                    }

                    $response = Invoke-RestMethod -Uri $Uri

                    Write-Verbose "Part where we look for $templateStructure"
                    switch ($templateStructure) {
                        "ARM"   {   $ARM    = ([regex]'(?<=lang-json">)[\S\s]*?(?=<\/code><\/pre>)').Matches($response)[0].Value.Replace('&quot;','"')      }
                        "Bicep" {   $Bicep  = ([regex]'(?<=lang-bicep">)[\S\s]*?(?=<\/code><\/pre>)').Matches($response)[0].Value                           }
                        "Both"  {   $ARM    = ([regex]'(?<=lang-json">)[\S\s]*?(?=<\/code><\/pre>)').Matches($response)[0].Value.Replace('&quot;','"'); `
                                    $Bicep  = ([regex]'(?<=lang-bicep">)[\S\s]*?(?=<\/code><\/pre>)').Matches($response)[0].Value                           }
                        Default {   $ARM    = ([regex]'(?<=lang-json">)[\S\s]*?(?=<\/code><\/pre>)').Matches($response)[0].Value.Replace('&quot;','"'); `
                                    $Bicep  = ([regex]'(?<=lang-bicep">)[\S\s]*?(?=<\/code><\/pre>)').Matches($response)[0].Value                           }
                    }

                    Write-Verbose "Part where we create the output object"
                    $props = [ordered]@{'providerNamespace'  = $providerNamespace
                                        'resourceType'       = $resourceType
                                        'apiVersion'         = $apiVersion
                            
                                        'Template Reference' = $Uri

                                        'ARM'                = $ARM
                                        'Bicep'              = $Bicep}
                    $obj = New-Object -TypeName psobject -Property $props
                    Write-Output $obj
                } catch {
                    Write-Warning "FAILED to retrieve from URL: $Uri"
                    if ($check = Get-jtAzProviderNamespace -Name $providerNamespace) {
                        Write-Verbose "$providerNamespace is a valid provider namespace. Possible resource types are:"
                        $check | Get-jtAzResourceTypes
                    } else {
                        Write-Warning "The provider namespace '$providerNamespace' does not exist. Please run 'Get-jtAzProviderNamespace'."
                    }
                } #catch
            } #foreach
        } #else
    } #process
    END {
        Write-Verbose "Ending: $($MyInvocation.MyCommand)"
    } #end
} # function