Skip to content

Commit 2f94e55

Browse files
add params to New-Azvm (#15605)
* four cmdlets * location to follow resource group * add supportshouldprocess * update test * update key save location * capitalization * change * change * add two new parameter sets * update * update * update * update * changelog * update code * four cmdlets * location to follow resource group * update test * update key save location * change * add two new parameter sets * update * update * update * changelog * update code * in progress * address comments * update test recording * update * updated validation * Update ChangeLog.md Co-authored-by: Yabo Hu <[email protected]>
1 parent 0fd280b commit 2f94e55

File tree

8 files changed

+3586
-28
lines changed

8 files changed

+3586
-28
lines changed

src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,5 +430,13 @@ public void TestCapacityReservation()
430430
{
431431
TestRunner.RunTestScript("Test-CapacityReservation");
432432
}
433+
434+
[Fact]
435+
[Trait(Category.AcceptanceType, Category.CheckIn)]
436+
public void TestVMwithSSHKey()
437+
{
438+
TestRunner.RunTestScript("Test-VMwithSSHKey");
439+
}
440+
433441
}
434442
}

src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5134,6 +5134,42 @@ function Test-CapacityReservation
51345134
$CRG = Get-AzCapacityReservationGroup -ResourceGroupName $rgname
51355135
Assert-AreEqual 0 $CRG.count
51365136

