Framework/Core/SVT/SVTResourceResolver.ps1
Set-StrictMode -Version Latest class SVTResourceResolver: AzSdkRoot { [string] $ResourceName = ""; [string] $ResourceType = ""; [ResourceTypeName] $ResourceTypeName = [ResourceTypeName]::All; [Hashtable] $Tag = $null; [string] $TagName = ""; [string] $TagValue = ""; hidden [string[]] $ResourceGroups = @(); [SVTResource[]] $SVTResources = @(); # Indicates to fetch all resource groups SVTResourceResolver([string] $subscriptionId): Base($subscriptionId) { } SVTResourceResolver([string] $subscriptionId, [string] $resourceGroupNames): Base($subscriptionId) { if(-not [string]::IsNullOrEmpty($resourceGroupNames)) { $this.ResourceGroups += $this.ConvertToStringArray($resourceGroupNames); if ($this.ResourceGroups.Count -eq 0) { throw [System.ArgumentException] "The parameter 'resourceGroupNames' does not contain any string." } } } [void] LoadAzureResources() { #Lazy load the SVT resource array if($this.SVTResources.Count -eq 0) { $resources = @(); #Fetch Resources from Azure if($this.ResourceGroups.Count -eq 0) { #Target resource group param is not passed. Pass appropriate input params $resources += $this.FindAzureRmResource(""); } else { #Fetch resources for all resource groups $this.ResourceGroups | ForEach-Object { $resouresFound = $this.FindAzureRmResource($_ ); if(($resouresFound | Measure-Object).Count -eq 0) { $this.PublishCustomMessage("Could not find any resources under Resource Group [$_]", [MessageType]::Warning); } else { $resources += $resouresFound; } } } if($resources.Count -eq 0) { throw "Could not find any resources that match the specified criteria." } $allResourceTypes = [string[]] [ConfigurationManager]::LoadServerConfigFile("AllResourceTypes.json"); $erVnetResourceGroups = $null; $resources | ForEach-Object { $resType = $_.ResourceType; $svtResource = [SVTResource]::new(); $svtResource.ResourceId = $_.ResourceId; $svtResource.ResourceGroupName = $_.ResourceGroupName; $svtResource.ResourceName = $_.ResourceName; $svtResource.ResourceType = $resType; $svtResource.Location = $_.Location; if($this.ResourceTypeName -ne [ResourceTypeName]::All) { $svtResource.ResourceTypeMapping = ([SVTMapping]::Mapping | Where-Object { $_.ResourceTypeName -eq $this.ResourceTypeName } | Select-Object -First 1); } else { $svtResource.ResourceTypeMapping = ([SVTMapping]::Mapping | Where-Object { $_.ResourceType -eq $resType } | Select-Object -First 1); } # Checking if Vnet is ErVNet or not if($svtResource.ResourceTypeMapping -and $svtResource.ResourceTypeMapping.ResourceTypeName -eq [SVTMapping]::VirtualNetworkTypeName) { if(-not $erVnetResourceGroups) { $erVnetResourceGroups = $this.ConvertToStringArray([ConfigurationManager]::GetAzSdkConfigData().ERvNetResourceGroupNames); } # Check if the resource group name corresponds to ERvNet if($erVnetResourceGroups -contains $svtResource.ResourceGroupName) { # Set the ERvNet type $svtResource.ResourceTypeMapping = ([SVTMapping]::Mapping | Where-Object { $_.ResourceTypeName -eq [SVTMapping]::ERvNetTypeName } | Select-Object -First 1); } } $ignoredType = $false; if(-not $svtResource.ResourceTypeMapping) { $ignoredType = [string]::IsNullOrEmpty(($allResourceTypes | Where-Object { $_ -eq $resType } | Select-Object -First 1)); } if(-not $ignoredType) { $this.SVTResources += $svtResource; } } } } hidden [System.Object[]] FindAzureRmResource([string] $resourceGroupName) { $expression = "Find-AzureRmResource"; if(-not [string]::IsNullOrEmpty($resourceGroupName)) { $expression += " -ResourceGroupNameEquals '$resourceGroupName' " ; } if([string]::IsNullOrEmpty($this.ResourceType) -and ($this.ResourceTypeName -ne [ResourceTypeName]::All)) { $mapping = ([SVTMapping]::Mapping | Where-Object { $_.ResourceTypeName -eq $this.ResourceTypeName.ToString() } | Select-Object -First 1); if($mapping) { $this.ResourceType = $mapping.ResourceType; } } if(-not [string]::IsNullOrEmpty($this.ResourceType)) { $expression += " -ResourceType '$($this.ResourceType)' " ; } if(-not [string]::IsNullOrEmpty($this.ResourceName)) { $expression += " -ResourceNameEquals '$($this.ResourceName)' " ; } if($this.Tag -and $this.Tag.Count -ne 0) { $tagValues = ""; $this.Tag.GetEnumerator() | ForEach-Object { $tagValues += "'$($_.Name)'='$($_.Value)'; " }; $expression += " -Tag @{ $tagValues }" ; } elseif((-not [string]::IsNullOrEmpty($this.TagName)) -and (-not [string]::IsNullOrEmpty($this.TagValue))) { $expression += " -TagName '$($this.TagName)' -TagValue '$($this.TagValue)'" ; } $result = @(); $expressionResult = Invoke-Expression $expression if($expressionResult) { $result += $expressionResult } return $result; } } |