Functions/Public/Set-vRAReservationNetwork.psm1

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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
function Set-vRAReservationNetwork {
<#
    .SYNOPSIS
    Set vRA reservation network properties

    .DESCRIPTION
    Set vRA reservation network properties.

    This function enables you to:

    - Add a new network path to a reservation
    - Add a new network path to a reservation and assign a network profile
    - Update the network profile of an existing network path

    If the network path you supply is already selected in the reservation and no network profile is supplied, no action will be taken.

    .PARAMETER Id
    The Id of the reservation

    .PARAMETER NetworkPath
    The network path
    
    .PARAMETER NetworkProfile
    The network profile

    .INPUTS
    System.String

    .OUTPUTS
    None

    .EXAMPLE
    Get-vRAReservation -Name "Reservation01" | Set-vRAReservationNetwork -NetworkPath "VM Network" -NetworkProfile "Test Profile 1"

    .EXAMPLE
    Get-vRAReservation -Name "Reservation01" | Set-vRAReservationNetwork -NetworkPath "VM Network" -NetworkProfile "Test Profile 2"

    .EXAMPLE
    Get-vRAReservation -Name "Reservation01" | Set-vRAReservationNetwork -NetworkPath "Test Network"

#>

[CmdletBinding(SupportsShouldProcess,ConfirmImpact="High")][OutputType()]

    Param (

        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$Id,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$NetworkPath,

        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [String]$NetworkProfile

    )
 
    begin {
    
    }
    
    process {

        try {

            # --- Get the reservation

            $Reservation = Invoke-vRARestMethod -Method GET -URI "/reservation-service/api/reservations/$($id)" -Verbose:$VerbosePreference
            
            $ReservationTypeName = (Get-vRAReservationType -Id $Reservation.reservationTypeId -Verbose:$VerbosePreference).name

            $ComputeResourceId = ($Reservation.extensionData.entries | Where-Object {$_.key -eq "computeResource"}).value.id                         

            # ---
            # --- Set Network Properties
            # ---

            $NetworkPathId = ((Get-vRAReservationComputeResourceNetwork -Type $ReservationTypeName -ComputeResourceId $ComputeResourceId -Name $NetworkPath -Verbose:$VerbosePreference).values.entries | Where-Object {$_.key -eq "networkPath"}).value.id

            # --- Check to see if the provided network path is available for the reservation
            If(!$NetworkPathId) {

                throw "Could not find network path $($NetworkPath) in Compute Resource $($ComputeResourceId)"

            }

            # --- Get a list of networks currently selected by the reservation
            $SelectedReservationNetworks = ($Reservation.extensionData.entries | Where-Object {$_.key -eq "reservationNetworks"}).value.items

            # --- Check to see if the provided networkpath is currently selected in the reservation
            $ExistingReservationNetwork = getNetworkByPath $SelectedReservationNetworks $NetworkPath

            if ($ExistingReservationNetwork) {

                # --- If the network path exists and network profile is passed update otherwise exit with nothing to do
                if ($PSBoundParameters.ContainsKey("NetworkProfile")) {

                    Write-Verbose -Message "Network path exists in reservation and Network Profile has been specified"
                
                    $NetworkProfileObject = getNetworkProfileByName $NetworkProfile

                    # --- Check to see if the network path already has a profile assigned, if one exists, update it, if not add it
                    $ExistingReservationNetworkProfile = $ExistingReservationNetwork | Where-Object{$_.key -eq "networkProfile"}

                    if ($ExistingReservationNetworkProfile) {

                        Write-Verbose -Message "Updating existing Network Profile"

                        $ExistingReservationNetworkProfile.value.id = $NetworkProfileObject.id
                        $ExistingReservationNetworkProfile.value.label = $NetworkProfileObject.name

                    } else {

                        Write-Verbose -Message "Adding new Network Profile"

                        $ReservationNetworkProfileTemplate = @"

                            {
                                "key": "networkProfile",
                                "value": {
                                    "type": "entityRef",
                                    "componentId": null,
                                    "classId": "networkProfile",
                                    "id": "$($NetworkProfileObject.id)",
                                    "label": "$($NetworkProfileObject.name)"
                                }
                            }

"@


                            $ExistingReservationNetwork.values.entries += ($ReservationNetworkProfileTemplate | ConvertFrom-Json)

                    }

                } else {

                    # --- It would be nice to exit cleanly here
                    Write-Verbose -Message "Network path exists in reservation but no Network profile has been specified"
                    Write-Verbose -Message "Exiting gracefully"
                    return

                }

            } else {

                # --- If the network path doesn't exist add it and also a network profile if passed

                Write-Verbose -Message "Adding new Network Path to reservation"

                $ReservationNetworkTemplate = @"

                    {
                        "type": "complex",
                        "componentTypeId": "com.vmware.csp.iaas.blueprint.service",
                        "componentId": null,
                        "classId": "Infrastructure.Reservation.Network",
                        "typeFilter": null,
                        "values": {
                            "entries": [
                                {
                                    "key": "networkPath",
                                    "value": {
                                        "type": "entityRef",
                                        "componentId": null,
                                        "classId": "Network",
                                        "id": "$($NetworkPathId)",
                                        "label": "$($NetworkPath)"
                                    }
                                }
                            ]
                        }
                    }

"@


                $ReservationNetworkObject = $ReservationNetworkTemplate | ConvertFrom-Json

                if ($PSBoundParameters.ContainsKey("NetworkProfile")) {

                    Write-Verbose -Message "Assigning a Network Profile to new Network Path"

                    $NetworkProfileObject = getNetworkProfileByName $NetworkProfile

                    $ReservationNetworkProfileTemplate = @"

                        {
                            "key": "networkProfile",
                            "value": {
                                "type": "entityRef",
                                "componentId": null,
                                "classId": "networkProfile",
                                "id": "$($NetworkProfileObject.id)",
                                "label": "$($NetworkProfileObject.name)"
                            }
                        }

"@


                        $ReservationNetworkObject.values.entries += $ReservationNetworkProfileTemplate | ConvertFrom-Json

                }

                $ReservationNetworks = $Reservation.extensionData.entries | Where-Object {$_.key -eq "reservationNetworks"}

                $ReservationNetworks.value.items += $ReservationNetworkObject

            }

            if ($PSCmdlet.ShouldProcess($Id)){

                $URI = "/reservation-service/api/reservations/$($Id)"
                
                Write-Verbose -Message "Preparing PUT to $($URI)"  

                # --- Run vRA REST Request
                Invoke-vRARestMethod -Method PUT -URI $URI -Body ($Reservation | ConvertTo-Json -Depth 100) -Verbose:$VerbosePreference | Out-Null

            }

        }
        catch [Exception]{

            throw

        }
    }
    end {
        
    }
}

function getNetworkProfileByName($Network) {

<#

    Internal helper fucntion to retrieve a network profile by it's name

#>


    $Response = (Invoke-vRARestMethod -Method GET -URI "/iaas-proxy-provider/api/network/profiles?`$filter=name%20eq%20'$($NetworkProfile)'").content

    if (!$Response) {

        throw "Could not find Network Profile with name $($NetworkProfile)"

    }

    return $Response

}

function getNetworkByPath ($ReservationNetworks, $NetworkPath) {

<#

    Internal helper function to retrieve an existing network path

#>


    foreach ($ReservationNetwork in $ReservationNetworks) {

        $ExistingNetworkPath = $ReservationNetwork.values.entries | Where-Object  {$_.key -eq "networkPath"} 

        if ($ExistingNetworkPath.value.label -eq $NetworkPath) {

            return $ReservationNetwork

        }
        
    }

}