Skip to content

Commit

Permalink
Resource strategies.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Nov 16, 2017
1 parent e37b2a4 commit cad25fb
Show file tree
Hide file tree
Showing 19 changed files with 398 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AsyncOperationVisitor.cs" />
<Compile Include="Compute\ComputeStrategy.cs" />
<Compile Include="Compute\VirtualMachineStrategy.cs" />
<Compile Include="CreateOrUpdateAsyncOperation.cs" />
<Compile Include="CreateOrUpdateAsyncParams.cs" />
<Compile Include="Extensions.cs" />
Expand All @@ -77,6 +79,13 @@
<Compile Include="IState.cs" />
<Compile Include="NestedResourceConfig.cs" />
<Compile Include="NestedResourceStrategy.cs" />
<Compile Include="Network\NetworkInterfaceStrategy.cs" />
<Compile Include="Network\NetworkSecurityGroupPolicy.cs" />
<Compile Include="Network\NetworkStrategy.cs" />
<Compile Include="Network\PublicIPAddressStrategy.cs" />
<Compile Include="Network\SubnetStrategy.cs" />
<Compile Include="Network\VirtualNetworkStrategy.cs" />
<Compile Include="ResourceManager\ResourceGroupStrategy.cs" />
<Compile Include="TargetState.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ResourceConfig.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using System;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.Common.Strategies.Compute
{
public static class ComputePolicy
{
public static ResourceStrategy<Model> Create<Model, Operations>(
string header,
Func<IComputeManagementClient, Operations> getOperations,
Func<GetAsyncParams<Operations>, Task<Model>> getAsync,
Func<CreateOrUpdateAsyncParams<Operations, Model>, Task<Model>> createOrUpdateAsync)
where Model : Resource
=> ResourceStrategy.Create(
new[] { "Microsoft.Compute", header },
getOperations,
getAsync,
createOrUpdateAsync,
config => config.Location,
(config, location) => config.Location = location);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.Azure.Management.Network.Models;
using Microsoft.Azure.Management.ResourceManager.Models;

namespace Microsoft.Azure.Commands.Common.Strategies.Compute
{
public static class VirtualMachineStrategy
{
public static ResourceStrategy<VirtualMachine> Strategy { get; }
= ComputePolicy.Create(
"virtualMachines",
client => client.VirtualMachines,
p => p.Operations.GetAsync(
p.ResourceGroupName, p.Name, null, p.CancellationToken),
p => p.Operations.CreateOrUpdateAsync(
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));

public static ResourceConfig<VirtualMachine> CreateVirtualMachineConfig(
this ResourceConfig<ResourceGroup> resourceGroup,
string name,
ResourceConfig<NetworkInterface> networkInterface,
string adminUsername,
string adminPassword)
=> Strategy.CreateConfig(
resourceGroup,
name,
subscription => new VirtualMachine
{
OsProfile = new OSProfile
{
ComputerName = name,
WindowsConfiguration = new WindowsConfiguration
{
},
AdminUsername = adminUsername,
AdminPassword = adminPassword,
},
NetworkProfile = new NetworkProfile
{
NetworkInterfaces = new[]
{
new NetworkInterfaceReference
{
Id = networkInterface.GetId(subscription).IdToString()
}
}
},
HardwareProfile = new HardwareProfile
{
VmSize = "Standard_DS1_v2"
},
StorageProfile = new StorageProfile
{
ImageReference = new ImageReference
{
Publisher = "MicrosoftWindowsServer",
Offer = "WindowsServer",
Sku = "2016-Datacenter",
Version = "latest"
}
},
},
new[] { networkInterface });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
}
var tasks = config.Dependencies.Select(GetOrAddUntyped);
await Task.WhenAll(tasks);
return await config.Policy.CreateOrUpdateAsync(CreateOrUpdateAsyncParams.Create(
return await config.Strategy.CreateOrUpdateAsync(CreateOrUpdateAsyncParams.Create(
Client,
config.ResourceGroupName,
config.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
Model info;
try
{
info = await config.Policy.GetAsync(GetAsyncParams.Create(
info = await config.Strategy.GetAsync(GetAsyncParams.Create(
Client, config.ResourceGroupName, config.Name, CancellationToken));
}
catch (CloudException e) when (e.Response.StatusCode == HttpStatusCode.NotFound)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public NestedResourceStrategy(
}
}

public static class NestedResourceStraegy
public static class NestedResourceStrategy
{
public static NestedResourceStrategy<Model, ParentModel> Create<Model, ParentModel>(
string header,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using Microsoft.Azure.Management.ResourceManager.Models;

namespace Microsoft.Azure.Commands.Common.Strategies.Network
{
public static class NetworkInterfaceStrategy
{
public static ResourceStrategy<NetworkInterface> Strategy { get; }
= NetworkStrategy.Create(
"networkInterfaces",
client => client.NetworkInterfaces,
p => p.Operations.GetAsync(
p.ResourceGroupName, p.Name, null, p.CancellationToken),
p => p.Operations.CreateOrUpdateAsync(
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));

public static ResourceConfig<NetworkInterface> CreateNetworkInterfaceConfig(
this ResourceConfig<ResourceGroup> resourceGroup,
string name,
NestedResourceConfig<Subnet, VirtualNetwork> subnet,
ResourceConfig<PublicIPAddress> publicIPAddress,
ResourceConfig<NetworkSecurityGroup> networkSecurityGroup)
=> Strategy.CreateConfig(
resourceGroup,
name,
subscription => new NetworkInterface
{
IpConfigurations = new []
{
new NetworkInterfaceIPConfiguration
{
Name = name,
Subnet = new Subnet { Id = subnet.GetId(subscription).IdToString() },
PublicIPAddress = new PublicIPAddress
{
Id = publicIPAddress.GetId(subscription).IdToString()
}
}
},
NetworkSecurityGroup = new NetworkSecurityGroup
{
Id = networkSecurityGroup.GetId(subscription).IdToString()
}
},
new IResourceConfig[] { subnet, publicIPAddress, networkSecurityGroup });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using Microsoft.Azure.Management.ResourceManager.Models;

namespace Microsoft.Azure.Commands.Common.Strategies.Network
{
public static class NetworkSecurityGroupStrategy
{
public static ResourceStrategy<NetworkSecurityGroup> Strategy { get; }
= NetworkStrategy.Create(
"networkSecurityGroups",
client => client.NetworkSecurityGroups,
p => p.Operations.GetAsync(
p.ResourceGroupName, p.Name, null, p.CancellationToken),
p => p.Operations.CreateOrUpdateAsync(
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));

public static ResourceConfig<NetworkSecurityGroup> CreateNetworkSecurityGroupConfig(
this ResourceConfig<ResourceGroup> resourceGroup, string name)
=> Strategy.CreateConfig(resourceGroup, name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using System;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.Common.Strategies.Network
{
public static class NetworkStrategy
{
public static ResourceStrategy<Model> Create<Model, Operations>(
string header,
Func<INetworkManagementClient, Operations> getOperations,
Func<GetAsyncParams<Operations>, Task<Model>> getAsync,
Func<CreateOrUpdateAsyncParams<Operations, Model>, Task<Model>> createOrUpdateAsync)
where Model : Resource
=> ResourceStrategy.Create(
new [] { "Microsoft.Network", header },
getOperations,
getAsync,
createOrUpdateAsync,
model => model.Location,
(model, location) => model.Location = location);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using Microsoft.Azure.Management.ResourceManager.Models;

namespace Microsoft.Azure.Commands.Common.Strategies.Network
{
public static class PublicIPAddressStrategy
{
public static ResourceStrategy<PublicIPAddress> Strategy { get; }
= NetworkStrategy.Create(
"publicIPAddresses",
client => client.PublicIPAddresses,
p => p.Operations.GetAsync(
p.ResourceGroupName, p.Name, null, p.CancellationToken),
p => p.Operations.CreateOrUpdateAsync(
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));

public static ResourceConfig<PublicIPAddress> CreatePublicIPAddressConfig(
this ResourceConfig<ResourceGroup> resourceGroup, string name)
=> Strategy.CreateConfig(resourceGroup, name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.Azure.Management.Network.Models;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.Azure.Commands.Common.Strategies.Network
{
public static class SubnetPolicy
{
public static NestedResourceStrategy<Subnet, VirtualNetwork> Strategy { get; }
= NestedResourceStrategy.Create<Subnet, VirtualNetwork>(
"subnets",
(vn, name) => vn.Subnets?.FirstOrDefault(s => s.Name == name),
(vn, name, subnet) =>
{
subnet.Name = name;
if (vn.Subnets == null)
{
vn.Subnets = new List<Subnet> { subnet };
return;
}
var s = vn
.Subnets
.Select((sn, i) => new { sn, i })
.FirstOrDefault(p => p.sn.Name == name);
if (s != null)
{
vn.Subnets[s.i] = subnet;
return;
}
vn.Subnets.Add(subnet);
});

public static NestedResourceConfig<Subnet, VirtualNetwork> CreateSubnet(
this ResourceConfig<VirtualNetwork> virtualNetwork, string name, string addressPrefix)
=> Strategy.CreateConfig(
virtualNetwork,
name,
() => new Subnet { Name = name, AddressPrefix = addressPrefix });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using Microsoft.Azure.Management.ResourceManager.Models;

namespace Microsoft.Azure.Commands.Common.Strategies.Network
{
public static class VirtualNetworkStrategy
{
public static ResourceStrategy<VirtualNetwork> Strategy { get; }
= NetworkStrategy.Create(
"virtualNetworks",
client => client.VirtualNetworks,
p => p.Operations.GetAsync(
p.ResourceGroupName, p.Name, null, p.CancellationToken),
p => p.Operations.CreateOrUpdateAsync(
p.ResourceGroupName, p.Name, p.Model, p.CancellationToken));

public static ResourceConfig<VirtualNetwork> CreateVirtualNetworkConfig(
this ResourceConfig<ResourceGroup> resourceGroup,
string name,
string addressPrefix)
=> Strategy.CreateConfig(
resourceGroup,
name,
_ => new VirtualNetwork
{
AddressSpace = new AddressSpace
{
AddressPrefixes = new[] { addressPrefix }
}
});
}
}
Loading

0 comments on commit cad25fb

Please sign in to comment.