cgroup.psm1


$CGdefaultProps = ('name', 'index', 'sys-name', 'num-of-vols', 'num-of-snap-sets')

$CGVolsDefaultProps = ('name', 'num-of-vols', 'vol-list')

.(commonLib) 
 
Function Get-XtremConsistencyGroups {
  <#
    .DESCRIPTION
    Displays the list of Consistency Groups.
 
    .PARAMETER Properties
    Array of properties requested from this call.
 
    .PARAMETER Filters
    Array of filters for this call.
 
    .PARAMETER ShowRest
    Return an object represents the REST operation including URI , Method and JSON
 
    .EXAMPLE
    Get-XtremConsistencyGroups
  #>

    [cmdletbinding()]
    [Alias('xmsConsistencyGroupList')]
    Param (
        [parameter()]
        $XtremClusterName = (Get-XtremDefaultSession)._XtremClusterName,
        [parameter()]
        [Alias("Properties")]
        [Argumentcompleter( { doComplete $args 'consistency-groups' prop })] 
        [string[]]$Property = $CGdefaultProps,
        [parameter()]
        [Alias("Filters")]
        [Argumentcompleter( { doComplete $args 'consistency-groups' filter })] 
        [string[]]$Filter,
        [Parameter()]
        [object]$Session =  (Get-XtremDefaultSession),
        [Parameter()]
        [switch]$ShowRest,
        [Parameter()]
        [switch]$Full = $false
    )
    initCommand
    $Route = '/types/consistency-groups'

    if ($Full) { $Property = '' }

    $result = NewXtremRequest -Method GET -endpoint $Route -Session $Session -XtremClusterName $XtremClusterName -Properties $Property -Filters $Filter -ObjectSelection $ObjectSelection -ShowRest:$ShowRest.IsPresent -Multi -Full:$Full.IsPresent

    $result = formatOutPut $Property $result

    finalizeCommand
    return $result
}

