Skip to content

Commit

Permalink
Ids instead of objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Nov 17, 2017
1 parent 37909d0 commit db8bd30
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class AsyncOperationVisitor : IResourceConfigVisitor<Task<object>>

public async Task<object> GetOrAddUntyped(IResourceConfig config)
=> await TaskMap.GetOrAdd(
config,
config.DefaultIdStr(),
async _ =>
{
var model = await config.Apply(this);
Expand Down Expand Up @@ -49,7 +49,7 @@ public abstract Task<object> Visit<Model, ParentModel>(
where Model : class
where ParentModel : class;

ConcurrentDictionary<IResourceConfig, Task<object>> TaskMap { get; }
= new ConcurrentDictionary<IResourceConfig, Task<object>>();
ConcurrentDictionary<string, Task<object>> TaskMap { get; }
= new ConcurrentDictionary<string, Task<object>>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,8 @@ public static ResourceConfig<Model> CreateConfig<Model>(

public static string IdToString(this IEnumerable<string> id)
=> "/" + string.Join("/", id);

public static string DefaultIdStr(this IResourceConfig config)
=> config.GetId(string.Empty).IdToString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ sealed class State : IState
{
public Model GetOrNull<Model>(IResourceConfig<Model> config)
where Model : class
=> Map.GetOrNull(config) as Model;
=> Map.GetOrNull(config.DefaultIdStr()) as Model;

public Model GetOrAdd<Model>(IResourceConfig<Model> config, Func<Model> f)
where Model : class
=> GetOrAddUntyped(config, f) as Model;

public object GetOrAddUntyped(IResourceConfig config, Func<object> f)
=> Map.GetOrAdd(config, _ => f());
=> Map.GetOrAdd(config.DefaultIdStr(), _ => f());

ConcurrentDictionary<IResourceConfig, object> Map { get; }
= new ConcurrentDictionary<IResourceConfig, object>();
ConcurrentDictionary<string, object> Map { get; }
= new ConcurrentDictionary<string, object>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
Mandatory = true,
Position = 0,
ValueFromPipelineByPropertyName = true)]
[Parameter(
ParameterSetName = StrategyParameterSet,
Mandatory = false)]
[ValidateNotNullOrEmpty]
public string ResourceGroupName { get; set; }

Expand All @@ -64,6 +67,9 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
Mandatory = true,
Position = 1,
ValueFromPipelineByPropertyName = true)]
[Parameter(
ParameterSetName = StrategyParameterSet,
Mandatory = false)]
[ValidateNotNullOrEmpty]
public string Location { get; set; }

Expand All @@ -78,9 +84,10 @@ public class NewAzureVMCommand : VirtualMachineBaseCmdlet
public PSVirtualMachine VM { get; set; }

[Parameter(
Mandatory = false,
Position = 3,
ValueFromPipelineByPropertyName = true)]
ParameterSetName = DefaultParameterSet,
Mandatory = false,
Position = 3,
ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
public string[] Zone { get; set; }

Expand Down Expand Up @@ -154,7 +161,12 @@ public T GetClient<T>()

public void StrategyExecuteCmdlet()
{
var resourceGroup = ResourceGroupStrategy.CreateResourceGroupConfig(Name);
if (ResourceGroupName == null)
{
ResourceGroupName = Name;
}

var resourceGroup = ResourceGroupStrategy.CreateResourceGroupConfig(ResourceGroupName);
var virtualNetwork = resourceGroup.CreateVirtualNetworkConfig(Name, AddressPrefix);
var subnet = virtualNetwork.CreateSubnet(Name, SubnetAddressPrefix);
var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig(Name);
Expand All @@ -173,12 +185,17 @@ public void StrategyExecuteCmdlet()
.GetAsync(client, new CancellationToken())
.GetAwaiter()
.GetResult();
var location = state.GetLocation(virtualMachine);
if (location == null)

if (Location == null)
{
location = "eastus";
Location = state.GetLocation(virtualMachine);
if (Location == null)
{
Location = "eastus";
}
}
var target = virtualMachine.GetTargetState(client.SubscriptionId, location);

var target = virtualMachine.GetTargetState(client.SubscriptionId, Location);
var result = virtualMachine
.CreateOrUpdateAsync(client, state, target, new CancellationToken())
.GetAwaiter()
Expand Down

0 comments on commit db8bd30

Please sign in to comment.