Skip to content

Commit baba2cc

Browse files
Domain name label with randomness.
1 parent 633f4b6 commit baba2cc

File tree

5 files changed

+60
-11
lines changed

5 files changed

+60
-11
lines changed

src/ResourceManager/Common/Commands.Common.Strategies/Commands.Common.Strategies.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<Compile Include="IResourceStrategy.cs" />
6161
<Compile Include="IShouldProcess.cs" />
6262
<Compile Include="ITaskProgress.cs" />
63+
<Compile Include="Mutable.cs" />
6364
<Compile Include="ProgressMap.cs" />
6465
<Compile Include="TimeSlot.cs" />
6566
<Compile Include="StateOperationContext.cs" />
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Microsoft.Azure.Commands.Common.Strategies
2+
{
3+
public sealed class Mutable<T>
4+
{
5+
public T Value { get; set; }
6+
}
7+
8+
public static class Mutable
9+
{
10+
public static Mutable<T> Create<T>(T value)
11+
=> new Mutable<T> { Value = value };
12+
}
13+
}

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
160160
VirtualNetworkName = VirtualNetworkName ?? VMScaleSetName;
161161
SubnetName = SubnetName ?? VMScaleSetName;
162162
PublicIpAddressName = PublicIpAddressName ?? VMScaleSetName;
163-
DomainNameLabel = DomainNameLabel ?? (VMScaleSetName + ResourceGroupName).ToLower();
164163
SecurityGroupName = SecurityGroupName ?? VMScaleSetName;
165164
LoadBalancerName = LoadBalancerName ?? VMScaleSetName;
166165
FrontendPoolName = FrontendPoolName ?? VMScaleSetName;
@@ -204,12 +203,14 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
204203
}
205204

206205
BackendPort = BackendPort ?? (isWindows ? new[] { 3389, 5985 } : new[] { 22 });
207-
206+
207+
var domainNameLabel = Mutable.Create(DomainNameLabel);
208+
208209
var resourceGroup = ResourceGroupStrategy.CreateResourceGroupConfig(ResourceGroupName);
209210

210211
var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig(
211212
name: PublicIpAddressName,
212-
domainNameLabel: DomainNameLabel,
213+
domainNameLabel: domainNameLabel,
213214
allocationMethod: AllocationMethod);
214215

215216
var virtualNetwork = resourceGroup.CreateVirtualNetworkConfig(
@@ -245,7 +246,9 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
245246
image: image,
246247
vmSize: VmSize,
247248
instanceCount: InstanceCount,
248-
upgradeMode: (MyInvocation.BoundParameters.ContainsKey("UpgradePolicyMode") == true ) ? UpgradePolicyMode : (UpgradeMode?) null);
249+
upgradeMode: (MyInvocation.BoundParameters.ContainsKey("UpgradePolicyMode") == true )
250+
? UpgradePolicyMode
251+
: (UpgradeMode?) null);
249252

250253
var client = new Client(DefaultProfile.DefaultContext);
251254

@@ -261,6 +264,10 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
261264
}
262265
}
263266

267+
// generate a domain name label if it's not specified.
268+
await PublicIPAddressStrategy.FindDomainNameLabelAsync(
269+
domainNameLabel: domainNameLabel, name: VMScaleSetName, location: Location, client: client);
270+
264271
var target = virtualMachineScaleSet.GetTargetState(current, client.SubscriptionId, Location);
265272

266273
var newState = await virtualMachineScaleSet
@@ -269,7 +276,7 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
269276
target,
270277
new CancellationToken(),
271278
new ShouldProcess(asyncCmdlet),
272-
asyncCmdlet.ReportTaskProgress);
279+
asyncCmdlet.ReportTaskProgress);
273280

274281
var result = newState.Get(virtualMachineScaleSet);
275282
if(result == null)
@@ -280,7 +287,7 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
280287
if (result != null)
281288
{
282289
var psObject = new PSVirtualMachineScaleSet();
283-
ComputeAutomationAutoMapperProfile.Mapper.Map<VirtualMachineScaleSet, PSVirtualMachineScaleSet>(result, psObject);
290+
ComputeAutomationAutoMapperProfile.Mapper.Map(result, psObject);
284291
asyncCmdlet.WriteObject(psObject);
285292
}
286293
}

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

Lines changed: 20 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+
Mutable<string> domainNameLabel,
3941
string allocationMethod)
4042
=> Strategy.CreateResourceConfig(
4143
resourceGroup: resourceGroup,
@@ -45,8 +47,24 @@ public static ResourceConfig<PublicIPAddress> CreatePublicIPAddressConfig(
4547
PublicIPAllocationMethod = allocationMethod,
4648
DnsSettings = new PublicIPAddressDnsSettings
4749
{
48-
DomainNameLabel = domainNameLabel,
50+
DomainNameLabel = domainNameLabel.Value,
4951
}
5052
});
53+
54+
public static async Task FindDomainNameLabelAsync(
55+
Mutable<string> domainNameLabel,
56+
string name,
57+
string location,
58+
IClient client)
59+
{
60+
if (domainNameLabel.Value == null)
61+
{
62+
var networkClient = client.GetClient<NetworkManagementClient>();
63+
do
64+
{
65+
domainNameLabel.Value = (name + '-' + Guid.NewGuid().ToString().Substring(0, 6)).ToLower();
66+
} while ((await networkClient.CheckDnsNameAvailabilityAsync(location, domainNameLabel.Value)).Available != true);
67+
}
68+
}
5169
}
5270
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
2727
using Microsoft.Azure.Management.Compute;
2828
using Microsoft.Azure.Management.Compute.Models;
29+
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01;
30+
using Microsoft.Azure.Management.Internal.Resources;
2931
using Microsoft.Azure.Management.Storage;
3032
using Microsoft.Azure.Management.Storage.Models;
3133
using Microsoft.WindowsAzure.Commands.Sync.Download;
@@ -226,7 +228,6 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
226228
VirtualNetworkName = VirtualNetworkName ?? Name;
227229
SubnetName = SubnetName ?? Name;
228230
PublicIpAddressName = PublicIpAddressName ?? Name;
229-
DomainNameLabel = DomainNameLabel ?? (Name + '-' + ResourceGroupName).ToLower();
230231
SecurityGroupName = SecurityGroupName ?? Name;
231232

232233
bool isWindows;
@@ -269,6 +270,8 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
269270
image = osTypeAndImage.Image;
270271
isWindows = osTypeAndImage.OsType == "Windows";
271272
}
273+
274+
var domainNameLabel = Mutable.Create(DomainNameLabel);
272275

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

@@ -278,7 +281,7 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
278281
var subnet = virtualNetwork.CreateSubnet(SubnetName, SubnetAddressPrefix);
279282
var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig(
280283
name: PublicIpAddressName,
281-
domainNameLabel: DomainNameLabel,
284+
domainNameLabel: domainNameLabel,
282285
allocationMethod: AllocationMethod);
283286
var networkSecurityGroup = resourceGroup.CreateNetworkSecurityGroupConfig(
284287
name: SecurityGroupName,
@@ -372,7 +375,14 @@ async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
372375
}
373376
}
374377

375-
var fqdn = DomainNameLabel + "." + Location + ".cloudapp.azure.com";
378+
// generate a domain name label if it's not specified.
379+
await PublicIPAddressStrategy.FindDomainNameLabelAsync(
380+
domainNameLabel: domainNameLabel,
381+
name: Name,
382+
location: Location,
383+
client: client);
384+
385+
var fqdn = domainNameLabel.Value + "." + Location + ".cloudapp.azure.com";
376386

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

0 commit comments

Comments
 (0)