Function Get-XtremConsistencyGroup {
  <#
    .DESCRIPTION
    Displays the details of the selected Consistency Group.
 
    .PARAMETER ConsistencyGroupName
    Consistency Group's name or index number
 
    .PARAMETER Properties
    Array of properties requested from this call.
 
    .PARAMETER ShowRest
    Return an object represents the REST operation including URI , Method and JSON
 
    .EXAMPLE
    Get-XtremConsistencyGroup -ConsistencyGroupName group
    .EXAMPLE
    Get-XtremConsistencyGroup -ConsistencyGroupName group -prop name,index
  #>

    [cmdletbinding()]
    [Alias('xmsConsistencyGroupGet')]
    Param (
        [parameter()]
        $XtremClusterName = (Get-XtremDefaultSession)._XtremClusterName,
        [Alias("Name", "Index")]
        [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
        [Argumentcompleter( { doComplete $args 'consistency-groups' name })] 
        $ConsistencyGroupName,
        [Parameter()]
        [Alias("Properties")]
        [Argumentcompleter( { doComplete $args 'consistency-groups' prop })] 
        [string[]]$Property,
        [Parameter()]
        [object]$Session =  (Get-XtremDefaultSession),
        [Parameter()]
        [switch]$ShowRest
    )
    initCommand 
    
    $Route = '/types/consistency-groups'
    $Route, $GetProperty = SetParametersForRequestCG $ConsistencyGroupName
    
    $result = NewXtremRequest -Method GET -endpoint $Route -Session $Session  -XtremClusterName $XtremClusterName -ObjectSelection $ObjectSelection -GetProperty $GetProperty -Properties $Property -ShowRest:$ShowRest.IsPresent

    $result = formatOutPut $Property $result

    finalizeCommand
    return $result
}

Function Remove-XtremConsistencyGroup {
    <#
    .DESCRIPTION
    Enables you to delete selected Consistency Group.
 
    .PARAMETER ConsistencyGroupName
    Consistency Group's name or index number
 
    .PARAMETER ShowRest
    Return an object represents the REST operation including URI , Method and JSON
 
    .EXAMPLE
    Remove-XtremConsistencyGroup -ConsistencyGroupName group
    .EXAMPLE
    Remove-XtremConsistencyGroup -ConsistencyGroupName group -prop name,index
  #>

    [cmdletbinding()]
    [Alias('xmsConsistencyGroupRemove')]
    Param (
        [parameter()]
        $XtremClusterName = (Get-XtremDefaultSession)._XtremClusterName,
        [Alias("Name", "Index")]
        [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
        [Argumentcompleter( { doComplete $args 'consistency-groups' name })] 
        $ConsistencyGroupName,
        [Parameter()]
        [bool]$Confirm =  (Get-XtremDefaultSession)._XtremCLIMode,
        [Parameter()]
        [object]$Session =  (Get-XtremDefaultSession),
        [Parameter()]
        [switch]$ShowRest
    )
    $confirmed = IsConfirmed $Confirm
    if (!$confirmed) {
        return
    }
    initCommand 
    
    $Route = '/types/consistency-groups'
    $Route, $GetProperty = SetParametersForRequestCG $ConsistencyGroupName
    
    $result = NewXtremRequest -Method DELETE -endpoint $Route -Session $Session  -XtremClusterName $XtremClusterName -GetProperty $GetProperty -ShowRest:$ShowRest.IsPresent

    finalizeCommand 
    return $result
}

#Creates a new Consistency Group
Function New-XtremConsistencyGroup {
    <#
    .DESCRIPTION
    Enables you to create a Consistency Group.
 
    .PARAMETER ConsistencyGroupName
    Consistency Group's name or index number
 
    .PARAMETER TagList
    The list of Tags, including full name/path, to be included in the Consistency Group.
 
    .PARAMETER VolList
    The list of object IDs (ID or name).
 
    .PARAMETER QoSEnabledMode
    QosEnabledMode
 
    .PARAMETER QoSPolicyId
    QoSPolicyId
 
    .PARAMETER ShowRest
    Return an object represents the REST operation including URI , Method and JSON
 
    .EXAMPLE
    New-XtremConsistencyGroup -ConsistencyGroupName group
  #>

    [CmdletBinding()]
    [Alias('xmsConsistencyGroupCreate')]
    Param (
        [Alias("Name", "Index")]
        [Parameter(Mandatory = $true, Position = 0)]
        [Argumentcompleter( { doComplete $args 'consistency-groups' name })]
        [ValidatePattern("^[^\[\]&\(\)`";,<>'/]+$")] 
        $ConsistencyGroupName,
        [Parameter()]
        [Argumentcompleter( { doComplete $args 'volumes' name })] 
        $VolList,
        [Parameter()]
        [array]$TagList,
        [Parameter()]
        [ValidateSet('enabled','disabled','monitor_only')]
        [String]$QoSEnabledMode,
        [Parameter()]
        [Argumentcompleter( { doComplete $args 'qos-policies' name })] 
        [String]$QoSPolicyId,
        [Parameter()]
        [switch]$ShowRest,
        [Parameter()]
        $XtremClusterName = (Get-XtremDefaultSession)._XtremClusterName,
        [Parameter()]
        [object]$Session =  (Get-XtremDefaultSession)
    )
    $Route = '/types/consistency-groups'
    $BodyList = @{ }
    $VolListStr = '[' + ($VolList -join ',') + ']'
    
    AddIfExists -name "cluster-id" -value $XtremClusterName -list $BodyList
    AddIfExists -name "consistency-group-name" -value $ConsistencyGroupName -list $BodyList
    AddIfExists -name "tag-list" -value $TagList -list $BodyList
    AddIfExists -name "vol-list" -value $VolListStr -list $BodyList
    AddIfExists -name "qos-enabled-mode" -value $QoSEnabledMode -list $BodyList
    AddIfExists -name "qos-policy-id" -value $QoSPolicyId -list $BodyList
    AddIfExists -name "created-by-external-client" -value 'PowerShell' -list $BodyList

    initCommand 

    $Body = BuildXtremJson -list $BodyList
    
    $result = NewXtremRequest -Method POST -endpoint $Route -Session $Session  -XtremClusterName $XtremClusterName -Body $Body -ObjectSelection $ObjectSelection -ShowRest:$ShowRest.IsPresent

    finalizeCommand
    return $result
}


Function Set-XtremConsistencyGroup {
    <#
    .DESCRIPTION
    Enables you to rename a Consistency Group.
 
    .PARAMETER ConsistencyGroupName
    Consistency Group's current name or index number
 
    .PARAMETER NewName
    Consistency Group's new name.
 
    .PARAMETER QoSEnabledMode
    QosEnabledMode
 
      .PARAMETER QoSPolicyId
    QoSPolicyId
 
    .PARAMETER ShowRest
    Return an object represents the REST operation including URI , Method and JSON
 
    .EXAMPLE
    Set-XtremConsistencyGroup -ConsistencyGroupName group -NewName group1
  #>
    
  [CmdletBinding()]
  [Alias('xmsConsistencyGroupModify')]
    Param (
        [Parameter()]
        $XtremClusterName = (Get-XtremDefaultSession)._XtremClusterName,
        [Alias("Name", "Index")]
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
        [Argumentcompleter( { doComplete $args 'consistency-groups' name })] 
        $ConsistencyGroupName,
        [Parameter()]
        [ValidatePattern("^[^\[\]&\(\)`";,<>'/]+$")] 
        [String]$NewName,
        [Parameter()]
        [ValidateSet('enabled','disabled','monitor_only')]
        [String]$QoSEnabledMode,
        [Parameter()]
        [String]$QoSPolicyId,
        [Parameter()]
        [object]$Session =  (Get-XtremDefaultSession),
        [Parameter()]
        [switch]$ShowRest
    )
    $Route = '/types/consistency-groups'
    $Route, $GetProperty = SetParametersForRequestCG $ConsistencyGroupName
    
    $BodyList = @{ }
    AddIfExists -name "cluster-id" -value $XtremClusterName -list $BodyList
    AddIfExists -name "new-name" -value $NewName -list $BodyList
    AddIfExists -name "qos-enabled-mode" -value $QoSEnabledMode -list $BodyList
    AddIfExists -name "qos-policy-id" -value $QoSPolicyId -list $BodyList

    initCommand 
    
    $Body = BuildXtremJson -list $BodyList
    
    $result = NewXtremRequest -Method PUT -endpoint $Route -Session $Session  -XtremClusterName $XtremClusterName -Body $Body -GetProperty $GetProperty -ShowRest:$ShowRest.IsPresent

    finalizeCommand
    return $result
}

Function SetParametersForRequestCG ($Name) {

    If ($Name.GetType().Name -eq "Int32" -or $Name.GetType().Name -eq "Object[]") {
        $Route = $Route + "/" + $Name
        return $Route, $null
    }

    $commandName =  ($((Get-PSCallStack)[1].Command))

    if ($commandName -match "Set-|Remove-"){
        $GetProperty = 'name=' + $Name
    }else{
        $GetProperty = 'filter=name:eq:' + $Name
    }

    return $Route, $GetProperty
}

######### CONSISTENCY GROUP VOLUMES COMMANDS #########

Function Get-XtremConsistencyGroupVolumes {
<#
    .DESCRIPTION
    Displays the list of Consistency Group Volumes.
 
    .PARAMETER Properties
    Array of properties requested from this call.
 
    .PARAMETER Filters
    Array of filters for this call.
 
    .PARAMETER ShowRest
    Return an object represents the REST operation including URI , Method and JSON
 
    .EXAMPLE
    Get-XtremConsistencyGroupVolumes
  #>

    [cmdletbinding()]
    [Alias('xmsConsistencyGroupListVolumesOfAllCGs')]
    Param (
        [parameter()]
        $XtremClusterName = (Get-XtremDefaultSession)._XtremClusterName,
        [parameter()]
        [Alias("Properties")]
        [Argumentcompleter( { doComplete $args 'consistency-groups' prop })] 
        [string[]]$Property = $CGVolsDefaultProps,
        [parameter()]
        [Alias("Filters")]
        [Argumentcompleter( { doComplete $args 'consistency-groups' filter })] 
        [string[]]$Filter,
        [Parameter()]
        [object]$Session = (Get-XtremDefaultSession),
        [Parameter()]
        [switch]$ShowRest,
        [Parameter()]
        [switch]$Full = $false
    )
    initCommand 
    
    $Route = '/types/consistency-group-volumes'

    if ($Full) { $Property = '' }
        
    $result = NewXtremRequest -Method GET -endpoint $Route -Session $Session  -XtremClusterName $XtremClusterName -Properties $Property -Filters $Filter -ObjectSelection $ObjectSelection -ShowRest:$ShowRest.IsPresent -Multi -Full:$Full.IsPresent

    $result = formatOutPut $Property $result
    
    finalizeCommand
    return $result
}

Function Get-XtremConsistencyGroupVolume {
<#
    .DESCRIPTION
    Displays the volumes of the selected Consistency Group.
 
    .PARAMETER ConsistencyGroupVolumeName
    Consistency Group name or index number
 
    .PARAMETER Properties
    Array of properties requested from this call.
 
    .PARAMETER ShowRest
    Return an object represents the REST operation including URI , Method and JSON
 
    .EXAMPLE
    Get-XtremConsistencyGroupVolume -ConsistencyGroupVolumeName group
#>

    [cmdletbinding()]
    [Alias('xmsConsistencyGroupListVolumes')]
    Param (
        [parameter()]
        $XtremClusterName = (Get-XtremDefaultSession)._XtremClusterName,
        [Alias("Name", "Index")]
        [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
        [Argumentcompleter( { doComplete $args 'consistency-groups' name })] 
        $ConsistencyGroupVolumeName,
        [Parameter()]
        [Alias("Properties")]
        [ValidateSet( 'name', 'index', 'guid')]
        [string[]]$Property = ( 'name', 'index', 'guid'),
        [Parameter()]
        [object]$Session =  (Get-XtremDefaultSession),
        [Parameter()]
        [switch]$ShowRest
    )
    initCommand 
    
    $Route = '/types/consistency-groups'
    $Route, $GetProperty = SetParametersForRequestCG $ConsistencyGroupVolumeName

    addGetPropertyIfExists 'prop' 'vol-list'
    
    $result = NewXtremRequest -Method GET -endpoint $Route -Session $Session -XtremClusterName $XtremClusterName -ObjectSelection $ObjectSelection -GetProperty $GetProperty -Properties $Property -ShowRest:$ShowRest.IsPresent

    if($result.'vol-list'){
        $result = $result.'vol-list' | foreach { $guid, $name, $index = $_; [pscustomobject]@{'name' = $name; 'index' = $index; 'guid' = $guid; } }
    }else{
        $result = 'No volumes in ' + $result.name
    }

    $result = formatOutPut $Property $result

    finalizeCommand
    return $result
}

Function New-XtremConsistencyGroupVolume {
    <#
    .DESCRIPTION
    Enables you to add a Volume to a Consistency Group.
 
    .PARAMETER ConsistencyGroupName
    Consistency Group's name or index number
 
    .PARAMETER VolId
    Volume's name or index number.
 
    .PARAMETER ShowRest
    Return an object represents the REST operation including URI , Method and JSON
 
    .EXAMPLE
     New-XtremConsistencyGroupVolume -ConsistencyGroupName group -VolId 2
  #>

    [CmdletBinding()]
    [Alias('xmsConsistencyGroupAddVolume')]
    Param (
        [Parameter()]
        $XtremClusterName = (Get-XtremDefaultSession)._XtremClusterName,
        [Alias("Name", "Index")]
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
        [Argumentcompleter( { doComplete $args 'consistency-groups' name })] 
        $ConsistencyGroupName,
        [Parameter()]
        [Argumentcompleter( { doComplete $args 'volumes' name })] 
        $VolId,
        [Parameter()]
        [object]$Session =  (Get-XtremDefaultSession),
        [Parameter()]
        [switch]$ShowRest
    )
    $Route = '/types/consistency-group-volumes'
    
    $BodyList = @{ }
    
    AddIfExists -name "cluster-id" -value $XtremClusterName -list $BodyList
    AddIfExists -name "cg-id" -value $ConsistencyGroupName -list $BodyList
    AddIfExists -name "vol-id" -value $VolId -list $BodyList

    initCommand 

    $Body = BuildXtremJson -list $BodyList
    
    $result = NewXtremRequest -Method POST -endpoint $Route -Session $Session  -XtremClusterName $XtremClusterName -Body $Body -ObjectSelection $ObjectSelection -ShowRest:$ShowRest.IsPresent

    finalizeCommand
    return $result
}


#Deletes an Consistency Group volume
Function Remove-XtremConsistencyGroupVolume {
    <#
    .DESCRIPTION
    Enables you to remove a Volume from a Consistency Group.
 
    .PARAMETER ConsistencyGroupName
    Consistency Group's name or index number
 
    .PARAMETER VolId
    Volume's name or index number.
 
    .PARAMETER ShowRest
    Return an object represents the REST operation including URI , Method and JSON
 
    .EXAMPLE
    Remove-XtremConsistencyGroupVolume -ConsistencyGroupName group -VolId vol1
  #>

    [cmdletbinding()]
    [Alias('xmsConsistencyGroupRemoveVolume')]
    Param (
        [parameter()]
        $XtremClusterName = (Get-XtremDefaultSession)._XtremClusterName,
        [Alias("Name", "Index")]
        [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
        [Argumentcompleter( { doComplete $args 'consistency-groups' name })] 
        $ConsistencyGroupName,
        [Parameter()]
        [Argumentcompleter( { doComplete $args 'volumes' name })] 
        $VolId,
        [Parameter()]
        [bool]$Confirm =  (Get-XtremDefaultSession)._XtremCLIMode,
        [Parameter()]
        [object]$Session =  (Get-XtremDefaultSession),
        [Parameter()]
        [switch]$ShowRest
    )
    $confirmed = IsConfirmed $Confirm
    if (!$confirmed){
        return
    }
    initCommand 
    $BodyList = @{ }
    
    $Route = '/types/consistency-group-volumes'
    $Route, $GetProperty = SetParametersForRequestCG $ConsistencyGroupName
    
    AddIfExists -name "cg-id" -value $ConsistencyGroupName -list $BodyList
    AddIfExists -name "vol-id" -value $VolId -list $BodyList
    
    $Body = BuildXtremJson -list $BodyList
    
    $result = NewXtremRequest -Method DELETE -endpoint $Route -Session $Session  -XtremClusterName $XtremClusterName -Body $Body -GetProperty $GetProperty -ShowRest:$ShowRest.IsPresent
    
    finalizeCommand
    return $result
}
    


Export-ModuleMember *-* -Alias *