Skip to content

Commit e03403e

Browse files
A Target state is created from a Current state.
1 parent db8bd30 commit e03403e

File tree

7 files changed

+47
-20
lines changed

7 files changed

+47
-20
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
@@ -76,6 +76,7 @@
7676
<Compile Include="GetAsyncOperation.cs" />
7777
<Compile Include="GetAsyncParams.cs" />
7878
<Compile Include="IClient.cs" />
79+
<Compile Include="IReport.cs" />
7980
<Compile Include="IResourceConfig.cs" />
8081
<Compile Include="IResourceConfigVisitor.cs" />
8182
<Compile Include="IResourceStrategy.cs" />

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ public static class CreateOrUpdateAsyncOperation
1919
public static async Task<IState> CreateOrUpdateAsync<Model>(
2020
this IResourceConfig<Model> config,
2121
IClient client,
22-
IState current,
2322
IState target,
2423
CancellationToken cancellationToken)
2524
where Model : class
2625
{
27-
var visitor = new CreateAsyncVisitor(client, current, target, cancellationToken);
26+
var visitor = new CreateAsyncVisitor(client, target, cancellationToken);
2827
await visitor.GetOrAdd(config);
2928
return visitor.Result;
3029
}
@@ -33,10 +32,10 @@ sealed class CreateAsyncVisitor : AsyncOperationVisitor
3332
{
3433
public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
3534
{
36-
var current = Current.GetOrNull(config);
37-
if (current != null)
35+
var target = Target.GetOrNull(config);
36+
if (target == null)
3837
{
39-
return current;
38+
return null;
4039
}
4140
var tasks = config.Dependencies.Select(GetOrAddUntyped);
4241
await Task.WhenAll(tasks);
@@ -52,22 +51,23 @@ public override async Task<object> Visit<Model>(ResourceConfig<Model> config)
5251
public override async Task<object> Visit<Model, ParentModel>(
5352
NestedResourceConfig<Model, ParentModel> config)
5453
{
54+
var target = Target.GetOrNull(config);
55+
if (target == null)
56+
{
57+
return null;
58+
}
5559
var parent = await GetOrAdd(config.Parent);
5660
return config.Policy.Get(parent, config.Name);
5761
}
5862

5963
public CreateAsyncVisitor(
6064
IClient client,
61-
IState current,
6265
IState target,
6366
CancellationToken cancellationToken)
6467
: base(client, cancellationToken)
6568
{
66-
Current = current;
6769
Target = target;
6870
}
69-
70-
IState Current { get; }
7171

7272
IState Target { get; }
7373
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Microsoft.Azure.Commands.Common.Strategies
2+
{
3+
public interface IReport
4+
{
5+
void Start(IResourceConfig config);
6+
void Done(IResourceConfig config);
7+
}
8+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ public interface IState
77
{
88
Model GetOrNull<Model>(IResourceConfig<Model> config)
99
where Model : class;
10+
11+
object GetOrNullUntyped(IResourceConfig config);
1012
}
1113
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ namespace Microsoft.Azure.Commands.Common.Strategies
55
{
66
sealed class State : IState
77
{
8+
public object GetOrNullUntyped(IResourceConfig config)
9+
=> Map.GetOrNull(config.DefaultIdStr());
10+
811
public Model GetOrNull<Model>(IResourceConfig<Model> config)
912
where Model : class
10-
=> Map.GetOrNull(config.DefaultIdStr()) as Model;
13+
=> GetOrNullUntyped(config) as Model;
1114

1215
public Model GetOrAdd<Model>(IResourceConfig<Model> config, Func<Model> f)
1316
where Model : class

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
namespace Microsoft.Azure.Commands.Common.Strategies
1+
using System.Linq;
2+
3+
namespace Microsoft.Azure.Commands.Common.Strategies
24
{
35
public static class TargetState
46
{
57
public static IState GetTargetState<Model>(
68
this IResourceConfig<Model> config,
9+
IState current,
710
string subscription,
811
string location)
912
where Model : class
1013
{
11-
var visitor = new Visitor(subscription, location);
12-
visitor.Get(config);
14+
var visitor = new Visitor(current, subscription, location);
15+
// create a target model onyl if the resource doesn't exist.
16+
if (current.GetOrNull(config) == null)
17+
{
18+
visitor.Get(config);
19+
}
1320
return visitor.Result;
1421
}
1522

1623
sealed class Visitor : IResourceConfigVisitor<object>
1724
{
18-
public Visitor(string subscription, string location)
25+
public Visitor(IState current, string subscription, string location)
1926
{
27+
Current = current;
2028
Subscription = subscription;
2129
Location = location;
2230
}
@@ -28,10 +36,13 @@ public Model Get<Model>(IResourceConfig<Model> config)
2836
where Model : class
2937
=> GetUntyped(config) as Model;
3038

31-
public object Visit<Model>(ResourceConfig<Model> config)
39+
public object Visit<Model>(ResourceConfig<Model> config)
3240
where Model : class
3341
{
34-
foreach (var d in config.Dependencies)
42+
// create a dependency target model only if the dependency resource doesn't exist.
43+
foreach (var d in config
44+
.Dependencies
45+
.Where(d => Current.GetOrNullUntyped(d) == null))
3546
{
3647
GetUntyped(d);
3748
}
@@ -54,6 +65,8 @@ public object Visit<Model, ParentModel>(
5465

5566
string Location { get; }
5667

68+
IState Current { get; }
69+
5770
public State Result { get; } = new State();
5871
}
5972
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,23 @@ public void StrategyExecuteCmdlet()
181181

182182
//
183183
var client = new Client(DefaultProfile.DefaultContext);
184-
var state = virtualMachine
184+
var current = virtualMachine
185185
.GetAsync(client, new CancellationToken())
186186
.GetAwaiter()
187187
.GetResult();
188188

189189
if (Location == null)
190190
{
191-
Location = state.GetLocation(virtualMachine);
191+
Location = current.GetLocation(virtualMachine);
192192
if (Location == null)
193193
{
194194
Location = "eastus";
195195
}
196196
}
197197

198-
var target = virtualMachine.GetTargetState(client.SubscriptionId, Location);
198+
var target = virtualMachine.GetTargetState(current, client.SubscriptionId, Location);
199199
var result = virtualMachine
200-
.CreateOrUpdateAsync(client, state, target, new CancellationToken())
200+
.CreateOrUpdateAsync(client, target, new CancellationToken())
201201
.GetAwaiter()
202202
.GetResult();
203203
}

0 commit comments

Comments
 (0)