Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Get-CVClientLicense/Revoke-CVClientLicense function #9

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 245 additions & 0 deletions Modules/Commvault.CommCell/Commvault.CommCell.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,251 @@ function Set-CVClient {
}
}

function Get-CVClientLicense {
<#
.SYNOPSIS
Method to get licenses for a client.

.DESCRIPTION
Method to get licenses for a client.

.PARAMETER Name
Get licenses on client specified by Name.

.PARAMETER Id
Get licenses on client specified by Id.

.EXAMPLE
Get-CVClientLicences -Name 'carbonwincs1'

.OUTPUTS
[PsCustomObject] containing details of the license and the plaform type

.NOTES
Author: Craig Tolley
Based on API Documentation at https://documentation.commvault.com/commvault/v11/article?p=47685.htm
#>
[CmdletBinding(DefaultParameterSetName = 'Default')]
CraigTolley marked this conversation as resolved.
Show resolved Hide resolved
[OutputType([PSCustomObject])]
param(
[Alias('Client')]
[Parameter(Mandatory = $False, ParameterSetName = 'ByName')]
CraigTolley marked this conversation as resolved.
Show resolved Hide resolved
[ValidateNotNullorEmpty()]
[String] $Name,

[Alias('ClientId')]
[Parameter(Mandatory = $False, ParameterSetName = 'ById')]
CraigTolley marked this conversation as resolved.
Show resolved Hide resolved
[ValidateNotNullorEmpty()]
[Int32] $Id
)

begin { Write-Debug -Message "$($MyInvocation.MyCommand): begin"

try {
$sessionObj = Get-CVSessionDetail $MyInvocation.MyCommand.Name
$endpointSave = $sessionObj.requestProps.endpoint

if ($PSCmdlet.ParameterSetName -eq 'ByName' -or
$PSCmdlet.ParameterSetName -eq 'ById' ) {
$foundClient = $False
}
else {
$foundClient = $null
}
CraigTolley marked this conversation as resolved.
Show resolved Hide resolved
}
catch {
throw $_
}
}

process { Write-Debug -Message "$($MyInvocation.MyCommand): process"

try {
$sessionObj.requestProps.endpoint = $endpointSave

if ($PSCmdlet.ParameterSetName -eq 'ByName') {
$Id = (Get-CVClient -Name $Name).clientId
}
CraigTolley marked this conversation as resolved.
Show resolved Hide resolved

$sessionObj.requestProps.endpoint = $sessionObj.requestProps.endpoint -creplace ('{clientId}', $Id)
$headerObj = Get-CVRESTHeader $sessionObj
CraigTolley marked this conversation as resolved.
Show resolved Hide resolved
$body = ''
$payload = @{ }
$payload.Add('headerObject', $headerObj)
$payload.Add('body', $body)
$validate = 'licensesInfo'

$response = Submit-CVRESTRequest $payload $validate

if ($response.IsValid) {
Write-Output $response.Content.licensesInfo
}
CraigTolley marked this conversation as resolved.
Show resolved Hide resolved

}
catch {
throw $_
}
}

end { Write-Debug -Message "$($MyInvocation.MyCommand): end"
}
}

