Public/logical-ports.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#
# Copyright 2020, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#

function Add-NSXTLogicalPorts {

    <#
        .SYNOPSIS
        Add a Logical Port

        .DESCRIPTION
        Add a Logical Port

        .EXAMPLE
        Get-NSXTLogicalSwitches -display_name MyLogicalSwitch | Add-NSXTLogicalPorts -display_name MyLogicalPort

        Add a Logical Port type MyLogicalPort with admin state UP (default) on MyLogicalSwitch with generate attachement id

        .EXAMPLE
        Get-NSXTLogicalSwitches -display_name MyLogicalSwitch | Add-NSXTLogicalPorts -display_name MyLogicalPort -admin_state DOWN

        Add a LogicalPorts type MyLogicalPort with admin state DOWN on MyLogicalSwitch with generate attachement id

        .EXAMPLE
        Get-NSXTLogicalSwitches -display_name MyLogicalSwitch | Add-NSXTLogicalPorts -display_name MyLogicalPort -admin_state UP -attachement_id 0d6560fc-8b51-40fb-b6b1-588a0cea8f22

        Add a Logical Port type MyLogicalPort with admin state UP on MyLogicalSwitch with attachement id 0d6560fc-8b51-40fb-b6b1-588a0cea8f22

        .EXAMPLE
        Get-NSXTLogicalSwitches -display_name MyLogicalSwitch | Add-NSXTLogicalPorts -display_name MyLogicalPort -admin_state UP -attachement_id 0d6560fc-8b51-40fb-b6b1-588a0cea8f22 -description "Add by psNSXT"

        Add a Logical Port type MyLogicalPort with admin state UP on MyLogicalSwitch with attachement id 0d6560fc-8b51-40fb-b6b1-588a0cea8f22 with a description

        .EXAMPLE
        Get-NSXTLogicalSwitches -display_name MyLogicalSwitch | Add-NSXTLogicalPorts -display_name MyLogicalPort -admin_state UP -attachement_id 0d6560fc-8b51-40fb-b6b1-588a0cea8f22 -init_state UNBLOCKED_VLAN

        Add a Logical Port type MyLogicalPort with admin state UP on MyLogicalSwitch with attachement id 0d6560fc-8b51-40fb-b6b1-588a0cea8f22 and init state set to Unblocked vlan
    #>


    Param(
        [Parameter(Mandatory = $false)]
        [ValidateSet("UP", "DOWN", IgnoreCase = $false)]
        [string]$admin_state = "UP",
        [Parameter(Mandatory = $false)]
        [guid]$attachement_id,
        [Parameter(Mandatory = $true)]
        [string]$display_name,
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [ValidateScript( { Confirm-NSXTLogicalSwitches $_ })]
        [psobject]$logical_switch_id,
        [Parameter(Mandatory = $false)]
        [ValidateSet("UNBLOCKED_VLAN", IgnoreCase = $false)]
        [string]$init_state,
        [Parameter(Mandatory = $false)]
        [string]$description,
        [Parameter(Mandatory = $false)]
        [psobject]$connection = $DefaultNSXTConnection
    )

    Begin {
    }

    Process {

        $uri = 'api/v1/logical-ports'

        $_lg = new-Object -TypeName PSObject

        $_lg | add-member -name "admin_state" -membertype NoteProperty -Value $admin_state

        $_lg | add-member -name "display_name" -membertype NoteProperty -Value $display_name

        $attachment = new-Object -TypeName PSObject
        $attachment | Add-member -name "attachment_type" -membertype NoteProperty -Value "VIF"
        if ( -not $PsBoundParameters.ContainsKey('attachement_id') ) {
            $attachement_id = (New-Guid).guid
        }
        $attachment | Add-member -name "id" -membertype NoteProperty -Value $attachement_id

        $_lg | add-member -name "attachment" -membertype NoteProperty -Value $attachment

        $_lg | add-member -name "logical_switch_id" -membertype NoteProperty -Value $logical_switch_id.id

        if ( $PsBoundParameters.ContainsKey('init_state') ) {
            $_lg | add-member -name "init_state" -membertype NoteProperty -Value $init_state
        }

        if ( $PsBoundParameters.ContainsKey('description') ) {
            $_lg | add-member -name "description" -membertype NoteProperty -Value $description
        }

        $response = Invoke-NSXTRestMethod -uri $uri -method 'POST' -body $_lg -connection $connection
        $response

    }

    End {
    }
}

