Skip to content

Commit 9db6d0d

Browse files
IReportProgress
1 parent ace1441 commit 9db6d0d

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
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
@@ -69,6 +69,7 @@
6969
<ItemGroup>
7070
<Compile Include="Compute\Image.cs" />
7171
<Compile Include="Compute\Images.cs" />
72+
<Compile Include="IReportProgress.cs" />
7273
<Compile Include="IResourceConfig.cs" />
7374
<Compile Include="IResourceConfigVisitor.cs" />
7475
<Compile Include="IShouldProcess.cs" />
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 IProgressReport
4+
{
5+
void Report<TModel>(ResourceConfig<TModel> config, double progress)
6+
where TModel : class;
7+
}
8+
}

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ public static async Task<IState> UpdateStateAsync<TModel>(
1212
IClient client,
1313
IState target,
1414
CancellationToken cancellationToken,
15-
IShouldProcess shouldProcess)
15+
IShouldProcess shouldProcess,
16+
IProgressReport progressReport)
1617
where TModel : class
1718
{
1819
var context = new Context(
19-
new StateOperationContext(client, cancellationToken), target, shouldProcess);
20+
new StateOperationContext(client, cancellationToken),
21+
target,
22+
shouldProcess,
23+
progressReport);
2024
await context.UpdateStateAsync(config);
2125
return context.Result;
2226
}
@@ -31,12 +35,18 @@ sealed class Context
3135

3236
readonly IShouldProcess _ShouldProcess;
3337

38+
readonly IProgressReport _ProgressReport;
39+
3440
public Context(
35-
StateOperationContext operationContext, IState target, IShouldProcess shouldProcess)
41+
StateOperationContext operationContext,
42+
IState target,
43+
IShouldProcess shouldProcess,
44+
IProgressReport progressReport)
3645
{
3746
_OperationContext = operationContext;
3847
_Target = target;
3948
_ShouldProcess = shouldProcess;
49+
_ProgressReport = progressReport;
4050
}
4151

4252
public async Task UpdateStateAsync<TModel>(ResourceConfig<TModel> config)
@@ -57,10 +67,13 @@ await _OperationContext.GetOrAdd(
5767
// call the CreateOrUpdateAsync function for the resource.
5868
if (_ShouldProcess.ShouldCreate(config, model))
5969
{
60-
return await config.CreateOrUpdateAsync(
70+
_ProgressReport.Report(config, 0.0);
71+
var result = await config.CreateOrUpdateAsync(
6172
_OperationContext.Client,
6273
model,
6374
_OperationContext.CancellationToken);
75+
_ProgressReport.Report(config, 1.0);
76+
return result;
6477
}
6578
else
6679
{

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,28 @@ public ShouldProcessType(Cmdlet cmdlet)
195195

196196
public bool ShouldCreate<TModel>(ResourceConfig<TModel> config, TModel model)
197197
where TModel : class
198-
=> _Cmdlet.ShouldProcess(config.Name + " " + config.Strategy.Type, VerbsCommon.New);
198+
=> _Cmdlet.ShouldProcess(config.Name, VerbsCommon.New + " " + config.Strategy.Type);
199+
}
200+
201+
private sealed class ProgressReportType : IProgressReport
202+
{
203+
readonly Cmdlet _Cmdlet;
204+
205+
public ProgressReportType(Cmdlet cmdlet)
206+
{
207+
_Cmdlet = cmdlet;
208+
}
209+
210+
public void Report<TModel>(ResourceConfig<TModel> config, double progress)
211+
where TModel : class
212+
{
213+
214+
}
215+
/*
216+
=> _Cmdlet.WriteVerbose(
217+
progress == 0 ? "Creating " + config.Name + " " + config.Strategy.Type + "..."
218+
: config.Name + " " + config.Strategy.Type + " is created.");
219+
*/
199220
}
200221

201222
public void StrategyExecuteCmdlet()
@@ -256,7 +277,11 @@ public void StrategyExecuteCmdlet()
256277

257278
var result = virtualMachine
258279
.UpdateStateAsync(
259-
client, target, new CancellationToken(), new ShouldProcessType(this))
280+
client,
281+
target,
282+
new CancellationToken(),
283+
new ShouldProcessType(this),
284+
new ProgressReportType(this))
260285
.GetAwaiter()
261286
.GetResult();
262287
WriteObject(result);

0 commit comments

Comments
 (0)