function Revoke-CVClientLicense {
<#
.SYNOPSIS
Method to release licenses from a client.

.DESCRIPTION
Method to release licenses from a client.

.PARAMETER Name
Release licenses on client specified by Name.

.PARAMETER Id
Release licenses on client specified by Id.

.PARAMETER LicensesToRelease
Collection of licenses to release. Details of licenses and format can be found from running Get-CVClientLicense

.PARAMETER Force
Switch to Force override of default 'WhatIf' confirmation behavior.

.EXAMPLE
$licenses = Get-CVClientLicences -Name 'carbonwincs1'
Revoke-CvClientLicenses -Name 'carbonwincs1' -LicensesToRelease $licenses

.OUTPUTS
Outputs errorMessage and errorCode. Error code 0 is success.

.NOTES
Author: Craig Tolley
Based on API Documentation at https://documentation.commvault.com/commvault/v11/article?p=92013.htm
#>
Param (
CraigTolley marked this conversation as resolved.
Show resolved Hide resolved
[Alias('ClientName')]
CraigTolley marked this conversation as resolved.
Show resolved Hide resolved
[Parameter(Mandatory = $True, ParameterSetName = 'ByName')]
[ValidateNotNullorEmpty()]
[String] $Name,

[Alias('ClientId')]
[Parameter(Mandatory = $True, ParameterSetName = 'ById')]
[ValidateNotNullorEmpty()]
[Int32] $Id,

[Parameter(Mandatory = $True, ParameterSetName = 'ByName')]
[Parameter(Mandatory = $True, ParameterSetName = 'ById')]
$LicensesToRelease
CraigTolley marked this conversation as resolved.
Show resolved Hide resolved
)

begin { Write-Debug -Message "$($MyInvocation.MyCommand): begin"

try {
$sessionObj = Get-CVSessionDetail $MyInvocation.MyCommand.Name
$endpointSave = $sessionObj.requestProps.endpoint
}
catch {
throw $_
}
}

process { Write-Debug -Message "$($MyInvocation.MyCommand): process"

try {
$sessionObj.requestProps.endpoint = $endpointSave

if ($PSCmdlet.ParameterSetName -eq 'ById' ) {
$sessionObj.requestProps.endpoint = $sessionObj.requestProps.endpoint -creplace ('{clientId}', $Id)
}
else {
$clientObj = Get-CVClient -Name $Name
if ($null -eq $clientObj) {
Write-Information -InformationAction Continue -MessageData "INFO: $($MyInvocation.MyCommand): client not found having name [$Name]"
return
}
$Id = $clientObj.clientId
}

<#
{
"isClientLevelOperation": true,
"licensesInfo": [{
"platformType": 1,
"license": {
"appType": 33,
"licenseName": "Server File System - Windows File System"
}
}],
"clientEntity": {
"clientId": "client001"
}
}
#>
$body = @{}

$client = @{}
$client.Add('clientId', $Id)
$body.Add('clientEntity', $client)

$licensesinfos = @()
ForEach ($l in $LicensesToRelease) {
$licenseinfo = @{}
$licenseinfo.Add("platformType",$LicensesToRelease.platformType)

$license = @{}
$license.Add("appType",$l.license.appType)
$license.Add("licenseName",$l.license.licenseName)
$licenseinfo.Add("license",$license)

$licensesinfos += $licenseinfo
}

$body.Add("licensesInfo",$licensesinfos)
$body = ($body | ConvertTo-Json -Depth 10)

$headerObj = Get-CVRESTHeader $sessionObj
$payload = @{ }
$payload.Add('headerObject', $headerObj)
$payload.Add('body', $body)
$validate = 'errorCode'

if ($PSCmdlet.ParameterSetName -eq 'ById' ) {
if ($Force -or $PSCmdlet.ShouldProcess($Id)) {
$response = Submit-CVRESTRequest $payload $validate
}
else {
$response = Submit-CVRESTRequest $payload $validate -DryRun
}
}
else {
if ($Force -or $PSCmdlet.ShouldProcess($clientObj.clientName)) {
$response = Submit-CVRESTRequest $payload $validate
}
else {
$response = Submit-CVRESTRequest $payload $validate -DryRun
}
}

if ($response.IsValid) {
Write-Output $response.Content
}
else {
if ($PSCmdlet.ParameterSetName -eq 'ById' ) {
Write-Information -InformationAction Continue -MessageData "INFO: $($MyInvocation.MyCommand): release client license request failed for client id[$Id]"
}
else {
Write-Information -InformationAction Continue -MessageData "INFO: $($MyInvocation.MyCommand): release client license request failed for client name [$($clientObj.clientName)]"
}
}
}
catch {
throw $_
}
}

end { Write-Debug -Message "$($MyInvocation.MyCommand): end"
}
}

function GetClientProperties ([System.Object] $ClientObject) {

Expand Down
17 changes: 16 additions & 1 deletion Modules/Commvault.RESTSession/Commvault.RESTSession.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,6 @@ function GetAPIDetail ([String] $Request) {
Endpoint = 'Client'
Method = 'Get'
Body = ''

}

'GetClientProperties' = @{
Expand Down Expand Up @@ -600,6 +599,22 @@ function GetAPIDetail ([String] $Request) {

}

'Get-CVClientLicense' = @{

Description = 'Get client licenses from CommServe'
Endpoint = 'Client/{clientId}/License'
Method = 'Get'
Body = ''
}

'Revoke-CVClientLicense' = @{

Description = 'Release a license from a client'
Endpoint = 'Client/License/Release'
Method = 'Post'
Body = ''

}
'Add-CVClientGroup' = @{
CraigTolley marked this conversation as resolved.
Show resolved Hide resolved

Description = 'Add new client group on CommServe'
Expand Down