Skip to content

Commit 8777f60

Browse files
author
Hovsep Mkrtchyan
committed
Moved BackendAddressPool and FrontendIPConfiguration to Nested Resources.
1 parent 8b71493 commit 8777f60

File tree

12 files changed

+232
-100
lines changed

12 files changed

+232
-100
lines changed

src/ResourceManager/Common/Commands.Common.Strategies/NestedResourceConfig.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public sealed class NestedResourceConfig<TModel, TParenModel> : IEntityConfig<TM
3636
/// </summary>
3737
public IEntityConfig<TParenModel> Parent { get; }
3838

39-
public Func<TModel> CreateModel { get; }
39+
public Func<string, TModel> CreateModel { get; }
4040

4141
public IResourceConfig Resource => Parent.Resource;
4242

@@ -46,7 +46,7 @@ public NestedResourceConfig(
4646
NestedResourceStrategy<TModel, TParenModel> strategy,
4747
IEntityConfig<TParenModel> parent,
4848
string name,
49-
Func<TModel> createModel)
49+
Func<string, TModel> createModel)
5050
{
5151
Strategy = strategy;
5252
Name = name;

src/ResourceManager/Common/Commands.Common.Strategies/NestedResourceConfigExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static NestedResourceConfig<TModel, TParenModel> CreateConfig<TModel, TPa
2222
this NestedResourceStrategy<TModel, TParenModel> strategy,
2323
IEntityConfig<TParenModel> parent,
2424
string name,
25-
Func<TModel> createModel)
25+
Func<string, TModel> createModel)
2626
where TModel : class
2727
where TParenModel : class
2828
=> new NestedResourceConfig<TModel, TParenModel>(strategy, parent, name, createModel);

src/ResourceManager/Common/Commands.Common.Strategies/NestedResourceStrategy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ public NestedResourceStrategy(
3939
public static class NestedResourceStrategy
4040
{
4141
public static NestedResourceStrategy<TModel, TParentModel> Create<TModel, TParentModel>(
42-
string header,
42+
string provider,
4343
Func<TParentModel, string, TModel> get,
4444
Action<TParentModel, string, TModel> createOrUpdate)
4545
where TModel : class
4646
where TParentModel : class
4747
=> new NestedResourceStrategy<TModel, TParentModel>(
48-
name => new[] { header, name},
48+
name => new[] { provider, name},
4949
get,
5050
createOrUpdate);
5151
}

src/ResourceManager/Common/Commands.Common.Strategies/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
35+
[assembly: AssemblyVersion("2.0.0.0")]
36+
[assembly: AssemblyFileVersion("2.0.0.0")]

src/ResourceManager/Common/Commands.Common.Strategies/TargetStateExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ sealed class Context
4141

4242
public IState Current { get; }
4343

44-
public string Subscription { get; }
44+
public string SubscriptionId { get; }
4545

4646
public string Location { get; }
4747

48-
public Context(IState current, string subscription, string location)
48+
public Context(IState current, string subscriptionId, string location)
4949
{
5050
Current = current;
51-
Subscription = subscription;
51+
SubscriptionId = subscriptionId;
5252
Location = location;
5353
}
5454

@@ -70,7 +70,7 @@ public TModel GetOrAdd<TModel>(ResourceConfig<TModel> config)
7070
{
7171
AddIfRequired(dependency);
7272
}
73-
var model = config.CreateModel(Subscription);
73+
var model = config.CreateModel(SubscriptionId);
7474
config.Strategy.SetLocation(model, Location);
7575
return model;
7676
});
@@ -84,7 +84,7 @@ public TModel GetOrAdd<TModel, TParentModel>(
8484
var model = config.Strategy.Get(parentModel, config.Name);
8585
if (model == null)
8686
{
87-
model = config.CreateModel();
87+
model = config.CreateModel(SubscriptionId);
8888
config.Strategy.CreateOrUpdate(parentModel, config.Name, model);
8989
}
9090
return model;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@
278278
<Compile Include="Strategies\Compute\VirtualMachineScaleSetStrategy.cs" />
279279
<Compile Include="Strategies\Compute\VirtualMachineStrategy.cs" />
280280
<Compile Include="Strategies\IAsyncCmdlet.cs" />
281+
<Compile Include="Strategies\Network\BackendAddressPoolStrategy.cs" />
282+
<Compile Include="Strategies\Network\FrontendIPConfigurationStrategy.cs" />
281283
<Compile Include="Strategies\Network\LoadBalancerStrategy.cs" />
282284
<Compile Include="Strategies\Network\NetworkInterfaceStrategy.cs" />
283285
<Compile Include="Strategies\Network\NetworkSecurityGroupPolicy.cs" />

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,20 @@ async Task SimpleParameterSetExecuteCmdlet(IAsyncCmdlet asyncCmdlet)
197197
publicIPAddress: publicIpAddress,
198198
subnet: subnet);
199199

