Skip to content

Commit

Permalink
A Target state is created from a Current state.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Nov 17, 2017
1 parent db8bd30 commit e03403e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<Compile Include="GetAsyncOperation.cs" />
<Compile Include="GetAsyncParams.cs" />
<Compile Include="IClient.cs" />
<Compile Include="IReport.cs" />
<Compile Include="IResourceConfig.cs" />
<Compile Include="IResourceConfigVisitor.cs" />
<Compile Include="IResourceStrategy.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ public static class CreateOrUpdateAsyncOperation
public static async Task<IState> CreateOrUpdateAsync<Model>(
this IResourceConfig<Model> config,
IClient client,
IState current,
IState target,
CancellationToken cancellationToken)
where Model : class
{
var visitor = new CreateAsyncVisitor(client, current, target, cancellationToken);
var visitor = new CreateAsyncVisitor(client, target, cancellationToken);
await visitor.GetOrAdd(config);
return visitor.Result;
}
Expand All @@ -33,10 +32,10 @@ sealed class CreateAsyncVisitor : AsyncOperationVisitor
{
public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
{
var current = Current.GetOrNull(config);
if (current != null)
var target = Target.GetOrNull(config);
if (target == null)
{
return current;
return null;
}
var tasks = config.Dependencies.Select(GetOrAddUntyped);
await Task.WhenAll(tasks);
Expand All @@ -52,22 +51,23 @@ public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
public override async Task<object> Visit<Model, ParentModel>(
NestedResourceConfig<Model, ParentModel> config)
{
var target = Target.GetOrNull(config);
if (target == null)
{
return null;
}
var parent = await GetOrAdd(config.Parent);
return config.Policy.Get(parent, config.Name);
}

public CreateAsyncVisitor(
IClient client,
IState current,
IState target,
CancellationToken cancellationToken)
: base(client, cancellationToken)
{
Current = current;
Target = target;
}

IState Current { get; }

IState Target { get; }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Microsoft.Azure.Commands.Common.Strategies
{
public interface IReport
{
void Start(IResourceConfig config);
void Done(IResourceConfig config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ public interface IState
{
Model GetOrNull<Model>(IResourceConfig<Model> config)
where Model : class;

object GetOrNullUntyped(IResourceConfig config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ namespace Microsoft.Azure.Commands.Common.Strategies
{
sealed class State : IState
{
public object GetOrNullUntyped(IResourceConfig config)
=> Map.GetOrNull(config.DefaultIdStr());

public Model GetOrNull<Model>(IResourceConfig<Model> config)
where Model : class
=> Map.GetOrNull(config.DefaultIdStr()) as Model;
=> GetOrNullUntyped(config) as Model;

public Model GetOrAdd<Model>(IResourceConfig<Model> config, Func<Model> f)
where Model : class
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
namespace Microsoft.Azure.Commands.Common.Strategies
using System.Linq;

namespace Microsoft.Azure.Commands.Common.Strategies
{
public static class TargetState
{
public static IState GetTargetState<Model>(
this IResourceConfig<Model> config,
IState current,
string subscription,
string location)
where Model : class
{
var visitor = new Visitor(subscription, location);
visitor.Get(config);
var visitor = new Visitor(current, subscription, location);
// create a target model onyl if the resource doesn't exist.
if (current.GetOrNull(config) == null)
{
visitor.Get(config);
}
return visitor.Result;
}

sealed class Visitor : IResourceConfigVisitor<object>
{
public Visitor(string subscription, string location)
public Visitor(IState current, string subscription, string location)
{
Current = current;
Subscription = subscription;
Location = location;
}
Expand All @@ -28,10 +36,13 @@ public Model Get<Model>(IResourceConfig<Model> config)
where Model : class
=> GetUntyped(config) as Model;

public object Visit<Model>(ResourceConfig<Model> config)
public object Visit<Model>(ResourceConfig<Model> config)
where Model : class
{
foreach (var d in config.Dependencies)
// create a dependency target model only if the dependency resource doesn't exist.
foreach (var d in config
.Dependencies
.Where(d => Current.GetOrNullUntyped(d) == null))
{
GetUntyped(d);
}
Expand All @@ -54,6 +65,8 @@ public object Visit<Model, ParentModel>(

string Location { get; }

IState Current { get; }

public State Result { get; } = new State();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,23 +181,23 @@ public void StrategyExecuteCmdlet()

//
var client = new Client(DefaultProfile.DefaultContext);
var state = virtualMachine
var current = virtualMachine
.GetAsync(client, new CancellationToken())
.GetAwaiter()
.GetResult();

if (Location == null)
{
Location = state.GetLocation(virtualMachine);
Location = current.GetLocation(virtualMachine);
if (Location == null)
{
Location = "eastus";
}
}

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

0 comments on commit e03403e

Please sign in to comment.