Skip to content

Commit fe5a16a

Browse files
authored
Merge pull request #4198 from matthchr/feature/download-ranges
Add download range to Get-AzureBatchNodeFileContent
2 parents b739d15 + 921ca30 commit fe5a16a

File tree

9 files changed

+210
-14
lines changed

9 files changed

+210
-14
lines changed

src/ResourceManager/AzureBatch/ChangeLog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
- Additional information about change #1
1919
-->
2020
## Current Release
21+
- Added new Get-AzureBatchJobPreparationAndReleaseTaskStatus cmdlet.
22+
- Added byte range start and end to Get-AzureBatchNodeFileContent parameters.
2123

2224
## Version 4.1.0
23-
- Added new Get-AzureBatchJobPreparationAndReleaseTaskStatus cmdlet.
2425

2526
## Version 3.1.0
2627

src/ResourceManager/AzureBatch/Commands.Batch.Test/BatchTestHelpers.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,20 @@ public static RequestInterceptor CreateFakeGetFileAndPropertiesFromComputeNodeRe
313313
return interceptor;
314314
}
315315

316+
public static RequestInterceptor ExamineRequestInterceptor<T>(Action<T> assertAction) where T : class, IBatchRequest
317+
{
318+
RequestInterceptor interceptor = new RequestInterceptor(baseRequest =>
319+
{
320+
var request = baseRequest as T;
321+
322+
if (request != null)
323+
{
324+
assertAction(request);
325+
}
326+
});
327+
return interceptor;
328+
}
329+
316330
/// <summary>
317331
/// Builds a CertificateGetResponse object
318332
/// </summary>

src/ResourceManager/AzureBatch/Commands.Batch.Test/Files/GetBatchNodeFileContentCommandTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using Microsoft.Azure.Batch;
1616
using Microsoft.Azure.Batch.Protocol;
17+
using Microsoft.Azure.Batch.Protocol.BatchRequests;
1718
using Microsoft.WindowsAzure.Commands.ScenarioTest;
1819
using Moq;
1920
using System;
@@ -114,5 +115,54 @@ public void GetBatchNodeFileByComputeNodeContentParametersTest()
114115
cmdlet.ExecuteCmdlet();
115116
}
116117
}
118+
119+
[Theory]
120+
[InlineData(null, 14L)]
121+
[InlineData(7L, 14L)]
122+
[InlineData(7L, null)]
123+
[Trait(Category.AcceptanceType, Category.CheckIn)]
124+
public void GetBatchNodeFileByteRangeSet_IsPopulatedInRequest(long? rangeStart, long? rangeEnd)
125+
{
126+
// Setup cmdlet without required parameters
127+
BatchAccountContext context = BatchTestHelpers.CreateBatchContextWithKeys();
128+
cmdlet.BatchContext = context;
129+
cmdlet.JobId = null;
130+
cmdlet.TaskId = null;
131+
cmdlet.Name = null;
132+
cmdlet.InputObject = null;
133+
cmdlet.DestinationPath = null;
134+
cmdlet.ByteRangeStart = rangeStart;
135+
cmdlet.ByteRangeEnd = rangeEnd;
136+
137+
string fileName = "stdout.txt";
138+
bool hit = false;
139+
// Don't go to the service on a Get NodeFile call or Get NodeFile Properties call
140+
RequestInterceptor interceptor = BatchTestHelpers.CreateFakeGetFileAndPropertiesFromTaskResponseInterceptor(cmdlet.Name);
141+
RequestInterceptor examiner = BatchTestHelpers.ExamineRequestInterceptor<FileGetFromTaskBatchRequest>(req =>
142+
{
143+
hit = true;
144+
Assert.Equal($"bytes={cmdlet.ByteRangeStart}-{cmdlet.ByteRangeEnd}", req.Options.OcpRange);
145+
});
146+
147+
cmdlet.AdditionalBehaviors = new List<BatchClientBehavior> { examiner, interceptor };
148+
149+
using (MemoryStream memStream = new MemoryStream())
150+
{
151+
// Don't hit the file system during unit tests
152+
cmdlet.DestinationStream = memStream;
153+
154+
Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());
155+
156+
// Fill required task details
157+
cmdlet.JobId = "job-1";
158+
cmdlet.TaskId = "task";
159+
cmdlet.Name = fileName;
160+
161+
// Verify no exceptions occur
162+
cmdlet.ExecuteCmdlet();
163+
164+
Assert.True(hit);
165+
}
166+
}
117167
}
118168
}

