Scripts/StorageDomain/Set-CohesityStorageDomain.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
function Set-CohesityStorageDomain {
    <#
        .SYNOPSIS
        Request to update storage domain (view box) configuration with specified parameters.
        .DESCRIPTION
        The Set-CohesityStorageDomain function is used to update storage domain (view box) using REST API with given parameters and returns the updated storage domain (view box).
        If name of the storage domain (view box) that to be updated is not specified, then update all the storage domain (view box) with the given parameters.
        .EXAMPLE
        Set-CohesityStorageDomain -Name <string> -NewDomainName <string> -PhysicalQuota <integer>
        Update the specified storage domain (view box) with the user provided parameter values.
        .NOTES
        Mention PhysicalQuota value in GiB unit.
        .EXAMPLE
        Set-CohesityStorageDomain -Name <string> -Deduplication <boolean> -InlineDeduplication <boolean> -Compression <boolean> -InlineCompression <boolean>
        Update storage domain (view box) with user specified deduplication, Compression disabled. Based on enable/disable state of compression parameter, compression policy will be decided.
        .EXAMPLE
        Get-CohesityStorageDomain -Names <string> | Set-CohesityStorageDomain -PhysicalQuota <integer>
        Get the list of storage domain filtered out by storage domain name through pipeline. Update the physical quota for the fetched storage domain (view box).
        .EXAMPLE
        Get-CohesityStorageDomain | Set-CohesityStorageDomain -PhysicalQuota <integer>
        Update all the available storage domain (view box) with the specified parameter values.
        .EXAMPLE
        Set-CohesityStorageDomain -StorageDomain <object>
        Update the specified storage domain (view box) object.
    #>

    [CmdletBinding()]
    Param(
        # Determines whether the compression policy should be ‘kCompressionNone’ (disabled case) or ‘kCompressionLow’ (enabled case)
        # ‘kCompressionNone’ indicates that data is not compressed. ‘kCompressionLow’ indicates that data is compressed.
        [Parameter(Mandatory = $false, ParameterSetName = 'UpdateField')][ValidateSet("true", "false")]
        [String]$Compression,
        # If deduplication is enabled, the Cohesity Cluster eliminates duplicate blocks of repeating data stored on the Cluster,
        # thus reducing the amount of storage space needed to store data.
        [Parameter(Mandatory = $false, ParameterSetName = 'UpdateField')][ValidateSet("true", "false")]
        [String]$Deduplication,
        # Specifies if deduplication should occur inline (as the data is being written). This field is only relevant if deduplication is enabled.
        # If on, deduplication occurs as the Cluster saves blocks to the Partition. If off, deduplication occurs after the Cluster writes data to the Partition.
        [Parameter(Mandatory = $false, ParameterSetName = 'UpdateField')][ValidateSet("true", "false")]
        [String]$InlineDeduplication,
        # Specifies if compression should occur inline (as the data is being written). This field is only relevant if compression is enabled.
        # If on, compression occurs as the Cluster saves blocks to the Partition. If off, compression occurs after the Cluster writes data to the Partition.
        [Parameter(Mandatory = $false, ParameterSetName = 'UpdateField')][ValidateSet("true", "false")]
        [String]$InlineCompression,
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
        [string]$Name = $null,
        # New domain name to be update
        [Parameter(Mandatory = $false, ParameterSetName = 'UpdateField')]
        [string]$NewDomainName = $null,
        # Specifies an optional quota limit on the usage allowed for this resource. This limit is specified in GiB.
        # If no value is specified,there is no limit.
        [Parameter(Mandatory = $false, ParameterSetName = 'UpdateField')]
        [Int]$PhysicalQuota = $null,
        # List of storage domain (view box), returned from 'Get-CohesityStorageDomain' commandlet
        [Parameter(ValueFromPipeline = $true)]
        [object[]]$StorageDomain = $null
    )

    Begin {
        if (-not (Test-Path -Path "$HOME/.cohesity")) {
            throw "Failed to authenticate. Please connect to the Cohesity Cluster using 'Connect-CohesityCluster'"
        }
        $session = Get-Content -Path $HOME/.cohesity | ConvertFrom-Json

        $server = $session.ClusterUri

        $token = $session.Accesstoken.Accesstoken
    }

    Process {
        $payload = $null
        $domainUrl = $server + '/irisservices/api/v1/public/viewBoxes'
        $headers = @{'Authorization' = 'Bearer ' + $token }

        # Check if the storage domain with specified name already exist
        if ($NewDomainName) {
            $url = $domainUrl + '?names=' + $NewDomainName + '&allUnderHierarchy=true'
            $isDomainExist = Invoke-RestApi -Method 'Get' -Uri $url -Headers $headers

            if ($isDomainExist) {
                throw "Storage Domain with name '$NewDomainName' already exists."
            } elseif ($Global:CohesityAPIError.StatusCode -eq 'Unauthorized'){
                return
            }
        }

        if ($null -ne $StorageDomain) {
            $domainObj = $StorageDomain
            if ($Name) {
                $domainObj = $StorageDomain | Where-Object {$_.name -eq $Name}
            } else {
                $Name = $StorageDomain.name
            }
        }

        if (!$Name) {
            Write-Warning "Please provide atleast one Storage Domain (View Box) name to update."
            return
        }

        # Construct URL & header
        $StorageDomainUrl = $server + '/irisservices/api/v1/public/viewBoxes'

        if ($null -eq $StorageDomain) {
            $getUrl = $StorageDomainUrl + '?names=' + $Name + '&allUnderHierarchy=true'
            $domainObj = Invoke-RestApi -Method 'Get' -Uri $getUrl -Headers $headers

            if ($null -eq $domainObj) {
                if ($Global:CohesityAPIError) {
                    if ($Global:CohesityAPIError.StatusCode -eq 'NotFound') {
                        $errorMsg = "Storage domain (View Box) '$Name' doesn't exists."
                        Write-Warning $errorMsg
                    } else {
                        $errorMsg = "Failed to fetch Storage Domain (View Box) information with an error : " + $Global:CohesityAPIError
                    }
                } else {
                    $errorMsg = "Storage domain (View Box) '$Name' doesn't exist."
                    Write-Warning $errorMsg
                }
                CSLog -Message $errorMsg
            }
        }

        # Update the specified Storage domain
        if ($null -ne $domainObj) {
            $domainObj | ForEach-Object {
                $payload = $_
                $domainName = $payload.name

                # Update the payload with specified parameter values
                if ('UpdateField' -eq $PsCmdlet.ParameterSetName) {
                    $PsBoundParameters.keys | ForEach-Object {
                        switch ($_) {
                            'NewDomainName' {
                                $payload.name = $NewDomainName
                            }
                            'Deduplication' {
                                $payload.storagePolicy.deduplicationEnabled = [System.Convert]::ToBoolean($Deduplication)
                                if ($Deduplication -eq $false) { $payload.storagePolicy.inlineDeduplicate = [System.Convert]::ToBoolean($Deduplication) }
                            }
                            'InlineDeduplication' {
                                $payload.storagePolicy.inlineDeduplicate = [System.Convert]::ToBoolean($InlineDeduplicate)
                                if ($Deduplication -eq $false) { $payload.storagePolicy.inlineDeduplicate = [System.Convert]::ToBoolean($Deduplication) }
                            }
                            'Compression' {
                                $payload.storagePolicy.compressionPolicy = $(if ($Compression -eq $true) {'kCompressionLow'} else {'kCompressionNone'})
                                if ($Compression -eq $false) { $payload.storagePolicy.inlineCompress = [System.Convert]::ToBoolean($Compression) }
                            }
                            'InlineCompression' {
                                $payload.storagePolicy.inlineCompress = [System.Convert]::ToBoolean($InlineCompression)
                                if ($Compression -eq $false) { $payload.storagePolicy.inlineCompress = [System.Convert]::ToBoolean($Compression) }
                            }
                            'PhysicalQuota' {
                                $GibToBytes = (1024 * 1024 * 1024)
                                [Int64]$PhysicalQuota_bytes = ($PhysicalQuota * $GibToBytes)
                                if ($null -eq $payload.physicalQuota) {
                                    $payload | Add-Member -MemberType NoteProperty -Name physicalQuota -Value @{}
                                }
                                if ($null -ne $payload.physicalQuota.hardLimitBytes) {
                                    $payload.physicalQuota.hardLimitBytes = $PhysicalQuota_bytes
                                } else {
                                    $payload.physicalQuota | Add-Member -MemberType NoteProperty -Name hardLimitBytes -Value $PhysicalQuota_bytes
                                }
                            }
                        }
                    }
                }

                $StorageDomainId = $payload.id
                $payloadJson = $payload | ConvertTo-Json

                $updateUrl = $StorageDomainUrl + "/" + $StorageDomainId
                $StorageDomainObj = Invoke-RestApi -Method 'Put' -Uri $updateUrl -Headers $headers -Body $payloadJson

                if ($StorageDomainObj) {
                    Write-Host "Updated '$domainName' Storage Domain Successfully." -ForegroundColor Green
                    $StorageDomainObj
                }
            }
        }
    } # End of process
} # End of function