function Get-NSXTLogicalPorts {

    <#
        .SYNOPSIS
        Get information about Logical Ports

        .DESCRIPTION
        Returns information about Logical Ports

        .EXAMPLE
        Get-NSXTLogicalPorts

        Display all Logical Ports

        .EXAMPLE
        Get-NSXTLogicalPorts -id dd604e91-71df-40e3-9d7c-ac4a124e8497

        Display Logical Ports with id dd604e91-71df-40e3-9d7c-ac4a124e8497

        .EXAMPLE
        Get-NSXTLogicalPorts -switching_profile_id ede3e69a-6562-49a6-98c4-d23408bd832c

        Display Logical Ports with switching_profile_id ede3e69a-6562-49a6-98c4-d23408bd832c

        .EXAMPLE
        Get-NSXTLogicalPorts -display_name MyLogicalPorts

        Display Logical Ports with (display) name MyLogicalPorts

        .EXAMPLE
        Get-NSXTLogicalPorts -attachment_id 2c05288a-5ffb-4e52-bace-12960fc5baf9

        Display Logical Ports with attachment_id 2c05288a-5ffb-4e52-bace-12960fc5baf9

        .EXAMPLE
        Get-NSXTLogicalPorts -attachment_type VIF

        Display Logical Ports with attachment_type VIF

        .EXAMPLE
        Get-NSXTLogicalPorts -transport_zone_id ede3e69a-6562-49a6-98c4-d23408bd832c

        Display Logical Ports with transport_zone_id ede3e69a-6562-49a6-98c4-d23408bd832c
    #>


    [CmdletBinding(DefaultParametersetname = "Default")]
    Param(
        [Parameter(Mandatory = $false, ParameterSetName = "id")]
        [string]$id,
        [Parameter(Mandatory = $false, ParameterSetName = "Default")]
        [string]$switching_profile_id,
        [Parameter(Mandatory = $false, ParameterSetName = "display_name")]
        [string]$display_name,
        [Parameter(Mandatory = $false, ParameterSetName = "Default")]
        [string]$transport_zone_id,
        [Parameter(Mandatory = $false, ParameterSetName = "Default")]
        [string]$attachment_type,
        [Parameter(Mandatory = $false, ParameterSetName = "Default")]
        [string]$attachment_id,
        [Parameter(Mandatory = $false)]
        [psobject]$connection = $DefaultNSXTConnection
    )

    Begin {
    }

    Process {

        $uri = 'api/v1/logical-ports'

        if ( $PsBoundParameters.ContainsKey('id') ) {
            $uri += "/$id"
        }

        $uri += "?"

        if ( $PsBoundParameters.ContainsKey('switching_profile_id') ) {
            $uri += "&switching_profile_id=$switching_profile_id"
        }

        if ( $PsBoundParameters.ContainsKey('transport_zone_id') ) {
            $uri += "&transport_zone_id=$transport_zone_id"
        }

        if ( $PsBoundParameters.ContainsKey('attachment_type') ) {
            $uri += "&attachment_type=$attachment_type"
        }

        if ( $PsBoundParameters.ContainsKey('attachment_id') ) {
            $uri += "&attachment_id=$attachment_id"
        }

        $response = Invoke-NSXTRestMethod -uri $uri -method 'GET' -connection $connection

        switch ( $PSCmdlet.ParameterSetName ) {
            "id" {
                #When there is a id, it is directly the result...
                $response
            }
            "display_name" {
                #When there is a display_name, search on response
                $response.results | Where-Object { $_.display_name -eq $display_name }
            }
            default {
                $response.results
            }
        }

    }

    End {
    }
}

