From 4d7d1391ff07a17329654a61540cf64c60864e07 Mon Sep 17 00:00:00 2001 From: Wes Carroll Date: Tue, 9 Jul 2019 13:12:22 -0400 Subject: [PATCH] Create function and required supporting docs Import-ZertoVmNicSetting --- .../Public/Import-ZertoVmNicSetting.Tests.ps1 | 39 ++++++ .../Public/Import-ZertoVmNicSetting.ps1 | 125 ++++++++++++++++++ docs/Import-ZertoVmNicSetting.md | 60 +++++++++ 3 files changed, 224 insertions(+) create mode 100644 Tests/Public/Import-ZertoVmNicSetting.Tests.ps1 create mode 100644 ZertoApiWrapper/Public/Import-ZertoVmNicSetting.ps1 create mode 100644 docs/Import-ZertoVmNicSetting.md diff --git a/Tests/Public/Import-ZertoVmNicSetting.Tests.ps1 b/Tests/Public/Import-ZertoVmNicSetting.Tests.ps1 new file mode 100644 index 0000000..5647976 --- /dev/null +++ b/Tests/Public/Import-ZertoVmNicSetting.Tests.ps1 @@ -0,0 +1,39 @@ +#Requires -Modules Pester +$moduleFileName = "ZertoApiWrapper.psd1" +$here = (Split-Path -Parent $MyInvocation.MyCommand.Path).Replace("Tests", "ZertoApiWrapper") +$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".") +$file = Get-ChildItem "$here\$sut" +$modulePath = $here -replace "Public", "" +$moduleFile = Get-ChildItem "$modulePath\$moduleFileName" +Get-Module -Name ZertoApiWrapper | Remove-Module -Force +Import-Module $moduleFile -Force + +Describe $file.BaseName -Tag 'Unit' { + + It "is valid Powershell (Has no script errors)" { + $contents = Get-Content -Path $file -ErrorAction Stop + $errors = $null + $null = [System.Management.Automation.PSParser]::Tokenize($contents, [ref]$errors) + $errors | Should -HaveCount 0 + } + + Context "$($file.BaseName)::Parameter Unit Tests" { + + It "Has a mandatory string array parameter for the settings file to import" { + Get-Command $file.BaseName | Should -HaveParameter InputFile + Get-Command $file.BaseName | Should -HaveParameter InputFile -Mandatory + Get-Command $file.BaseName | Should -HaveParameter InputFile -Type String + } + + It "Will not accecpt a Null or Empty string for the settings file" { + { Import-ZertoVpg -InputFile $null } | Should -Throw + { Import-ZertoVpg -InputFile "" } | Should -Throw + { Import-ZertoVpg -InputFile @() } | Should -Throw + } + + } + + Context "$($file.BaseName)::Function Unit Tests" { + + } +} diff --git a/ZertoApiWrapper/Public/Import-ZertoVmNicSetting.ps1 b/ZertoApiWrapper/Public/Import-ZertoVmNicSetting.ps1 new file mode 100644 index 0000000..8a24dec --- /dev/null +++ b/ZertoApiWrapper/Public/Import-ZertoVmNicSetting.ps1 @@ -0,0 +1,125 @@ +<# .ExternalHelp ./en-us/ZertoApiWrapper-help.xml #> +function Import-ZertoVmNicSetting { + [CmdletBinding(SupportsShouldProcess)] + param( + # File to process for import + [Parameter(Helpmessage = "CSV file containing the required VM NIC settings", Mandatory)] + [ValidateNotNullOrEmpty()] + [string] + $InputFile + ) + + begin { + } + + process { + if (-not (Test-Path $InputFile)) { + Write-Error "Unable to find $InputFile. Please check the name and path and try again." -ErrorAction Stop + } elseif ((Get-Item $InputFile).Extension -notmatch '.csv') { + Write-Error "$InputFile does not have a 'csv' extension. Please check the name and path and try again." -ErrorAction Stop + } + $ExpectedHeaders = "VPGName", "VMName", "NicIdentifier", "LiveNetwork", "LiveShouldReplaceMac", "LiveIsDHCP", "LiveIpAddress", "LiveIpSubnetMask", "LiveIpDefaultGateway", "LivePrimaryDns", "LiveSecondayDns", "LiveDnsSuffix", "TestNetwork", "TestShouldReplaceMac", "TestIsDHCP", "TestIpAddress", "TestIpSubnetMask", "TestIpDefaultGateway", "TestPrimaryDns", "TestSecondayDns", "TestDnsSuffix" + $HeaderLine = ((Get-Content -Path $InputFile -First 1).Replace('"', '')).Split(',') + foreach ($header in $ExpectedHeaders) { + if ($header -notin $HeaderLine) { + Write-Error "$InputFile is malformed. Please ensure all headers are present." -ErrorAction Stop + } + } + $ImportData = Import-Csv -Path $InputFile + $VpgsToUpdate = $ImportData.VPGName | Select-Object -Unique + foreach ($Vpg in $VpgsToUpdate) { + $VpgInfo = Get-ZertoVpg -vpgName $Vpg + $VpgIdentifier = $VpgInfo.VpgIdentifier + $RecoveryNetworks = Get-ZertoVirtualizationSite -siteIdentifier $VpgInfo.RecoverySite.Identifier -networks + $NetworkMap = @{ } + foreach ($Network in $RecoveryNetworks) { + $NetworkMap[$Network.VirtualizationNetworkName] = $Network.NetworkIdentifier + } + $VpgVms = Get-ZertoProtectedVm -vpgName $Vpg + $VmMap = @{ } + foreach ($Vm in $VpgVms) { + $VmMap[$Vm.vmName] = $Vm.vmIdentifier + } + $VpgSettingsId = New-ZertoVpgSettingsIdentifier -vpgIdentifier $VpgIdentifier + $VmsToUpdate = $ImportData | Where-Object { $_.VPGName -eq $Vpg } + foreach ($vm in $VmsToUpdate) { + if ([string]::IsNullOrWhiteSpace($vm.VpgName) -or + [string]::IsNullOrWhiteSpace($Vm.VMName) -or + [string]::IsNullOrWhiteSpace($Vm.NicIdentifier) -or + [string]::IsNullOrWhiteSpace($Vm.LiveNetwork) -or + [string]::IsNullOrWhiteSpace($Vm.LiveShouldReplaceMac) -or + [string]::IsNullOrWhiteSpace($Vm.TestNetwork) -or + [string]::IsNullOrWhiteSpace($Vm.TestShouldReplaceMac)) { + Write-Error "$($Vm.VMName) does not contain all the required data. Please check the CSV entry for this item and try again." -ErrorAction Continue + } else { + $uri = "vpgSettings/{0}/vms/{1}" -f $vpgSettingsId, $vmMap[$vm.VMName] + $VmNicSettings = Get-ZertoVpgSetting -vpgSettingsIdentifier $vpgSettingsId -vmIdentifier $vmMap[$vm.VMName] + foreach ($nic in $VmNicSettings.nics) { + if ($nic.NicIdentifier -eq $vm.NicIdentifier) { + $nic.failover.Hypervisor.NetworkIdentifier = $NetworkMap[$vm.LiveNetwork] + $nic.failover.Hypervisor.ShouldReplaceMacAddress = $vm.LiveShouldReplaceMac + if ($null -eq $nic.failover.Hypervisor.IpConfig -and ($null -ne $vm.LiveIsDHCP -or $null -ne $vm.LiveIpAddress)) { + $IpConfig = [PSCustomObject]@{ + IsDhcp = $vm.LiveIsDHCP + StaticIp = $vm.LiveIpAddress + SubnetMask = $vm.LiveIpSubnetMask + Gateway = $vm.LiveIpDefaultGateway + PrimaryDns = $vm.LivePrimaryDns + SecondaryDns = $vm.LiveSecondayDns + } + $nic.failover.Hypervisor.IpConfig = $IpConfig + } elseif ($null -eq $nic.failover.Hypervisor.IpConfig -and $null -eq $vm.LiveIsDHCP -and $null -eq $vm.LiveIpAddress) { + $nic.failover.Hypervisor.IpConfig = $null + } else { + $nic.failover.Hypervisor.IpConfig.IsDhcp = $vm.LiveIsDHCP + $nic.failover.Hypervisor.IpConfig.StaticIp = $vm.LiveIpAddress + $nic.failover.Hypervisor.IpConfig.SubnetMask = $vm.LiveIpSubnetMask + $nic.failover.Hypervisor.IpConfig.Gateway = $vm.LiveIpDefaultGateway + $nic.failover.Hypervisor.IpConfig.PrimaryDns = $vm.LivePrimaryDns + $nic.failover.Hypervisor.IpConfig.SecondaryDns = $vm.LiveSecondayDns + } + $nic.failover.Hypervisor.DnsSuffix = $vm.LiveDnsSuffix + $nic.failoverTest.Hypervisor.NetworkIdentifier = $NetworkMap[$vm.TestNetwork] + $nic.failoverTest.Hypervisor.ShouldReplaceMacAddress = $vm.TestShouldReplaceMac + if ($null -eq $nic.failoverTest.Hypervisor.IpConfig -and ($null -ne $vm.TestIsDHCP -or $null -ne $vm.TestIpAddress)) { + $IpConfig = [PsCustomObject]@{ + IsDhcp = $vm.TestIsDHCP + StaticIp = $vm.TestIpAddress + SubnetMask = $vm.TestIpSubnetMask + Gateway = $vm.TestIpDefaultGateway + PrimaryDns = $vm.TestPrimaryDns + SecondaryDns = $vm.TestSecondayDns + } + $nic.failoverTest.Hypervisor.IpConfig = $IpConfig + } elseif ($null -eq $nic.failoverTest.Hypervisor.IpConfig -and $null -eq $vm.TestIsDHCP -and $null -eq $vm.TestIpAddress) { + $nic.failoverTest.Hypervisor.IpConfig = $null + } else { + $nic.failoverTest.Hypervisor.IpConfig.IsDhcp = $vm.TestIsDHCP + $nic.failoverTest.Hypervisor.IpConfig.StaticIp = $vm.TestIpAddress + $nic.failoverTest.Hypervisor.IpConfig.SubnetMask = $vm.TestIpSubnetMask + $nic.failoverTest.Hypervisor.IpConfig.Gateway = $vm.TestIpDefaultGateway + $nic.failoverTest.Hypervisor.IpConfig.PrimaryDns = $vm.TestPrimaryDns + $nic.failoverTest.Hypervisor.IpConfig.SecondaryDns = $vm.TestSecondayDns + } + $nic.failoverTest.Hypervisor.DnsSuffix = $vm.TestDnsSuffix + } + } + Write-Verbose "Putting Updated Config for VM: $($vm.vmname) in Vpg: $Vpg" + if ($PSCmdlet.ShouldProcess($vm.NicIdentifier, "Updating Nic")) { + Invoke-ZertoRestRequest -uri $uri -Method "PUT" -Body ($VmNicSettings | ConvertTo-Json -Depth 10) > $null + } + } + } + Write-Verbose "Saving updated configuration for VPG: $Vpg" + if ($PSCmdlet.ShouldProcess($Vpg, "Saving Changes")) { + Save-ZertoVpgSetting -vpgSettingsIdentifier $vpgSettingsId + } + Write-Verbose "Waiting 5 Seconds for Next VPG Update" + Start-Sleep 5 + } + } + + + end { + } +} diff --git a/docs/Import-ZertoVmNicSetting.md b/docs/Import-ZertoVmNicSetting.md new file mode 100644 index 0000000..f2a8d62 --- /dev/null +++ b/docs/Import-ZertoVmNicSetting.md @@ -0,0 +1,60 @@ +--- +external help file: ZertoApiWrapper-help.xml +Module Name: ZertoApiWrapper +online version: https://github.com/ZertoPublic/ZertoApiWrapper/blob/master/docs/Get-ZertoZsspSession.md +schema: 2.0.0 +--- + +# Import-ZertoVmNicSetting + +## SYNOPSIS +Using a CSV file, will import updated Live and Test network settings for protected virtual machines. + +## SYNTAX + +``` +Import-ZertoVmNicSetting [-InputFile] [] +``` + +## DESCRIPTION +Using a CSV file, will import updated Live and Test network settings for protected virtual machines. This function will read the provided CSV file and only update VMs defined in the file. + +It should be noted, due to current API limitations once a NIC is defined to update to either a Static IP address or use a DHCP address, it is not possible to remove that configuration via the API. Should you require this functionality, you will need to manually update via the GUI to set the option to "No" + +## EXAMPLES + +### Example 1 +```powershell +PS C:\> Import-ZertoVmNicSetting -InputFile 'C:\ZertoScripts\UpdatedNicInformation.csv' +``` + +Imports the data in the CSV located at 'C:\ZertoScripts\UpdatedNicInformation.csv' and updates each VM and each NIC as defined in the CSV file. + +## PARAMETERS + +### -InputFile +File to process for import + +```yaml +Type: String +Parameter Sets: (All) +Aliases: + +Required: True +Position: 1 +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### CommonParameters +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). + +## INPUTS + +## OUTPUTS + +## NOTES + +## RELATED LINKS +[Zerto Virtual Manager REST API VpgSettings end point documentation](http://s3.amazonaws.com/zertodownload_docs/Latest/Zerto%20Virtual%20Replication%20Zerto%20Virtual%20Manager%20%28ZVM%29%20-%20vSphere%20Online%20Help/index.html#page/RestfulAPIs%2FStatusAPIs.5.110.html%23)