From 4bc9b04c6a3d532fd17a292144529907bf9df3cb Mon Sep 17 00:00:00 2001 From: "Zhilmil Gupta (Tata Consultancy Services Ltd)" Date: Thu, 16 Jan 2020 15:58:37 +0530 Subject: [PATCH] Rolling back Vnet changes --- src/AzSK/Framework/Core/SVT/SVTIaasBase.ps1 | 150 ++++++-------------- 1 file changed, 45 insertions(+), 105 deletions(-) diff --git a/src/AzSK/Framework/Core/SVT/SVTIaasBase.ps1 b/src/AzSK/Framework/Core/SVT/SVTIaasBase.ps1 index 78cf86f20..aca8a0598 100644 --- a/src/AzSK/Framework/Core/SVT/SVTIaasBase.ps1 +++ b/src/AzSK/Framework/Core/SVT/SVTIaasBase.ps1 @@ -38,67 +38,39 @@ class SVTIaasBase: AzSVTBase hidden [PSObject[]] GetvNetNics($VNetSubnets) { - if([FeatureFlightingManager]::GetFeatureStatus("EnableVnetFixForSub",$($this.SubscriptionContext.SubscriptionId))) + if (-not $this.vNetNics) { - if (-not $this.vNetNics) - { - $nics = Get-AzNetworkInterface #-ResourceGroupName $rgname - $ipc = $VNetSubnets| Select-Object -Property 'IpConfigurations' -ExpandProperty 'IpConfigurations' - - - if($null -ne $ipc -and ($ipc.IpConfigurations | Measure-Object).Count -gt 0) + $this.vNetNicsWIssues = @(); + $VNetSubnets | ForEach-Object{ + Set-Variable -Name currentsubnet -Scope Local -Value $_ + if($null -ne $currentsubnet.IpConfigurations ) { - $NICIpConfigs = $ipc.IpConfigurations.Id | Where-Object{$_ -in $nics.IpConfigurations.Id} - $NICresources = ($nics | Select-Object @{Name= 'ResourceId'; Expression = {$_.Id}}, @{Name="IpConfigurationId"; Expression={ $_.IpConfigurations | Select-Object Id }} |Select-Object -Property * -ExcludeProperty IpConfigurations -ExpandProperty IpConfigurationId | Where-Object{$_.Id -in $NICIpConfigs}) - if(($NICresources | Measure-Object).Count -gt 0) - { - $resourceIds = $NICresources.ResourceId - $this.VNetNics += $nics | Where-Object{$_.Id -in $resourceIds} - } - } - } - - return $this.vNetNics; - - } - else - { - if (-not $this.vNetNics) - { - $this.vNetNicsWIssues = @(); - $VNetSubnets | ForEach-Object{ - Set-Variable -Name currentsubnet -Scope Local -Value $_ - if($null -ne $currentsubnet.IpConfigurations ) - { - $currentsubnet.IpConfigurations | ForEach-Object{ - Set-Variable -Name currentipconfig -Scope Local -Value $_ - if($currentipconfig.Id.Contains("Microsoft.Network/networkInterfaces")) - { - $currentipconfig = $currentipconfig.Id.ToLower() - $nicresourceid = $currentipconfig.Substring(0,$currentipconfig.LastIndexOf("ipconfigurations")-1) - try - { - # - $nic = Get-AzResource -ResourceId $nicresourceid - $this.vNetNics += $nic - } - catch - { - $this.vNetNicsWIssues += $nicresourceid; - } - } + $currentsubnet.IpConfigurations | ForEach-Object{ + Set-Variable -Name currentipconfig -Scope Local -Value $_ + if($currentipconfig.Id.Contains("Microsoft.Network/networkInterfaces")) + { + $currentipconfig = $currentipconfig.Id.ToLower() + $nicresourceid = $currentipconfig.Substring(0,$currentipconfig.LastIndexOf("ipconfigurations")-1) + try + { + # + $nic = Get-AzResource -ResourceId $nicresourceid + $this.vNetNics += $nic + } + catch + { + $this.vNetNicsWIssues += $nicresourceid; + } } } } } - } + } return $this.vNetNics; } hidden [PSObject[]] GetvnetNicsProperties($vNetNics) { - - if(-not $this.vNetNicsOutput) { if($null -ne $vNetNics ) @@ -106,86 +78,54 @@ class SVTIaasBase: AzSVTBase $this.vNetPIPIssues = @(); $tempVNetNICS = [array]($vNetNics) $tempVNetNICS | ForEach-Object{ + Set-Variable -Name nic -Scope Local -Value $_ + Set-Variable -Name nicproperties -Scope Local -Value $_.Properties try { - Set-Variable -Name nic -Scope Local -Value $_ - $out = ""| Select-Object NICName, VMName, VMId, PrimaryStatus, NetworkSecurityGroupName,NetworkSecurityGroupId, PublicIpAddress, PrivateIpAddress, EnableIPForwarding, IpConfigurations + $out = ""| Select-Object NICName, VMName, VMId, PrimaryStatus, NetworkSecurityGroupName,NetworkSecurityGroupId, PublicIpAddress, PrivateIpAddress, EnableIPForwarding, IpConfigurations $out.NICName = $nic.Name - $out.IpConfigurations = $nic.IpConfigurations - $out.EnableIPForwarding = $nic.EnableIPForwarding + $out.IpConfigurations = $nicproperties.IpConfigurations + $out.EnableIPForwarding = $nicproperties.EnableIPForwarding $PublicIpAddresses = @() $PrivateIpAddresses = @() - if([FeatureFlightingManager]::GetFeatureStatus("EnableVnetFixForSub",$($this.SubscriptionContext.SubscriptionId))) - { - - $NICPublicIpAddresses = @(); - $NICPublicIpAddresses += $nic.ipconfigurations | Where-Object {$null -ne $_.PublicIpAddress} - $PrivateIpAddresses += $nic.ipconfigurations.PrivateIpAddress - if(($NICPublicIpAddresses |Measure-Object).Count -gt 0) + $nicproperties.IpConfigurations | ForEach-Object{ + Set-Variable -Name ipconfiguration -Scope Local -Value $_ + try { - $NICPublicIpAddresses | ForEach-Object{ - try - { - - $IPResource = Get-AzResource -ResourceId $_.PublicIpAddress.Id + if(($ipconfiguration | Get-Member -Name "Properties") -and ($ipconfiguration.Properties | Get-Member -Name "PublicIpAddress") -and $ipconfiguration.Properties.PublicIpAddress) + { + $IPResource = Get-AzResource -ResourceId $ipconfiguration.Properties.PublicIpAddress.Id $pubResourceName = Get-AzPublicIpAddress -Name $IPResource.Name -ResourceGroupName $IPResource.ResourceGroupName $PublicIpAddresses += $pubResourceName.IpAddress - } - catch - { - - $this.vNetPIPIssues += $nic.IpConfigurations - } - - - } - } - - - } - else - { - $nic.IpConfigurations | ForEach-Object{ - Set-Variable -Name ipconfiguration -Scope Local -Value $_ - try - { - if(($ipconfiguration | Get-Member -Name "Properties") -and ($ipconfiguration.Properties | Get-Member -Name "PublicIpAddress") -and $ipconfiguration.Properties.PublicIpAddress) - { - $IPResource = Get-AzResource -ResourceId $ipconfiguration.Properties.PublicIpAddress.Id - $pubResourceName = Get-AzPublicIpAddress -Name $IPResource.Name -ResourceGroupName $IPResource.ResourceGroupName - $PublicIpAddresses += $pubResourceName.IpAddress - } - $PrivateIpAddresses += $ipconfiguration.Properties.PrivateIpAddress - } - catch - { - $this.vNetPIPIssues += $ipconfiguration } + $PrivateIpAddresses += $ipconfiguration.Properties.PrivateIpAddress + } + catch + { + $this.vNetPIPIssues += $ipconfiguration } } $out.PublicIpAddress = ([System.String]::Join(";",$PublicIpAddresses)) $out.PrivateIpAddress = ([System.String]::Join(";",$PrivateIpAddresses)) - - if(($nic | Get-Member -Name "VirtualMachine") -and $nic.VirtualMachine ) + if(($nicproperties | Get-Member -Name "VirtualMachine") -and $nicproperties.VirtualMachine ) { - $vmresource = Get-AzResource -ResourceId $nic.VirtualMachine.Id + $vmresource = Get-AzResource -ResourceId $nicproperties.VirtualMachine.Id $out.VMName = $vmresource.Name } else { $out.VMName = "" } - if($null -ne ($nic | Get-Member primary)) + if($null -ne ($nicproperties | Get-Member primary)) { - $out.PrimaryStatus = $nic.primary + $out.PrimaryStatus = $nicproperties.primary } - if(($nic | Get-Member -Name "NetworkSecurityGroup") -and $nic.NetworkSecurityGroup) + if(($nicproperties | Get-Member -Name "NetworkSecurityGroup") -and $nicproperties.NetworkSecurityGroup) { - $nsgresource = Get-AzResource -ResourceId $nic.NetworkSecurityGroup.Id + $nsgresource = Get-AzResource -ResourceId $nicproperties.NetworkSecurityGroup.Id $out.NetworkSecurityGroupName = $nsgresource.Name } - $this.vNetNicsOutput += $out } catch @@ -198,4 +138,4 @@ class SVTIaasBase: AzSVTBase } return $this.vNetNicsOutput; } -} +} \ No newline at end of file