@@ -302,37 +302,27 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
302
302
HelpMessage = "Name of the SSH Public Key resource." ,
303
303
ParameterSetName = SimpleParameterSet ) ]
304
304
public string SshKeyName { get ; set ; }
305
-
306
- [ Parameter (
307
- Mandatory = false ,
308
- ParameterSetName = SimpleParameterSet ,
309
- HelpMessage = "Id of the capacity reservation Group that is used to allocate." ) ]
310
- [ Parameter (
311
- Mandatory = false ,
312
- ParameterSetName = DiskFileParameterSet ,
313
- HelpMessage = "Id of the capacity reservation Group that is used to allocate." ) ]
314
- [ ResourceIdCompleter ( "Microsoft.Compute/capacityReservationGroups" ) ]
315
- public string CapacityReservationGroupId { get ; set ; }
316
305
317
306
[ Parameter (
318
307
Mandatory = false ,
319
- HelpMessage = "Name of the SSH Public Key resource." ,
308
+ HelpMessage = "Generate a SSH Public/Private key pair and create a SSH Public Key resource on Azure ." ,
320
309
ParameterSetName = DefaultParameterSet ) ]
321
310
[ Parameter (
322
311
Mandatory = false ,
323
- HelpMessage = "Name of the SSH Public Key resource." ,
312
+ HelpMessage = "Generate a SSH Public/Private key pair and create a SSH Public Key resource on Azure ." ,
324
313
ParameterSetName = SimpleParameterSet ) ]
325
- public string SshKeyName { get ; set ; }
314
+ public SwitchParameter GenerateSshKey { get ; set ; }
326
315
327
316
[ Parameter (
328
317
Mandatory = false ,
329
- HelpMessage = "Generate a SSH Public/Private key pair and create a SSH Public Key resource on Azure." ,
330
- ParameterSetName = DefaultParameterSet ) ]
318
+ ParameterSetName = SimpleParameterSet ,
319
+ HelpMessage = "Id of the capacity reservation Group that is used to allocate." ) ]
331
320
[ Parameter (
332
321
Mandatory = false ,
333
- HelpMessage = "Generate a SSH Public/Private key pair and create a SSH Public Key resource on Azure." ,
334
- ParameterSetName = SimpleParameterSet ) ]
335
- public SwitchParameter GenerateSshKey { get ; set ; }
322
+ ParameterSetName = DiskFileParameterSet ,
323
+ HelpMessage = "Id of the capacity reservation Group that is used to allocate." ) ]
324
+ [ ResourceIdCompleter ( "Microsoft.Compute/capacityReservationGroups" ) ]
325
+ public string CapacityReservationGroupId { get ; set ; }
336
326
337
327
public override void ExecuteCmdlet ( )
338
328
{
@@ -630,27 +620,22 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
630
620
}
631
621
632
622
VirtualMachine result ;
633
- if ( this . GenerateSshKey . IsPresent )
623
+ try
634
624
{
635
- try
636
- {
637
- result = await client . RunAsync ( client . SubscriptionId , parameters , asyncCmdlet ) ;
638
- }
639
- catch ( Microsoft . Rest . Azure . CloudException ex )
625
+ result = await client . RunAsync ( client . SubscriptionId , parameters , asyncCmdlet ) ;
626
+ }
627
+ catch ( Microsoft . Rest . Azure . CloudException ex )
628
+ {
629
+ if ( this . GenerateSshKey . IsPresent )
640
630
{
641
-
642
631
//delete the created ssh key resource
643
-
644
632
WriteInformation ( "VM creation failed. Deleting the SSH key resource that was created." , new string [ ] { "PSHOST" } ) ;
645
- SshPublicKeyClient . DeleteWithHttpMessagesAsync ( this . ResourceGroupName , this . SshKeyName ) . GetAwaiter ( ) . GetResult ( ) ;
646
- // throw exception
647
- throw ex ;
633
+ this . ComputeClient . ComputeManagementClient . SshPublicKeys . Delete ( this . ResourceGroupName , this . SshKeyName ) ;
648
634
}
635
+ // throw exception
636
+ throw ex ;
649
637
}
650
- else
651
- {
652
- result = await client . RunAsync ( client . SubscriptionId , parameters , asyncCmdlet ) ;
653
- }
638
+
654
639
655
640
if ( result != null )
656
641
{
@@ -752,33 +737,27 @@ public void DefaultExecuteCmdlet()
752
737
}
753
738
754
739
parameters = addSshPublicKey ( parameters ) ;
755
-
756
- try
757
- {
758
- result = this . VirtualMachineClient . CreateOrUpdateWithHttpMessagesAsync (
759
- this . ResourceGroupName ,
760
- this . VM . Name ,
761
- parameters ,
762
- auxAuthHeader ) . GetAwaiter ( ) . GetResult ( ) ;
763
- }
764
- catch ( Exception ex )
765
- {
766
- // delete the created ssh key resource
767
- WriteWarning ( "VM creation failed. Deleting the SSH key resource that was created." ) ;
768
- Microsoft . Azure . Commands . Compute . Automation . NewAzureSshKey sshKeyClass = new Microsoft . Azure . Commands . Compute . Automation . NewAzureSshKey ( ) ;
769
- sshKeyClass . SshPublicKeyClient . Delete ( this . ResourceGroupName , this . SshKeyName ) ;
770
- // throw exception
771
- throw ex ;
772
- }
773
740
}
774
- else
741
+
742
+ try
775
743
{
776
744
result = this . VirtualMachineClient . CreateOrUpdateWithHttpMessagesAsync (
777
745
this . ResourceGroupName ,
778
746
this . VM . Name ,
779
747
parameters ,
780
748
auxAuthHeader ) . GetAwaiter ( ) . GetResult ( ) ;
781
749
}
750
+ catch ( Exception ex )
751
+ {
752
+ if ( this . GenerateSshKey . IsPresent )
753
+ {
754
+ // delete the created ssh key resource
755
+ WriteWarning ( "VM creation failed. Deleting the SSH key resource that was created." ) ;
756
+ this . ComputeClient . ComputeManagementClient . SshPublicKeys . Delete ( this . ResourceGroupName , this . SshKeyName ) ;
757
+ }
758
+ // throw exception
759
+ throw ex ;
760
+ }
782
761
783
762
var psResult = ComputeAutoMapperProfile . Mapper . Map < PSAzureOperationResponse > ( result ) ;
784
763
@@ -1078,41 +1057,28 @@ private static string GetStorageAccountNameFromUriString(string uriStr)
1078
1057
1079
1058
private string SshKeyForLinux ( )
1080
1059
{
1081
- if ( this . ConfigAsyncVisited )
1082
- {
1083
- this . GenerateSshKey = false ;
1084
- }
1085
1060
1086
1061
if ( this . GenerateSshKey . IsPresent && ! this . IsParameterBound ( c => c . SshKeyName ) )
1087
1062
{
1088
1063
throw new Exception ( "Please provide parameter '-SshKeyName' to be used with '-GenerateSshKey'" ) ;
1089
1064
}
1090
1065
1091
-
1092
- // check if -SshKeyName exists in azure
1093
- // TODO
1094
- //ComputeClient computeClinet = new ComputeClient(this.DefaultContext);
1095
- //computeClinet.ComputeManagementClient.SshPublicKeys sshPublicKeys
1096
- //ISshPublicKeysOperation sshKeyClass = Microsoft.Azure.Commands.Compute.Automation.ComputeAutomationBaseCmdlet.SshPublicKeyClient
1097
- Microsoft . Azure . Commands . Compute . Automation . NewAzureSshKey sshKeyClass = new Microsoft . Azure . Commands . Compute . Automation . NewAzureSshKey ( ) ;
1066
+ string publicKey = "" ;
1098
1067
SshPublicKeyResource SshPublicKey ;
1099
- string publicKey = "value" ;
1100
- try
1068
+ if ( ! this . ConfigAsyncVisited && this . GenerateSshKey . IsPresent )
1101
1069
{
1102
- SshPublicKey = sshKeyClass . SshPublicKeyClient . Get ( this . ResourceGroupName , this . SshKeyName ) ;
1103
- publicKey = SshPublicKey . PublicKey ;
1104
- }
1105
- catch ( Rest . Azure . CloudException ex )
1106
- {
1107
- // SshKey Does not exist
1108
- // is -GenerateSshKeyGiven?
1109
- if ( this . GenerateSshKey . IsPresent )
1070
+ try
1071
+ {
1072
+ SshPublicKey = this . ComputeClient . ComputeManagementClient . SshPublicKeys . Get ( this . ResourceGroupName , this . SshKeyName ) ;
1073
+ publicKey = SshPublicKey . PublicKey ;
1074
+ }
1075
+ catch ( Rest . Azure . CloudException )
1110
1076
{
1111
1077
//create key
1112
1078
SshPublicKeyResource sshkey = new SshPublicKeyResource ( ) ;
1113
1079
sshkey . Location = this . Location != null ? this . Location : "eastus" ;
1114
- SshPublicKey = sshKeyClass . SshPublicKeyClient . Create ( this . ResourceGroupName , this . SshKeyName , sshkey ) ;
1115
- SshPublicKeyGenerateKeyPairResult keypair = sshKeyClass . SshPublicKeyClient . GenerateKeyPair ( this . ResourceGroupName , this . SshKeyName ) ;
1080
+ SshPublicKey = this . ComputeClient . ComputeManagementClient . SshPublicKeys . Create ( this . ResourceGroupName , this . SshKeyName , sshkey ) ;
1081
+ SshPublicKeyGenerateKeyPairResult keypair = this . ComputeClient . ComputeManagementClient . SshPublicKeys . GenerateKeyPair ( this . ResourceGroupName , this . SshKeyName ) ;
1116
1082
1117
1083
string sshFolder = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) , ".ssh" ) ;
1118
1084
if ( ! Directory . Exists ( sshFolder ) )
@@ -1139,21 +1105,24 @@ private string SshKeyForLinux()
1139
1105
1140
1106
return keypair . PublicKey ;
1141
1107
}
1142
- else
1108
+
1109
+ throw new Exception ( "The SSH Public Key resource with name '-SshKeyName' already exists. Either remove '-GenerateSshKey' to use the existing resource or update '-SshKeyName' to create a SSH Public Key resource with a different name." ) ;
1110
+
1111
+ }
1112
+ else
1113
+ {
1114
+ try
1115
+ {
1116
+ SshPublicKey = this . ComputeClient . ComputeManagementClient . SshPublicKeys . Get ( this . ResourceGroupName , this . SshKeyName ) ;
1117
+ publicKey = SshPublicKey . PublicKey ;
1118
+ }
1119
+ catch ( Rest . Azure . CloudException )
1143
1120
{
1144
- // Generate Ssh Key needs to be provided
1145
1121
throw new Exception ( "The SSH Public Key resource with name '-SshKeyName' was not found. Either provide '-GenerateSshKey' to create the resource or provide a '-SshKeyName' that exists in the provided Resource Group." ) ;
1146
- throw ex ;
1147
1122
}
1148
- }
1149
-
1150
- // SshKey does exist
1151
- if ( this . GenerateSshKey . IsPresent )
1152
- {
1153
- throw new Exception ( "The SSH Public Key resource with name '-SshKeyName' already exists. Either remove '-GenerateSshKey' to use the existing resource or update '-SshKeyName' to create a SSH Public Key resource with a different name." ) ;
1154
- }
1155
1123
1156
- return publicKey ;
1124
+ return publicKey ;
1125
+ }
1157
1126
}
1158
1127
1159
1128
private VirtualMachine addSshPublicKey ( VirtualMachine parameters )
0 commit comments