Skip to content

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

Merged
merged 1 commit into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -172,6 +172,7 @@
<Compile Include="ScenarioTests\DiagnosticsExtensionTests.cs" />
<Compile Include="ScenarioTests\DiskRPTests.cs" />
<Compile Include="ScenarioTests\ImageTests.cs" />
<Compile Include="ScenarioTests\NewVhdVMTests.cs" />
<Compile Include="ScenarioTests\ResourceSkuTests.cs" />
<Compile Include="ScenarioTests\SqlIaaSExtensionTests.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand Down Expand Up @@ -305,6 +306,9 @@
<None Include="ScenarioTests\ImageTests.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="ScenarioTests\NewVhdVMTests.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="ScenarioTests\ResourceSkuTests.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -376,6 +380,9 @@
<None Include="Templates\tstorgnztn-validator.pem">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="VhdFiles\tiny.vhd">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Expand Down
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.

Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
<Compile Include="Strategies\Compute\Images.cs" />
<Compile Include="Strategies\Compute\VirtualMachineStrategy.cs" />
<Compile Include="Strategies\IAsyncCmdlet.cs" />
<Compile Include="Strategies\Compute\ManagedDiskStrategy.cs" />
<Compile Include="Strategies\Network\NetworkInterfaceStrategy.cs" />
<Compile Include="Strategies\Network\NetworkSecurityGroupPolicy.cs" />
<Compile Include="Strategies\Network\NetworkStrategy.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public UploadParameters(BlobUri destinationUri, BlobUri baseImageUri, FileInfo l

public int NumberOfUploaderThreads { get; private set; }

public AddAzureVhdCommand Cmdlet { get; set; }
public ComputeClientBaseCmdlet Cmdlet { get; set; }
Copy link
Member

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

Copy link
Contributor Author

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.


public CloudPageBlobObjectFactory BlobObjectFactory { get; set; }
}
Expand Down
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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO, we should modify the existing CreateVirtualMachineConfig and have ResourceConfig<Disk> disk as an optional parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 });
}
}
Loading