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
function Set-vRAReservationNetwork {
<#
    .SYNOPSIS
    Set vRA reservation network properties

    .DESCRIPTION
    Set vRA reservation network properties. This cmdlet can be used to set the Network Profile for a
    Network Path in a reservation.

    .PARAMETER Id
    The Id of the reservation

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

    .INPUTS
    System.String.

    .OUTPUTS
    System.Management.Automation.PSObject

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

#>

[CmdletBinding(SupportsShouldProcess,ConfirmImpact="High")][OutputType('System.Management.Automation.PSObject')]

    Param (

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

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

    [parameter(Mandatory=$false)]
    [ValidateNotNull()]
    [String]$NetworkProfile

    )
 
    begin {
    
    }
    
    process {

        try {

            # --- Get the reservation

            $URI = "/reservation-service/api/reservations/$($id)"

            $Reservation = Invoke-vRARestMethod -Method GET -URI $URI
            
            $ReservationTypeName = (Get-vRAReservationType -Id $Reservation.reservationTypeId).name

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

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

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

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

            $ReservationNetworkItems = $ReservationNetworks.value.items

            foreach ($ReservationNetworkItem in $ReservationNetworkItems) {

                $NetworkPathId = ($ReservationNetworkItem.values.entries | Where-Object {$_.key -eq "networkPath"}).value.id                 
               
                if ($NetworkPathId -eq $ReservationNetworkPathId) {

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

                        # --- Test to see if a network profile exists in the reservation
                        $ExistingNetworkProfile = $ReservationNetworkItem.values.entries | Where-Object {$_.key -eq "networkProfile"}

                        if ($ExistingNetworkProfile) {

                            if ($NetworkProfile -eq '') {

                                # --- Remove an existing network profile

                                Write-Verbose -Message "Removing Network Profile"

                                $ReservationNetworkItem.values.entries = @($ReservationNetworkItem.values.entries | Where-Object {$_.key -ne "networkProfile"})

                            }
                            else {

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

                                if ($Response.content.Count -eq 0) {

                                    throw "Could not find network profile with name $($NetworkProfile)"

                                }

                                # --- Handle updating an existing network profile

                                Write-Verbose -Message "Updating Network Profile: $($ExistingNetworkProfile.value.label) >> $($NetworkProfile)"

                                $ExistingNetworkProfile.value.id = $Response.content[0].id

                                $ExistingNetworkProfile.value.label = $Response.content[0].name

                            }

                        }
                        else {

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

                            if ($Response.content.Count -eq 0) {

                                throw "Could not find network profile with name $($NetworkProfile)"

                            }

                            # --- Handle adding a new network profile to an existing path

                            Write-Verbose -Message "Adding Network Profile: $($NetworkProfile)"

                            $NetworkProfileTemplate = @"

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

"@


                            $ReservationNetworkItem.values.entries += ($NetworkProfileTemplate | ConvertFrom-Json)

                        }

                    }

                }

            }

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

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

                # --- Run vRA REST Request
                $Response = Invoke-vRARestMethod -Method PUT -URI $URI -Body ($Reservation | ConvertTo-Json -Depth 500)

                Write-Verbose -Message "SUCCESS"

            }

        }
        catch [Exception]{

            throw

        }
    }
    end {
        
    }
}