Skip to content

Commit 44f20d8

Browse files
authored
Merge pull request #5399 from sergey-shandar/sergey-domain-name-fix
Domain name label with randomness for New-AzureRMVM and VMSS.
2 parents 2fb2085 + f67e9aa commit 44f20d8

File tree

11 files changed

+75
-17
lines changed

11 files changed

+75
-17
lines changed

src/ResourceManager/Compute/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Current Release
21+
* Added `FullyQualifiedDomainName` to `PSVirtualMachinScaleSet`.
2122
* Added `AvailabilitySetName` parameter to the simplified parameterset of `New-AzureRmVm`.
2223
* Corrected usage of `Login-AzureRmAccount` to use `Connect-AzureRmAccount`
2324
* User assigned identity support for VM and VM scale set

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/NewVhdVMTests.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ function Test-NewAzureRmVhdVMWithValidDiskFile
3333
# Create a new VM using the tiny VHD file
3434
[string]$file = ".\VhdFiles\tiny.vhd";
3535
$vmname = $rgname + 'vm';
36-
$vm = New-AzureRmVM -ResourceGroupName $rgname -Name $vmname -Location $loc -DiskFile $file -OpenPorts 1234;
36+
[string]$domainNameLabel = "$vmname-$rgname".tolower();
37+
$vm = New-AzureRmVM -ResourceGroupName $rgname -Name $vmname -Location $loc -DiskFile $file -OpenPorts 1234 -DomainNameLabel $domainNameLabel;
3738
Assert-AreEqual $vm.Name $vmname;
3839
Assert-AreEqual $vm.Location $loc;
3940
Assert-Null $vm.OSProfile $null;
@@ -80,7 +81,8 @@ function Test-NewAzureRmVhdVMWithInvalidDiskFile
8081
$expectedErrorMessage = "*unsupported format*";
8182
try
8283
{
83-
$st = New-AzureRmVM -ResourceGroupName $rgname -Name $rgname -Location $loc -Linux -DiskFile $file1 -OpenPorts 1234;
84+
[string]$domainNameLabel = "$rgname-$rgname".tolower();
85+
$st = New-AzureRmVM -ResourceGroupName $rgname -Name $rgname -Location $loc -Linux -DiskFile $file1 -OpenPorts 1234 -DomainNameLabel $domainNameLabel;
8486
}
8587
catch
8688
{

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/StrategiesVirtualMachineTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public StrategiesVirtualMachineTests(Xunit.Abstractions.ITestOutputHelper output
2626
}
2727

2828
[Fact]
29-
[Trait(Category.RunType, Category.CheckIn)]
29+
[Trait(Category.AcceptanceType, Category.CheckIn)]
3030
public void TestSimpleNewVm()
3131
{
3232
ComputeTestController.NewInstance.RunPsTest("Test-SimpleNewVm");

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/StrategiesVirtualMachineTests.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ function Test-SimpleNewVm
2626
$username = "admin01"
2727
$password = "werWER345#%^" | ConvertTo-SecureString -AsPlainText -Force
2828
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
29+
[string]$domainNameLabel = "$vmname-$vmname".tolower();
2930

3031
# Common
31-
$x = New-AzureRmVM -Name $vmname -Credential $cred
32+
$x = New-AzureRmVM -Name $vmname -Credential $cred -DomainNameLabel $domainNameLabel
3233

3334
Assert-AreEqual $vmname $x.Name;
3435
}

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/StrategiesVmssTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public StrategiesVmssTests(Xunit.Abstractions.ITestOutputHelper output)
2626
}
2727

2828
[Fact]
29-
[Trait(Category.RunType, Category.CheckIn)]
29+
[Trait(Category.AcceptanceType, Category.CheckIn)]
3030
public void TestSimpleNewVmss()
3131
{
3232
ComputeTestController.NewInstance.RunPsTest("Test-SimpleNewVmss");

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/StrategiesVmssTests.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ function Test-SimpleNewVmss
2626
$username = "admin01"
2727
$password = "werWER345#%^" | ConvertTo-SecureString -AsPlainText -Force
2828
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
29+
[string]$domainNameLabel = "$vmssname$vmssname".tolower();
2930

3031
# Common
31-
$x = New-AzureRmVmss -Name $vmssname -Credential $cred
32+
$x = New-AzureRmVmss -Name $vmssname -Credential $cred -DomainNameLabel $domainNameLabel
3233

3334
Assert-AreEqual $vmssname $x.Name;
3435
Assert-AreEqual $vmssname $x.ResourceGroupName;

src/ResourceManager/Compute/Commands.Compute/Generated/Models/PSVirtualMachineScaleSet.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@ public string ResourceGroupName
3737
return m.Success ? m.Groups["rgname"].Value : null;
3838
}
3939
}
40+
41+
// Gets or sets the FQDN.
42+
public string FullyQualifiedDomainName { get; set; }
4043
}
4144
}

