1+ <#
2+ Copyright 2016 Dominique Broeglin
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ #>
16+
17+ function New-NSResponderAction {
18+ <#
19+ . SYNOPSIS
20+ Adds a responder action.
21+
22+ . DESCRIPTION
23+ Adds a responder action.
24+
25+ . EXAMPLE
26+ New-NSResponderAction -Name 'act-redirect' -Type Redirect `
27+ -Target '"https://" + HTTP.REQ.HOSTNAME.HTTP_URL_SAFE + "/test/"' -ResponseStatusCode 302
28+
29+ Creates a new responder action which redirects to /test
30+
31+ . PARAMETER Session
32+ The NetScaler session object.
33+
34+ . PARAMETER Name
35+ The name of responder action.
36+
37+ . PARAMETER Type
38+ The type of responder action to create.
39+
40+ Default value: NOOP
41+ Possible values = NOOP, Redirect, RespondWith, RespondWithSQLOK, RespondWithSQLError, RespondWithHTMLPage
42+
43+ . PARAMETER Target
44+ The target expression for the responder action.
45+ Valid only for types NOOP, Redirect and RespondWith.
46+
47+ . PARAMETER ResponseStatusCode
48+ The HTTP response status code returned by the responder action.
49+ Valid only for types Redirect, RespondWith, RespondWithSQLOK and RespondWithSQLError.
50+
51+ Range: 100 - 599
52+
53+ . PARAMETER ReasonPhrase
54+ The Reason Phrase returned with the HTTP status code.
55+ Valid only for types Redirect, RespondWith, RespondWithSQLOK and RespondWithSQLError.
56+
57+ Minimum length: 0
58+ Maximum length: 8191
59+
60+ . PARAMETER HtmlPage
61+ The name of the HTML page to respond with.
62+
63+ Valid only for type RespondWithHTMLPage.
64+
65+ . PARAMETER Comment
66+ Any information about the responder action.
67+
68+ Minimum length: 0
69+ Maximum length: 256
70+
71+ . PARAMETER Passthru
72+ Return the newly created responder action.
73+ #>
74+ [cmdletbinding (SupportsShouldProcess = $true , ConfirmImpact = ' Low' )]
75+ param (
76+ $Session = $script :session ,
77+
78+ [parameter (Mandatory , ValueFromPipeline = $true , Position = 0 , ValueFromPipelineByPropertyName = $true )]
79+ [string []]$Name ,
80+
81+ [ValidateSet (' NOOP' , ' Redirect' , ' RespondWith' , ' RespondWithSQLOK' , ' RespondWithSQLError' , ' RespondWithHTMLPage' )]
82+ [string ]$Type = ' NOOP' ,
83+
84+ [ValidateLength (0 , 8191 )]
85+ [Alias (' Expression' )]
86+ [string ]$Target = [string ]::Empty,
87+
88+ [ValidateRange (100 , 599 )]
89+ [int ]$ResponseStatusCode ,
90+
91+ [ValidateLength (0 , 8191 )]
92+ [string ]$ReasonPhrase = [string ]::Empty,
93+
94+ [ValidateLength (0 , 256 )]
95+ [string ]$Comment = [string ]::Empty,
96+
97+ [Switch ]$PassThru
98+ )
99+
100+ begin {
101+ _AssertSessionActive
102+ }
103+
104+ process {
105+ foreach ($Item in $Name ) {
106+ if ($PSCmdlet.ShouldProcess ($Item , ' Create responder action' )) {
107+ try {
108+ $NitroType = $ (
109+ switch ($Type ) {
110+ " RespondWithSQLOK" { " sqlresponse_ok" }
111+ " RespondWithSQLError" { " sqlresponse_error" }
112+ " RespondWithHTMLPage" { " respondwithhtmlpage" }
113+ default { $Type.ToLower () }
114+ }
115+ )
116+
117+ $params = @ {
118+ name = $Item
119+ type = $NitroType
120+ comment = $Comment
121+ }
122+ switch - regex ($Type ) {
123+ " ^(noop|redirect|respondwith)$" {
124+ if ($PSBoundParameters.ContainsKey (' Target' )) {
125+ $params.Add (' target' , $Target )
126+ } else {
127+ throw " Target is mandatory if type is NOOP, Redirect or RespondWith"
128+ }
129+ }
130+ " ^(redirect|sqlresponse_ok|sqlresponse_error|respondwithhtmlpage)$" {
131+ if ($PSBoundParameters.ContainsKey (' ResponseStatusCode' )) {
132+ $params.Add (' responsestatuscode' , $ResponseStatusCode )
133+ }
134+ if ($PSBoundParameters.ContainsKey (' ReasonPhrase' )) {
135+ $params.Add (' reasonphrase' , $ReasonPhrase )
136+ }
137+ }
138+ " respondwithhtmlpage" {
139+ if ($PSBoundParameters.ContainsKey (' HtmlPage' )) {
140+ $params.Add (' htmlpage' , $HtmlPage )
141+ }
142+ }
143+ }
144+ _InvokeNSRestApi - Session $Session - Method POST - Type responderaction - Payload $params - Action add
145+
146+ if ($PSBoundParameters.ContainsKey (' PassThru' )) {
147+ return Get-NSResponderAction - Session $Session - Name $Item
148+ }
149+ } catch {
150+ throw $_
151+ }
152+ }
153+ }
154+ }
155+ }
0 commit comments