Skip to content

Commit 4280292

Browse files
authored
Merge pull request #5105 from huangpf/vhd2
Add a VHD DiskFile parameter set to existing New-AzureRmVM cmdlet
2 parents de77b11 + a2345c3 commit 4280292

File tree

12 files changed

+18814
-40
lines changed

12 files changed

+18814
-40
lines changed

src/ResourceManager/Compute/Commands.Compute.Test/Commands.Compute.Test.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
<Compile Include="ScenarioTests\DiagnosticsExtensionTests.cs" />
173173
<Compile Include="ScenarioTests\DiskRPTests.cs" />
174174
<Compile Include="ScenarioTests\ImageTests.cs" />
175+
<Compile Include="ScenarioTests\NewVhdVMTests.cs" />
175176
<Compile Include="ScenarioTests\ResourceSkuTests.cs" />
176177
<Compile Include="ScenarioTests\SqlIaaSExtensionTests.cs">
177178
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
@@ -305,6 +306,9 @@
305306
<None Include="ScenarioTests\ImageTests.ps1">
306307
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
307308
</None>
309+
<None Include="ScenarioTests\NewVhdVMTests.ps1">
310+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
311+
</None>
308312
<None Include="ScenarioTests\ResourceSkuTests.ps1">
309313
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
310314
</None>
@@ -376,6 +380,9 @@
376380
<None Include="Templates\tstorgnztn-validator.pem">
377381
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
378382
</None>
383+
<None Include="VhdFiles\tiny.vhd">
384+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
385+
</None>
379386
</ItemGroup>
380387
<ItemGroup>
381388
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.ServiceManagemenet.Common.Models;
16+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
17+
using Xunit;
18+
19+
namespace Microsoft.Azure.Commands.Compute.Test.ScenarioTests
20+
{
21+
public class NewVhdVMTests
22+
{
23+
public NewVhdVMTests(Xunit.Abstractions.ITestOutputHelper output)
24+
{
25+
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
26+
}
27+
28+
[Fact]
29+
[Trait(Category.AcceptanceType, Category.CheckIn)]
30+
public void TestWithValidVhdDiskFile()
31+
{
32+
ComputeTestController.NewInstance.RunPsTest("Test-NewAzureRmVhdVMWithValidDiskFile");
33+
}
34+
35+
[Fact]
36+
[Trait(Category.AcceptanceType, Category.CheckIn)]
37+
public void TestWithInvalidVhdDiskFile()
38+
{
39+
ComputeTestController.NewInstance.RunPsTest("Test-NewAzureRmVhdVMWithInvalidDiskFile");
40+
}
41+
}
42+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
<#
16+
.SYNOPSIS
17+
Test New-AzureRmVhdVM with a valid disk file
18+
#>
19+
function Test-NewAzureRmVhdVMWithValidDiskFile
20+
{
21+
22+
# Setup
23+
$rgname = Get-ComputeTestResourceName
24+
25+
try
26+
{
27+
# Common
28+
[string]$loc = Get-ComputeVMLocation;
29+
$loc = $loc.Replace(' ', '');
30+
31+
New-AzureRmResourceGroup -Name $rgname -Location $loc -Force;
32+
33+
# Create a new VM using the tiny VHD file
34+
[string]$file = ".\VhdFiles\tiny.vhd";
35+
$vm = New-AzureRmVM -ResourceGroupName $rgname -Name $rgname -Location $loc -DiskFile $file;
36+
Assert-AreEqual $vm.Name $rgname;
37+
Assert-AreEqual $vm.Location $loc;
38+
Assert-Null $vm.OSProfile $null;
39+
Assert-Null $vm.StorageProfile.DataDisks;
40+
Assert-NotNull $vm.StorageProfile.OSDisk.ManagedDisk;
41+
}
42+
finally
43+
{
44+
# Cleanup
45+
Clean-ResourceGroup $rgname;
46+
}
47+
}
48+
49+
<#
50+
.SYNOPSIS
51+
Test New-AzureRmVhdVM with an invalid disk file
52+
#>
53+
function Test-NewAzureRmVhdVMWithInvalidDiskFile
54+
{
55+
# Setup
56+
$rgname = Get-ComputeTestResourceName;
57+
58+
try
59+
{
60+
# Create an invalid VHD file
61+
[string]$file1 = ".\test_invalid_file_1.vhd";
62+
$st = Set-Content -Path $file1 -Value "test1" -Force;
63+
64+
# Common
65+
[string]$loc = Get-ComputeVMLocation;
66+
$loc = $loc.Replace(' ', '');
67+
68+
New-AzureRmResourceGroup -Name $rgname -Location $loc -Force;
69+
70+
# Try to create a VM using the VHD file
71+
$expectedException = $false;
72+
$expectedErrorMessage = "*unsupported format*";
73+
try
74+
{
75+
$st = New-AzureRmVM -ResourceGroupName $rgname -Name $rgname -Location $loc -Linux -DiskFile $file1;
76+
}
77+
catch
78+
{
79+
if ($_ -like $expectedErrorMessage)
80+
{
81+
$expectedException = $true;
82+
}
83+
}
84+
85+
if (-not $expectedException)
86+
{
87+
throw "Expected exception from calling New-AzureRmVM was not caught: '$expectedErrorMessage'.";
88+
}
89+
}
90+
finally
91+
{
92+
# Cleanup
93+
Clean-ResourceGroup $rgname;
94+
}
95+
}

src/ResourceManager/Compute/Commands.Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.NewVhdVMTests/TestWithInvalidVhdDiskFile.json

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

src/ResourceManager/Compute/Commands.Compute.Test/SessionRecords/Microsoft.Azure.Commands.Compute.Test.ScenarioTests.NewVhdVMTests/TestWithValidVhdDiskFile.json

Lines changed: 10102 additions & 0 deletions
Large diffs are not rendered by default.
Binary file not shown.

src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@
277277
<Compile Include="Strategies\Compute\Images.cs" />
278278
<Compile Include="Strategies\Compute\VirtualMachineStrategy.cs" />
279279
<Compile Include="Strategies\IAsyncCmdlet.cs" />
280+
<Compile Include="Strategies\Compute\ManagedDiskStrategy.cs" />
280281
<Compile Include="Strategies\Network\NetworkInterfaceStrategy.cs" />
281282
<Compile Include="Strategies\Network\NetworkSecurityGroupPolicy.cs" />
282283
<Compile Include="Strategies\Network\NetworkStrategy.cs" />

src/ResourceManager/Compute/Commands.Compute/Models/UploadParameters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public UploadParameters(BlobUri destinationUri, BlobUri baseImageUri, FileInfo l
3939

4040
public int NumberOfUploaderThreads { get; private set; }
4141

42-
public AddAzureVhdCommand Cmdlet { get; set; }
42+
public ComputeClientBaseCmdlet Cmdlet { get; set; }
4343

4444
public CloudPageBlobObjectFactory BlobObjectFactory { get; set; }
4545
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Compute.Strategies.ResourceManager;
16+
using Microsoft.Azure.Management.Compute;
17+
using Microsoft.Azure.Management.Compute.Models;
18+
using Microsoft.Azure.Management.Internal.Resources.Models;
19+
20+
namespace Microsoft.Azure.Commands.Common.Strategies.Compute
21+
{
22+
static class ManagedDiskStrategy
23+
{
24+
public static ResourceStrategy<Disk> Strategy { get; }
25+
= ComputePolicy.Create(
26+
type: "disk",
27+
provider: "disks",
28+
getOperations: client => client.Disks,
29+
getAsync: (o, p) => o.GetAsync(
30+
p.ResourceGroupName, p.Name, p.CancellationToken),
31+
createOrUpdateAsync: (o, p) => o.CreateOrUpdateAsync(
32+
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken),
33+
createTime: d => 120);
34+
35+
public static ResourceConfig<Disk> CreateManagedDiskConfig(
36+
this ResourceConfig<ResourceGroup> resourceGroup,
37+
string name,
38+
string sourceUri)
39+
=> Strategy.CreateResourceConfig(
40+
resourceGroup: resourceGroup,
41+
name: name,
42+
createModel: subscription => new Disk
43+
{
44+
Sku = new DiskSku
45+
{
46+
Name = StorageAccountTypes.PremiumLRS
47+
},
48+
CreationData = new CreationData
49+
{
50+
CreateOption = DiskCreateOption.Import,
51+
SourceUri = sourceUri
52+
}
53+
});
54+
}
55+
}

src/ResourceManager/Compute/Commands.Compute/Strategies/Compute/VirtualMachineStrategy.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static class VirtualMachineStrategy
3131
p.ResourceGroupName, p.Name, null, p.CancellationToken),
3232
createOrUpdateAsync: (o, p) => o.CreateOrUpdateAsync(
3333
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken),
34-
createTime: c => c.OsProfile.WindowsConfiguration != null ? 240 : 120);
34+
createTime: c => c != null && c.OsProfile != null && c.OsProfile.WindowsConfiguration != null ? 240 : 120);
3535

