Public/Policy/Infra/segments.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
#
# Copyright 2019-2020, Alexis La Goutte <alexis dot lagoutte at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#

function Add-NSXTPolicyInfraSegments {

    <#
        .SYNOPSIS
        Add a Segment

        .DESCRIPTION
        Add a Segment (Vlan)

        .EXAMPLE
        Get-NSXTTransportZones -display_name MyTZ-Vlan | Add-NSXTPolicyInfraSegments -segment MySegment -vlan_ids 2

        Add a (vlan) Segment MySegment on MyTZ-Vlan with vlan id 2

        .EXAMPLE
        Get-NSXTTransportZones -display_name MyTZ-Vlan | Add-NSXTPolicyInfraSegments -segment MySegment -vlan_ids 2,44 -display_name MySegment2and44

        Add a (vlan) Segment with (display_)name MySegment2and44 on MyTZ-Vlan with vlan id 2 and 44

    #>


    Param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [ValidateScript( { Confirm-NSXTTransportZones $_ })]
        [psobject]$tz,
        [Parameter(Mandatory = $true)]
        [string]$segment,
        [Parameter(Mandatory = $false)]
        [string]$display_name,
        [Parameter(Mandatory = $true)]
        [ValidateRange(0, 4096)]
        [int[]]$vlan_ids,
        [Parameter(Mandatory = $false)]
        [psobject]$connection = $DefaultNSXTConnection
    )

    Begin {
    }

    Process {

        $uri = 'policy/api/v1/infra/segments'
        $uri += "/" + $segment
        $transport_zone_path = "/infra/sites/default/enforcement-points/default/transport-zones/" + $tz.id

        $_sg = new-Object -TypeName PSObject

        $_sg | add-member -name "type" -membertype NoteProperty -Value "DISCONNECTED"

        $_sg | add-member -name "transport_zone_path" -membertype NoteProperty -Value $transport_zone_path

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

        $_sg | add-member -name "vlan_ids" -membertype NoteProperty -Value @($vlan_ids)

        $response = Invoke-NSXTRestMethod -uri $uri -method 'PATCH' -body $_sg -connection $connection
        $response

        Get-NSXTPolicyInfraSegments -segment $segment -connection $connection
    }

    End {
    }
}

function Get-NSXTPolicyInfraSegments {

    <#
        .SYNOPSIS
        Get information about Segments

        .DESCRIPTION
        Returns information about Segments

        .EXAMPLE
        Get-NSXTPolicyInfraSegments

        Display all Transport Zones

        .EXAMPLE
        Get-NSXTPolicyInfraSegments -segment mySegment

        Display Segments with segment mySegment

        .EXAMPLE
        Get-NSXTPolicyInfraSegments -display_name mySegment

        Display Segments with (display) name mySegment

    #>


    [CmdletBinding(DefaultParametersetname = "Default")]
    Param(
        [Parameter(Mandatory = $false, ParameterSetName = "segment")]
        [string]$segment,
        [Parameter(Mandatory = $false, ParameterSetName = "display_name")]
        [string]$display_name,
        [Parameter(Mandatory = $false)]
        [psobject]$connection = $DefaultNSXTConnection
    )

    Begin {
    }

    Process {

        $uri = 'policy/api/v1/infra/segments'

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

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

        switch ( $PSCmdlet.ParameterSetName ) {
            "segment" {
                #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 }
            }
            default {
                $response.results
            }
        }

    }

    End {
    }
}

function Set-NSXTPolicyInfraSegments {

    <#
        .SYNOPSIS
        Configure a Segment

        .DESCRIPTION
        Configure a Segment (Vlan)

        .EXAMPLE
        Get-NSXTPolicyInfraSegments -display_name MySegment | Set-NSXTPolicyInfraSegments -vlan_ids 23

        Configure a (vlan) Segment MySegment with vlan id 23

        .EXAMPLE
        Get-NSXTPolicyInfraSegments -display_name MySegment | Set-NSXTPolicyInfraSegments -vlan_ids 23,44

        Configure a (vlan) Segment MySegment with vlan id 23 and 44

        .EXAMPLE
        Get-NSXTPolicyInfraSegments -display_name MySegment | Set-NSXTPolicyInfraSegments -display_name MySegment2

        Configure a (vlan) Segment MySegment with (display_)name MySegment2


    #>

    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'medium')]
    Param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [ValidateScript( { Confirm-NSXTSegments $_ })]
        [psobject]$segment,
        [Parameter(Mandatory = $false)]
        [string]$display_name,
        [Parameter(Mandatory = $false)]
        [ValidateRange(0, 4096)]
        [int[]]$vlan_ids,
        [Parameter(Mandatory = $false)]
        [psobject]$connection = $DefaultNSXTConnection
    )

    Begin {
    }

    Process {

        $uri = 'policy/api/v1/infra/segments'
        $uri += "/" + $segment.id
        $segment_name = $segment.display_name


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

        if ( $PsBoundParameters.ContainsKey('vlan_ids') ) {
            $segment.vlan_ids = @($vlan_ids)
        }

        if ($PSCmdlet.ShouldProcess("$segment_name", 'Configure Segment')) {
            $response = Invoke-NSXTRestMethod -uri $uri -method 'PUT' -body $segment -connection $connection
            $response.results
        }

        Get-NSXTPolicyInfraSegments -segment $segment.id -connection $connection
    }

    End {
    }
}

function Remove-NSXTPolicyInfraSegments {

    <#
        .SYNOPSIS
        Remove a Segment

        .DESCRIPTION
        Remove a Segment

        .EXAMPLE
        $sg = Get-NSXTPolicyInfraSegments -display_name MySegment
        PS C:\>$sg | Remove-NSXTPolicyInfraSegments

        Remove Segment e with ff8140b0-010e-4e92-aa62-a55766f17da0

        .EXAMPLE
        Remove-NSXTPolicyInfraSegments -segment MySegment -confirm:$false

        Remove Segment MySegment with no confirmation
    #>


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

    Begin {
    }

    Process {

        #get segment name froms segment object
        if ($sg) {
            $segment = $sg.id
            $name = " (" + $sg.display_name + ")"
        }

        $uri = "policy/api/v1/infra/segments/$segment"

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

    End {
    }
}