1
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1
+ // ----------------------------------------------------------------------------------
2
+ //
3
+ // Copyright Microsoft Corporation
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
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2
11
// See the License for the specific language governing permissions and
3
12
// limitations under the License.
4
13
// ----------------------------------------------------------------------------------
5
- using System ;
6
- using System . Collections ;
7
- using System . Collections . Generic ;
8
- using System . Net ;
9
- using AutoMapper ;
10
- using Microsoft . Azure . Commands . Network . Models ;
11
- using Microsoft . Azure . Commands . ResourceManager . Common . Tags ;
12
- using Microsoft . Azure . Management . Network ;
13
- using MNM = Microsoft . Azure . Management . Network . Models ;
14
14
15
15
namespace Microsoft . Azure . Commands . Network . Bastion
16
16
{
17
+ using System ;
18
+ using System . Collections . Generic ;
19
+ using System . Net ;
20
+
21
+ using Microsoft . Azure . Commands . Network . Models ;
22
+ using Microsoft . Azure . Commands . Network . Models . Bastion ;
23
+ using Microsoft . Azure . Commands . ResourceManager . Common . Tags ;
24
+ using Microsoft . Azure . Management . Network ;
25
+ using MNM = Management . Network . Models ;
26
+
17
27
public abstract class BastionBaseCmdlet : NetworkBaseCmdlet
18
28
{
19
29
public IBastionHostsOperations BastionClient
@@ -46,20 +56,38 @@ public bool IsResourcePresent(string resourceGroupName, string name)
46
56
{
47
57
GetBastion ( resourceGroupName , name ) ;
48
58
}
49
- catch ( Microsoft . Rest . Azure . CloudException exception )
59
+ catch ( Rest . Azure . CloudException exception )
50
60
{
51
61
if ( exception . Response . StatusCode == HttpStatusCode . NotFound )
52
62
{
53
63
// Resource is not present
54
64
return false ;
55
65
}
56
-
57
66
throw ;
58
67
}
59
68
60
69
return true ;
61
70
}
62
71
72
+ public bool TryGetBastion ( string resourceGroupName , string name , out PSBastion psBastion )
73
+ {
74
+ psBastion = null ;
75
+ try
76
+ {
77
+ psBastion = GetBastion ( resourceGroupName , name ) ;
78
+ }
79
+ catch ( Rest . Azure . CloudException exception )
80
+ {
81
+ if ( exception . Response . StatusCode == HttpStatusCode . NotFound )
82
+ {
83
+ // Resource is not present
84
+ return false ;
85
+ }
86
+ throw ;
87
+ }
88
+ return true ;
89
+ }
90
+
63
91
public PSBastion GetBastion ( string resourceGroupName , string name )
64
92
{
65
93
var bastion = this . BastionClient . Get ( resourceGroupName , name ) ;
@@ -69,6 +97,11 @@ public PSBastion GetBastion(string resourceGroupName, string name)
69
97
psBastion . Sku . Name = bastion . Sku . Name ;
70
98
psBastion . ScaleUnit = bastion . ScaleUnits ;
71
99
psBastion . Tag = TagsConversionHelper . CreateTagHashtable ( bastion . Tags ) ;
100
+ psBastion . EnableKerberos = bastion . EnableKerberos ;
101
+ psBastion . DisableCopyPaste = bastion . DisableCopyPaste ;
102
+ psBastion . EnableTunneling = bastion . EnableTunneling ;
103
+ psBastion . EnableIpConnect = bastion . EnableIpConnect ;
104
+ psBastion . EnableShareableLink = bastion . EnableShareableLink ;
72
105
73
106
return psBastion ;
74
107
}
@@ -79,6 +112,11 @@ public PSBastion ToPsBastion(MNM.BastionHost host)
79
112
bastion . Sku . Name = host . Sku . Name ;
80
113
bastion . ScaleUnit = host . ScaleUnits ;
81
114
bastion . Tag = TagsConversionHelper . CreateTagHashtable ( host . Tags ) ;
115
+ bastion . EnableKerberos = bastion . EnableKerberos ;
116
+ bastion . DisableCopyPaste = host . DisableCopyPaste ;
117
+ bastion . EnableTunneling = host . EnableTunneling ;
118
+ bastion . EnableIpConnect = host . EnableIpConnect ;
119
+ bastion . EnableShareableLink = host . EnableShareableLink ;
82
120
83
121
return bastion ;
84
122
}
@@ -95,12 +133,105 @@ public List<PSBastion> ListBastions(string resourceGroupName)
95
133
foreach ( MNM . BastionHost bastion in bastions )
96
134
{
97
135
PSBastion bastionToReturn = ToPsBastion ( bastion ) ;
98
- bastionToReturn . ResourceGroupName = NetworkBaseCmdlet . GetResourceGroup ( bastion . Id ) ;
136
+ bastionToReturn . ResourceGroupName = GetResourceGroup ( bastion . Id ) ;
99
137
bastionsToReturn . Add ( bastionToReturn ) ;
100
138
}
101
139
}
102
140
103
141
return bastionsToReturn ;
104
142
}
143
+
144
+ public bool IsSkuDowngrade ( PSBastion bastion , string sku )
145
+ {
146
+ if ( PSBastionSku . TryGetSkuTier ( sku , out string newSkuTier )
147
+ && PSBastionSku . TryGetSkuTier ( bastion . Sku . Name , out string existingSkuTier ) )
148
+ {
149
+ switch ( existingSkuTier )
150
+ {
151
+ case PSBastionSku . Basic :
152
+ return false ;
153
+ // Standard -> Basic
154
+ case PSBastionSku . Standard :
155
+ if ( newSkuTier == PSBastionSku . Basic )
156
+ {
157
+ return true ;
158
+ }
159
+ return false ;
160
+ default :
161
+ return true ;
162
+ }
163
+ }
164
+
165
+ return true ;
166
+ }
167
+
168
+ public void ValidateScaleUnits ( PSBastion bastion , int ? scaleUnits = 2 )
169
+ {
170
+ if ( PSBastionSku . TryGetSkuTier ( bastion . Sku . Name , out string skuTierValue ) )
171
+ {
172
+ switch ( skuTierValue )
173
+ {
174
+ case PSBastionSku . Basic :
175
+ if ( scaleUnits != PSBastion . MinimumScaleUnits )
176
+ {
177
+ throw new ArgumentException ( $ "Bastion scalable host is available on Standard SKU") ;
178
+ }
179
+ break ;
180
+ case PSBastionSku . Standard :
181
+ if ( scaleUnits < PSBastion . MinimumScaleUnits
182
+ || scaleUnits > PSBastion . MaximumScaleUnits )
183
+ {
184
+ throw new ArgumentException ( $ "Please select scale units value between { PSBastion . MinimumScaleUnits } and { PSBastion . MaximumScaleUnits } ") ;
185
+ }
186
+ break ;
187
+ default :
188
+ throw new ArgumentException ( $ "Please enter a valid value for Bastion SKU") ;
189
+ }
190
+ }
191
+ else
192
+ {
193
+ throw new ArgumentException ( $ "Please enter a valid value for Bastion SKU") ;
194
+ }
195
+ }
196
+
197
+ public void ValidateFeatures ( PSBastion bastion ,
198
+ bool ? disableCopyPaste = false ,
199
+ bool ? enableTunneling = false ,
200
+ bool ? enableIpConnect = false ,
201
+ bool ? enableShareableLink = false )
202
+ {
203
+ if ( PSBastionSku . TryGetSkuTier ( bastion . Sku . Name , out string skuTierValue ) )
204
+ {
205
+ switch ( skuTierValue )
206
+ {
207
+ case PSBastionSku . Basic :
208
+ if ( disableCopyPaste != null && disableCopyPaste != false )
209
+ {
210
+ throw new ArgumentException ( $ "Toggling copy/paste is available on Standard SKU or higher") ;
211
+ }
212
+ if ( enableTunneling != null && enableTunneling != false )
213
+ {
214
+ throw new ArgumentException ( $ "Toggling tunneling is available on Standard SKU or higher") ;
215
+ }
216
+ if ( enableIpConnect != null && enableIpConnect != false )
217
+ {
218
+ throw new ArgumentException ( $ "Toggling IP connect is available on Standard SKU or higher") ;
219
+ }
220
+ if ( enableShareableLink != null && enableShareableLink != false )
221
+ {
222
+ throw new ArgumentException ( $ "Toggling shareable link is available on Standard SKU or higher") ;
223
+ }
224
+ break ;
225
+ case PSBastionSku . Standard :
226
+ break ;
227
+ default :
228
+ throw new ArgumentException ( $ "Please enter a valid value for Bastion SKU") ;
229
+ }
230
+ }
231
+ else
232
+ {
233
+ throw new ArgumentException ( $ "Please enter a valid value for Bastion SKU") ;
234
+ }
235
+ }
105
236
}
106
237
}
0 commit comments