3636
public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
3737
this ResourceConfig<ResourceGroup> resourceGroup,
@@ -78,8 +78,52 @@ public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
7878
Sku = image.sku,
7979
Version = image.version
8080
}
81-
},
81+
}
8282
},
8383
dependencies: new[] { networkInterface });
84+
85+
public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
86+
this ResourceConfig<ResourceGroup> resourceGroup,
87+
string name,
88+
ResourceConfig<NetworkInterface> networkInterface,
89+
bool isWindows,
90+
ResourceConfig<Disk> disk,
91+
string size)
92+
=> Strategy.CreateResourceConfig(
93+
resourceGroup: resourceGroup,
94+
name: name,
95+
createModel: subscription => new VirtualMachine
96+
{
97+
OsProfile = null,
98+
NetworkProfile = new NetworkProfile
99+
{
100+
NetworkInterfaces = new[]
101+
{
102+
new NetworkInterfaceReference
103+
{
104+
Id = networkInterface.GetId(subscription).IdToString()
105+
}
106+
}
107+
},
108+
HardwareProfile = new HardwareProfile
109+
{
110+
VmSize = size
111+
},
112+
StorageProfile = new StorageProfile
113+
{
114+
OsDisk = new OSDisk
115+
{
116+
Name = disk.Name,
117+
CreateOption = DiskCreateOptionTypes.Attach,
118+
OsType = isWindows ? OperatingSystemTypes.Windows : OperatingSystemTypes.Linux,
119+
ManagedDisk = new ManagedDiskParameters
120+
{
121+
StorageAccountType = StorageAccountTypes.PremiumLRS,
122+
Id = disk.GetId(subscription).IdToString()
123+
}
124+
}
125+
},
126+
},
127+
dependencies: new IEntityConfig[] { networkInterface, disk });
84128
}
85129
}

0 commit comments

Comments
 (0)