function Set-NSXTLogicalPorts {

    <#
        .SYNOPSIS
        Change settings of a Logical Ports

        .DESCRIPTION
        Change settings (description, admin state) of a Logical Ports

        .EXAMPLE
        Get-NSXTLogicalPorts -id ff8140b0-010e-4e92-aa62-a55766f17da0 | Set-NSXTLogicalPorts -description "Modified by psNSXT"

        Change description of Logical Ports ff8140b0-010e-4e92-aa62-a55766f17da0

        .EXAMPLE
        Get-NSXTLogicalPorts -display_name MyLogicalPort | Set-NSXTLogicalPorts -admin_state "Down"

        Change admin state to Down of Logical Ports MyLogicalPort

    #>


    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'medium')]
    Param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [ValidateScript( { Confirm-NSXTLogicalPorts $_ })]
        [psobject]$lp,
        [Parameter(Mandatory = $false)]
        [string]$description,
        [Parameter(Mandatory = $false)]
        [ValidateSet("UP", "DOWN", IgnoreCase = $false)]
        [string]$admin_state,
        [Parameter(Mandatory = $false)]
        [psobject]$connection = $DefaultNSXTConnection
    )

    Begin {
    }

    Process {

        $lp_id = $lp.id
        $lp_name = "(" + $lp.display_name + ")"

        $uri = "api/v1/logical-ports/$lp_id"

        if ( $PsBoundParameters.ContainsKey('admin_state') ) {
            $lp.admin_state = $admin_state
        }

        if ( $PsBoundParameters.ContainsKey('description') ) {
            if ($null -eq $lp.description) {
                $lp | Add-member -name "description" -membertype NoteProperty -Value ""
            }
            $lp.description = $description
        }

        if ($PSCmdlet.ShouldProcess("$lp_id $lp_name", 'Configure Logical Ports')) {
            $response = Invoke-NSXTRestMethod -uri $uri -method 'PUT' -body $lp -connection $connection
            $response.results
        }

        #Display update Logical Ports
        Get-NSXTLogicalPorts -id $lp_id -connection $connection
    }

    End {
    }
}

function Remove-NSXTLogicalPorts {

    <#
        .SYNOPSIS
        Remove a Logical Port

        .DESCRIPTION
        Remove a Logical Port

        .EXAMPLE
        $lp = Get-NSXTLogicalPorts -id ff8140b0-010e-4e92-aa62-a55766f17da0
        PS C:\>$lp | Remove-NSXTLogicalPorts

        Remove Logical Port with id ff8140b0-010e-4e92-aa62-a55766f17da0

        .EXAMPLE
        Remove-NSXTLogicalPorts -id ff8140b0-010e-4e92-aa62-a55766f17da0 -confirm:$false

        Remove Logical Port with id ff8140b0-010e-4e92-aa62-a55766f17da0 with no confirmation

        .EXAMPLE
        $lp = Get-NSXTLogicalPorts -id ff8140b0-010e-4e92-aa62-a55766f17da0
        PS C:\>$lp | Remove-NSXTLogicalPorts -force

        Force remove Logical Port with id ff8140b0-010e-4e92-aa62-a55766f17da0
    #>


    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'high')]
    Param(
        [Parameter (Mandatory = $true, ParameterSetName = "id")]
        [string]$id,
        [Parameter (Mandatory = $true, ValueFromPipeline = $true, Position = 1, ParameterSetName = "lp")]
        [ValidateScript( { Confirm-NSXTLogicalPorts $_ })]
        [psobject]$lp,
        [Parameter (Mandatory = $false)]
        [switch]$force,
        [Parameter(Mandatory = $false)]
        [psobject]$connection = $DefaultNSXTConnection
    )

    Begin {
    }

    Process {

        #get lp id from lp object
        if ($lp) {
            $id = $lp.id
            $name = "(" + $lp.display_name + ")"
        }

        $uri = "api/v1/logical-ports/" + $id + "?"

        if ( $PsBoundParameters.ContainsKey('force') ) {
            $uri += "&detach=true"
        }

        if ($PSCmdlet.ShouldProcess("LP", "Remove Logical Port ${id} ${name}")) {
            Invoke-NSXTRestMethod -method "DELETE" -uri $uri -connection $connection
        }
    }

    End {
    }
}