Skip to content

Commit d11b0e0

Browse files
address comments.
1 parent e60eb4b commit d11b0e0

File tree

4 files changed

+101
-15
lines changed

4 files changed

+101
-15
lines changed

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

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,25 @@ namespace Microsoft.Azure.Commands.Common.Strategies
2323
{
2424
public static class AsyncCmdletExtensions
2525
{
26+
/// <summary>
27+
/// WriteVerbose function with formatting.
28+
/// </summary>
29+
/// <param name="cmdlet">Cmdlet</param>
30+
/// <param name="message">message with formatting</param>
31+
/// <param name="p">message parameters</param>
2632
public static void WriteVerbose(this IAsyncCmdlet cmdlet, string message, params object[] p)
2733
=> cmdlet.WriteVerbose(string.Format(message, p));
2834

29-
public static string UpdateLocation(
30-
this IState current, string location, IResourceConfig config)
31-
=> location ?? current.GetLocation(config) ?? "eastus";
32-
35+
/// <summary>
36+
/// The function read current Azure state and update it according to the `parameters`.
37+
/// </summary>
38+
/// <typeparam name="TModel">A resource model type.</typeparam>
39+
/// <param name="client">Azure SDK client.</param>
40+
/// <param name="subscriptionId">Subbscription Id.</param>
41+
/// <param name="parameters">Cmdlet parameters.</param>
42+
/// <param name="asyncCmdlet">Asynchronous cmdlet interface.</param>
43+
/// <param name="cancellationToken">Cancellation token.</param>
44+
/// <returns></returns>
3345
public static async Task<TModel> RunAsync<TModel>(
3446
this IClient client,
3547
string subscriptionId,
@@ -40,51 +52,56 @@ public static async Task<TModel> RunAsync<TModel>(
4052
{
4153
// create a DAG of configs.
4254
var config = await parameters.CreateConfigAsync();
43-
44-
// reade current Azure state.
55+
// read current Azure state.
4556
var current = await config.GetStateAsync(client, cancellationToken);
46-
4757
// update location.
4858
parameters.Location = current.UpdateLocation(parameters.Location, config);
49-
5059
// update a DAG of configs.
5160
config = await parameters.CreateConfigAsync();
52-
53-
var engine = new SdkEngine(subscriptionId);
54-
var target = config.GetTargetState(current, engine, parameters.Location);
55-
61+
// create a target.
62+
var target = config.GetTargetState(
63+
current, new SdkEngine(subscriptionId), parameters.Location);
64+
// print paramaters to a verbose stream.
5665
foreach (var p in asyncCmdlet.Parameters)
5766
{
5867
asyncCmdlet.WriteVerbose(p.Key + " = " + ToPowerShellString(p.Value));
5968
}
6069

61-
// apply target state
70+
// apply the target state
6271
var newState = await config.UpdateStateAsync(
6372
client,
6473
target,
6574
cancellationToken,
6675
new ShouldProcess(asyncCmdlet),
6776
asyncCmdlet.ReportTaskProgress);
68-
77+
// return a resource model
6978
return newState.Get(config) ?? current.Get(config);
7079
}
7180

81+
static string UpdateLocation(
82+
this IState current, string location, IResourceConfig config)
83+
=> location ?? current.GetLocation(config) ?? "eastus";
84+
85+
7286
static string ToPowerShellString(object value)
7387
{
7488
if (value == null)
7589
{
7690
return "$null";
7791
}
92+
7893
var s = value as string;
7994
if (s != null)
8095
{
8196
return "\"" + s + "\"";
8297
}
98+
8399
var e = value as IEnumerable;
84100
if (e != null)
85101
{
86102
return string.Join(",", e.Cast<object>().Select(ToPowerShellString));
87103
}
104+
88105
return value.ToString();
89106
}
90107

@@ -128,8 +145,10 @@ public static void CmdletStartAndWait(
128145
var config = taskProgress.Config;
129146
activeTasks.Add(config.GetFullName());
130147
}
148+
131149
progress += taskProgress.GetProgress();
132150
}
151+
133152
var percent = (int)(progress * 100.0);
134153
var r = new[] { "|", "/", "-", "\\" };
135154
var x = r[DateTime.Now.Second % 4];

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,47 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using Microsoft.Azure.Commands.Common.Strategies;
1615
using System.Collections.Generic;
1716
using System.Threading.Tasks;
1817

1918
namespace Microsoft.Azure.Commands.Common.Strategies
2019
{
20+
/// <summary>
21+
/// Asynchronous cmdlet interface.
22+
/// </summary>
2123
public interface IAsyncCmdlet
2224
{
25+
/// <summary>
26+
/// Cmdlet parameters.
27+
/// </summary>
2328
IEnumerable<KeyValuePair<string, object>> Parameters { get; }
2429

30+
/// <summary>
31+
/// Thread-safe `WriteVerbose` function.
32+
/// </summary>
33+
/// <param name="message"></param>
2534
void WriteVerbose(string message);
2635

36+
/// <summary>
37+
/// Asynchronous, thread-safe `ShouldProcess` function.
38+
/// </summary>
2739
Task<bool> ShouldProcessAsync(string target, string action);
2840

41+
/// <summary>
42+
/// Thread-safe `WriteVerbose` function.
43+
/// </summary>
44+
/// <param name="value"></param>
2945
void WriteObject(object value);
3046

47+
/// <summary>
48+
/// Report task progress. The function is used to report current task and cmdlet progress.
49+
/// </summary>
50+
/// <param name="taskProgress"></param>
3151
void ReportTaskProgress(ITaskProgress taskProgress);
3252

53+
/// <summary>
54+
/// A `New` verb.
55+
/// </summary>
3356
string VerbsNew { get; }
3457
}
3558
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,52 @@
1616

1717
namespace Microsoft.Azure.Commands.Common.Strategies
1818
{
19+
/// <summary>
20+
/// An interface for a cmdlet for dependency injection.
21+
/// </summary>
1922
public interface ICmdlet
2023
{
24+
/// <summary>
25+
/// Verbose output. See also PowerShell `WriteVerbose`.
26+
/// </summary>
27+
/// <param name="message"></param>
2128
void WriteVerbose(string message);
29+
30+
/// <summary>
31+
/// See PowerShell `ShouldProcess`.
32+
/// </summary>
33+
/// <param name="target"></param>
34+
/// <param name="action"></param>
35+
/// <returns></returns>
2236
bool ShouldProcess(string target, string action);
37+
38+
/// <summary>
39+
/// See also PowerShell `WriteObject`.
40+
/// </summary>
41+
/// <param name="value"></param>
2342
void WriteObject(object value);
43+
44+
/// <summary>
45+
/// See also PowerShell `WriteProgress`.
46+
/// </summary>
47+
/// <param name="activity"></param>
48+
/// <param name="statusDescription"></param>
49+
/// <param name="currentOperation"></param>
50+
/// <param name="percentComplete"></param>
2451
void WriteProgress(
2552
string activity,
2653
string statusDescription,
2754
string currentOperation,
2855
int percentComplete);
2956

57+
/// <summary>
58+
/// See also `VerbsCommon.New`.
59+
/// </summary>
3060
string VerbsNew { get; }
3161

62+
/// <summary>
63+
/// Cmdlet parameters.
64+
/// </summary>
3265
IEnumerable<KeyValuePair<string, object>> Parameters { get; }
3366
}
3467
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,22 @@
1616

1717
namespace Microsoft.Azure.Commands.Common.Strategies
1818
{
19+
/// <summary>
20+
/// Describes Azure operation parameter and a target resource.
21+
/// </summary>
22+
/// <typeparam name="TModel"></typeparam>
1923
public interface IParameters<TModel>
2024
where TModel : class
2125
{
26+
/// <summary>
27+
/// Azure location. For example, "eastus".
28+
/// </summary>
2229
string Location { get; set; }
2330

31+
/// <summary>
32+
/// Create an Azure resource configuration according to the parameters.
33+
/// </summary>
34+
/// <returns></returns>
2435
Task<ResourceConfig<TModel>> CreateConfigAsync();
2536
}
2637
}

0 commit comments

Comments
 (0)