Skip to content

Domain name label with randomness for New-AzureRMVM and VMSS. #5399

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 16 commits into from
Feb 7, 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
1 change: 1 addition & 0 deletions src/ResourceManager/Compute/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Current Release
* Added `FullyQualifiedDomainName` to `PSVirtualMachinScaleSet`.
* Added `AvailabilitySetName` parameter to the simplified parameterset of `New-AzureRmVm`.
* Corrected usage of `Login-AzureRmAccount` to use `Connect-AzureRmAccount`
* User assigned identity support for VM and VM scale set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ function Test-NewAzureRmVhdVMWithValidDiskFile
# Create a new VM using the tiny VHD file
[string]$file = ".\VhdFiles\tiny.vhd";
$vmname = $rgname + 'vm';
$vm = New-AzureRmVM -ResourceGroupName $rgname -Name $vmname -Location $loc -DiskFile $file -OpenPorts 1234;
[string]$domainNameLabel = "$vmname-$rgname".tolower();
$vm = New-AzureRmVM -ResourceGroupName $rgname -Name $vmname -Location $loc -DiskFile $file -OpenPorts 1234 -DomainNameLabel $domainNameLabel;
Assert-AreEqual $vm.Name $vmname;
Assert-AreEqual $vm.Location $loc;
Assert-Null $vm.OSProfile $null;
Expand Down Expand Up @@ -80,7 +81,8 @@ function Test-NewAzureRmVhdVMWithInvalidDiskFile
$expectedErrorMessage = "*unsupported format*";
try
{
$st = New-AzureRmVM -ResourceGroupName $rgname -Name $rgname -Location $loc -Linux -DiskFile $file1 -OpenPorts 1234;
[string]$domainNameLabel = "$rgname-$rgname".tolower();
$st = New-AzureRmVM -ResourceGroupName $rgname -Name $rgname -Location $loc -Linux -DiskFile $file1 -OpenPorts 1234 -DomainNameLabel $domainNameLabel;
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public StrategiesVirtualMachineTests(Xunit.Abstractions.ITestOutputHelper output
}

[Fact]
[Trait(Category.RunType, Category.CheckIn)]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestSimpleNewVm()
{
ComputeTestController.NewInstance.RunPsTest("Test-SimpleNewVm");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ function Test-SimpleNewVm
$username = "admin01"
$password = "werWER345#%^" | ConvertTo-SecureString -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
[string]$domainNameLabel = "$vmname-$vmname".tolower();

# Common
$x = New-AzureRmVM -Name $vmname -Credential $cred
$x = New-AzureRmVM -Name $vmname -Credential $cred -DomainNameLabel $domainNameLabel

Assert-AreEqual $vmname $x.Name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public StrategiesVmssTests(Xunit.Abstractions.ITestOutputHelper output)
}

[Fact]
[Trait(Category.RunType, Category.CheckIn)]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestSimpleNewVmss()
{
ComputeTestController.NewInstance.RunPsTest("Test-SimpleNewVmss");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ function Test-SimpleNewVmss
$username = "admin01"
$password = "werWER345#%^" | ConvertTo-SecureString -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
[string]$domainNameLabel = "$vmssname$vmssname".tolower();

# Common
$x = New-AzureRmVmss -Name $vmssname -Credential $cred
$x = New-AzureRmVmss -Name $vmssname -Credential $cred -DomainNameLabel $domainNameLabel

Assert-AreEqual $vmssname $x.Name;
Assert-AreEqual $vmssname $x.ResourceGroupName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@ public string ResourceGroupName
return m.Success ? m.Groups["rgname"].Value : null;
}
}

// Gets or sets the FQDN.
public string FullyQualifiedDomainName { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
VirtualNetworkName = VirtualNetworkName ?? VMScaleSetName;
SubnetName = SubnetName ?? VMScaleSetName;
PublicIpAddressName = PublicIpAddressName ?? VMScaleSetName;
DomainNameLabel = DomainNameLabel ?? (VMScaleSetName + ResourceGroupName).ToLower();
SecurityGroupName = SecurityGroupName ?? VMScaleSetName;
LoadBalancerName = LoadBalancerName ?? VMScaleSetName;
FrontendPoolName = FrontendPoolName ?? VMScaleSetName;
Expand Down Expand Up @@ -203,12 +202,12 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
}

BackendPort = BackendPort ?? (isWindows ? new[] { 3389, 5985 } : new[] { 22 });

var resourceGroup = ResourceGroupStrategy.CreateResourceGroupConfig(ResourceGroupName);

var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig(
name: PublicIpAddressName,
domainNameLabel: DomainNameLabel,
getDomainNameLabel: () => DomainNameLabel,
allocationMethod: AllocationMethod);

var virtualNetwork = resourceGroup.CreateVirtualNetworkConfig(
Expand Down Expand Up @@ -244,7 +243,9 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
image: image,
vmSize: VmSize,
instanceCount: InstanceCount,
upgradeMode: (MyInvocation.BoundParameters.ContainsKey("UpgradePolicyMode") == true ) ? UpgradePolicyMode : (UpgradeMode?) null);
upgradeMode: (MyInvocation.BoundParameters.ContainsKey("UpgradePolicyMode") == true )
? UpgradePolicyMode
: (UpgradeMode?) null);