src/ResourceManager/Compute/Commands.Compute/Generated/VirtualMachineScaleSet/VirtualMachineScaleSetCreateOrUpdateMethod.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
159159
VirtualNetworkName = VirtualNetworkName ?? VMScaleSetName;
160160
SubnetName = SubnetName ?? VMScaleSetName;
161161
PublicIpAddressName = PublicIpAddressName ?? VMScaleSetName;
162-
DomainNameLabel = DomainNameLabel ?? (VMScaleSetName + ResourceGroupName).ToLower();
163162
SecurityGroupName = SecurityGroupName ?? VMScaleSetName;
164163
LoadBalancerName = LoadBalancerName ?? VMScaleSetName;
165164
FrontendPoolName = FrontendPoolName ?? VMScaleSetName;
@@ -203,12 +202,12 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
203202
}
204203

205204
BackendPort = BackendPort ?? (isWindows ? new[] { 3389, 5985 } : new[] { 22 });
206-
205+
207206
var resourceGroup = ResourceGroupStrategy.CreateResourceGroupConfig(ResourceGroupName);
208207

209208
var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig(
210209
name: PublicIpAddressName,
211-
domainNameLabel: DomainNameLabel,
210+
getDomainNameLabel: () => DomainNameLabel,
212211
allocationMethod: AllocationMethod);
213212

214213
var virtualNetwork = resourceGroup.CreateVirtualNetworkConfig(
@@ -244,7 +243,9 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
244243
image: image,
245244
vmSize: VmSize,
246245
instanceCount: InstanceCount,
247-
upgradeMode: (MyInvocation.BoundParameters.ContainsKey("UpgradePolicyMode") == true ) ? UpgradePolicyMode : (UpgradeMode?) null);
246+
upgradeMode: (MyInvocation.BoundParameters.ContainsKey("UpgradePolicyMode") == true )
247+
? UpgradePolicyMode
248+
: (UpgradeMode?) null);
248249

249250
var client = new Client(DefaultProfile.DefaultContext);
250251

@@ -260,6 +261,15 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
260261
}
261262
}
262263

264+
// generate a domain name label if it's not specified.
265+
DomainNameLabel = await PublicIPAddressStrategy.UpdateDomainNameLabelAsync(
266+
domainNameLabel: DomainNameLabel,
267+
name: VMScaleSetName,
268+
location: Location,
269+
client: client);
270+
271+
var fqdn = PublicIPAddressStrategy.Fqdn(DomainNameLabel, Location);
272+
263273
var target = virtualMachineScaleSet.GetTargetState(current, client.SubscriptionId, Location);
264274

265275
var newState = await virtualMachineScaleSet
@@ -268,7 +278,7 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
268278
target,
269279
new CancellationToken(),
270280
new ShouldProcess(asyncCmdlet),
271-
asyncCmdlet.ReportTaskProgress);
281+
asyncCmdlet.ReportTaskProgress);
272282

273283
var result = newState.Get(virtualMachineScaleSet);
274284
if(result == null)
@@ -279,7 +289,8 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
279289
if (result != null)
280290
{
281291
var psObject = new PSVirtualMachineScaleSet();
282-
ComputeAutomationAutoMapperProfile.Mapper.Map<VirtualMachineScaleSet, PSVirtualMachineScaleSet>(result, psObject);
292+
ComputeAutomationAutoMapperProfile.Mapper.Map(result, psObject);
293+
psObject.FullyQualifiedDomainName = fqdn;
283294
asyncCmdlet.WriteObject(psObject);
284295
}
285296
}

src/ResourceManager/Compute/Commands.Compute/Strategies/Network/PublicIPAddressStrategy.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01;
1717
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01.Models;
1818
using Microsoft.Azure.Management.Internal.Resources.Models;
19+
using System;
20+
using System.Threading.Tasks;
1921