src/ResourceManager/AzureBatch/Commands.Batch/Files/GetBatchNodeFileContentCommand.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,44 @@ public class GetBatchNodeFileContentCommand : BatchObjectModelCmdletBase
8383
[ValidateNotNullOrEmpty]
8484
public Stream DestinationStream { get; set; }
8585

86+
[Parameter(HelpMessage = "The start of the byte range to be downloaded. If this is not specified and ByteRangeEnd is, this defaults to 0.")]
87+
[ValidateNotNullOrEmpty]
88+
[ValidateRange(0, long.MaxValue)]
89+
public long? ByteRangeStart { get; set; }
90+
91+
[Parameter(HelpMessage = "The end of the byte range to be downloaded. If this is not specified and ByteRangeStart is, this defaults to the end of the file.")]
92+
[ValidateNotNullOrEmpty]
93+
[ValidateRange(0, long.MaxValue)]
94+
public long? ByteRangeEnd { get; set; }
95+
8696
public override void ExecuteCmdlet()
8797
{
88-
DownloadNodeFileOptions options = new DownloadNodeFileOptions(this.BatchContext, this.JobId, this.TaskId, this.PoolId,
89-
this.ComputeNodeId, this.Name, this.InputObject, this.DestinationPath, this.DestinationStream, this.AdditionalBehaviors);
98+
if (this.ByteRangeEnd != null && this.ByteRangeStart == null)
99+
{
100+
this.ByteRangeStart = 0;
101+
}
102+
103+
if (this.ByteRangeEnd == null && this.ByteRangeStart != null)
104+
{
105+
this.ByteRangeEnd = long.MaxValue;
106+
}
107+
108+
var byteRange = this.ByteRangeStart != null && this.ByteRangeEnd != null
109+
? new DownloadNodeFileOptions.ByteRange(this.ByteRangeStart.Value, this.ByteRangeEnd.Value)
110+
: null;
111+
112+
DownloadNodeFileOptions options = new DownloadNodeFileOptions(
113+
this.BatchContext,
114+
this.JobId,
115+
this.TaskId,
116+
this.PoolId,
117+
this.ComputeNodeId,
118+
this.Name,
119+
this.InputObject,
120+
this.DestinationPath,
121+
this.DestinationStream,
122+
byteRange,
123+
this.AdditionalBehaviors);
90124

91125
BatchClient.DownloadNodeFile(options);
92126
}

src/ResourceManager/AzureBatch/Commands.Batch/Models/BatchClient.Files.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
using System;
1818
using System.Collections.Generic;
1919
using System.IO;
20+
using System.Linq;
21+
using Protocol = Microsoft.Azure.Batch.Protocol;
22+
using BatchRequests = Microsoft.Azure.Batch.Protocol.BatchRequests;
2023
using NodeFile = Microsoft.Azure.Batch.NodeFile;
2124

2225
namespace Microsoft.Azure.Commands.Batch.Models
@@ -219,12 +222,36 @@ public void DownloadNodeFile(DownloadNodeFileOptions options)
219222
}
220223
}
221224

222-
DownloadNodeFileByInstance(nodeFile, options.DestinationPath, options.Stream, options.AdditionalBehaviors);
225+
DownloadNodeFileByInstance(nodeFile, options.DestinationPath, options.Stream, options.Range, options.AdditionalBehaviors);
223226
}
224227

