Skip to content

Commit c09b981

Browse files
authored
Added Tail parameter to Get-AzDeploymentScriptLog and Save-AzDeploymentScriptLog cmdlets (#11826)
* Add support for Tail and refactor code for new SDK version * Added test cases * Edited test cases * Re-record test cases * Modify help files * Review fixes * Fix CLI test case
1 parent 1032a3d commit c09b981

17 files changed

+119135
-26975
lines changed

src/Resources/ResourceManager/Implementation/DeploymentScripts/GetAzDeploymentScriptLog.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
1717
using Microsoft.Azure.Commands.ResourceManager.Common;
1818
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
19+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
1920
using System;
2021
using System.Management.Automation;
2122

@@ -49,29 +50,40 @@ public class GetAzDeploymentScriptLog : DeploymentScriptCmdletBase
4950
[ValidateNotNullOrEmpty]
5051
public PsDeploymentScript DeploymentScriptInputObject { get; set; }
5152

53+
[Parameter(Position = 2, ParameterSetName = GetDeploymentScriptLogByName, Mandatory = false, HelpMessage = "Limit output to last n lines")]
54+
[Parameter(Position = 1, ParameterSetName = GetDeploymentScriptLogByResourceId, Mandatory = false, HelpMessage = "Limit output to last n lines")]
55+
[Parameter(Position = 1, ParameterSetName = GetDeploymentScriptLogByInputObject, Mandatory = false, HelpMessage = "Limit output to last n lines")]
56+
[ValidateNotNullOrEmpty]
57+
public int Tail { get; set; }
58+
5259
#endregion
5360

5461
#region Cmdlet Overrides
5562

5663
public override void ExecuteCmdlet()
5764
{
5865
PsDeploymentScriptLog deploymentScriptLog;
66+
int tailParam = this.IsParameterBound(c => c.Tail) ? Tail : 0;
67+
5968
try
6069
{
6170
switch (ParameterSetName)
6271
{
6372
case GetDeploymentScriptLogByName:
6473
deploymentScriptLog =
65-
DeploymentScriptsSdkClient.GetDeploymentScriptLog(Name, ResourceGroupName);
74+
DeploymentScriptsSdkClient.GetDeploymentScriptLog(Name, ResourceGroupName, tailParam);
6675
break;
6776
case GetDeploymentScriptLogByResourceId:
6877
deploymentScriptLog = DeploymentScriptsSdkClient.GetDeploymentScriptLog(
6978
ResourceIdUtility.GetResourceName(this.DeploymentScriptResourceId),
70-
ResourceIdUtility.GetResourceGroupName(this.DeploymentScriptResourceId));
79+
ResourceIdUtility.GetResourceGroupName(this.DeploymentScriptResourceId),
80+
tailParam);
7181
break;
7282
case GetDeploymentScriptLogByInputObject:
73-
deploymentScriptLog = DeploymentScriptsSdkClient.GetDeploymentScriptLog(DeploymentScriptInputObject.Name,
74-
ResourceIdUtility.GetResourceGroupName(DeploymentScriptInputObject.Id));
83+
deploymentScriptLog = DeploymentScriptsSdkClient.GetDeploymentScriptLog(
84+
DeploymentScriptInputObject.Name,
85+
ResourceIdUtility.GetResourceGroupName(DeploymentScriptInputObject.Id),
86+
tailParam);
7587
break;
7688
default:
7789
throw new PSInvalidOperationException();

src/Resources/ResourceManager/Implementation/DeploymentScripts/SaveAzDeploymentScriptLog.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
1818
using Microsoft.Azure.Commands.ResourceManager.Common;
1919
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
20+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2021
using System;
2122
using System.IO;
2223
using System.Management.Automation;
@@ -64,6 +65,12 @@ public class SaveAzDeploymentScriptLog : DeploymentScriptCmdletBase
6465
[ValidateNotNullOrEmpty]
6566
public string OutputPath { get; set; }
6667

68+
[Parameter(Position = 3, ParameterSetName = SaveDeploymentScriptLogByName, Mandatory = false, HelpMessage = "Limit output to last n lines")]
69+
[Parameter(Position = 2, ParameterSetName = SaveDeploymentScriptLogByResourceId, Mandatory = false, HelpMessage = "Limit output to last n lines")]
70+
[Parameter(Position = 2, ParameterSetName = SaveDeploymentScriptLogByInputObject, Mandatory = false, HelpMessage = "Limit output to last n lines")]
71+
[ValidateNotNullOrEmpty]
72+
public int Tail { get; set; }
73+
6774
[Parameter(Mandatory = false,
6875
HelpMessage = "Forces the overwrite of the existing file.")]
6976
public SwitchParameter Force { get; set; }
@@ -76,24 +83,27 @@ public override void ExecuteCmdlet()
7683
{
7784

7885
PsDeploymentScriptLog deploymentScriptLog;
86+
int tailParam = this.IsParameterBound(c => c.Tail) ? Tail : 0;
7987

8088
try
8189
{
8290
switch (ParameterSetName)
8391
{
8492
case SaveDeploymentScriptLogByName:
8593
deploymentScriptLog =
86-
DeploymentScriptsSdkClient.GetDeploymentScriptLog(Name, ResourceGroupName);
94+
DeploymentScriptsSdkClient.GetDeploymentScriptLog(Name, ResourceGroupName, tailParam);
8795
break;
8896
case SaveDeploymentScriptLogByResourceId:
8997
deploymentScriptLog = DeploymentScriptsSdkClient.GetDeploymentScriptLog(
9098
ResourceIdUtility.GetResourceName(this.DeploymentScriptResourceId),
91-
ResourceIdUtility.GetResourceGroupName(this.DeploymentScriptResourceId));
99+
ResourceIdUtility.GetResourceGroupName(this.DeploymentScriptResourceId),
100+
tailParam);
92101
break;
93102
case SaveDeploymentScriptLogByInputObject:
94103
deploymentScriptLog = DeploymentScriptsSdkClient.GetDeploymentScriptLog(
95104
DeploymentScriptInputObject.Name,
96-
ResourceIdUtility.GetResourceGroupName(DeploymentScriptInputObject.Id));
105+
ResourceIdUtility.GetResourceGroupName(DeploymentScriptInputObject.Id),
106+
tailParam);
97107
break;
98108
default:
99109
throw new PSInvalidOperationException();

src/Resources/ResourceManager/SdkClient/DeploymentScriptsSdkClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ public PsDeploymentScript GetDeploymentScript(string scriptName, string resource
7171
return psDeploymentScriptObject;
7272
}
7373

74-
public virtual PsDeploymentScriptLog GetDeploymentScriptLog(string scriptName, string resourceGroupName)
74+
public virtual PsDeploymentScriptLog GetDeploymentScriptLog(string scriptName, string resourceGroupName, int tail)
7575
{
7676
var deploymentScriptLog =
77-
DeploymentScriptsClient.DeploymentScripts.GetLogsDefault(resourceGroupName, scriptName);
77+
DeploymentScriptsClient.DeploymentScripts.GetLogsDefault(resourceGroupName, scriptName, tail);
7878

7979
return PsDeploymentScriptLog.ToPsDeploymentScriptLog(deploymentScriptLog);
8080
}

src/Resources/Resources.Test/DeploymentScriptsTests/SaveAzDeploymentScriptLogCommandTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ public void SaveDeploymentScriptLogToDisk()
7373
var expectedOutputPath = Path.Combine(TestLogPath, $"{deploymentScriptName}.txt");
7474

7575
deploymentScriptsMockClient
76-
.Setup(f => f.GetDeploymentScriptLog(deploymentScriptName, resourceGroupName))
76+
.Setup(f => f.GetDeploymentScriptLog(deploymentScriptName, resourceGroupName, 0))
7777
.Returns(expected);
7878
commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
79-
79+
8080
var dataStore = new MockDataStore();
8181
AzureSession.Instance.DataStore = dataStore;
8282

@@ -87,6 +87,7 @@ public void SaveDeploymentScriptLogToDisk()
8787
cmdlet.ResourceGroupName = resourceGroupName;
8888
cmdlet.Name = deploymentScriptName;
8989
cmdlet.OutputPath = TestLogPath;
90+
cmdlet.Tail = 0;
9091
cmdlet.SetParameterSet("SaveDeploymentScriptLogByName");
9192

9293
// Test

src/Resources/Resources.Test/ScenarioTests/DeploymentScriptsTests.ps1

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ function Test-GetDeploymentScriptLog-PowerShell
225225
$deployment = New-AzResourceGroupDeployment -Name $rname -ResourceGroupName $rgname -TemplateFile TemplateScriptDeployment.json -TemplateParameterFile TemplateScriptDeploymentParameters.json
226226
$deploymentScriptName = "PsTest-DeploymentScripts-" + $deployment.parameters.scriptSuffix.Value
227227
$resourceId = "/subscriptions/$subId/resourcegroups/$rgname/providers/Microsoft.Resources/deploymentScripts/$deploymentScriptName"
228+
$tailInteger = 5
228229

229230
# Test - GetLogByNameAndResourceGroup
230231
$getLogByNameAndResourceGroup = Get-AzDeploymentScriptLog -ResourceGroupName $rgname -Name $deploymentScriptName
@@ -235,13 +236,34 @@ function Test-GetDeploymentScriptLog-PowerShell
235236
Assert-AreEqual $deploymentScriptName $getLogByNameAndResourceGroup.DeploymentScriptName
236237

237238

239+
# Test - GetLogByNameAndResourceGroup - WithTailParameter
240+
$getLogByNameAndResourceGroupWithTail = Get-AzDeploymentScriptLog -ResourceGroupName $rgname -Name $deploymentScriptName -Tail $tailInteger
241+
$tail = [math]::min( ($getLogByNameAndResourceGroup.Log -split '\n').count, $tailInteger)
242+
243+
# Assert
244+
Assert-NotNull $getLogByNameAndResourceGroupWithTail
245+
Assert-NotNull $deploymentScriptName $getLogByNameAndResourceGroupWithTail.Log
246+
Assert-AreEqual $deploymentScriptName $getLogByNameAndResourceGroupWithTail.DeploymentScriptName
247+
Assert-AreEqual ($getLogByNameAndResourceGroupWithTail.Log -split '\n').count $tail
248+
249+
238250
# Test - GetLogByDeploymentScriptResourceId
239251
$getLogByResourceId = Get-AzDeploymentScriptLog -DeploymentScriptResourceId $resourceId
240252

241253
#Assert
242254
Assert-NotNull $getLogByResourceId
243255
Assert-NotNull $deploymentScriptName $getLogByResourceId.Log
244256
Assert-AreEqual $deploymentScriptName $getLogByResourceId.DeploymentScriptName
257+
258+
# Test - GetLogByDeploymentScriptResourceId - WithTailParameter
259+
$getLogByResourceIdWithTail = Get-AzDeploymentScriptLog -DeploymentScriptResourceId $resourceId -Tail $tailInteger
260+
$tail = [math]::min( ($getLogByResourceId.Log -split '\n').count, $tailInteger)
261+
262+
#Assert
263+
Assert-NotNull $getLogByResourceIdWithTail
264+
Assert-NotNull $deploymentScriptName $getLogByResourceIdWithTail.Log
265+
Assert-AreEqual $deploymentScriptName $getLogByResourceIdWithTail.DeploymentScriptName
266+
Assert-AreEqual ($getLogByResourceIdWithTail.Log -split '\n').count $tail
245267

246268

247269
#Test - GetLogByInputObject
@@ -252,6 +274,17 @@ function Test-GetDeploymentScriptLog-PowerShell
252274
Assert-NotNull $getLogByInputObject
253275
Assert-NotNull $deploymentScriptName $getLogByInputObject.Log
254276
Assert-AreEqual $deploymentScriptName $getLogByInputObject.DeploymentScriptName
277+
278+
#Test - GetLogByInputObject - WithTailParameter
279+
$deploymentScript = Get-AzDeploymentScript -ResourceGroupName $rgname -Name $deploymentScriptName
280+
$getLogByInputObjectWithTail = Get-AzDeploymentScriptLog -DeploymentScriptInputObject $deploymentScript -Tail $tailInteger
281+
$tail = [math]::min( ($getLogByInputObject.Log -split '\n').count, $tailInteger)
282+
283+
# Assert
284+
Assert-NotNull $getLogByInputObjectWithTail
285+
Assert-NotNull $deploymentScriptName $getLogByInputObjectWithTail.Log
286+
Assert-AreEqual $deploymentScriptName $getLogByInputObjectWithTail.DeploymentScriptName
287+
Assert-AreEqual ($getLogByResourceIdWithTail.Log -split '\n').count $tail
255288
}
256289
finally
257290
{
@@ -280,15 +313,30 @@ function Test-GetDeploymentScriptLog-Cli
280313
$deployment = New-AzResourceGroupDeployment -Name $rname -ResourceGroupName $rgname -TemplateFile TemplateScriptDeploymentCli.json -TemplateParameterFile TemplateScriptDeploymentParametersCli.json
281314
$deploymentScriptName = "PsTest-DeploymentScripts-" + $deployment.parameters.scriptSuffix.Value
282315
$resourceId = "/subscriptions/$subId/resourcegroups/$rgname/providers/Microsoft.Resources/deploymentScripts/$deploymentScriptName"
316+
$tailInteger = 2
283317

284318
# Test - GetLogByNameAndResourceGroup
285-
$getLogByNameAndResourceGroup = Get-AzDeploymentScriptLog -ResourceGroupName $rgname -Name $deploymentScriptName
319+
$getLogByNameAndResourceGroup = Get-AzDeploymentScriptLog -ResourceGroupName $rgname -Name $deploymentScriptName
286320

287321
# Assert
288322
Assert-NotNull $getLogByNameAndResourceGroup
289323
Assert-NotNull $deploymentScriptName $getLogByNameAndResourceGroup.Log
290324
Assert-AreEqual $deploymentScriptName $getLogByNameAndResourceGroup.DeploymentScriptName
291325

326+
# Test - GetLogByNameAndResourceGroup - WithTailParameter
327+
$getLogByNameAndResourceGroupWithTail = Get-AzDeploymentScriptLog -ResourceGroupName $rgname -Name $deploymentScriptName -Tail $tailInteger
328+
329+
#\r is used in the template deployment script to denote line breaks in the CLI log output
330+
$tail = [math]::min( ($getLogByNameAndResourceGroup.Log -split '\r').count, $tailInteger)
331+
332+
# Assert
333+
Assert-NotNull $getLogByNameAndResourceGroupWithTail
334+
Assert-NotNull $deploymentScriptName $getLogByNameAndResourceGroupWithTail.Log
335+
Assert-AreEqual $deploymentScriptName $getLogByNameAndResourceGroupWithTail.DeploymentScriptName
336+
337+
#Counting \n instead of \r as every instance of \r is replaced with \n when the backend processes the Tail parameter.
338+
Assert-AreEqual ($getLogByNameAndResourceGroupWithTail.Log -split '\n').count $tail
339+
292340
# Test - GetLogByDeploymentScriptResourceId
293341
$getLogByResourceId = Get-AzDeploymentScriptLog -DeploymentScriptResourceId $resourceId
294342

@@ -298,6 +346,17 @@ function Test-GetDeploymentScriptLog-Cli
298346
Assert-AreEqual $deploymentScriptName $getLogByResourceId.DeploymentScriptName
299347

300348

349+
# Test - GetLogByDeploymentScriptResourceId - WithTailParameter
350+
$getLogByResourceIdWithTail = Get-AzDeploymentScriptLog -DeploymentScriptResourceId $resourceId -Tail $tailInteger
351+
$tail = [math]::min( ($getLogByResourceId.Log -split '\r').count, $tailInteger)
352+
353+
#Assert
354+
Assert-NotNull $getLogByResourceIdWithTail
355+
Assert-NotNull $deploymentScriptName $getLogByResourceIdWithTail.Log
356+
Assert-AreEqual $deploymentScriptName $getLogByResourceIdWithTail.DeploymentScriptName
357+
Assert-AreEqual ($getLogByResourceIdWithTail.Log -split '\n').count $tail
358+
359+
301360
#Test - GetLogByInputObject
302361
$deploymentScript = Get-AzDeploymentScript -ResourceGroupName $rgname -Name $deploymentScriptName
303362
$getLogByInputObject = Get-AzDeploymentScriptLog -DeploymentScriptInputObject $deploymentScript
@@ -306,6 +365,18 @@ function Test-GetDeploymentScriptLog-Cli
306365
Assert-NotNull $getLogByInputObject
307366
Assert-NotNull $deploymentScriptName $getLogByInputObject.Log
308367
Assert-AreEqual $deploymentScriptName $getLogByInputObject.DeploymentScriptName
368+
369+
370+
#Test - GetLogByInputObject - WithTailParameter
371+
$deploymentScript = Get-AzDeploymentScript -ResourceGroupName $rgname -Name $deploymentScriptName
372+
$getLogByInputObjectWithTail = Get-AzDeploymentScriptLog -DeploymentScriptInputObject $deploymentScript -Tail $tailInteger
373+
$tail = [math]::min( ($getLogByResourceId.Log -split '\r').count, $tailInteger)
374+
375+
# Assert
376+
Assert-NotNull $getLogByInputObjectWithTail
377+
Assert-NotNull $deploymentScriptName $getLogByInputObjectWithTail.Log
378+
Assert-AreEqual $deploymentScriptName $getLogByInputObjectWithTail.DeploymentScriptName
379+
Assert-AreEqual ($getLogByInputObjectWithTail.Log -split '\n').count $tail
309380
}
310381
finally
311382
{
@@ -323,6 +394,7 @@ function Test-PipeDeploymentScriptObjectToGetLogs
323394
$rgname = Get-ResourceGroupName
324395
$rname = Get-ResourceName
325396
$rglocation = "West US 2"
397+
$tailInteger = 2
326398

327399
try
328400
{
@@ -340,6 +412,17 @@ function Test-PipeDeploymentScriptObjectToGetLogs
340412
Assert-NotNull $getLogByInputObject
341413
Assert-NotNull $deploymentScriptName $getLogByInputObject.Log
342414
Assert-AreEqual $deploymentScriptName $getLogByInputObject.DeploymentScriptName
415+
416+
417+
#Test - GetLogsByInputObjectPiped - WithTailParameter
418+
$getLogByInputObjectWithTail = Get-AzDeploymentScript -ResourceGroupName $rgname -Name $deploymentScriptName | Get-AzDeploymentScriptLog -Tail $tailInteger
419+
$tail = [math]::min( ($getLogByInputObject.Log -split '\r').count, $tailInteger)
420+
421+
# Assert
422+
Assert-NotNull $getLogByInputObjectWithTail
423+
Assert-NotNull $deploymentScriptName $getLogByInputObjectWithTail.Log
424+
Assert-AreEqual $deploymentScriptName $getLogByInputObjectWithTail.DeploymentScriptName
425+
Assert-AreEqual ($getLogByInputObjectWithTail.Log -split '\n').count $tail
343426
}
344427
finally
345428
{
@@ -359,6 +442,7 @@ function Test-TrySaveNonExistingFilePathForLogFile
359442
$rname = Get-ResourceName
360443
$rglocation = "West US 2"
361444
$badPath = "bad-path"
445+
$tailInteger = 3
362446

363447
try
364448
{
@@ -379,7 +463,8 @@ function Test-TrySaveNonExistingFilePathForLogFile
379463
$path = (Get-Item ".\").FullName
380464
$fullPath = Join-Path $path $badPath
381465
$exceptionMessage = "Cannot find path '$fullPath'"
382-
Assert-Throws { Save-AzDeploymentScriptLog -DeploymentScriptInputObject $deploymentScript -OutputPath $badPath } $exceptionMessage
466+
Assert-Throws { Save-AzDeploymentScriptLog -DeploymentScriptInputObject $deploymentScript -OutputPath $badPath } $exceptionMessage
467+
Assert-Throws { Save-AzDeploymentScriptLog -DeploymentScriptInputObject $deploymentScript -OutputPath $badPath -Tail $tailInteger} $exceptionMessage
383468
}
384469
finally
385470
{

0 commit comments

Comments
 (0)