-
Notifications
You must be signed in to change notification settings - Fork 4k
Add a VHD DiskFile parameter set to existing New-AzureRmVM cmdlet #5105
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using Microsoft.Azure.ServiceManagemenet.Common.Models; | ||
using Microsoft.WindowsAzure.Commands.ScenarioTest; | ||
using Xunit; | ||
|
||
namespace Microsoft.Azure.Commands.Compute.Test.ScenarioTests | ||
{ | ||
public class NewVhdVMTests | ||
{ | ||
public NewVhdVMTests(Xunit.Abstractions.ITestOutputHelper output) | ||
{ | ||
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output)); | ||
} | ||
|
||
[Fact] | ||
[Trait(Category.AcceptanceType, Category.CheckIn)] | ||
public void TestWithValidVhdDiskFile() | ||
{ | ||
ComputeTestController.NewInstance.RunPsTest("Test-NewAzureRmVhdVMWithValidDiskFile"); | ||
} | ||
|
||
[Fact] | ||
[Trait(Category.AcceptanceType, Category.CheckIn)] | ||
public void TestWithInvalidVhdDiskFile() | ||
{ | ||
ComputeTestController.NewInstance.RunPsTest("Test-NewAzureRmVhdVMWithInvalidDiskFile"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# ---------------------------------------------------------------------------------- | ||
# | ||
# Copyright Microsoft Corporation | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ---------------------------------------------------------------------------------- | ||
|
||
<# | ||
.SYNOPSIS | ||
Test New-AzureRmVhdVM with a valid disk file | ||
#> | ||
function Test-NewAzureRmVhdVMWithValidDiskFile | ||
{ | ||
|
||
# Setup | ||
$rgname = Get-ComputeTestResourceName | ||
|
||
try | ||
{ | ||
# Common | ||
[string]$loc = Get-ComputeVMLocation; | ||
$loc = $loc.Replace(' ', ''); | ||
|
||
New-AzureRmResourceGroup -Name $rgname -Location $loc -Force; | ||
|
||
# Create a new VM using the tiny VHD file | ||
[string]$file = ".\VhdFiles\tiny.vhd"; | ||
$vm = New-AzureRmVM -ResourceGroupName $rgname -Name $rgname -Location $loc -DiskFile $file; | ||
Assert-AreEqual $vm.Name $rgname; | ||
Assert-AreEqual $vm.Location $loc; | ||
Assert-Null $vm.OSProfile $null; | ||
Assert-Null $vm.StorageProfile.DataDisks; | ||
Assert-NotNull $vm.StorageProfile.OSDisk.ManagedDisk; | ||
} | ||
finally | ||
{ | ||
# Cleanup | ||
Clean-ResourceGroup $rgname; | ||
} | ||
} | ||
|
||
<# | ||
.SYNOPSIS | ||
Test New-AzureRmVhdVM with an invalid disk file | ||
#> | ||
function Test-NewAzureRmVhdVMWithInvalidDiskFile | ||
{ | ||
# Setup | ||
$rgname = Get-ComputeTestResourceName; | ||
|
||
try | ||
{ | ||
# Create an invalid VHD file | ||
[string]$file1 = ".\test_invalid_file_1.vhd"; | ||
$st = Set-Content -Path $file1 -Value "test1" -Force; | ||
|
||
# Common | ||
[string]$loc = Get-ComputeVMLocation; | ||
$loc = $loc.Replace(' ', ''); | ||
|
||
New-AzureRmResourceGroup -Name $rgname -Location $loc -Force; | ||
|
||
# Try to create a VM using the VHD file | ||
$expectedException = $false; | ||
$expectedErrorMessage = "*unsupported format*"; | ||
try | ||
{ | ||
$st = New-AzureRmVM -ResourceGroupName $rgname -Name $rgname -Location $loc -Linux -DiskFile $file1; | ||
} | ||
catch | ||
{ | ||
if ($_ -like $expectedErrorMessage) | ||
{ | ||
$expectedException = $true; | ||
} | ||
} | ||
|
||
if (-not $expectedException) | ||
{ | ||
throw "Expected exception from calling New-AzureRmVM was not caught: '$expectedErrorMessage'."; | ||
} | ||
} | ||
finally | ||
{ | ||
# Cleanup | ||
Clean-ResourceGroup $rgname; | ||
} | ||
} |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using Microsoft.Azure.Commands.Compute.Strategies.ResourceManager; | ||
using Microsoft.Azure.Management.Compute; | ||
using Microsoft.Azure.Management.Compute.Models; | ||
using Microsoft.Azure.Management.Internal.Resources.Models; | ||
|
||
namespace Microsoft.Azure.Commands.Common.Strategies.Compute | ||
{ | ||
static class ManagedDiskStrategy | ||
{ | ||
public static ResourceStrategy<Disk> Strategy { get; } | ||
= ComputePolicy.Create( | ||
type: "disk", | ||
provider: "disks", | ||
getOperations: client => client.Disks, | ||
getAsync: (o, p) => o.GetAsync( | ||
p.ResourceGroupName, p.Name, p.CancellationToken), | ||
createOrUpdateAsync: (o, p) => o.CreateOrUpdateAsync( | ||
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken), | ||
createTime: d => 120); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it take 2 minutes (120 seconds) in average to create a disk ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's about right. What would happen the real timespan is more? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use the number to show progress, it's ok if takes longer or shorter. We need an average. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. Should be fine. |
||
|
||
public static ResourceConfig<Disk> CreateManagedDiskConfig( | ||
this ResourceConfig<ResourceGroup> resourceGroup, | ||
string name, | ||
string sourceUri) | ||
=> Strategy.CreateResourceConfig( | ||
resourceGroup: resourceGroup, | ||
name: name, | ||
createModel: subscription => new Disk | ||
{ | ||
Sku = new DiskSku | ||
{ | ||
Name = StorageAccountTypes.PremiumLRS | ||
}, | ||
CreationData = new CreationData | ||
{ | ||
CreateOption = DiskCreateOption.Import, | ||
SourceUri = sourceUri | ||
} | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ static class VirtualMachineStrategy | |
p.ResourceGroupName, p.Name, null, p.CancellationToken), | ||
createOrUpdateAsync: (o, p) => o.CreateOrUpdateAsync( | ||
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken), | ||
createTime: c => c.OsProfile.WindowsConfiguration != null ? 240 : 120); | ||
createTime: c => c != null && c.OsProfile != null && c.OsProfile.WindowsConfiguration != null ? 240 : 120); | ||
|
||
public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig( | ||
this ResourceConfig<ResourceGroup> resourceGroup, | ||
|
@@ -78,8 +78,52 @@ public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig( | |
Sku = image.sku, | ||
Version = image.version | ||
} | ||
}, | ||
} | ||
}, | ||
dependencies: new[] { networkInterface }); | ||
|
||
public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig( | ||
this ResourceConfig<ResourceGroup> resourceGroup, | ||
string name, | ||
ResourceConfig<NetworkInterface> networkInterface, | ||
bool isWindows, | ||
ResourceConfig<Disk> disk, | ||
string size) | ||
=> Strategy.CreateResourceConfig( | ||
resourceGroup: resourceGroup, | ||
name: name, | ||
createModel: subscription => new VirtualMachine | ||
{ | ||
OsProfile = null, | ||
NetworkProfile = new NetworkProfile | ||
{ | ||
NetworkInterfaces = new[] | ||
{ | ||
new NetworkInterfaceReference | ||
{ | ||
Id = networkInterface.GetId(subscription).IdToString() | ||
} | ||
} | ||
}, | ||
HardwareProfile = new HardwareProfile | ||
{ | ||
VmSize = size | ||
}, | ||
StorageProfile = new StorageProfile | ||
{ | ||
OsDisk = new OSDisk | ||
{ | ||
Name = disk.Name, | ||
CreateOption = DiskCreateOptionTypes.Attach, | ||
OsType = isWindows ? OperatingSystemTypes.Windows : OperatingSystemTypes.Linux, | ||
ManagedDisk = new ManagedDiskParameters | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO, we should modify the existing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the other comment. |
||
{ | ||
StorageAccountType = StorageAccountTypes.PremiumLRS, | ||
Id = disk.GetId(subscription).IdToString() | ||
} | ||
} | ||
}, | ||
}, | ||
dependencies: new IEntityConfig[] { networkInterface, disk }); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, I don't think this is necessary if we model this cmdlet correctly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would need to address this part in a separate PR.