@@ -353,6 +353,7 @@ public Parameters(NewAzureVMCommand cmdlet, Client client, IResourceManagementCl
353
353
_cmdlet = cmdlet ;
354
354
_client = client ;
355
355
_resourceClient = resourceClient ;
356
+ _cmdlet . validate ( ) ;
356
357
}
357
358
358
359
public ImageAndOsType ImageAndOsType { get ; set ; }
@@ -445,17 +446,13 @@ public async Task<ResourceConfig<VirtualMachine>> CreateConfigAsync()
445
446
446
447
447
448
List < SshPublicKey > sshPublicKeyList = null ;
448
- if ( ! String . IsNullOrEmpty ( _cmdlet . SshKeyName ) || _cmdlet . GenerateSshKey )
449
+ if ( ! String . IsNullOrEmpty ( _cmdlet . SshKeyName ) )
449
450
{
450
- if ( ImageAndOsType ? . OsType != OperatingSystemTypes . Linux )
451
+ SshPublicKey sshPublicKey = _cmdlet . createPublicKeyObject ( _cmdlet . Credential . UserName ) ;
452
+ sshPublicKeyList = new List < SshPublicKey > ( )
451
453
{
452
- throw new Exception ( "Parameters '-SshKeyName' and '-GenerateSshKey' are only allowed with Linux VMs" ) ;
453
- }
454
-
455
- string publicKey = _cmdlet . SshKeyForLinux ( ) ;
456
- SshPublicKey sshPublicKey = new SshPublicKey ( "/home/" + _cmdlet . Credential . UserName + "/.ssh/authorized_keys" , publicKey ) ;
457
- sshPublicKeyList = new List < SshPublicKey > ( ) ;
458
- sshPublicKeyList . Add ( sshPublicKey ) ;
454
+ sshPublicKey
455
+ } ;
459
456
}
460
457
461
458
_cmdlet . ConfigAsyncVisited = true ;
@@ -626,13 +623,7 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
626
623
}
627
624
catch ( Microsoft . Rest . Azure . CloudException ex )
628
625
{
629
- if ( this . GenerateSshKey . IsPresent )
630
- {
631
- //delete the created ssh key resource
632
- WriteInformation ( "VM creation failed. Deleting the SSH key resource that was created." , new string [ ] { "PSHOST" } ) ;
633
- this . ComputeClient . ComputeManagementClient . SshPublicKeys . Delete ( this . ResourceGroupName , this . SshKeyName ) ;
634
- }
635
- // throw exception
626
+ cleanUp ( ) ;
636
627
throw ex ;
637
628
}
638
629
@@ -654,6 +645,8 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
654
645
655
646
public void DefaultExecuteCmdlet ( )
656
647
{
648
+ validate ( ) ;
649
+
657
650
base . ExecuteCmdlet ( ) ;
658
651
if ( this . VM . DiagnosticsProfile == null )
659
652
{
@@ -729,13 +722,8 @@ public void DefaultExecuteCmdlet()
729
722
730
723
Rest . Azure . AzureOperationResponse < VirtualMachine > result ;
731
724
732
- if ( this . IsParameterBound ( c => c . SshKeyName ) || this . GenerateSshKey . IsPresent )
733
- {
734
- if ( ! IsLinuxOs ( ) )
735
- {
736
- throw new Exception ( "Parameters '-SshKeyName' and '-GenerateSshKey' are only allowed with Linux VMs" ) ;
737
- }
738
-
725
+ if ( this . IsParameterBound ( c => c . SshKeyName ) )
726
+ {
739
727
parameters = addSshPublicKey ( parameters ) ;
740
728
}
741
729
@@ -748,14 +736,8 @@ public void DefaultExecuteCmdlet()
748
736
auxAuthHeader ) . GetAwaiter ( ) . GetResult ( ) ;
749
737
}
750
738
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
739
+ {
740
+ cleanUp ( ) ;
759
741
throw ex ;
760
742
}
761
743
@@ -1055,14 +1037,16 @@ private static string GetStorageAccountNameFromUriString(string uriStr)
1055
1037
return storageUri . Substring ( 0 , index ) ;
1056
1038
}
1057
1039
1058
- private string SshKeyForLinux ( )
1040
+ private SshPublicKey createPublicKeyObject ( string username )
1059
1041
{
1042
+ string publicKeyPath = "/home/" + username + "/.ssh/authorized_keys" ;
1043
+ string publicKey = GenerateOrFindSshKey ( ) ;
1044
+ SshPublicKey sshPublicKey = new SshPublicKey ( publicKeyPath , publicKey ) ;
1045
+ return sshPublicKey ;
1060
1046
1061
- if ( this . GenerateSshKey . IsPresent && ! this . IsParameterBound ( c => c . SshKeyName ) )
1062
- {
1063
- throw new Exception ( "Please provide parameter '-SshKeyName' to be used with '-GenerateSshKey'" ) ;
1064
- }
1065
-
1047
+ }
1048
+ private string GenerateOrFindSshKey ( )
1049
+ {
1066
1050
string publicKey = "" ;
1067
1051
SshPublicKeyResource SshPublicKey ;
1068
1052
if ( ! this . ConfigAsyncVisited && this . GenerateSshKey . IsPresent )
@@ -1127,19 +1111,7 @@ private string SshKeyForLinux()
1127
1111
1128
1112
private VirtualMachine addSshPublicKey ( VirtualMachine parameters )
1129
1113
{
1130
- string publicKeyPath ;
1131
- if ( parameters . OsProfile ? . AdminUsername != null )
1132
- {
1133
- publicKeyPath = "/home/" + parameters . OsProfile . AdminUsername + "/.ssh/authorized_keys" ;
1134
- }
1135
- else
1136
- {
1137
- publicKeyPath = "/home/azureuser/.ssh/authorized_keys" ;
1138
- }
1139
-
1140
- string publicKey = SshKeyForLinux ( ) ;
1141
-
1142
- SshPublicKey sshPublicKey = new SshPublicKey ( publicKeyPath , publicKey ) ;
1114
+ SshPublicKey sshPublicKey = createPublicKeyObject ( parameters . OsProfile . AdminUsername ) ;
1143
1115
List < SshPublicKey > sshPublicKeys = new List < SshPublicKey > ( )
1144
1116
{
1145
1117
sshPublicKey
@@ -1153,5 +1125,39 @@ private VirtualMachine addSshPublicKey(VirtualMachine parameters)
1153
1125
1154
1126
return parameters ;
1155
1127
}
1128
+
1129
+ private void cleanUp ( )
1130
+ {
1131
+ if ( this . GenerateSshKey . IsPresent )
1132
+ {
1133
+ //delete the created ssh key resource
1134
+ WriteInformation ( "VM creation failed. Deleting the SSH key resource that was created." , new string [ ] { "PSHOST" } ) ;
1135
+ this . ComputeClient . ComputeManagementClient . SshPublicKeys . Delete ( this . ResourceGroupName , this . SshKeyName ) ;
1136
+ }
1137
+ }
1138
+
1139
+ private void validate ( )
1140
+ {
1141
+ if ( this . GenerateSshKey . IsPresent && ! this . IsParameterBound ( c => c . SshKeyName ) )
1142
+ {
1143
+ throw new Exception ( "Please provide parameter '-SshKeyName' to be used with '-GenerateSshKey'" ) ;
1144
+ }
1145
+
1146
+ if ( this . ParameterSetName == "DefaultParameterSet" && ! IsLinuxOs ( ) )
1147
+ {
1148
+ throw new Exception ( "Parameters '-SshKeyName' and '-GenerateSshKey' are only allowed with Linux VMs" ) ;
1149
+ }
1150
+
1151
+ if ( this . ParameterSetName == "SimpleParameterSet" )
1152
+ {
1153
+ var client = new Client ( DefaultProfile . DefaultContext ) ;
1154
+ ImageAndOsType ImageAndOsType = client . UpdateImageAndOsTypeAsync (
1155
+ null , this . ResourceGroupName , this . Image , "" ) . Result ;
1156
+ if ( ImageAndOsType ? . OsType != OperatingSystemTypes . Linux )
1157
+ {
1158
+ throw new Exception ( "Parameters '-SshKeyName' and '-GenerateSshKey' are only allowed with Linux VMs" ) ;
1159
+ }
1160
+ }
1161
+ }
1156
1162
}
1157
1163
}
0 commit comments