200+
var frontendIpConfiguration = loadBalancer.CreateFrontendIPConfiguration(
201+
name: FrontendPoolName,
202+
zones: Zone,
203+
publicIPAddress: publicIpAddress,
204+
subnet: subnet);
205+
206+
var backendAddressPool = loadBalancer.CreateBackendAddressPool(
207+
name: BackendPoolName);
208+
200209
var virtualMachineScaleSet = resourceGroup.CreateVirtualMachineScaleSetConfig(
201210
name: VMScaleSetName,
202211
subnet: subnet,
203-
loadBalancer: loadBalancer,
212+
frontendIpConfigurations: new[] { frontendIpConfiguration },
213+
backendAdressPool: backendAddressPool,
204214
isWindows: isWindows,
205215
adminUsername: Credential.UserName,
206216
adminPassword: new NetworkCredential(string.Empty, Credential.Password).Password,

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ internal static ResourceConfig<VirtualMachineScaleSet> CreateVirtualMachineScale
3939
this ResourceConfig<ResourceGroup> resourceGroup,
4040
string name,
4141
NestedResourceConfig<Subnet, VirtualNetwork> subnet,
42-
ResourceConfig<LoadBalancer> loadBalancer,
42+
IEnumerable<NestedResourceConfig<FrontendIPConfiguration, LoadBalancer>> frontendIpConfigurations,
43+
NestedResourceConfig<BackendAddressPool, LoadBalancer> backendAdressPool,
4344
bool isWindows,
4445
string adminUsername,
4546
string adminPassword,
@@ -54,9 +55,8 @@ internal static ResourceConfig<VirtualMachineScaleSet> CreateVirtualMachineScale
5455
{
5556
var vmss = new VirtualMachineScaleSet()
5657
{
57-
Zones = loadBalancer
58-
.CreateModel(subscriptionId)
59-
.FrontendIPConfigurations
58+
Zones = frontendIpConfigurations
59+
?.Select(f => f.CreateModel(subscriptionId))
6060
?.Where(z => z?.Zones != null)
6161
.SelectMany(z => z.Zones)
6262
.Where(z => z != null)
@@ -106,8 +106,7 @@ internal static ResourceConfig<VirtualMachineScaleSet> CreateVirtualMachineScale
106106
LoadBalancerBackendAddressPools = new List<Microsoft.Azure.Management.Compute.Models.SubResource>(
107107
new[] {
108108
new Microsoft.Azure.Management.Compute.Models.SubResource(
109-
id: loadBalancer.GetId(subscriptionId).Concat(new [] { "backendAddressPools", name})
110-
.IdToString())
109+
id: backendAdressPool.GetId(subscriptionId).IdToString())
111110
}),
112111
Subnet = new ApiEntityReference { Id = subnet.GetId(subscriptionId).IdToString() }
113112
};
@@ -130,6 +129,7 @@ internal static ResourceConfig<VirtualMachineScaleSet> CreateVirtualMachineScale
130129

131130
return vmss;
132131
},
133-
dependencies: new IEntityConfig[] { subnet, loadBalancer });
132+
dependencies: new IEntityConfig[] { subnet, backendAdressPool }
133+
.Concat(frontendIpConfigurations));
134134
}
135135
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.Common.Strategies;
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using System.Text;
20+
using System.Threading.Tasks;
21+
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01;
22+
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01.Models;
23+
24+
namespace Microsoft.Azure.Commands.Common.Strategies.Network
25+
{
26+
static class BackendAddressPoolStrategy
27+
{
28+
public static NestedResourceStrategy<BackendAddressPool, LoadBalancer> Strategy { get; }
29+
= NestedResourceStrategy.Create<BackendAddressPool, LoadBalancer>(
30+
provider: "backendAddressPools",
31+
get: (lb, name) => lb.BackendAddressPools?.FirstOrDefault(s => s?.Name == name),
32+
createOrUpdate: (lb, name, backendConfigurationPool) =>
33+
{
34+
backendConfigurationPool.Name = name;
35+
if (lb.BackendAddressPools == null)
36+
{
37+
lb.BackendAddressPools = new List<BackendAddressPool> { backendConfigurationPool };
38+
return;
39+
}
40+
var b = lb
41+
.BackendAddressPools
42+
.Select((bn, i) => new { bn, i })
43+
.FirstOrDefault(p => p.bn.Name == name);
44+
45+
if (b != null)
46+
{
47+
lb.BackendAddressPools[b.i] = backendConfigurationPool;
48+
return;
49+
}
50+
lb.BackendAddressPools.Add(backendConfigurationPool);
51+
});
52+
53+
public static NestedResourceConfig<BackendAddressPool, LoadBalancer> CreateBackendAddressPool(
54+
this ResourceConfig<LoadBalancer> loadBalancer,
55+
string name)
56+
=> Strategy.CreateConfig(
57+
parent: loadBalancer,
58+
name: name,
59+
createModel: subscriptionId => CreateBackendAddressPoolConfig(backendPoolName:name , subscriptionId: subscriptionId));
60+
61+
internal static BackendAddressPool CreateBackendAddressPoolConfig(
62+
string backendPoolName,
63+
string subscriptionId)
64+
{
65+
var backendAddressPool = new BackendAddressPool();
66+
backendAddressPool.Name = backendPoolName;
67+
68+
backendAddressPool.Id =
69+
LoadBalancerStrategy.GetResourceNotSetId(
70+
subscriptionId,
71+
LoadBalancerStrategy.LoadBalancerBackendAddressPoolName,
72+
backendAddressPool.Name);
73+
74+
return backendAddressPool;
75+
}
76+
}
77+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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.Common.Strategies;
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using System.Text;
20+
using System.Threading.Tasks;
21+
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01;
22+
using Microsoft.Azure.Management.Internal.Network.Version2017_10_01.Models;
23+
24+
namespace Microsoft.Azure.Commands.Common.Strategies.Network
25+
{
26+
static class FrontendIPConfigurationStrategy
27+
{
28+
public static NestedResourceStrategy<FrontendIPConfiguration, LoadBalancer> Strategy { get; }
29+
= NestedResourceStrategy.Create<FrontendIPConfiguration, LoadBalancer>(
30+
provider: "frontendIPConfigurations",
31+
get: (lb, name) => lb.FrontendIPConfigurations?.FirstOrDefault(s => s?.Name == name),
32+
createOrUpdate: (lb, name, frontendIpConfiguration) =>
33+
{
34+
frontendIpConfiguration.Name = name;
35+
if (lb.FrontendIPConfigurations == null)
36+
{
37+
lb.FrontendIPConfigurations = new List<FrontendIPConfiguration> { frontendIpConfiguration };
38+
return;
39+
}
40+
var f = lb
41+
.FrontendIPConfigurations
42+
.Select((fn, i) => new { fn, i })
43+
.FirstOrDefault(p => p.fn.Name == name);
44+
45+
if (f != null)
46+
{
47+
lb.FrontendIPConfigurations[f.i] = frontendIpConfiguration;
48+
return;
49+
}
50+
lb.FrontendIPConfigurations.Add(frontendIpConfiguration);
51+
});
52+
53+
public static NestedResourceConfig<FrontendIPConfiguration, LoadBalancer> CreateFrontendIPConfiguration(
54+
this ResourceConfig<LoadBalancer> loadBalancer,
55+
string name,
56+
IList<string> zones,
57+
ResourceConfig<PublicIPAddress> publicIPAddress,
58+
NestedResourceConfig<Subnet, VirtualNetwork> subnet)
59+
=> Strategy.CreateConfig(
60+
parent: loadBalancer,
61+
name: name,
62+
createModel: subscriptionId => {
63+
64+
var frontEndConfig = CreateFrontendIpConfig(
65+
froontendPoolName: name,
66+
subscriptionId: subscriptionId,
67+
subnetId: subnet.GetId(subscriptionId).IdToString(),
68+
publicIpAddressId: publicIPAddress.GetId(subscriptionId).IdToString(),
69+
privateIpAddress: null,
70+
zones: zones);
71+
72+
return frontEndConfig;
73+
});
74+
75+
internal static FrontendIPConfiguration CreateFrontendIpConfig(
76+
string froontendPoolName,
77+
string subscriptionId,
78+
string subnetId,
79+
string publicIpAddressId,
80+
string privateIpAddress,
81+
IList<string> zones)
82+
{
83+
var frontendIpConfig = new FrontendIPConfiguration();
84+
frontendIpConfig.Name = froontendPoolName;
85+
frontendIpConfig.Zones = zones;
86+
87+
if (!string.IsNullOrEmpty(subnetId))
88+
{
89+
frontendIpConfig.Subnet = new Subnet(subnetId);
90+
91+
if (!string.IsNullOrEmpty(privateIpAddress))
92+
{
93+
frontendIpConfig.PrivateIPAddress = privateIpAddress;
94+
frontendIpConfig.PrivateIPAllocationMethod = LoadBalancerStrategy.Static;
95+
}
96+
else
97+
{
98+
frontendIpConfig.PrivateIPAllocationMethod = LoadBalancerStrategy.Dynamic;
99+
}
100+
}
101+
else if (!string.IsNullOrEmpty(publicIpAddressId))
102+
{
103+
frontendIpConfig.PublicIPAddress = new PublicIPAddress(publicIpAddressId);
104+
}
105+
106+
frontendIpConfig.Id =
107+
LoadBalancerStrategy.GetResourceNotSetId(
108+
subscriptionId,
109+
LoadBalancerStrategy.LoadBalancerFrontendIpConfigName,
110+
frontendIpConfig.Name);
111+
112+
return frontendIpConfig;
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)