| | | 1 | | // Copyright (c) Microsoft and contributors. All rights reserved. |
| | | 2 | | // |
| | | 3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
| | | 4 | | // you may not use this file except in compliance with the License. |
| | | 5 | | // You may obtain a copy of the License at |
| | | 6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
| | | 7 | | // |
| | | 8 | | // Unless required by applicable law or agreed to in writing, software |
| | | 9 | | // distributed under the License is distributed on an "AS IS" BASIS, |
| | | 10 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | | 11 | | // |
| | | 12 | | // See the License for the specific language governing permissions and |
| | | 13 | | // limitations under the License. |
| | | 14 | | |
| | | 15 | | using Microsoft.Azure.Batch.Conventions.Files.Utilities; |
| | | 16 | | using Microsoft.WindowsAzure.Storage; |
| | | 17 | | using System; |
| | | 18 | | using System.Collections.Generic; |
| | | 19 | | using System.Linq; |
| | | 20 | | using System.Text; |
| | | 21 | | using System.Threading.Tasks; |
| | | 22 | | |
| | | 23 | | namespace Microsoft.Azure.Batch.Conventions.Files |
| | | 24 | | { |
| | | 25 | | /// <summary> |
| | | 26 | | /// Provides methods for working with the outputs of a <see cref="CloudTask"/>. |
| | | 27 | | /// </summary> |
| | | 28 | | public static class CloudTaskExtensions |
| | | 29 | | { |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the <see cref="TaskOutputStorage"/> for a specified <see cref="CloudTask"/>. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="task">The task for which to get output storage.</param> |
| | | 34 | | /// <param name="storageAccount">The storage account linked to the Azure Batch account.</param> |
| | | 35 | | /// <returns>A TaskOutputStorage for the specified task.</returns> |
| | | 36 | | public static TaskOutputStorage OutputStorage(this CloudTask task, CloudStorageAccount storageAccount) |
| | | 37 | | { |
| | 2 | 38 | | if (task == null) |
| | | 39 | | { |
| | 1 | 40 | | throw new ArgumentNullException(nameof(task)); |
| | | 41 | | } |
| | 1 | 42 | | if (storageAccount == null) |
| | | 43 | | { |
| | 1 | 44 | | throw new ArgumentNullException(nameof(storageAccount)); |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | return new TaskOutputStorage(storageAccount, task.JobId(), task.Id); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets the Blob name prefix/folder where files of the given kind are stored |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="task">The task to calculate the output storage destination for.</param> |
| | | 54 | | /// <param name="kind">The output kind.</param> |
| | | 55 | | /// <returns>The Blob name prefix/folder where files of the given kind are stored.</returns> |
| | | 56 | | public static string GetOutputStoragePath(this CloudTask task, TaskOutputKind kind) |
| | 4 | 57 | | => StoragePath.TaskStoragePath.BlobNamePrefixImpl(kind, task.Id); |
| | | 58 | | |
| | | 59 | | private static string JobId(this CloudTask task) |
| | | 60 | | { |
| | | 61 | | // Workaround for CloudTask not knowing its parent job ID. |
| | | 62 | | |
| | 0 | 63 | | if (task.Url == null) |
| | | 64 | | { |
| | 0 | 65 | | throw new ArgumentException("Task Url property must be populated", nameof(task)); |
| | | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | var jobId = UrlUtils.GetUrlValueSegment(task.Url, "jobs"); |
| | | 69 | | |
| | 0 | 70 | | if (jobId == null) |
| | | 71 | | { |
| | 0 | 72 | | throw new ArgumentException($"Task URL is malformed: unable to obtain job ID from URL '{task.Url}'", nam |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | return jobId; |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | } |