Skip to content

add params to New-Azvm #15605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
640e981
four cmdlets
grizzlytheodore May 25, 2021
c409671
location to follow resource group
grizzlytheodore May 26, 2021
9f1003c
add supportshouldprocess
grizzlytheodore May 26, 2021
87245d9
update test
grizzlytheodore May 27, 2021
3f83244
update key save location
grizzlytheodore May 28, 2021
26fa526
capitalization
grizzlytheodore Jun 1, 2021
6c3651d
change
grizzlytheodore Jun 1, 2021
1813d9b
change
grizzlytheodore Jun 1, 2021
b266e41
add two new parameter sets
grizzlytheodore Jun 2, 2021
92c785f
update
grizzlytheodore Jun 17, 2021
2a26186
update
grizzlytheodore Jun 29, 2021
242ad63
rebase commit
grizzlytheodore Jul 28, 2021
37f5ffe
update
grizzlytheodore Aug 2, 2021
8d0b2a6
update
grizzlytheodore Aug 4, 2021
174f4e6
changelog
grizzlytheodore Aug 4, 2021
6920983
update code
grizzlytheodore Aug 4, 2021
3565bd3
four cmdlets
grizzlytheodore May 25, 2021
1158a5f
location to follow resource group
grizzlytheodore May 26, 2021
5144461
update test
grizzlytheodore May 27, 2021
5ede851
update key save location
grizzlytheodore May 28, 2021
82a89e3
change
grizzlytheodore Jun 1, 2021
98460cc
add two new parameter sets
grizzlytheodore Jun 2, 2021
f1294d4
update
grizzlytheodore Jun 29, 2021
f5d7fb5
update
grizzlytheodore Aug 2, 2021
7c9e99c
update
grizzlytheodore Aug 4, 2021
9aa5ec1
changelog
grizzlytheodore Aug 4, 2021
f272be6
update code
grizzlytheodore Aug 4, 2021
8f44634
in progress
grizzlytheodore Aug 4, 2021
911fc86
update
grizzlytheodore Aug 4, 2021
6b690df
address comments
grizzlytheodore Aug 4, 2021
5a488b6
update test recording
grizzlytheodore Aug 4, 2021
0139426
update
grizzlytheodore Aug 6, 2021
02d848b
updated validation
grizzlytheodore Aug 11, 2021
9c72965
Update ChangeLog.md
VeryEarly Aug 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -430,5 +430,13 @@ public void TestCapacityReservation()
{
TestRunner.RunTestScript("Test-CapacityReservation");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestVMwithSSHKey()
{
TestRunner.RunTestScript("Test-VMwithSSHKey");
}

}
}
36 changes: 36 additions & 0 deletions src/Compute/Compute.Test/ScenarioTests/VirtualMachineTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5134,6 +5134,42 @@ function Test-CapacityReservation
$CRG = Get-AzCapacityReservationGroup -ResourceGroupName $rgname
Assert-AreEqual 0 $CRG.count

}
finally
{
# Cleanup
Clean-ResourceGroup $rgname;
}
}