225228
// Downloads the file represented by an NodeFile instance to the specified path.
226-
private void DownloadNodeFileByInstance(NodeFile file, string destinationPath, Stream stream, IEnumerable<BatchClientBehavior> additionalBehaviors = null)
229+
private void DownloadNodeFileByInstance(NodeFile file, string destinationPath, Stream stream, DownloadNodeFileOptions.ByteRange byteRange, IEnumerable<BatchClientBehavior> additionalBehaviors = null)
227230
{
231+
// TODO: Update this to use the new built in support in the C# SDK when we update the C# SDK to 6.x or later
232+
Protocol.RequestInterceptor interceptor = new Protocol.RequestInterceptor(baseRequest =>
233+
{
234+
var fromTaskRequest = baseRequest as BatchRequests.FileGetFromTaskBatchRequest;
235+
if (fromTaskRequest != null && byteRange != null)
236+
{
237+
fromTaskRequest.Options.OcpRange = $"bytes={byteRange.Start}-{byteRange.End}";
238+
}
239+
240+
var fromNodeRequest = baseRequest as BatchRequests.FileGetFromComputeNodeBatchRequest;
241+
if (fromNodeRequest != null && byteRange != null)
242+
{
243+
fromNodeRequest.Options.OcpRange = $"bytes={byteRange.Start}-{byteRange.End}";
244+
}
245+
});
246+
247+
additionalBehaviors = additionalBehaviors != null ? new List<BatchClientBehavior> { interceptor }.Union(additionalBehaviors) :
248+
new List<BatchClientBehavior>() { interceptor };
249+
250+
if (byteRange != null)
251+
{
252+
WriteVerbose(string.Format(Resources.DownloadingNodeFileByteRange, byteRange.Start, byteRange.End));
253+
}
254+
228255
if (stream != null)
229256
{
230257
// Don't dispose supplied Stream

src/ResourceManager/AzureBatch/Commands.Batch/Models/DownloadNodeFileOptions.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,30 @@ namespace Microsoft.Azure.Commands.Batch.Models
2222
{
2323
public class DownloadNodeFileOptions : NodeFileOperationParameters
2424
{
25-
public DownloadNodeFileOptions(BatchAccountContext context, string jobId, string taskId, string poolId, string computeNodeId, string nodeFileName,
26-
PSNodeFile nodeFile, string destinationPath, Stream stream, IEnumerable<BatchClientBehavior> additionalBehaviors = null)
25+
public class ByteRange
26+
{
27+
public ByteRange(long start, long end)
28+
{
29+
this.Start = start;
30+
this.End = end;
31+
}
32+
33+
public long Start { get; }
34+
public long End { get; }
35+
}
36+
37+
public DownloadNodeFileOptions(
38+
BatchAccountContext context,
39+
string jobId,
40+
string taskId,
41+
string poolId,
42+
string computeNodeId,
43+
string nodeFileName,
44+
PSNodeFile nodeFile,
45+
string destinationPath,
46+
Stream stream,
47+
ByteRange byteRange,
48+
IEnumerable<BatchClientBehavior> additionalBehaviors = null)
2749
: base(context, jobId, taskId, poolId, computeNodeId, nodeFileName, nodeFile, additionalBehaviors)
2850
{
2951
if (string.IsNullOrWhiteSpace(destinationPath) && stream == null)
@@ -32,6 +54,7 @@ public DownloadNodeFileOptions(BatchAccountContext context, string jobId, string
3254
}
3355

3456
this.DestinationPath = destinationPath;
57+
this.Range = byteRange;
3558
this.Stream = stream;
3659
}
3760

@@ -40,6 +63,11 @@ public DownloadNodeFileOptions(BatchAccountContext context, string jobId, string
4063
/// </summary>
4164
public string DestinationPath { get; set; }
4265

66+
/// <summary>
67+
/// The byte range to be downloaded.
68+
/// </summary>
69+
public ByteRange Range { get; }
70+
4371
/// <summary>
4472
/// The Stream into which the node file data will be written. This stream will not be closed or rewound by this call.
4573
/// </summary>

src/ResourceManager/AzureBatch/Commands.Batch/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/AzureBatch/Commands.Batch/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,7 @@
484484
<data name="GetJobPreparationAndReleaseStatus" xml:space="preserve">
485485
<value>Getting job preparation and release status for job "{0}"</value>
486486
</data>
487+
<data name="DownloadingNodeFileByteRange" xml:space="preserve">
488+
<value>Downloading byte range {0} to {1}</value>
489+
</data>
487490
</root>

src/ResourceManager/AzureBatch/Commands.Batch/help/Get-AzureBatchNodeFileContent.md

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,39 @@ Gets a Batch node file.
1515
### Task_Id_Path
1616
```
1717
Get-AzureBatchNodeFileContent -JobId <String> -TaskId <String> [-Name] <String> -DestinationPath <String>
18-
-BatchContext <BatchAccountContext> [<CommonParameters>]
18+
[-ByteRangeStart <Int64>] [-ByteRangeEnd <Int64>] -BatchContext <BatchAccountContext> [<CommonParameters>]
1919
```
2020

2121
### Task_Id_Stream
2222
```
2323
Get-AzureBatchNodeFileContent -JobId <String> -TaskId <String> [-Name] <String> -DestinationStream <Stream>
24-
-BatchContext <BatchAccountContext> [<CommonParameters>]
24+
[-ByteRangeStart <Int64>] [-ByteRangeEnd <Int64>] -BatchContext <BatchAccountContext> [<CommonParameters>]
2525
```
2626

2727
### ComputeNode_Id_Path
2828
```
2929
Get-AzureBatchNodeFileContent [-PoolId] <String> [-ComputeNodeId] <String> [-Name] <String>
30-
-DestinationPath <String> -BatchContext <BatchAccountContext> [<CommonParameters>]
30+
-DestinationPath <String> [-ByteRangeStart <Int64>] [-ByteRangeEnd <Int64>]
31+
-BatchContext <BatchAccountContext> [<CommonParameters>]
3132
```
3233

3334
### ComputeNode_Id_Stream
3435
```
3536
Get-AzureBatchNodeFileContent [-PoolId] <String> [-ComputeNodeId] <String> [-Name] <String>
36-
-DestinationStream <Stream> -BatchContext <BatchAccountContext> [<CommonParameters>]
37+
-DestinationStream <Stream> [-ByteRangeStart <Int64>] [-ByteRangeEnd <Int64>]
38+
-BatchContext <BatchAccountContext> [<CommonParameters>]
3739
```
3840

3941
### InputObject_Path
4042
```
41-
Get-AzureBatchNodeFileContent [[-InputObject] <PSNodeFile>] -DestinationPath <String>
42-
-BatchContext <BatchAccountContext> [<CommonParameters>]
43+
Get-AzureBatchNodeFileContent [[-InputObject] <PSNodeFile>] -DestinationPath <String> [-ByteRangeStart <Int64>]
44+
[-ByteRangeEnd <Int64>] -BatchContext <BatchAccountContext> [<CommonParameters>]
4345
```
4446

4547
### InputObject_Stream
4648
```
4749
Get-AzureBatchNodeFileContent [[-InputObject] <PSNodeFile>] -DestinationStream <Stream>
48-
-BatchContext <BatchAccountContext> [<CommonParameters>]
50+
[-ByteRangeStart <Int64>] [-ByteRangeEnd <Int64>] -BatchContext <BatchAccountContext> [<CommonParameters>]
4951
```
5052

5153
## DESCRIPTION
@@ -130,6 +132,34 @@ Accept pipeline input: True (ByValue)
130132
Accept wildcard characters: False
131133
```
132134
135+
### -ByteRangeEnd
136+
The end of the byte range to be downloaded.
137+
```yaml
138+
Type: Int64
139+
Parameter Sets: (All)
140+
Aliases:
141+
142+
Required: False
143+
Position: Named
144+
Default value: None
145+
Accept pipeline input: False
146+
Accept wildcard characters: False
147+
```
148+
149+
### -ByteRangeStart
150+
The start of the byte range to be downloaded.
151+
```yaml
152+
Type: Int64
153+
Parameter Sets: (All)
154+
Aliases:
155+
156+
Required: False
157+
Position: Named
158+
Default value: None
159+
Accept pipeline input: False
160+
Accept wildcard characters: False
161+
```
162+
133163
### -ComputeNodeId
134164
Specifies the ID of the compute node that contains the node file that this cmdlet returns.
135165

0 commit comments

Comments
 (0)