forked from Azure/azure-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceManagementCmdletTestHelper.cs
2308 lines (1934 loc) · 109 KB
/
ServiceManagementCmdletTestHelper.cs
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.Commands.Common.Storage;
using Microsoft.WindowsAzure.Commands.Profile.Models;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Extensions;
using Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.Extensions;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Network.Gateway.Model;
using Microsoft.WindowsAzure.Commands.ServiceManagement.PlatformImageRepository.Model;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.ConfigDataInfo;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.IaasCmdletInfo;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.IaasCmdletInfo.DiskRepository;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.IaasCmdletInfo.Extensions.BGInfo;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.IaasCmdletInfo.Extensions.Common;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.IaasCmdletInfo.Extesnions.CustomScript;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.IaasCmdletInfo.Extensions.SqlServer;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.IaasCmdletInfo.Extesnions.VMAccess;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.IaasCmdletInfo.ILB;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.PaasCmdletInfo;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.PIRCmdletInfo;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.NetworkCmdletInfo;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.SubscriptionCmdletInfo;
using Microsoft.WindowsAzure.Commands.Storage.Common;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Microsoft.WindowsAzure.Management.Network.Models;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Management.Automation;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Xml;
namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests
{
using Azure.Commands.Common.Authentication.Abstractions;
using SM = Model;
public class ServiceManagementCmdletTestHelper
{
private T RunPSCmdletAndReturnFirst<T>(PowershellCore.CmdletsInfo cmdlet, bool debug = false, bool retryOnConflict = true)
{
var result = default(T);
if (retryOnConflict)
{
Utilities.RetryActionUntilSuccess(
() => result = RunPSCmdletAndReturnFirstHelper<T>(cmdlet),
"ConflictError", 3, 60);
}
else
{
result = RunPSCmdletAndReturnFirstHelper<T>(cmdlet);
}
return result;
}
/// <summary>
/// Run a powershell cmdlet that returns the first PSObject as a return value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cmdlet"></param>
/// <param name="debug"></param>
/// <returns></returns>
private T RunPSCmdletAndReturnFirstHelper<T>(PowershellCore.CmdletsInfo cmdlet, bool debug = false)
{
var azurePowershellCmdlet = new WindowsAzurePowershellCmdlet(cmdlet);
Collection<PSObject> result = azurePowershellCmdlet.Run(debug);
if (result.Count == 1)
{
try
{
var operation = (ManagementOperationContext)result[0].BaseObject;
Console.WriteLine("Operation ID: {0} \nOperation Status: {1}\n", operation.OperationId, operation.OperationStatus);
}
catch (Exception e)
{
if (e is InvalidCastException)
{
// continue
}
else
{
Console.WriteLine(e);
throw;
}
}
return (T) result[0].BaseObject;
}
return default(T);
}
private Collection<T> RunPSCmdletAndReturnAll<T>(PowershellCore.CmdletsInfo cmdlet, bool debug = false, bool retryOnConflict = true)
{
var result = new Collection<T>();
if (retryOnConflict)
{
Utilities.RetryActionUntilSuccess(
() => result = RunPSCmdletAndReturnAllHelper<T>(cmdlet),
"ConflictError", 3, 60);
}
else
{
result = RunPSCmdletAndReturnAllHelper<T>(cmdlet);
}
return result;
}
/// <summary>
/// Run a powershell cmdlet that returns a collection of PSObjects as a return value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="cmdlet"></param>
/// <param name="debug"></param>
/// <returns></returns>
private Collection<T> RunPSCmdletAndReturnAllHelper<T>(PowershellCore.CmdletsInfo cmdlet, bool debug = false)
{
var azurePowershellCmdlet = new WindowsAzurePowershellCmdlet(cmdlet);
Collection<PSObject> result = azurePowershellCmdlet.Run(debug);
var resultCollection = new Collection<T>();
foreach (PSObject re in result)
{
resultCollection.Add((T)re.BaseObject);
}
try
{
if (result.Count > 0)
{
var operation = (ManagementOperationContext) result[0].BaseObject;
Console.WriteLine("Operation ID: {0} \nOperation Status: {1}\n", operation.OperationId,
operation.OperationStatus);
}
}
catch (Exception e)
{
if (e is InvalidCastException)
{
// continue
}
else
{
Console.WriteLine(e);
throw;
}
}
return resultCollection;
}
public Collection <PSObject> RunPSScript(string script, bool debug = false)
{
List<string> st = new List<string>();
st.Add(script);
WindowsAzurePowershellScript azurePowershellCmdlet = new WindowsAzurePowershellScript(st);
return azurePowershellCmdlet.Run(debug);
}
public CopyState CheckCopyBlobStatus(string destContainer, string destBlob, bool debug = false)
{
List<string> st = new List<string>();
st.Add(string.Format("{0}-{1} -Container {2} -Blob {3}",
VerbsCommon.Get, StorageNouns.CopyBlobStatus, destContainer, destBlob));
WindowsAzurePowershellScript azurePowershellCmdlet = new WindowsAzurePowershellScript(st);
return (CopyState)azurePowershellCmdlet.Run(debug)[0].BaseObject;
}
public bool TestAzureServiceName(string serviceName)
{
return RunPSCmdletAndReturnFirst<bool>(new TestAzureNameCmdletInfo("Service", serviceName));
}
public Collection<SM.LocationsContext> GetAzureLocation()
{
return RunPSCmdletAndReturnAll<SM.LocationsContext>(new GetAzureLocationCmdletInfo());
}
public string GetAzureLocationName(string[] keywords)
{
Collection<SM.LocationsContext> locations;
try
{
locations = GetAzureLocation();
}
catch
{
Console.WriteLine("Error occurred during Get-AzureLocation... Default location is not set.");
return null;
}
if (keywords != null)
{
foreach (SM.LocationsContext location in locations)
{
if (MatchExactWords(location.Name, keywords) >= 0)
{
return location.Name;
}
}
}
else
{
if (locations.Count == 1)
{
return locations[0].Name;
}
}
return null;
}
private static int MatchExactWords(string input, string[] keywords)
{ //returns -1 for no match, 0 for exact match, and a positive number for how many keywords are matched.
int result = 0;
if (string.IsNullOrEmpty(input) || keywords.Length == 0)
return -1;
foreach (string keyword in keywords)
{
//For whole word match, modify pattern to be "\b{0}\b"
if (!string.IsNullOrEmpty(keyword) && keyword.ToLowerInvariant().Equals(input.ToLowerInvariant()))
{
result++;
}
}
if (result == keywords.Length)
{
return 0;
}
else if (result == 0)
{
return -1;
}
else
{
return result;
}
}
public Collection<SM.OSVersionsContext> GetAzureOSVersion()
{
return RunPSCmdletAndReturnAll<SM.OSVersionsContext>(new GetAzureOSVersionCmdletInfo());
}
#region CertificateSetting, VMConifig, ProvisioningConfig
public SM.CertificateSetting NewAzureCertificateSetting(string store, string thumbprint)
{
return RunPSCmdletAndReturnFirst<SM.CertificateSetting>(new NewAzureCertificateSettingCmdletInfo(store, thumbprint));
}
public SM.PersistentVM NewAzureVMConfig(AzureVMConfigInfo vmConfig)
{
return RunPSCmdletAndReturnFirst<SM.PersistentVM>(new NewAzureVMConfigCmdletInfo(vmConfig));
}
public SM.PersistentVM AddAzureProvisioningConfig(AzureProvisioningConfigInfo provConfig)
{
return RunPSCmdletAndReturnFirst<SM.PersistentVM>(new AddAzureProvisioningConfigCmdletInfo(provConfig));
}
#endregion
#region AzureAffinityGroup
public ManagementOperationContext NewAzureAffinityGroup(string name, string location, string label, string description)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext> (new NewAzureAffinityGroupCmdletInfo(name, location, label, description));
}
public Collection<SM.AffinityGroupContext> GetAzureAffinityGroup(string name = null)
{
return RunPSCmdletAndReturnAll<SM.AffinityGroupContext>(new GetAzureAffinityGroupCmdletInfo(name));
}
public ManagementOperationContext SetAzureAffinityGroup(string name, string label, string description)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext> (new SetAzureAffinityGroupCmdletInfo(name, label, description));
}
public ManagementOperationContext RemoveAzureAffinityGroup(string name)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new RemoveAzureAffinityGroupCmdletInfo(name));
}
#endregion
#region AzureAvailabilitySet
public SM.PersistentVM SetAzureAvailabilitySet(string availabilitySetName, SM.PersistentVM vm)
{
return RunPSCmdletAndReturnFirst<SM.PersistentVM>(new SetAzureAvailabilitySetCmdletInfo(availabilitySetName, vm));
}
public SM.PersistentVM SetAzureAvailabilitySet(string vmName, string serviceName, string availabilitySetName)
{
SM.PersistentVM vm = GetAzureVM(vmName, serviceName).VM;
return RunPSCmdletAndReturnFirst<SM.PersistentVM>(new SetAzureAvailabilitySetCmdletInfo(availabilitySetName, vm));
}
public SM.PersistentVM RemoveAzureAvailabilitySet(SM.PersistentVM vm)
{
return RunPSCmdletAndReturnFirst<SM.PersistentVM>(new RemoveAzureAvailabilitySetCmdletInfo(vm));
}
public SM.PersistentVM RemoveAzureAvailabilitySet(string vmName, string serviceName)
{
SM.PersistentVM vm = GetAzureVM(vmName, serviceName).VM;
return RunPSCmdletAndReturnFirst<SM.PersistentVM>(new RemoveAzureAvailabilitySetCmdletInfo(vm));
}
#endregion AzureAvailabilitySet
#region AzureCertificate
public ManagementOperationContext AddAzureCertificate(string serviceName, PSObject cert, string password = null)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new AddAzureCertificateCmdletInfo(serviceName, cert, password));
}
public Collection <SM.CertificateContext> GetAzureCertificate(string serviceName, string thumbprint = null, string algorithm = null)
{
return RunPSCmdletAndReturnAll<SM.CertificateContext> (new GetAzureCertificateCmdletInfo(serviceName, thumbprint, algorithm));
}
public ManagementOperationContext RemoveAzureCertificate(string serviceName, string thumbprint, string algorithm)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new RemoveAzureCertificateCmdletInfo(serviceName, thumbprint, algorithm));
}
#endregion
#region AzureDataDisk
public SM.PersistentVM AddAzureDataDisk(AddAzureDataDiskConfig diskConfig)
{
return RunPSCmdletAndReturnFirst<SM.PersistentVM>(new AddAzureDataDiskCmdletInfo(diskConfig));
}
public void AddDataDisk(string vmName, string serviceName, AddAzureDataDiskConfig [] diskConfigs)
{
SM.PersistentVM vm = GetAzureVM(vmName, serviceName).VM;
foreach (AddAzureDataDiskConfig config in diskConfigs)
{
config.Vm = vm;
vm = AddAzureDataDisk(config);
}
UpdateAzureVM(vmName, serviceName, vm);
}
public SM.PersistentVM SetAzureDataDisk(SetAzureDataDiskConfig discCfg)
{
return RunPSCmdletAndReturnFirst<SM.PersistentVM>(new SetAzureDataDiskCmdletInfo(discCfg));
}
public SM.PersistentVM SetAzureDataDisk(SetAzureDataDiskResizeConfig discCfg)
{
return RunPSCmdletAndReturnFirst<SM.PersistentVM>(new SetAzureDataDiskCmdletInfo(discCfg));
}
public void SetDataDisk(string vmName, string serviceName, HostCaching hc, int lun)
{
SetAzureDataDiskConfig config = new SetAzureDataDiskConfig(hc, lun);
config.Vm = GetAzureVM(vmName, serviceName).VM;
UpdateAzureVM(vmName, serviceName, SetAzureDataDisk(config));
}
public Collection<SM.DataVirtualHardDisk> GetAzureDataDisk(string vmName, string serviceName)
{
SM.PersistentVMRoleContext vmRolectx = GetAzureVM(vmName, serviceName);
return RunPSCmdletAndReturnAll<SM.DataVirtualHardDisk>(new GetAzureDataDiskCmdletInfo(vmRolectx.VM));
}
public Collection<SM.DataVirtualHardDisk> GetAzureDataDisk(SM.PersistentVM vm)
{
return RunPSCmdletAndReturnAll<SM.DataVirtualHardDisk>(new GetAzureDataDiskCmdletInfo(vm));
}
public SM.PersistentVM RemoveAzureDataDisk(RemoveAzureDataDiskConfig discCfg)
{
return RunPSCmdletAndReturnFirst<SM.PersistentVM>(new RemoveAzureDataDiskCmdletInfo(discCfg));
}
public void RemoveDataDisk(string vmName, string serviceName, int [] lunSlots)
{
SM.PersistentVM vm = GetAzureVM(vmName, serviceName).VM;
foreach (int lun in lunSlots)
{
RemoveAzureDataDiskConfig config = new RemoveAzureDataDiskConfig(lun, vm);
RemoveAzureDataDisk(config);
}
UpdateAzureVM(vmName, serviceName, vm);
}
#endregion
#region AzureDeployment
public ManagementOperationContext NewAzureDeployment(string serviceName, string packagePath, string configPath,
string slot, string label, string name, bool doNotStart, bool warning,
ExtensionConfigurationInput config = null)
{
return
RunPSCmdletAndReturnFirst<ManagementOperationContext>(new NewAzureDeploymentCmdletInfo(serviceName,
packagePath, configPath, slot, label, name, doNotStart, warning, config));
}
public SM.DeploymentInfoContext GetAzureDeployment(string serviceName, string slot)
{
return RunPSCmdletAndReturnFirst<SM.DeploymentInfoContext>(new GetAzureDeploymentCmdletInfo(serviceName, slot));
}
public SM.DeploymentInfoContext GetAzureDeployment(string serviceName)
{
return GetAzureDeployment(serviceName, SM.DeploymentSlotType.Production);
}
public Collection<SM.DeploymentRebootEventContext> GetAzureDeploymentEvent(string serviceName, string deploymentName, DateTime startTime, DateTime endTime)
{
return RunPSCmdletAndReturnAll<SM.DeploymentRebootEventContext>(new GetAzureDeploymentEventCmdletInfo(serviceName, deploymentName, startTime, endTime));
}
public Collection<SM.DeploymentRebootEventContext> GetAzureDeploymentEventBySlot(string serviceName, string deploymentSlot, DateTime startTime, DateTime endTime)
{
return RunPSCmdletAndReturnAll<SM.DeploymentRebootEventContext>(new GetAzureDeploymentEventBySlotCmdletInfo(serviceName, deploymentSlot, startTime, endTime));
}
private ManagementOperationContext SetAzureDeployment(SetAzureDeploymentCmdletInfo cmdletInfo)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(cmdletInfo);
}
public ManagementOperationContext SetAzureDeploymentStatus(string serviceName, string slot, string newStatus)
{
return SetAzureDeployment(SetAzureDeploymentCmdletInfo.SetAzureDeploymentStatusCmdletInfo(serviceName, slot, newStatus));
}
public ManagementOperationContext SetAzureDeploymentConfig(string serviceName, string slot, string configPath, ExtensionConfigurationInput extConfig = null)
{
return SetAzureDeployment(SetAzureDeploymentCmdletInfo.SetAzureDeploymentConfigCmdletInfo(serviceName, slot, configPath, extConfig));
}
public ManagementOperationContext SetAzureDeploymentUpgrade(string serviceName, string slot, string mode, string packagePath, string configPath)
{
return SetAzureDeployment(SetAzureDeploymentCmdletInfo.SetAzureDeploymentUpgradeCmdletInfo(serviceName, slot, mode, packagePath, configPath));
}
public ManagementOperationContext SetAzureDeployment(string option, string serviceName, string packagePath, string newStatus, string configName, string slot, string mode, string label, string roleName, bool force)
{
return SetAzureDeployment(new SetAzureDeploymentCmdletInfo(option, serviceName, packagePath, newStatus, configName, slot, mode, label, roleName, force));
}
public ManagementOperationContext RemoveAzureDeployment(string serviceName, string slot, bool force)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new RemoveAzureDeploymentCmdletInfo(serviceName, slot, force));
}
public ManagementOperationContext MoveAzureDeployment(string serviceName)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new MoveAzureDeploymentCmdletInfo(serviceName));
}
public ManagementOperationContext SetAzureWalkUpgradeDomain(string serviceName, string slot, int domainNumber)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new SetAzureWalkUpgradeDomainCmdletInfo(serviceName, slot, domainNumber));
}
#endregion
#region AzureDisk
// Add-AzureDisk
public SM.DiskContext AddAzureDisk(string diskName, string mediaPath, string label, string os)
{
SM.DiskContext result = new SM.DiskContext();
Utilities.RetryActionUntilSuccess(
() => result = RunPSCmdletAndReturnFirst<SM.DiskContext>(new AddAzureDiskCmdletInfo(diskName, mediaPath, label, os)),
"409", 3, 60);
return result;
}
// Get-AzureDisk
public Collection<SM.DiskContext> GetAzureDisk(string diskName)
{
return GetAzureDisk(new GetAzureDiskCmdletInfo(diskName));
}
public Collection<SM.DiskContext> GetAzureDisk()
{
return GetAzureDisk(new GetAzureDiskCmdletInfo((string)null));
}
private Collection<SM.DiskContext> GetAzureDisk(GetAzureDiskCmdletInfo getAzureDiskCmdletInfo)
{
return RunPSCmdletAndReturnAll<SM.DiskContext>(getAzureDiskCmdletInfo);
}
public Collection<SM.DiskContext> GetAzureDiskAttachedToRoleName(string[] roleName, bool exactMatch = true)
{
Collection<SM.DiskContext> retDisks = new Collection<SM.DiskContext>();
Collection<SM.DiskContext> disks = GetAzureDisk();
foreach (SM.DiskContext disk in disks)
{
if (disk.AttachedTo != null && disk.AttachedTo.RoleName != null)
{
if (Utilities.MatchKeywords(disk.AttachedTo.RoleName, roleName, exactMatch) >= 0)
retDisks.Add(disk);
}
}
return retDisks;
}
// Remove-AzureDisk
public ManagementOperationContext RemoveAzureDisk(string diskName, bool deleteVhd)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new RemoveAzureDiskCmdletInfo(diskName, deleteVhd));
}
// Update-AzureDisk
public SM.DiskContext UpdateAzureDisk(string diskName, string label)
{
return RunPSCmdletAndReturnFirst<SM.DiskContext>(new UpdateAzureDiskCmdletInfo(diskName, label, null));
}
public ManagementOperationContext UpdateAzureDisk(string diskName, string label, int? resizedSize)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new UpdateAzureDiskCmdletInfo(diskName, label, resizedSize));
}
#endregion
#region AzureDns
public SM.DnsServer NewAzureDns(string name, string ipAddress)
{
return RunPSCmdletAndReturnFirst<SM.DnsServer>(new NewAzureDnsCmdletInfo(name, ipAddress));
}
public SM.DnsServerList GetAzureDns(SM.DnsSettings settings, bool debug = true)
{
var getAzureDnsCmdletInfo = new GetAzureDnsCmdletInfo(settings);
var azurePowershellCmdlet = new WindowsAzurePowershellCmdlet(getAzureDnsCmdletInfo);
Collection<PSObject> result = azurePowershellCmdlet.Run(debug);
var dnsList = new SM.DnsServerList();
foreach (PSObject re in result)
{
dnsList.Add((SM.DnsServer)re.BaseObject);
}
return dnsList;
}
public ManagementOperationContext AddAzureDns(string name, string ipAddress, string serviceName)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new AddAzureDnsCmdletInfo(name, ipAddress, serviceName));
}
public ManagementOperationContext SetAzureDns(string name, string ipAddress, string serviceName)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new SetAzureDnsCmdletInfo(name, ipAddress, serviceName));
}
public ManagementOperationContext RemoveAzureDns(string name, string ipAddress, bool force = false)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new RemoveAzureDnsCmdletInfo(name, ipAddress, force));
}
#endregion
#region AzureEndpoint
public SM.PersistentVM AddAzureEndPoint(AzureEndPointConfigInfo endPointConfig)
{
return RunPSCmdletAndReturnFirst<SM.PersistentVM>(new AddAzureEndpointCmdletInfo(endPointConfig));
}
public void AddEndPoint(string vmName, string serviceName, AzureEndPointConfigInfo [] endPointConfigs)
{
SM.PersistentVM vm = GetAzureVM(vmName, serviceName).VM;
foreach (AzureEndPointConfigInfo config in endPointConfigs)
{
config.Vm = vm;
vm = AddAzureEndPoint(config);
}
UpdateAzureVM(vmName, serviceName, vm);
}
public Collection <SM.InputEndpointContext> GetAzureEndPoint(SM.PersistentVMRoleContext vmRoleCtxt)
{
return RunPSCmdletAndReturnAll<SM.InputEndpointContext>(new GetAzureEndpointCmdletInfo(vmRoleCtxt));
}
public Collection<SM.InputEndpointContext> GetAzureEndPoint(SM.PersistentVM vm)
{
return RunPSCmdletAndReturnAll<SM.InputEndpointContext>(new GetAzureEndpointCmdletInfo(vm));
}
public void SetEndPoint(string vmName, string serviceName, AzureEndPointConfigInfo endPointConfig)
{
endPointConfig.Vm = GetAzureVM(vmName, serviceName).VM;
UpdateAzureVM(vmName, serviceName, SetAzureEndPoint(endPointConfig));
}
public SM.PersistentVM SetAzureEndPoint(AzureEndPointConfigInfo endPointConfig)
{
if (null != endPointConfig)
{
return RunPSCmdletAndReturnFirst<SM.PersistentVM>(new SetAzureEndpointCmdletInfo(endPointConfig));
}
return null;
}
public void SetLBEndPoint(string vmName, string serviceName, AzureEndPointConfigInfo endPointConfig, AzureEndPointConfigInfo.ParameterSet paramset)
{
endPointConfig.Vm = GetAzureVM(vmName, serviceName).VM;
SetAzureLoadBalancedEndPoint(endPointConfig, paramset);
//UpdateAzureVM(vmName, serviceName, SetAzureLoadBalancedEndPoint(endPointConfig, paramset));
}
public ManagementOperationContext SetAzureLoadBalancedEndPoint(AzureEndPointConfigInfo endPointConfig, AzureEndPointConfigInfo.ParameterSet paramset)
{
if (null != endPointConfig)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new SetAzureLoadBalancedEndpointCmdletInfo(endPointConfig, paramset));
}
return null;
}
public SM.PersistentVMRoleContext RemoveAzureEndPoint(string epName, SM.PersistentVMRoleContext vmRoleCtxt)
{
return RunPSCmdletAndReturnFirst<SM.PersistentVMRoleContext>(new RemoveAzureEndpointCmdletInfo(epName, vmRoleCtxt));
}
public SM.PersistentVM RemoveAzureEndPoint(string epName, SM.PersistentVM vm)
{
return RunPSCmdletAndReturnFirst<SM.PersistentVM>(new RemoveAzureEndpointCmdletInfo(epName, vm));
}
public void RemoveEndPoint(string vmName, string serviceName, string [] epNames)
{
SM.PersistentVMRoleContext vmRoleCtxt = GetAzureVM(vmName, serviceName);
foreach (string ep in epNames)
{
vmRoleCtxt.VM = RemoveAzureEndPoint(ep, vmRoleCtxt).VM;
}
UpdateAzureVM(vmName, serviceName, vmRoleCtxt.VM);
}
#endregion
#region AzureOSDisk
public SM.PersistentVM SetAzureOSDisk(HostCaching? hc, SM.PersistentVM vm, int? resizedSize = null)
{
return RunPSCmdletAndReturnFirst<SM.PersistentVM>(new SetAzureOSDiskCmdletInfo(hc, vm, resizedSize));
}
public SM.OSVirtualHardDisk GetAzureOSDisk(SM.PersistentVM vm)
{
return RunPSCmdletAndReturnFirst<SM.OSVirtualHardDisk>(new GetAzureOSDiskCmdletInfo(vm));
}
#endregion
#region AzureRole
public ManagementOperationContext SetAzureRole(string serviceName, string slot, string roleName, int count)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new SetAzureRoleCmdletInfo(serviceName, slot, roleName, count));
}
public Collection<SM.RoleContext> GetAzureRole(string serviceName, string slot, string roleName, bool details)
{
return RunPSCmdletAndReturnAll<SM.RoleContext>(new GetAzureRoleCmdletInfo(serviceName, slot, roleName, details));
}
#endregion
#region AzureQuickVM
public ManagementOperationContext NewAzureQuickVM(OS os, string name, string serviceName, string imageName,
string userName, string password, string locationName, string instanceSize)
{
ManagementOperationContext result = new ManagementOperationContext();
try
{
result = RunPSCmdletAndReturnFirst<ManagementOperationContext>(new NewAzureQuickVMCmdletInfo(os, name, serviceName, imageName, userName, password, locationName, instanceSize));
}
catch (Exception e)
{
if (e.ToString().Contains("409"))
{
Utilities.RetryActionUntilSuccess(
() => result = RunPSCmdletAndReturnFirst<ManagementOperationContext>(new NewAzureQuickVMCmdletInfo(os, name, serviceName, imageName, userName, password, null, instanceSize)),
"409", 4, 60);
}
else
{
Console.WriteLine(e.InnerException.ToString());
throw;
}
}
return result;
}
public ManagementOperationContext NewAzureQuickVM(OS os, string name, string serviceName, string imageName,
string userName, string password, string locationName, string instanceSize, bool disableWinRMHttps,
string reservedIpName = null, string vnetName = null)
{
var result = new ManagementOperationContext();
try
{
result = RunPSCmdletAndReturnFirst<ManagementOperationContext>(new NewAzureQuickVMCmdletInfo(
os, name, serviceName, imageName, userName, password, locationName,
instanceSize, disableWinRMHttps, reservedIpName, vnetName));
}
catch (Exception e)
{
if (e.ToString().Contains("Service already exists") ||
(e.InnerException != null && e.InnerException.ToString().Contains("Service already exists")))
{
RunPSCmdletAndReturnFirst<ManagementOperationContext>(new NewAzureQuickVMCmdletInfo(
os, name, serviceName, imageName, userName, password, null,
instanceSize, disableWinRMHttps, reservedIpName, vnetName));
}
else
{
Console.WriteLine(e.ToString());
if (e.InnerException != null)
{
Console.WriteLine(e.InnerException.ToString());
}
throw;
}
}
return result;
}
public ManagementOperationContext NewAzureQuickVM(OS os, string name, string serviceName, string imageName,
string userName = null, string password = null, string locationName = null)
{
return NewAzureQuickVM(os, name, serviceName, imageName, userName, password, locationName, null);
}
public ManagementOperationContext NewAzureQuickVM(OS os, string name, string serviceName, string imageName, string[] subnetNames,
InstanceSize instanceSize, string userName, string password, string vNetName, string affinityGroup, string reservedIP = null)
{
var result = new ManagementOperationContext();
try
{
result = RunPSCmdletAndReturnFirst<ManagementOperationContext>(new NewAzureQuickVMCmdletInfo(
os, name, serviceName, imageName, instanceSize.ToString(), userName, password,
vNetName, subnetNames, affinityGroup, reservedIP));
}
catch (Exception e)
{
if (e.ToString().Contains("Service already exists") ||
(e.InnerException != null && e.InnerException.ToString().Contains("Service already exists")))
{
RunPSCmdletAndReturnFirst<ManagementOperationContext>(new NewAzureQuickVMCmdletInfo(
os, name, serviceName, imageName, instanceSize.ToString(), userName, password,
vNetName, subnetNames, null, reservedIP));
}
else
{
Console.WriteLine(e.ToString());
if (e.InnerException != null)
{
Console.WriteLine(e.InnerException.ToString());
}
throw;
}
}
return result;
}
#endregion
#region WinRM
public Uri GetAzureWinRMUri(string servicename, string name)
{
Uri result = null;
try
{
result = RunPSCmdletAndReturnFirst<Uri>(new WinRMCmdletInfo(servicename, name));
}
catch (Exception e)
{
if (e.ToString().Contains("409"))
{
Utilities.RetryActionUntilSuccess(
() => result = RunPSCmdletAndReturnFirst<Uri>(new WinRMCmdletInfo(servicename, name)),
"409", 4, 60);
}
else
{
Console.WriteLine(e.InnerException.ToString());
throw;
}
}
return result;
}
#endregion WinRM
#region AzurePlatformExtension
internal ExtensionCertificateConfig NewAzurePlatformExtensionCertificateConfig(
string storeLocation, string storeName, string thumbAlgorithm, bool thumbRequired = false)
{
return RunPSCmdletAndReturnFirst<ExtensionCertificateConfig>(
new NewAzurePlatformExtensionCertificateConfigCmdletInfo(storeLocation, storeName, thumbAlgorithm, thumbRequired));
}
internal ExtensionEndpointConfigSet NewAzurePlatformExtensionEndpointConfigSet()
{
return RunPSCmdletAndReturnFirst<ExtensionEndpointConfigSet>(
new NewAzurePlatformExtensionEndpointConfigSetCmdletInfo());
}
internal ExtensionEndpointConfigSet SetAzurePlatformExtensionEndpoint(
ExtensionEndpointConfigSet endpointConfig, string inputEndpoint,
string protocol, int port, string localPort)
{
return RunPSCmdletAndReturnFirst<ExtensionEndpointConfigSet>(
new SetAzurePlatformExtensionEndpointCmdletInfo(endpointConfig,
inputEndpoint, null, null, protocol, port, localPort, null, null));
}
internal ExtensionEndpointConfigSet SetAzurePlatformExtensionEndpoint(
ExtensionEndpointConfigSet endpointConfig, string internalEndpoint,
string protocol, int port)
{
return RunPSCmdletAndReturnFirst<ExtensionEndpointConfigSet>(
new SetAzurePlatformExtensionEndpointCmdletInfo(endpointConfig,
null, internalEndpoint, null, protocol, port, null, null, null));
}
internal ExtensionEndpointConfigSet SetAzurePlatformExtensionEndpoint(
ExtensionEndpointConfigSet endpointConfig, string instanceInputEndpoint,
string protocol, string localPort, int portMax, int portMin)
{
return RunPSCmdletAndReturnFirst<ExtensionEndpointConfigSet>(
new SetAzurePlatformExtensionEndpointCmdletInfo(endpointConfig,
null, null, instanceInputEndpoint, protocol, null, localPort, portMax, portMin));
}
internal ExtensionEndpointConfigSet RemoveAzurePlatformExtensionEndpoint(
ExtensionEndpointConfigSet endpointConfig, string epName, string kind)
{
return RunPSCmdletAndReturnFirst<ExtensionEndpointConfigSet>(
new RemoveAzurePlatformExtensionEndpointCmdletInfo(endpointConfig, epName, kind));
}
internal ExtensionLocalResourceConfigSet NewAzurePlatformExtensionLocalResourceConfigSet()
{
return RunPSCmdletAndReturnFirst<ExtensionLocalResourceConfigSet>(
new NewAzurePlatformExtensionLocalResourceConfigSetCmdletInfo());
}
internal ExtensionLocalResourceConfigSet SetAzurePlatformExtensionLocalResource(
ExtensionLocalResourceConfigSet lrConfig, string name, int sizeInMb)
{
return RunPSCmdletAndReturnFirst<ExtensionLocalResourceConfigSet>(
new SetAzurePlatformExtensionLocalResourceCmdletInfo(lrConfig, name, sizeInMb));
}
internal ExtensionLocalResourceConfigSet RemoveAzurePlatformExtensionLocalResource(
ExtensionLocalResourceConfigSet lrConfig, string name)
{
return RunPSCmdletAndReturnFirst<ExtensionLocalResourceConfigSet>(
new RemoveAzurePlatformExtensionLocalResourceCmdletInfo(lrConfig, name));
}
internal ManagementOperationContext PublishAzurePlatformExtension(
string extensionName, string publisher, string version, string hr,
Uri media, string label, string description, string company,
ExtensionCertificateConfig certConfig, ExtensionEndpointConfigSet epConfig, ExtensionLocalResourceConfigSet lrConfig,
DateTime publishDate, string publicSchema, string privateSchema, string sample,
string eula, Uri privacyUri, Uri homepage, string os, string regions,
bool blockRole, bool disallowUpgrade, bool xmlExtension, bool force)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(
new PublishAzurePlatformExtensionCmdletInfo(
extensionName, publisher, version, hr,
media, label, description, company,
certConfig, epConfig, lrConfig,
publishDate, publicSchema, privateSchema, sample,
eula, privacyUri, homepage, os, regions,
blockRole, disallowUpgrade, xmlExtension, force));
}
internal ManagementOperationContext UnpublishAzurePlatformExtension(
string extensionName, string publisher, string version, bool force)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(
new UnpublishAzurePlatformExtensionCmdletInfo(extensionName, publisher, version, force));
}
#endregion
#region AzurePlatformVMImage
internal ComputeImageConfig NewAzurePlatformComputeImageConfig(string offer, string sku, string version)
{
return RunPSCmdletAndReturnFirst<ComputeImageConfig>(new NewAzurePlatformComputeImageConfigCmdletInfo(offer, sku, version));
}
internal MarketplaceImageConfig NewAzurePlatformMarketplaceImageConfig(string planName, string product, string publisher, string publisherId)
{
return RunPSCmdletAndReturnFirst<MarketplaceImageConfig>(new NewAzurePlatformMarketplaceImageConfigCmdletInfo(planName, product, publisher, publisherId));
}
internal ManagementOperationContext SetAzurePlatformVMImageReplicate(string imageName, string[] locations, ComputeImageConfig compCfg, MarketplaceImageConfig marketCfg)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new SetAzurePlatformVMImageCmdletInfo(imageName, null, locations, compCfg, marketCfg));
}
internal ManagementOperationContext SetAzurePlatformVMImagePublic(string imageName)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new SetAzurePlatformVMImageCmdletInfo(imageName, "Public", null));
}
internal ManagementOperationContext SetAzurePlatformVMImagePrivate(string imageName)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new SetAzurePlatformVMImageCmdletInfo(imageName, "Private", null));
}
internal OSImageDetailsContext GetAzurePlatformVMImage(string imageName)
{
return RunPSCmdletAndReturnFirst<OSImageDetailsContext>(new GetAzurePlatformVMImageCmdletInfo(imageName));
}
internal ManagementOperationContext RemoveAzurePlatformVMImage(string imageName)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new RemoveAzurePlatformVMImageCmdletInfo(imageName));
}
#endregion
#region AzureReservedIP
internal ManagementOperationContext NewAzureReservedIP(string name, string location, string label = null)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new NewAzureReservedIPCmdletInfo(name, location, label));
}
internal ManagementOperationContext NewAzureReservedIP(string name, string location, string serviceName, string label = null, string slot = SM.DeploymentSlotType.Production)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new NewAzureReservedIPCmdletInfo(name, location, serviceName, slot, label));
}
internal ManagementOperationContext SetAzureReservedIPAssociation(string reservedIpName, string serviceName, string slot = SM.DeploymentSlotType.Production)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new SetAzureReservedIPAssociationCmdletInfo(reservedIpName, serviceName, slot));
}
internal ManagementOperationContext RemoveAzureReservedIPAssociation(string reservedIpName, string serviceName, bool force, string slot = SM.DeploymentSlotType.Production)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new RemoveAzureReservedIPAssociationCmdletInfo(reservedIpName, serviceName, slot, force));
}
internal Collection<SM.ReservedIPContext> GetAzureReservedIP(string name = null)
{
return RunPSCmdletAndReturnAll<SM.ReservedIPContext>(new GetAzureReservedIPCmdletInfo(name));
}
internal ManagementOperationContext RemoveAzureReservedIP(string name, bool force = false)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new RemoveAzureReservedIPCmdletInfo(name, force));
}
internal ManagementOperationContext AddAzureVirtualIP(string virtualIPName, string serviceName)
{
return RunPSCmdletAndReturnFirst<ManagementOperationContext>(new AddAzureVirtualIPCmdletInfo(virtualIPName, serviceName));
}
internal ManagementOperationContext RemoveAzureVirtualIP(string virtualIPName, string serviceName, bool force = true)
{