Skip to content

Commit 6b690df

Browse files
address comments
1 parent 911fc86 commit 6b690df

File tree

2 files changed

+56
-105
lines changed

2 files changed

+56
-105
lines changed

src/Compute/Compute/Usage/UpdateAzureSshKey.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,6 @@ public partial class UpdateAzureSshKey : ComputeAutomationBaseCmdlet
5151
[ValidateNotNullOrEmpty]
5252
public PSSshPublicKeyResource InputObject { get; set; }
5353

54-
[Parameter(
55-
Mandatory = true,
56-
ParameterSetName = ResourceIDParameterSet,
57-
ValueFromPipelineByPropertyName = true,
58-
HelpMessage = "Resource ID for your SSH Public Key Resource.")]
59-
[ResourceIdCompleter("Microsoft.Compute/SshPublicKeys")]
60-
public string ResourceId { get; set; }
61-
62-
[Alias("SshKey")]
63-
[Parameter(
64-
Mandatory = true,
65-
ParameterSetName = InputObjectParameterSet,
66-
ValueFromPipeline = true,
67-
ValueFromPipelineByPropertyName = true,
68-
HelpMessage = "PowerShell Ssh Public Key Object")]
69-
[ValidateNotNullOrEmpty]
70-
public PSSshPublicKeyResource InputObject { get; set; }
71-
7254
[Parameter(
7355
Mandatory = true,
7456
ValueFromPipelineByPropertyName = true)]

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

Lines changed: 56 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -302,37 +302,27 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
302302
HelpMessage = "Name of the SSH Public Key resource.",
303303
ParameterSetName = SimpleParameterSet)]
304304
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; }
316305

317306
[Parameter(
318307
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.",
320309
ParameterSetName = DefaultParameterSet)]
321310
[Parameter(
322311
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.",
324313
ParameterSetName = SimpleParameterSet)]
325-
public string SshKeyName { get; set; }
314+
public SwitchParameter GenerateSshKey { get; set; }
326315

327316
[Parameter(
328317
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.")]
331320
[Parameter(
332321
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; }
336326

337327
public override void ExecuteCmdlet()
338328
{
@@ -630,27 +620,22 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
630620
}
631621

632622
VirtualMachine result;
633-
if (this.GenerateSshKey.IsPresent)
623+
try
634624
{
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)
640630
{
641-
642631
//delete the created ssh key resource
643-
644632
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);
648634
}
635+
// throw exception
636+
throw ex;
649637
}
650-
else
651-
{
652-
result = await client.RunAsync(client.SubscriptionId, parameters, asyncCmdlet);
653-
}
638+
654639

655640
if (result != null)
656641
{
@@ -752,33 +737,27 @@ public void DefaultExecuteCmdlet()
752737
}
753738

754739
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-
}
773740
}
774-
else
741+
742+
try
775743
{
776744
result = this.VirtualMachineClient.CreateOrUpdateWithHttpMessagesAsync(
777745
this.ResourceGroupName,
778746
this.VM.Name,
779747
parameters,
780748
auxAuthHeader).GetAwaiter().GetResult();
781749
}
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+
}
782761

783762
var psResult = ComputeAutoMapperProfile.Mapper.Map<PSAzureOperationResponse>(result);
784763

@@ -1078,41 +1057,28 @@ private static string GetStorageAccountNameFromUriString(string uriStr)
10781057

10791058
private string SshKeyForLinux()
10801059
{
1081-
if (this.ConfigAsyncVisited)
1082-
{
1083-
this.GenerateSshKey = false;
1084-
}
10851060

10861061
if (this.GenerateSshKey.IsPresent && ! this.IsParameterBound(c => c.SshKeyName))
10871062
{
10881063
throw new Exception("Please provide parameter '-SshKeyName' to be used with '-GenerateSshKey'");
10891064
}
10901065

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 = "";
10981067
SshPublicKeyResource SshPublicKey;
1099-
string publicKey = "value";
1100-
try
1068+
if (!this.ConfigAsyncVisited && this.GenerateSshKey.IsPresent)
11011069
{
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)
11101076
{
11111077
//create key
11121078
SshPublicKeyResource sshkey = new SshPublicKeyResource();
11131079
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);
11161082

11171083
string sshFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh");
11181084
if (!Directory.Exists(sshFolder))
@@ -1139,21 +1105,24 @@ private string SshKeyForLinux()
11391105

11401106
return keypair.PublicKey;
11411107
}
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)
11431120
{
1144-
// Generate Ssh Key needs to be provided
11451121
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;
11471122
}
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-
}
11551123

1156-
return publicKey;
1124+
return publicKey;
1125+
}
11571126
}
11581127

11591128
private VirtualMachine addSshPublicKey(VirtualMachine parameters)

0 commit comments

Comments
 (0)