Skip to content

Commit 0139426

Browse files
update
1 parent 5a488b6 commit 0139426

File tree

1 file changed

+56
-50
lines changed

1 file changed

+56
-50
lines changed

src/Compute/Compute/VirtualMachine/Operation/NewAzureVMCommand.cs

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ public Parameters(NewAzureVMCommand cmdlet, Client client, IResourceManagementCl
353353
_cmdlet = cmdlet;
354354
_client = client;
355355
_resourceClient = resourceClient;
356+
_cmdlet.validate();
356357
}
357358

358359
public ImageAndOsType ImageAndOsType { get; set; }
@@ -445,17 +446,13 @@ public async Task<ResourceConfig<VirtualMachine>> CreateConfigAsync()
445446

446447

447448
List<SshPublicKey> sshPublicKeyList = null;
448-
if ( !String.IsNullOrEmpty(_cmdlet.SshKeyName) || _cmdlet.GenerateSshKey)
449+
if (!String.IsNullOrEmpty(_cmdlet.SshKeyName))
449450
{
450-
if (ImageAndOsType?.OsType != OperatingSystemTypes.Linux)
451+
SshPublicKey sshPublicKey = _cmdlet.createPublicKeyObject(_cmdlet.Credential.UserName);
452+
sshPublicKeyList = new List<SshPublicKey>()
451453
{
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+
};
459456
}
460457

461458
_cmdlet.ConfigAsyncVisited = true;
@@ -626,13 +623,7 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
626623
}
627624
catch (Microsoft.Rest.Azure.CloudException ex)
628625
{
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();
636627
throw ex;
637628
}
638629

@@ -654,6 +645,8 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
654645

655646
public void DefaultExecuteCmdlet()
656647
{
648+
validate();
649+
657650
base.ExecuteCmdlet();
658651
if (this.VM.DiagnosticsProfile == null)
659652
{
@@ -729,13 +722,8 @@ public void DefaultExecuteCmdlet()
729722

730723
Rest.Azure.AzureOperationResponse<VirtualMachine> result;
731724

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+
{
739727
parameters = addSshPublicKey(parameters);
740728
}
741729

@@ -748,14 +736,8 @@ public void DefaultExecuteCmdlet()
748736
auxAuthHeader).GetAwaiter().GetResult();
749737
}
750738
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();
759741
throw ex;
760742
}
761743

@@ -1055,14 +1037,16 @@ private static string GetStorageAccountNameFromUriString(string uriStr)
10551037
return storageUri.Substring(0, index);
10561038
}
10571039

1058-
private string SshKeyForLinux()
1040+
private SshPublicKey createPublicKeyObject(string username)
10591041
{
1042+
string publicKeyPath = "/home/" + username + "/.ssh/authorized_keys";
1043+
string publicKey = GenerateOrFindSshKey();
1044+
SshPublicKey sshPublicKey = new SshPublicKey(publicKeyPath, publicKey);
1045+
return sshPublicKey;
10601046

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+
{
10661050
string publicKey = "";
10671051
SshPublicKeyResource SshPublicKey;
10681052
if (!this.ConfigAsyncVisited && this.GenerateSshKey.IsPresent)
@@ -1127,19 +1111,7 @@ private string SshKeyForLinux()
11271111

11281112
private VirtualMachine addSshPublicKey(VirtualMachine parameters)
11291113
{
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);
11431115
List<SshPublicKey> sshPublicKeys = new List<SshPublicKey>()
11441116
{
11451117
sshPublicKey
@@ -1153,5 +1125,39 @@ private VirtualMachine addSshPublicKey(VirtualMachine parameters)
11531125

11541126
return parameters;
11551127
}
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+
}
11561162
}
11571163
}

0 commit comments

Comments
 (0)