5137+
}
5138+
finally
5139+
{
5140+
# Cleanup
5141+
Clean-ResourceGroup $rgname;
5142+
}
5143+
}
5144+
5145+
function Test-VMwithSSHKey
5146+
{
5147+
# Setup
5148+
$rgname = Get-ComputeTestResourceName;
5149+
$loc = Get-ComputeVMLocation;
5150+
5151+
try
5152+
{
5153+
New-AzResourceGroup -Name $rgname -Location $loc -Force;
5154+
5155+
5156+
# create credential
5157+
$securePassword = Get-PasswordForVM | ConvertTo-SecureString -AsPlainText -Force;
5158+
$user = "admin01";
5159+
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
5160+
5161+
# Add one VM from creation
5162+
$vmname = '1' + $rgname;
5163+
$domainNameLabel = "d1" + $rgname;
5164+
$sshKeyName = "s" + $rgname
5165+
$vm = New-AzVM -ResourceGroupName $rgname -Name $vmname -Credential $cred -Image CentOS -DomainNameLabel $domainNameLabel -SshKeyname $sshKeyName -generateSshkey
5166+
5167+
$vm = Get-AzVm -ResourceGroupName $rgname -Name $vmname
5168+
$sshKey = Get-AzSshKey -ResourceGroupName $rgname -Name $sshKeyName
5169+
5170+
#assert compare
5171+
Assert-AreEqual $vm.OSProfile.LinuxConfiguration.Ssh.PublicKeys[0].KeyData $sshKey.publickey
5172+
51375173
}
51385174
finally
51395175
{

src/Compute/Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachineTests/TestVMwithSSHKey.json

Lines changed: 3280 additions & 0 deletions
Large diffs are not rendered by default.

src/Compute/Compute/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2121
-->
2222
## Upcoming Release
23+
* Added new parameters `-SshKeyName` and `-GenerateSshKey` to `New-AzVM` to create a VM with SSH Key
2324

2425
## Version 4.16.0
2526
* Fixed the warning in `New-AzVM` cmdlet stating the sku of the VM is being defaulted even if a sku size is provided by the user. Now it only occurs when the user does not provide a sku size.

src/Compute/Compute/Strategies/ComputeRp/VirtualMachineStrategy.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
6161
string evictionPolicy,
6262
double? maxPrice,
6363
bool encryptionAtHostPresent,
64+
List<SshPublicKey> sshPublicKeys,
6465
string networkInterfaceDeleteOption = null,
6566
string osDiskDeleteOption = null,
6667
string dataDiskDeleteOption = null)
@@ -73,8 +74,11 @@ public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
7374
OsProfile = new OSProfile
7475
{
7576
ComputerName = name,
76-
WindowsConfiguration = imageAndOsType.CreateWindowsConfiguration(),
77-
LinuxConfiguration = imageAndOsType.CreateLinuxConfiguration(),
77+
WindowsConfiguration = imageAndOsType?.CreateWindowsConfiguration(),
78+
LinuxConfiguration = (imageAndOsType?.OsType != OperatingSystemTypes.Linux) ? null : new LinuxConfiguration
79+
{
80+
Ssh = new SshConfiguration(sshPublicKeys)
81+
},
7882
AdminUsername = adminUsername,
7983
AdminPassword = adminPassword,
8084
},

src/Compute/Compute/Usage/NewAzureSshKey.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,29 @@ public override void ExecuteCmdlet()
4242
base.ExecuteCmdlet();
4343
ExecuteClientAction(() =>
4444
{
45-
string resourceGroupName = this.ResourceGroupName;
46-
string sshKeyName = this.Name;
47-
SshPublicKeyResource result;
48-
SshPublicKeyResource sshkey = new SshPublicKeyResource();
49-
ResourceGroup rg = ArmClient.ResourceGroups.Get(resourceGroupName);
50-
sshkey.Location = rg.Location;
45+
string resourceGroupName = this.ResourceGroupName;
46+
string sshKeyName = this.Name;
47+
SshPublicKeyResource result;
48+
SshPublicKeyResource sshkey = new SshPublicKeyResource();
49+
ResourceGroup rg = ArmClient.ResourceGroups.Get(resourceGroupName);
50+
sshkey.Location = rg.Location;
5151

52-
if (this.IsParameterBound(c => c.PublicKey))
53-
{
5452

55-
sshkey.PublicKey = this.PublicKey;
56-
result = SshPublicKeyClient.Create(resourceGroupName, sshKeyName, sshkey);
57-
}
58-
else
59-
{
60-
WriteDebug("No public key is provided. A key pair is being generated for you.");
53+
if (this.IsParameterBound(c => c.PublicKey))
54+
{
6155

62-
result = SshPublicKeyClient.Create(resourceGroupName, sshKeyName, sshkey);
63-
SshPublicKeyGenerateKeyPairResult keypair = SshPublicKeyClient.GenerateKeyPair(resourceGroupName, sshKeyName);
64-
result.PublicKey = keypair.PublicKey;
56+
sshkey.PublicKey = this.PublicKey;
57+
result = SshPublicKeyClient.Create(resourceGroupName, sshKeyName, sshkey);
58+
}
59+
else
60+
{
61+
WriteDebug("No public key is provided. A key pair is being generated for you.");
62+
63+
result = SshPublicKeyClient.Create(resourceGroupName, sshKeyName, sshkey);
64+
SshPublicKeyGenerateKeyPairResult keypair = SshPublicKeyClient.GenerateKeyPair(resourceGroupName, sshKeyName);
65+
result.PublicKey = keypair.PublicKey;
6566

66-
string sshFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh" );
67+
string sshFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh" );
6768
if (!Directory.Exists(sshFolder))
6869
{
6970
Directory.CreateDirectory(sshFolder);
@@ -72,8 +73,8 @@ public override void ExecuteCmdlet()
7273
DateTimeOffset now = DateTimeOffset.UtcNow;
7374
string privateKeyFileName = now.ToUnixTimeSeconds().ToString();
7475
string publicKeyFileName = now.ToUnixTimeSeconds().ToString() + ".pub";
75-
string privateKeyFilePath = Path.Combine(sshFolder + privateKeyFileName);
76-
string publicKeyFilePath = Path.Combine(sshFolder + publicKeyFileName);
76+
string privateKeyFilePath = Path.Combine(sshFolder, privateKeyFileName);
77+
string publicKeyFilePath = Path.Combine(sshFolder, publicKeyFileName);
7778
using (StreamWriter writer = new StreamWriter(privateKeyFilePath))
7879
{
7980
writer.WriteLine(keypair.PrivateKey);

0 commit comments

Comments
 (0)