var client = new Client(DefaultProfile.DefaultContext);

Expand All @@ -260,6 +261,15 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
}
}

// generate a domain name label if it's not specified.
DomainNameLabel = await PublicIPAddressStrategy.UpdateDomainNameLabelAsync(
domainNameLabel: DomainNameLabel,
name: VMScaleSetName,
location: Location,
client: client);

var fqdn = PublicIPAddressStrategy.Fqdn(DomainNameLabel, Location);

var target = virtualMachineScaleSet.GetTargetState(current, client.SubscriptionId, Location);

var newState = await virtualMachineScaleSet
Expand All @@ -268,7 +278,7 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
target,
new CancellationToken(),
new ShouldProcess(asyncCmdlet),
asyncCmdlet.ReportTaskProgress);
asyncCmdlet.ReportTaskProgress);

var result = newState.Get(virtualMachineScaleSet);
if(result == null)
Expand All @@ -279,7 +289,8 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
if (result != null)
{
var psObject = new PSVirtualMachineScaleSet();
ComputeAutomationAutoMapperProfile.Mapper.Map<VirtualMachineScaleSet, PSVirtualMachineScaleSet>(result, psObject);
ComputeAutomationAutoMapperProfile.Mapper.Map(result, psObject);
psObject.FullyQualifiedDomainName = fqdn;
asyncCmdlet.WriteObject(psObject);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01;
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01.Models;
using Microsoft.Azure.Management.Internal.Resources.Models;
using System;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.Common.Strategies.Network
{
Expand All @@ -35,7 +37,7 @@ static class PublicIPAddressStrategy
public static ResourceConfig<PublicIPAddress> CreatePublicIPAddressConfig(
this ResourceConfig<ResourceGroup> resourceGroup,
string name,
string domainNameLabel,
Func<string> getDomainNameLabel,
string allocationMethod)
=> Strategy.CreateResourceConfig(
resourceGroup: resourceGroup,
Expand All @@ -45,8 +47,33 @@ public static ResourceConfig<PublicIPAddress> CreatePublicIPAddressConfig(
PublicIPAllocationMethod = allocationMethod,
DnsSettings = new PublicIPAddressDnsSettings
{
DomainNameLabel = domainNameLabel,
DomainNameLabel = getDomainNameLabel(),
}
});

public static async Task<string> UpdateDomainNameLabelAsync(
string domainNameLabel,
string name,
string location,
IClient client)
{
if (domainNameLabel == null)
{
var networkClient = client.GetClient<NetworkManagementClient>();
do
{
domainNameLabel = (name + '-' + Guid.NewGuid().ToString().Substring(0, 6))
.ToLower();
} while ((await networkClient.CheckDnsNameAvailabilityAsync(
location,
domainNameLabel))
.Available
!= true);
}
return domainNameLabel;
}

public static string Fqdn(string domainNameLabel, string location)
=> domainNameLabel + "." + location + ".cloudapp.azure.com";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01;
using Microsoft.Azure.Management.Internal.Resources;
using Microsoft.Azure.Management.Internal.Resources.Models;
using Microsoft.Azure.Management.Storage;
Expand Down Expand Up @@ -234,7 +235,6 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
VirtualNetworkName = VirtualNetworkName ?? Name;
SubnetName = SubnetName ?? Name;
PublicIpAddressName = PublicIpAddressName ?? Name;
DomainNameLabel = DomainNameLabel ?? (Name + '-' + ResourceGroupName).ToLower();
SecurityGroupName = SecurityGroupName ?? Name;

bool isWindows;
Expand Down Expand Up @@ -286,7 +286,7 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
var subnet = virtualNetwork.CreateSubnet(SubnetName, SubnetAddressPrefix);
var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig(
name: PublicIpAddressName,
domainNameLabel: DomainNameLabel,
getDomainNameLabel: () => DomainNameLabel,
allocationMethod: AllocationMethod);
var networkSecurityGroup = resourceGroup.CreateNetworkSecurityGroupConfig(
name: SecurityGroupName,
Expand Down Expand Up @@ -408,7 +408,14 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
}
}

var fqdn = DomainNameLabel + "." + Location + ".cloudapp.azure.com";
// generate a domain name label if it's not specified.
DomainNameLabel = await PublicIPAddressStrategy.UpdateDomainNameLabelAsync(
domainNameLabel: DomainNameLabel,
name: Name,
location: Location,
client: client);

var fqdn = PublicIPAddressStrategy.Fqdn(DomainNameLabel, Location);

// create target state
var target = virtualMachine.GetTargetState(current, client.SubscriptionId, Location);
Expand Down
5 changes: 5 additions & 0 deletions src/ResourceManager/Compute/Compute.sln
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.Common.Graph.RBAC"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.Common.Strategies.UnitTest", "..\Common\Commands.Common.Strategies.UnitTest\Commands.Common.Strategies.UnitTest.csproj", "{6756A7F2-1141-4065-BA23-0C555D2A2BC3}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B395D5F1-BB9A-481C-A395-9EA5B8F68E1B}"
ProjectSection(SolutionItems) = preProject
ChangeLog.md = ChangeLog.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down