functions/private/Test-AzureResourceLocation.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
function Test-AzureResourceLocation {
    [OutputType([System.Collections.ArrayList])]
    [CmdletBinding(DefaultParameterSetName="Type")]
    param(
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $Location,
        [Parameter(Mandatory=$true,ParameterSetName="Type")]
        [ValidateNotNullOrEmpty()]
        [string[]] $ResourceType,
        [Parameter(Mandatory=$true,ParameterSetName="File")]
        [ValidateNotNullOrEmpty()]
        [string] $ResourceFile

    )
    
    begin {
        $ErrorString = "Error while checking Azure location compatibility: "
        try{
            $Resources = @()
            $InvalidResources = new-object System.Collections.ArrayList
            $CompatibleResources = (Get-AzureRMLocation)
            write-verbose "Found Location: $CompatibleResources"
            if(!$Location){
                throw "Invalid Azure location provided"
            }
            switch($PSCmdlet.ParameterSetName){
                "Type" {
                    foreach($String in $ResourceType){
                        $Resources += $String.Split('/')[0]
                    }
                }

                "File" {
                    foreach($String in (get-content $ResourceFile)){
                        $Resources += $String.Split('/')[0]
                    }
                }
            }
            $Resources = $Resources|Select-Object -Unique
        }
        catch{
            throw "$($ErrorString): $_"
        }
    }
    
    process {
        try{
            foreach($Resource in $Resources){
                if($($CompatibleResources|Where-Object{$_.Location -eq $Location}).Providers -contains $Resource){
                    write-verbose "$Resource OK"
                } else {
                    $hash = @{
                        Resource = $Resource
                        AvailableLocations = $($CompatibleResources|Where-Object{$_.Providers -contains $Resource})|Select-Object -ExpandProperty Location
                    }
                    $null = $InvalidResources.Add($hash)
                    write-verbose "$Resource NOK"
                }
            }
        }
        catch {
            throw "$($ErrorString): $_"
        }

    }
    
    end {
        return $InvalidResources
    }
    
    
}