function Test-VMwithSSHKey
{
# Setup
$rgname = Get-ComputeTestResourceName;
$loc = Get-ComputeVMLocation;

try
{
New-AzResourceGroup -Name $rgname -Location $loc -Force;


# create credential
$securePassword = Get-PasswordForVM | ConvertTo-SecureString -AsPlainText -Force;
$user = "admin01";
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);

# Add one VM from creation
$vmname = '1' + $rgname;
$domainNameLabel = "d1" + $rgname;
$sshKeyName = "s" + $rgname
$vm = New-AzVM -ResourceGroupName $rgname -Name $vmname -Credential $cred -Image CentOS -DomainNameLabel $domainNameLabel -SshKeyname $sshKeyName -generateSshkey

$vm = Get-AzVm -ResourceGroupName $rgname -Name $vmname
$sshKey = Get-AzSshKey -ResourceGroupName $rgname -Name $sshKeyName

#assert compare
Assert-AreEqual $vm.OSProfile.LinuxConfiguration.Ssh.PublicKeys[0].KeyData $sshKey.publickey

}
finally
{
Expand Down

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Compute/Compute/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

-->
## Upcoming Release
* Added new parameters `-SshKeyName` and `-GenerateSshKey` to `New-AzVM` to create a VM with SSH Key

## Version 4.16.0
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
string evictionPolicy,
double? maxPrice,
bool encryptionAtHostPresent,
List<SshPublicKey> sshPublicKeys,
string networkInterfaceDeleteOption = null,
string osDiskDeleteOption = null,
string dataDiskDeleteOption = null)
Expand All @@ -73,8 +74,11 @@ public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
OsProfile = new OSProfile
{
ComputerName = name,
WindowsConfiguration = imageAndOsType.CreateWindowsConfiguration(),
LinuxConfiguration = imageAndOsType.CreateLinuxConfiguration(),
WindowsConfiguration = imageAndOsType?.CreateWindowsConfiguration(),
LinuxConfiguration = (imageAndOsType?.OsType != OperatingSystemTypes.Linux) ? null : new LinuxConfiguration
{
Ssh = new SshConfiguration(sshPublicKeys)
},
AdminUsername = adminUsername,
AdminPassword = adminPassword,
},
Expand Down
41 changes: 21 additions & 20 deletions src/Compute/Compute/Usage/NewAzureSshKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,29 @@ public override void ExecuteCmdlet()
base.ExecuteCmdlet();
ExecuteClientAction(() =>
{
string resourceGroupName = this.ResourceGroupName;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentations are fixed in this file

string sshKeyName = this.Name;
SshPublicKeyResource result;
SshPublicKeyResource sshkey = new SshPublicKeyResource();
ResourceGroup rg = ArmClient.ResourceGroups.Get(resourceGroupName);
sshkey.Location = rg.Location;
string resourceGroupName = this.ResourceGroupName;
string sshKeyName = this.Name;
SshPublicKeyResource result;
SshPublicKeyResource sshkey = new SshPublicKeyResource();
ResourceGroup rg = ArmClient.ResourceGroups.Get(resourceGroupName);
sshkey.Location = rg.Location;

if (this.IsParameterBound(c => c.PublicKey))
{

sshkey.PublicKey = this.PublicKey;
result = SshPublicKeyClient.Create(resourceGroupName, sshKeyName, sshkey);
}
else
{
WriteDebug("No public key is provided. A key pair is being generated for you.");
if (this.IsParameterBound(c => c.PublicKey))
{

result = SshPublicKeyClient.Create(resourceGroupName, sshKeyName, sshkey);
SshPublicKeyGenerateKeyPairResult keypair = SshPublicKeyClient.GenerateKeyPair(resourceGroupName, sshKeyName);
result.PublicKey = keypair.PublicKey;
sshkey.PublicKey = this.PublicKey;
result = SshPublicKeyClient.Create(resourceGroupName, sshKeyName, sshkey);
}
else
{
WriteDebug("No public key is provided. A key pair is being generated for you.");

result = SshPublicKeyClient.Create(resourceGroupName, sshKeyName, sshkey);
SshPublicKeyGenerateKeyPairResult keypair = SshPublicKeyClient.GenerateKeyPair(resourceGroupName, sshKeyName);
result.PublicKey = keypair.PublicKey;

string sshFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh" );
string sshFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh" );
if (!Directory.Exists(sshFolder))
{
Directory.CreateDirectory(sshFolder);
Expand All @@ -72,8 +73,8 @@ public override void ExecuteCmdlet()
DateTimeOffset now = DateTimeOffset.UtcNow;
string privateKeyFileName = now.ToUnixTimeSeconds().ToString();
string publicKeyFileName = now.ToUnixTimeSeconds().ToString() + ".pub";
string privateKeyFilePath = Path.Combine(sshFolder + privateKeyFileName);
string publicKeyFilePath = Path.Combine(sshFolder + publicKeyFileName);
string privateKeyFilePath = Path.Combine(sshFolder, privateKeyFileName);
string publicKeyFilePath = Path.Combine(sshFolder, publicKeyFileName);
using (StreamWriter writer = new StreamWriter(privateKeyFilePath))
{
writer.WriteLine(keypair.PrivateKey);
Expand Down
Loading