Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
mtboren committed Mar 29, 2017
2 parents 22bb295 + a476e89 commit c8c8e28
Show file tree
Hide file tree
Showing 11 changed files with 667 additions and 255 deletions.
2 changes: 1 addition & 1 deletion DRSRule.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'DRSRule.psm1'

# Version number of this module.
ModuleVersion = '1.0.3'
ModuleVersion = '1.1.0'

# ID used to uniquely identify this module
GUID = 'f7edec59-261e-43e3-818c-6d92653ff05c'
Expand Down
615 changes: 437 additions & 178 deletions DRSRule.psm1

Large diffs are not rendered by default.

18 changes: 7 additions & 11 deletions DRSRuleUtil.psm1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
## functions that are used by the module, but not published for end-user consumption

Function Get-DrsRuleObject
{
function Get-DrsRuleObject {
<# .Description
Supporting function to get Rule object of given type
#>
Expand All @@ -10,17 +9,14 @@ Function Get-DrsRuleObject
[Parameter(ValueFromPipeline = $True)]
[ValidateNotNullOrEmpty()]
[PSObject[]]$Object,

[String]$Type
)

Process
{
$Object | where {$_.Type -match $Type}
}
process {$Object | Where-Object {$_.Type -match $Type}}
}

function Get-ClusterObjFromClusterParam
{
function Get-ClusterObjFromClusterParam {
<# .Description
Function to get cluster object(s) from given PSObject (expecting string, ClusterImpl object, or $null). If $null, then gets all clusters in connected VIServers
.Outputs
Expand All @@ -31,7 +27,7 @@ function Get-ClusterObjFromClusterParam
)

process {
if($null -eq $Cluster) {Get-Cluster}
if($null -eq $Cluster) {return (Get-Cluster)}
else {
$Cluster | Foreach-Object {
if($_ -is [System.String]){
Expand All @@ -43,8 +39,7 @@ function Get-ClusterObjFromClusterParam
}
} ## end function

function _Test-TypeOrString
{
function _Test-TypeOrString {
<# .Description
Helper function to test if object is either of type String or $Type
.Outputs
Expand All @@ -53,6 +48,7 @@ function _Test-TypeOrString
param(
## Object to test
[parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][PSObject[]]$ObjectToTest,

## Type of object for which to check
[parameter(Mandatory=$true)][Type]$Type
)
Expand Down
2 changes: 1 addition & 1 deletion MITLicense.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 Luc Dekens, Matt Boren
Copyright (c) since 2015 Luc Dekens, Matt Boren

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
# DRSRule PowerShell module ReadMe#
# DRSRule PowerShell module ReadMe
The DRSRule module allows you to work with all types of vSphere DRS rules.
The module provides support for VM and VMHost groups. And, it works with affinity/anti-affinity VM rules and VM to VMHost rules.

### Brief info on module ###
### Brief info on module
The module came from the need for DRS rule/group info gathering, exporting, and recreating. Initially there were some code blocks for exporting rule info and for importing again those rules, but things evolved into a module.

A couple of examples:
- Export rule/group info: `Export-DrsRule -Path c:\someFolder\myDrsRuleAndGroupInfo.json`
- Import rule/group info: `Import-DrsRule -Path c:\someFolder\myDrsRuleAndGroupInfo.json`

### How to set up the DRSRule module for use ###
### How to set up the DRSRule module for use
* Download and extract the module .zip file
* `Unblock-File` on the extracted contents
* `Import-Module <path\To\ModuleFolder>`
* Use `Get-Help` as per usual for cmdlet help and examples

### Cmdlets in this module ###
### Cmdlets in this module
- Export/Import:
- Export-DrsRule
- Import-DrsRule
- `Export-DrsRule`
- `Import-DrsRule`
- Get:
- Get-DrsVMGroup
- Get-DrsVMHostGroup
- Get-DrsVMToVMHostRule
- Get-DrsVMToVMRule
- `Get-DrsVMGroup`
- `Get-DrsVMHostGroup`
- `Get-DrsVMToVMHostRule`
- `Get-DrsVMToVMRule`
- New:
- New-DrsVMGroup
- New-DrsVMHostGroup
- New-DrsVMToVMHostRule
- New-DrsVMToVMRule
- `New-DrsVMGroup`
- `New-DrsVMHostGroup`
- `New-DrsVMToVMHostRule`
- `New-DrsVMToVMRule`
- Remove:
- Remove-DrsVMGroup
- Remove-DrsVMHostGroup
- Remove-DrsVMToVMHostRule
- Remove-DrsVMToVMRule
- `Remove-DrsVMGroup`
- `Remove-DrsVMHostGroup`
- `Remove-DrsVMToVMHostRule`
- `Remove-DrsVMToVMRule`
- Set:
- Set-DrsVMGroup
- Set-DrsVMHostGroup
- Set-DrsVMToVMHostRule
- Set-DrsVMToVMRule
- `Set-DrsVMGroup`
- `Set-DrsVMHostGroup`
- `Set-DrsVMToVMHostRule`
- `Set-DrsVMToVMRule`
16 changes: 12 additions & 4 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
## DRSRule PowerShell module ##
## DRSRule PowerShell module

### Changelog ###
### Changelog

### v1.0.1 ###
### v1.1.0
29 Mar 2017
- \[enhancement] added support for enhancement suggested in [Issue #7](https://github.com/PowerCLIGoodies/DRSRule/issues/7): get a DRS `VMGroup` by the related VM; while at it, added support for:
- getting a DRS `VMHostGroup` by the related VMHost
- getting DRS `VMToVMRule` by VM
- getting DRS `VMToVMHostRule` by VM or VMHost
- \[improvment] added [Pester](https://github.com/pester/Pester) tests for `Get-Drs*` cmdlets

### v1.0.1

30 Jun 2015

- \[bugfix] fixed rare problem with creating new `DRSRule.VMHostGroup` for VMHost group item where `Host` property of corresponding `VMware.Vim.ClusterHostGroup` object is empty and user is using PowerShell v5 (credit [gpduck](https://github.com/gpduck)). This was in `Get-DrsVMHostGroup`. Updated similar situation for functions `Get-DrsVMGroup` and `Get-DrsVMToVMRule`.

### v1.0.0 ###
### v1.0.0

Initial release, 21 Jan 2015.
30 changes: 30 additions & 0 deletions testing/DRSRule.TestingInit.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<# .Description
Initialization code for use by multiple *.Tests.ps1 files for testing DRSRule PowerShell module
#>
param (
## Name of the vCenter cluster to use in the DRSRule testing
[parameter(Mandatory=$true)][string]$Cluster
)

$strThisModuleName = "DRSRule"
## if module not already loaded, try to load it (assumes that module is in PSModulePath)
if (-not ($oModuleInfo = Get-Module $strThisModuleName)) {
$oModuleInfo = Import-Module $strThisModuleName -PassThru
if (-not ($oModuleInfo -is [System.Management.Automation.PSModuleInfo])) {Throw "Could not load module '$strThisModuleName' -- is it available in the PSModulePath? You can manually load the module and start tests again"}
} ## end if
Write-Verbose -Verbose ("Starting testing of module '{0}' (version '{1}' from '{2}')" -f $oModuleInfo.Name, $oModuleInfo.Version, $oModuleInfo.Path)

## get the VIServer connection to use
$oVIServerConnectionToUse = if (-not (($global:DefaultVIServers | Measure-Object).Count -gt 0)) {
$hshParamForConnectVIServer = @{Server = $(Read-Host -Prompt "vCenter server to which to connect for testing")}
Connect-VIServer @hshParamForConnectVIServer
} ## end if
else {$global:DefaultVIServers[0]}
Write-Verbose -Verbose "Testing using VIServer of name '$($oVIServerConnectionToUse.Name)'"


## get the cluster use for testing
$oTmpClusterToUse = Get-Cluster -Name $Cluster | Select-Object -First 1
Write-Verbose -Verbose "Testing using cluster '$oTmpClusterToUse'"

return $oTmpClusterToUse
107 changes: 107 additions & 0 deletions testing/DRSRule.Tests_Get.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<# .Description
Pester tests for DRSRule PowerShell module. Expects that:
0) DRSRule module is already loaded (but, will try to load it if not)
1) a connection to at least one vCenter is in place (but, will prompt for XMS to which to connect if not)
2) there are one or more existing DRS items of each type to test (VMGroup, VMHostGroup, etc.) in the specified cluster
.Example
Invoke-Pester -Script @{Path = '\\some\path\DRSRule\testing\DRSRule.Tests_Get_Unit.ps1'; Parameters = @{Cluster = "myFavoriteCluster"}}
Invokes the tests in said Tests script, passing the given Cluster parameter value, to be used for the cluster-specific tests
#>
param (
## Name of the vCenter cluster to use in the DRSRule testing
[parameter(Mandatory=$true)][string]$Cluster
)

## initialize things, preparing for tests
$oClusterToUse = & $PSScriptRoot\DRSRule.TestingInit.ps1 -Cluster $Cluster
## hashtable of DRSRule object short typename to VMware "real" object full typename info, to be used with -ReturnRawGroup testing
$hshDrsruletypeToVMwareTypeInfo = @{
VMGroup = "VMware.Vim.ClusterVmGroup"
VMHostGroup = "VMware.Vim.ClusterHostGroup"
VMToVMRule = "VMware.Vim.ClusterAffinityRuleSpec", "VMware.Vim.ClusterAntiAffinityRuleSpec"
VMToVMHostRule = "VMware.Vim.ClusterVmHostRuleInfo"
} ## end hsh


## for each of the object types whose typenames match the object name in the cmdlet noun, test getting such an object
$arrObjectTypesToGet = Write-Output VMGroup, VMHostGroup, VMtoVMRule, VMToVMHostRule
$arrObjectTypesToGet | Foreach-Object {
Describe -Tags "Get" -Name "Get-Drs$_" {
It "Gets a DRSRule $_ object" {
$arrReturnTypes = if ($arrTmpObj = Invoke-Command -ScriptBlock {& "Get-Drs$_" | Select-Object -First 2}) {$arrTmpObj | Get-Member -ErrorAction:Stop | Select-Object -Unique -ExpandProperty TypeName} else {$null}
New-Variable -Name "bGetsOnly${_}Type" -Value ($arrReturnTypes -eq "DRSRule.$_")

## gets only the desired object type should be $true
(Get-Variable -ValueOnly -Name "bGetsOnly${_}Type") | Should Be $true
}

It "Gets a 'raw' $($hshDrsruletypeToVMwareTypeInfo[$_] -join ' or ') object, via -ReturnRawGroup" {
$strThisTypeShortname = $_
## should be only one type returned
$arrReturnTypes = if ($arrTmpObj = Invoke-Command -ScriptBlock {& "Get-Drs$_" -ReturnRaw | Select-Object -First 2}) {$arrTmpObj | Get-Member -ErrorAction:Stop | Select-Object -Unique -ExpandProperty TypeName} else {$null}
New-Variable -Name "bGetsOnly${_}Type" -Value (($arrReturnTypes | Foreach-Object {$hshDrsruletypeToVMwareTypeInfo[$strThisTypeShortname] -contains $_}) -notcontains $false)

## gets only the desired object type should be $true
(Get-Variable -ValueOnly -Name "bGetsOnly${_}Type") | Should Be $true
}

It "Gets a DRSRule $_ object (from specific cluster, '$oClusterToUse')" {
$arrReturnTypes = if ($arrTmpObj = Invoke-Command -ScriptBlock {& "Get-Drs$_" -Cluster $oClusterToUse | Select-Object -First 1}) {$arrTmpObj | Get-Member -ErrorAction:Stop | Select-Object -Unique -ExpandProperty TypeName} else {$null}
New-Variable -Name "bGetsOnly${_}Type" -Value ($arrReturnTypes -eq "DRSRule.$_")
$bGetsItemFromSpecifiedCluster = $arrTmpObj.Cluster -eq $oClusterToUse.Name

## gets only the desired object type should be $true
(Get-Variable -ValueOnly -Name "bGetsOnly${_}Type") | Should Be $true
$bGetsItemFromSpecifiedCluster | Should Be $true
}

It "Gets a DRSRule $_ object (from specific cluster, '$oClusterToUse', taking cluster from pipeline)" {
$arrReturnTypes = if ($arrTmpObj = Invoke-Command -ScriptBlock {$oClusterToUse | & "Get-Drs$_" | Select-Object -First 1}) {$arrTmpObj | Get-Member -ErrorAction:Stop | Select-Object -Unique -ExpandProperty TypeName} else {$null}
New-Variable -Name "bGetsOnly${_}Type" -Value ($arrReturnTypes -eq "DRSRule.$_")
$bGetsItemFromSpecifiedCluster = $arrTmpObj.Cluster -eq $oClusterToUse.Name

## gets only the desired object type should be $true
(Get-Variable -ValueOnly -Name "bGetsOnly${_}Type") | Should Be $true
$bGetsItemFromSpecifiedCluster | Should Be $true
}
}
}


Describe -Tags "Get" -Name "Get-DrsVMToVMRule" {
It "Gets a DRS VM-To-VM Rule from related VM" {
$oSomeRule = Get-DrsVMToVMRule -ReturnRaw | Select-Object -First 1
$morefVMInRule = $oSomeRule.Vm | Select-Object -Last 1

$bGetsRuleByRelatedVM = (Get-VM -Id $morefVMInRule | Get-DrsVMToVMRule).Name -contains $oSomeRule.Name

## gets only the desired object type should be $true
$bGetsRuleByRelatedVM | Should Be $true
}
}


Describe -Tags "Get" -Name "Get-DrsVMToVMHostRule" {
It "Gets a DRS VM-To-VMHost Rule from related VM" {
## get a DRS VM-to-VMHost rule where the associated DRS VMGroup has one or more VMs in it
$oSomeRule = Get-DrsVMToVMHostRule | Where-Object {(Get-DrsVMGroup -Name $_.VmGroupName -Cluster $_.Cluster -ReturnRaw).Vm.Count -gt 0} | Select-Object -First 1
$morefVMInRule = (Get-DrsVMGroup -Name $oSomeRule.VmGroupName -Cluster $oSomeRule.Cluster -ReturnRaw).Vm | Select-Object -Last 1

$bGetsRuleByRelatedVM = (Get-VM -Id $morefVMInRule | Get-DrsVMToVMHostRule).Name -contains $oSomeRule.Name

## gets only the desired object type should be $true
$bGetsRuleByRelatedVM | Should Be $true
}

It "Gets a DRS VM-To-VMHost Rule from related VMHost" {
## get a DRS VM-to-VMHost rule where the associated DRS VMHostGroup has one or more VMHosts in it
$oSomeRule = Get-DrsVMToVMHostRule | Where-Object {(Get-DrsVMHostGroup -Name ($_.AffineHostGroupName,$_.AntiAffineHostGroupName | Where-Object {-not [String]::IsNullOrEmpty($_)}) -Cluster $_.Cluster -ReturnRaw).Host.Count -gt 0} | Select-Object -First 1
$morefVMHostInRule = (Get-DrsVMHostGroup -Name ($oSomeRule.AffineHostGroupName,$oSomeRule.AntiAffineHostGroupName | Where-Object {-not [String]::IsNullOrEmpty($_)}) -Cluster $oSomeRule.Cluster -ReturnRaw).Host | Select-Object -Last 1

$bGetsRuleByRelatedVMHost = (Get-VMHost -Id $morefVMHostInRule | Get-DrsVMToVMHostRule).Name -contains $oSomeRule.Name

## gets only the desired object type should be $true
$bGetsRuleByRelatedVMHost | Should Be $true
}
}
Loading

0 comments on commit c8c8e28

Please sign in to comment.