Public/transport-zones.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
351
352
353
354
355
356
357
#
# Copyright 2019, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#

function Add-NSXTTransportZones {

    <#
        .SYNOPSIS
        Add a Transport Zones

        .DESCRIPTION
        Add a Transport Zones (Vlan or Overlay)

        .EXAMPLE
        Add-NSXTTransportZones -transport_type VLAN -host_switch_name NVDS-psNSXT -display_name MyTZ-Vlan -description "Add via psNSXT"

        Add a TransportZones type Vlan with NVDS (host_switch_name) NVDS-psNSXT named myTZ-Vlan with an description

        .EXAMPLE
        Add-NSXTTransportZones -transport_type OVERLAY -host_switch_name NVDS-psNSXT -display_name MyTZ-Overlay -description "Add via psNSXT"

        Add a TransportZones type Overlay with NVDS (host_switch_name) NVDS-psNSXT named myTZ-Overlay with an description

        .EXAMPLE
        Add-NSXTTransportZones -transport_type OVERLAY -host_switch_name NVDS-psNSXT -host_switch_mode ENS

        Add a TransportZones type Overlay with NVDS (host_switch_name) NVDS-psNSXT using Enhanced Datapath

        .EXAMPLE
        Add-NSXTTransportZones -transport_type OVERLAY -host_switch_name NVDS-psNSXT -nested_nsx -is_default

        Add a TransportZones type Overlay with NVDS (host_switch_name) NVDS-psNSXT and enable NSX nested and default Transport Zone
    #>


    Param(
        [Parameter(Mandatory = $false)]
        [string]$display_name,
        [Parameter(Mandatory = $true)]
        [string]$host_switch_name,
        [Parameter(Mandatory = $false)]
        [string]$description,
        [Parameter(Mandatory = $true)]
        [ValidateSet("VLAN", "OVERLAY", IgnoreCase = $false)]
        [string]$transport_type,
        [Parameter(Mandatory = $false)]
        [ValidateSet("ENS", "STANDARD", IgnoreCase = $false)]
        [string]$host_switch_mode,
        [Parameter(Mandatory = $false)]
        [switch]$nested_nsx,
        [Parameter(Mandatory = $false)]
        [switch]$is_default,
        [Parameter(Mandatory = $false)]
        [psobject]$connection = $DefaultNSXTConnection
    )

    Begin {
    }

    Process {

        $uri = 'api/v1/transport-zones'

        $_tz = new-Object -TypeName PSObject

        $_tz | add-member -name "host_switch_name" -membertype NoteProperty -Value $host_switch_name

        $_tz | add-member -name "transport_type" -membertype NoteProperty -Value $transport_type

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

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

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

        if ( $PsBoundParameters.ContainsKey('nested_nsx') ) {
            if ( $nested_nsx ) {
                $_tz | Add-Member -name "nested_nsx" -membertype NoteProperty -Value $True
            }
            else {
                $_tz | Add-Member -name "nested_nsx" -membertype NoteProperty -Value $false
            }
        }

        if ( $PsBoundParameters.ContainsKey('is_default') ) {
            if ( $is_default ) {
                $_tz | Add-Member -name "is_default" -membertype NoteProperty -Value $True
            }
            else {
                $_tz | Add-Member -name "is_default" -membertype NoteProperty -Value $false
            }
        }

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

    }

    End {
    }
}

function Get-NSXTTransportZones {

    <#
        .SYNOPSIS
        Get information about Transport Zones

        .DESCRIPTION
        Returns information about Transport Zones

        .EXAMPLE
        Get-NSXTTransportZones

        Display all Transport Zones

        .EXAMPLE
        Get-NSXTTransportZones -zone_id ede3e69a-6562-49a6-98c4-d23408bd832c

        Display Transport Zones with (zone) id ede3e69a-6562-49a6-98c4-d23408bd832c

        .EXAMPLE
        Get-NSXTTransportZones -display_name TZ-VLAN

        Display Transport Zones with (display) name TZ-VLAN

        .EXAMPLE
        Get-NSXTTransportZones -host_switch_name NVDS-psNSXT

        Display Transport Zones with host_switch_name (N-VDS) NVDS-psNSXT

        .EXAMPLE
        Get-NSXTTransportZones -zone_id ede3e69a-6562-49a6-98c4-d23408bd832c -summary

        Display Transport Zones Summary (Number of Transport Nodes, Logical Switches, Logical Ports) with (zone) id ede3e69a-6562-49a6-98c4-d23408bd832c

        .EXAMPLE
        Get-NSXTTransportZones -status

        Display Transport Zones Status (Up, Down, degraded, Unknown)
    #>


    [CmdletBinding(DefaultParametersetname = "Default")]
    Param(
        [Parameter(Mandatory = $false, ParameterSetName = "zone_id")]
        [string]$zone_id,
        [Parameter(Mandatory = $false, ParameterSetName = "zone_id")]
        [switch]$summary,
        [Parameter(Mandatory = $false, ParameterSetName = "display_name")]
        [string]$display_name,
        [Parameter(Mandatory = $false, ParameterSetName = "host_switch_name")]
        [string]$host_switch_name,
        [Parameter(Mandatory = $false, ParameterSetName = "status")]
        [switch]$status,
        [Parameter(Mandatory = $false)]
        [psobject]$connection = $DefaultNSXTConnection
    )

    Begin {
    }

    Process {

        $uri = 'api/v1/transport-zones'

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

            #if summary is enable add summary on URI
            if ( $PsBoundParameters.ContainsKey('summary') ) {
                if ( $summary ) {
                    $uri += "/summary"
                }
            }
        }

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

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

        switch ( $PSCmdlet.ParameterSetName ) {
            "zone_id" {
                #When there is a zone_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 }
            }
            "host_switch_name" {
                #When there is a host_switch_name, search on response
                $response.results | Where-Object { $_.host_switch_name -eq $host_switch_name }
            }
            "status" {
                #When there is a status, it is directly the result...
                $response
            }
            default {
                $response.results
            }
        }

    }

    End {
    }
}

