Scripts/StorageDomain/New-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
function New-CohesityStorageDomain {
    <#
        .SYNOPSIS
        Request to create storage domain (view box) configuration with specified parameters.
        .DESCRIPTION
        The New-CohesityStorageDomain function is used to create storage domain (view box) using REST API with given parameters. If no parameters are specified, storage domain (view box) will be cretaed with default settings.
        .EXAMPLE
        New-CohesityStorageDomain -Name <string>
        Create storage domain (view box) with default settings.
        .EXAMPLE
        New-CohesityStorageDomain -Name <string> -PhysicalQuota <integer>
        Create storage domain (view box) with specific physical quota.
        .NOTES
        Mention PhysicalQuota value in GiB unit.
        .EXAMPLE
        New-CohesityStorageDomain -Name <string> -Deduplication <boolean> -InlineDeduplication <boolean> -Compression <boolean> -InlineCompression <boolean> -Encryption <boolean>
        Create storage domain (view box) with deduplication, Compression disabled and Encryption enabled. Based on enable/disable state of compression and encryption parameter, compression and encryption policy will be decided respectively.
    #>

    [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)][ValidateSet("true", "false")]
        [String]$Compression = $true,
        # 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)][ValidateSet("true", "false")]
        [String]$Deduplication = $true,
        # Specifies the encryption setting for the Storage Domain (View Box).
        # ‘kEncryptionNone’ (disabled case) indicates the data is not encrypted. ‘kEncryptionStrong’ (enabled case) indicates the data is encrypted.
        [Parameter(Mandatory = $false)][ValidateSet("true", "false")]
        [String]$Encryption = $false,
        # 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)][ValidateSet("true", "false")]
        [String]$InlineCompression = $true,
        # 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)][ValidateSet("true", "false")]
        [String]$InlineDeduplication = $true,
        [Parameter(Mandatory = $True)]
        [String]$Name,
        # 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)]
        [Int]$PhysicalQuota = $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 {
        # Check if the storage domain with specified name already exist
        $domainUrl = $server + '/irisservices/api/v1/public/viewBoxes'
        $headers = @{'Authorization' = 'Bearer ' + $token }

        $url = $domainUrl + '?names=' + $Name + '&allUnderHierarchy=true'
        $isDomainExist = Invoke-RestApi -Method 'Get' -Uri $url -Headers $headers

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

        # Get the cluster partion ID
        $clusterUrl = $server + '/irisservices/api/v1/public/clusterPartitions'
        $clusterPartition = Invoke-RestApi -Method 'Get' -Uri $clusterUrl -Headers $headers

        if ($clusterPartition) {
            $ClusterPartitionId = $clusterPartition[0].id
        }

        # If parameter is specified by user
        if ($Deduplication -eq $false) {
            $InlineDeduplication = $false
        }
        if ($Compression -eq $false) {
            $InlineCompression = $false
        }

        # Convert Physical quota value to bytes, if physical quota is specified by user
        $PhysicalQuotaObj = @{}
        $GibToBytes = (1024 * 1024 * 1024)

        if ($PhysicalQuota) {
            [Int64]$PhysicalQuota_bytes = ($PhysicalQuota * $GibToBytes)
            $PhysicalQuotaObj = @{hardLimitBytes = $PhysicalQuota_bytes}
        }

        $payload = @{
            name                    =       $Name
            clusterPartitionId      =       $ClusterPartitionId
            storagePolicy           =       @{
                                                deduplicationEnabled    =   [System.Convert]::ToBoolean($Deduplication)
                                                inlineDeduplicate       =   [System.Convert]::ToBoolean($InlineDeduplication)
                                                inlineCompress          =   [System.Convert]::ToBoolean($InlineCompression)
                                                compressionPolicy       =   $(if ($Compression -eq $true) {'kCompressionLow'} else {'kCompressionNone'})
                                                encryptionPolicy        =   $(if ($Encryption -eq $true) {'kEncryptionStrong'} else {'kEncryptionNone'})
                                            }
            physicalQuota           =       $PhysicalQuotaObj
            defaultViewQuotaPolicy  =       @{}
        }
        $payloadJson = $payload | ConvertTo-Json

        # Construct URL & header
        $StorageDomain = Invoke-RestApi -Method 'Post' -Uri $domainUrl -Headers $headers -Body $payloadJson

        if ($StorageDomain) {
            Write-Host "Created '$Name' Storage Domain Successfully." -ForegroundColor Green
            Get-CohesityStorageDomain -Names $Name
            $successMsg = "Created '$Name' Storage Domain Successfully `n $StorageDomain"
            CSLog -Message $successMsg
        } else {
            $errorMsg = "Failed to create storage domain '$Name'."
            Write-Error $errorMsg
            CSLog -Message $errorMsg
        }
    } # End of process
} # End of function