Public/Set-UnityLUN.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
Function Set-UnityLUN {

  <#
      .SYNOPSIS
      Modifies LUN parameters.
      .DESCRIPTION
      Modifies LUN parameters.
      You need to have an active session with the array.
      .NOTES
      Written by Erwan Quelin under MIT licence - https://github.com/equelin/Unity-Powershell/blob/master/LICENSE
      .LINK
      https://github.com/equelin/Unity-Powershell
      .PARAMETER Session
      Specify an UnitySession Object.
      .PARAMETER Confirm
      If the value is $true, indicates that the cmdlet asks for confirmation before running.
      If the value is $false, the cmdlet runs without asking for user confirmation.
      .PARAMETER WhatIf
      Indicate that the cmdlet is run only to display the changes that would be made and actually no objects are modified.
      .PARAMETER ID
      ID or Object.
      .PARAMETER Name
      New name of the LUN unique to the system.
      .PARAMETER Description
      New description of the LUN.
      .PARAMETER Size
      New LUN size. The size parameter can be greater than the current LUN size in this case the LUN is expanded.
      .PARAMETER fastVPParameters
      FAST VP settings for the storage resource
      .PARAMETER isCompressionEnabled
      Indicates whether to enable inline compression for the LUN. Default is True
      .PARAMETER host
      List of host to grant access to LUN.
      .PARAMETER accessMask
      Host access mask. Might be:
      - NoAccess: No access.
      - Production: Access to production LUNs only.
      - Snapshot: Access to LUN snapshots only.
      - Both: Access to both production LUNs and their snapshots.
      .PARAMETER append
      Add new host access to the existing configuration.
      .PARAMETER snapSchedule
      Snapshot schedule assigned to the storage resource
      .PARAMETER isSnapSchedulePaused
      Indicates whether the assigned snapshot schedule is paused.
      .EXAMPLE
      Set-UnityLUN -Name 'LUN01' -Description 'Modified description'

      Change the description of the LUN named LUN01
  #>


  [CmdletBinding(SupportsShouldProcess = $True,ConfirmImpact = 'High')]
  Param (
    [Parameter(Mandatory = $false,HelpMessage = 'EMC Unity Session')]
    $session = ($global:DefaultUnitySession | where-object {$_.IsConnected -eq $true}),
    [Parameter(Mandatory = $true,Position = 0,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True,HelpMessage = 'LUN ID or Object')]
    [String[]]$ID,
    [Parameter(Mandatory = $false,HelpMessage = 'New Name of the LUN')]
    [String]$Name,
    [Parameter(Mandatory = $false,HelpMessage = 'New LUN Description')]
    [String]$Description,

    # lunParameters
    [Parameter(Mandatory = $false,HelpMessage = 'New LUN Size in Bytes')]
    [uint64]$Size,
    [Parameter(Mandatory = $false,HelpMessage = 'FAST VP settings for the storage resource')]
    [TieringPolicyEnum]$fastVPParameters,
    [Parameter(Mandatory = $false,HelpMessage = 'Indicates whether to enable inline compression for the LUN. Default is True')]
    [bool]$isCompressionEnabled,

    # snapScheduleParameters
    [Parameter(Mandatory = $false,HelpMessage = 'ID of a protection schedule to apply to the storage resource')]
    [String]$snapSchedule,
    [Parameter(Mandatory = $false,HelpMessage = 'Is assigned snapshot schedule is paused ? (Default is false)')]
    [bool]$isSnapSchedulePaused = $false,

    # hostParameters
    [Parameter(Mandatory = $false,HelpMessage = 'Host to grant access to LUN')]
    [String[]]$host,
    [Parameter(Mandatory = $false,HelpMessage = 'Append Host access to existing configuration')]
    [Switch]$append,
    [Parameter(Mandatory = $false,HelpMessage = 'Host access mask')]
    [HostLUNAccessEnum]$accessMask = 'Production'
  )

  Begin {
    Write-Debug -Message "[$($MyInvocation.MyCommand)] Executing function"

    # Variables
    $URI = '/api/instances/storageResource/<id>/action/modifyLun'
    $Type = 'LUN'
    $TypeName = 'UnityLUN'
    $StatusCode = 204
  }

  Process {

    Foreach ($sess in $session) {

      Write-Debug -Message "Processing Session: $($sess.Server) with SessionId: $($sess.SessionId)"

      If ($Sess.TestConnection()) {

        Foreach ($i in $ID) {

          Switch ($i.GetType().Name)
          {
            "String" {
              $Object = get-UnityLUN -Session $Sess -ID $i
              $ObjectID = $Object.id
              If ($Object.Name) {
                $ObjectName = $Object.Name
              } else {
                $ObjectName = $ObjectID
              }
            }
            "$TypeName" {
              Write-Verbose "Input object type is $($i.GetType().Name)"
              $ObjectID = $i.id
              If ($Object = Get-UnityLUN -Session $Sess -ID $ObjectID) {
                If ($Object.Name) {
                  $ObjectName = $Object.Name
                } else {
                  $ObjectName = $ObjectID
                }          
              }
            }
          }

          If ($ObjectID) {

            $UnitystorageResource = Get-UnitystorageResource -Session $sess | Where-Object {($_.Name -like $ObjectName) -and ($_.luns.id -like $ObjectID)}

            # Creation of the body hash
            $body = @{}

            # Name parameter
            If ($PSBoundParameters.ContainsKey('Name')) {
              $body["name"] = $Name
            }

            # Description parameter
            If ($PSBoundParameters.ContainsKey('description')) {
              $body["description"] = $Description
            }

            # lunParameters parameter
            If (($PSBoundParameters.ContainsKey('size')) -or ($PSBoundParameters.ContainsKey('host')) -or ($PSBoundParameters.ContainsKey('fastVPParameters')) -or ($PSBoundParameters.ContainsKey('isCompressionEnabled'))) {
              $body["lunParameters"] = @{}
              $lunParameters = @{}
            
              If ($PSBoundParameters.ContainsKey('Size')) {
                $lunParameters["size"] = $($Size)
              }

              If ($PSBoundParameters.ContainsKey('fastVPParameters')) {
                $lunParameters["fastVPParameters"] = @{}
                $fastVPParam = @{}
                $fastVPParam['tieringPolicy'] = $fastVPParameters
                $lunParameters["fastVPParameters"] = $fastVPParam
              }

              If ($PSBoundParameters.ContainsKey('isCompressionEnabled')) {
                $lunParameters["isCompressionEnabled"] = $isCompressionEnabled
              }

              If ($PSBoundParameters.ContainsKey('host')) {
              
                $lunParameters["hostAccess"] = @()
                $hostAccess = @()

                foreach ($h in $host) {
                  $blockHostAccessParam = @{}
                    $blockHostAccessParam['host'] = @{}
                      $HostParam = @{}
                      $HostParam['id'] = $h
                    $blockHostAccessParam['host'] = $HostParam
                    $blockHostAccessParam['accessMask'] = $accessMask
                  $hostAccess += $blockHostAccessParam
                }

                If ($PSBoundParameters.ContainsKey('append')) {

                  foreach ($h in $Object.hostAccess) {

                    if (-not ($hostAccess.host.id -contains $h.host.id)) {
                      $blockHostAccessParam = @{}
                      $blockHostAccessParam['host'] = @{}
                        $HostParam = @{}
                        $HostParam['id'] = $h.host.id
                      $blockHostAccessParam['host'] = $HostParam
                      $blockHostAccessParam['accessMask'] = $h.accessMask
                      $hostAccess += $blockHostAccessParam
                    }
                  }
                } else {
                  Write-Warning -Message 'The existing host access parameters will be overwritten by the new settings. It could result to data unavailibility. Use the -Append parameter to add the new settings to the existing configuration.'
                }

                $lunParameters["hostAccess"] = $hostAccess
          
              }
            
              $body["lunParameters"] = $lunParameters
      
            }

            #snapScheduleParameters
            If ($snapSchedule) {
              $body["snapScheduleParameters"] = @{}
              $snapScheduleParameters = @{}
              $snapScheduleParam = @{}
              $snapScheduleParam["id"] ="$($snapSchedule)"
              $snapScheduleParameters["snapSchedule"] = $snapScheduleParam
              $snapScheduleParameters["isSnapSchedulePaused"]= "$($isSnapSchedulePaused)"
              $body["snapScheduleParameters"] = $snapScheduleParameters
            }

            ####### END BODY - Do not edit beyond this line

            #Show $body in verbose message
            $Json = $body | ConvertTo-Json -Depth 10
            Write-Verbose $Json 

            #Building the URL
            $FinalURI = $URI -replace '<id>',$UnitystorageResource.id

            $URL = 'https://'+$sess.Server+$FinalURI
            Write-Verbose "URL: $URL"

            #Sending the request
            If ($pscmdlet.ShouldProcess($Sess.Name,"Modify $Type $ObjectName")) {
              $request = Send-UnityRequest -uri $URL -Session $Sess -Method 'POST' -Body $Body
            }
            
            If ($request.StatusCode -eq $StatusCode) {

              Write-Verbose "$Type with ID $ObjectID has been modified"

              Get-UnityLUN -Session $Sess -ID $ObjectID

            }  # End If ($request.StatusCode -eq $StatusCode)
          } else {
            Write-Warning -Message "$Type with ID $i does not exist on the array $($sess.Name)"
          } # End If ($ObjectID)
        } # End Foreach ($i in $ID)
      } # End If ($Sess.TestConnection())
    } # End Foreach ($sess in $session)
  } # End Process
} # End Function