function Set-NSXTTransportZones {

    <#
        .SYNOPSIS
        Change settings of a Transport Zones

        .DESCRIPTION
        Change settings (name, description) of a Transport Zones

        .EXAMPLE
        Get-NSXTTransportZones -zone_id ff8140b0-010e-4e92-aa62-a55766f17da0 | Set-NSXTTransportZones -display_name "My New TZ Name"

        Change name of Transport Zones ff8140b0-010e-4e92-aa62-a55766f17da0

        .EXAMPLE
        Get-NSXTTransportZones -display_name TZ-VLAN | Set-NSXTTransportZones -description "Modified by psNSXT"

        Change description of Transport Zones TZ-VLAN

        .EXAMPLE
        Get-NSXTTransportZones -display_name TZ-VLAN | Set-NSXTTransportZones -is_default

        Enable default for Transport Zones TZ-VLAN

    #>


    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'medium')]
    Param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [ValidateScript( { Confirm-NSXTTransportZones $_ })]
        [psobject]$tz,
        [Parameter(Mandatory = $false)]
        [string]$display_name,
        [Parameter(Mandatory = $false)]
        [string]$description,
        [Parameter(Mandatory = $false)]
        [switch]$is_default,
        [Parameter(Mandatory = $false)]
        [psobject]$connection = $DefaultNSXTConnection
    )

    Begin {
    }

    Process {

        $zone_id = $tz.id
        $zone_name = "(" + $tz.display_name + ")"

        $uri = "api/v1/transport-zones/$zone_id"

        if ( $PsBoundParameters.ContainsKey('display_name') ) {
            $tz.display_name = $display_name
        }

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

        if ( $PsBoundParameters.ContainsKey('is_default') ) {
            if ( $is_default ) {
                $tz.is_default = $true
            }
            else {
                $tz.is_default = $false
            }
        }

        if ($PSCmdlet.ShouldProcess("$zone_id $zone_name", 'Configure Transport Zones')) {
            $response = Invoke-NSXTRestMethod -uri $uri -method 'PUT' -body $tz -connection $connection
            $response.results
        }

        #Display update Transport Zones
        Get-NSXTTransportZones -zone_id $zone_id -connection $connection
    }

    End {
    }
}


function Remove-NSXTTransportZones {

    <#
        .SYNOPSIS
        Remove a Transport Zone

        .DESCRIPTION
        Remove a Transport Zone

        .EXAMPLE
        $tz = Get-TransportZones -zone_id ff8140b0-010e-4e92-aa62-a55766f17da0
        PS C:\>$tz | Remove-NSXTTransportZones

        Remove Transport Zone with (zone) id ff8140b0-010e-4e92-aa62-a55766f17da0

        .EXAMPLE
        Remove-NSXTTransportZones -zone_id ff8140b0-010e-4e92-aa62-a55766f17da0 -confirm:$false

        Remove Transport Zone with (zone) id ff8140b0-010e-4e92-aa62-a55766f17da0 with no confirmation
    #>


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

    Begin {
    }

    Process {

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

        $uri = "api/v1/transport-zones/$zone_id"

        if ($PSCmdlet.ShouldProcess("TZ", "Remove Transport Zone ${zone_id} ${name}")) {
            Write-Progress -activity "Remove TZ"
            Invoke-NSXTRestMethod -method "DELETE" -uri $uri -connection $connection
            Write-Progress -activity "Remove TZ" -completed
        }
    }

    End {
    }
}