2022
namespace Microsoft.Azure.Commands.Common.Strategies.Network
2123
{
@@ -35,7 +37,7 @@ static class PublicIPAddressStrategy
3537
public static ResourceConfig<PublicIPAddress> CreatePublicIPAddressConfig(
3638
this ResourceConfig<ResourceGroup> resourceGroup,
3739
string name,
38-
string domainNameLabel,
40+
Func<string> getDomainNameLabel,
3941
string allocationMethod)
4042
=> Strategy.CreateResourceConfig(
4143
resourceGroup: resourceGroup,
@@ -45,8 +47,33 @@ public static ResourceConfig<PublicIPAddress> CreatePublicIPAddressConfig(
4547
PublicIPAllocationMethod = allocationMethod,
4648
DnsSettings = new PublicIPAddressDnsSettings
4749
{
48-
DomainNameLabel = domainNameLabel,
50+
DomainNameLabel = getDomainNameLabel(),
4951
}
5052
});
53+
54+
public static async Task<string> UpdateDomainNameLabelAsync(
55+
string domainNameLabel,
56+
string name,
57+
string location,
58+
IClient client)
59+
{
60+
if (domainNameLabel == null)
61+
{
62+
var networkClient = client.GetClient<NetworkManagementClient>();
63+
do
64+
{
65+
domainNameLabel = (name + '-' + Guid.NewGuid().ToString().Substring(0, 6))
66+
.ToLower();
67+
} while ((await networkClient.CheckDnsNameAvailabilityAsync(
68+
location,
69+
domainNameLabel))
70+
.Available
71+
!= true);
72+
}
73+
return domainNameLabel;
74+
}
75+
76+
public static string Fqdn(string domainNameLabel, string location)
77+
=> domainNameLabel + "." + location + ".cloudapp.azure.com";
5178
}
5279
}

src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Operation/NewAzureVMCommand.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
2828
using Microsoft.Azure.Management.Compute;
2929
using Microsoft.Azure.Management.Compute.Models;
30+
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01;
3031
using Microsoft.Azure.Management.Internal.Resources;
3132
using Microsoft.Azure.Management.Internal.Resources.Models;
3233
using Microsoft.Azure.Management.Storage;
@@ -234,7 +235,6 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
234235
VirtualNetworkName = VirtualNetworkName ?? Name;
235236
SubnetName = SubnetName ?? Name;
236237
PublicIpAddressName = PublicIpAddressName ?? Name;
237-
DomainNameLabel = DomainNameLabel ?? (Name + '-' + ResourceGroupName).ToLower();
238238
SecurityGroupName = SecurityGroupName ?? Name;
239239

240240
bool isWindows;
@@ -286,7 +286,7 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
286286
var subnet = virtualNetwork.CreateSubnet(SubnetName, SubnetAddressPrefix);
287287
var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig(
288288
name: PublicIpAddressName,
289-
domainNameLabel: DomainNameLabel,
289+
getDomainNameLabel: () => DomainNameLabel,
290290
allocationMethod: AllocationMethod);
291291
var networkSecurityGroup = resourceGroup.CreateNetworkSecurityGroupConfig(
292292
name: SecurityGroupName,
@@ -408,7 +408,14 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
408408
}
409409
}
410410

411-
var fqdn = DomainNameLabel + "." + Location + ".cloudapp.azure.com";
411+
// generate a domain name label if it's not specified.
412+
DomainNameLabel = await PublicIPAddressStrategy.UpdateDomainNameLabelAsync(
413+
domainNameLabel: DomainNameLabel,
414+
name: Name,
415+
location: Location,
416+
client: client);
417+
418+
var fqdn = PublicIPAddressStrategy.Fqdn(DomainNameLabel, Location);
412419

413420
// create target state
414421
var target = virtualMachine.GetTargetState(current, client.SubscriptionId, Location);

src/ResourceManager/Compute/Compute.sln

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands.Common.Graph.RBAC"
4646
EndProject
4747
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}"
4848
EndProject
49+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B395D5F1-BB9A-481C-A395-9EA5B8F68E1B}"
50+
ProjectSection(SolutionItems) = preProject
51+
ChangeLog.md = ChangeLog.md
52+
EndProjectSection
53+
EndProject
4954
Global
5055
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5156
Debug|Any CPU = Debug|Any CPU

0 commit comments

Comments
 (0)