|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 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 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using Microsoft.Azure.Commands.Compute.Common; |
| 16 | +using Microsoft.Azure.Commands.Compute.Models; |
| 17 | +using Microsoft.Azure.Common.Authentication; |
| 18 | +using Microsoft.Azure.Common.Authentication.Models; |
| 19 | +using Microsoft.Azure.Management.Storage; |
| 20 | +using Microsoft.WindowsAzure.Commands.Sync.Download; |
| 21 | +using System; |
| 22 | +using System.IO; |
| 23 | +using System.Management.Automation; |
| 24 | + |
| 25 | +namespace Microsoft.Azure.Commands.Compute.StorageServices |
| 26 | +{ |
| 27 | + [Cmdlet(VerbsData.Save, ProfileNouns.Vhd), OutputType(typeof(VhdDownloadContext))] |
| 28 | + public class SaveAzureVhdCommand : ComputeClientBaseCmdlet |
| 29 | + { |
| 30 | + private const int DefaultNumberOfUploaderThreads = 8; |
| 31 | + private const string ResourceGroupParameterSet = "ResourceGroupParameterSetName"; |
| 32 | + private const string StorageKeyParameterSet = "StorageKeyParameterSetName"; |
| 33 | + |
| 34 | + [Parameter( |
| 35 | + Position = 0, |
| 36 | + Mandatory = true, |
| 37 | + ParameterSetName = ResourceGroupParameterSet, |
| 38 | + ValueFromPipelineByPropertyName = true)] |
| 39 | + [ValidateNotNullOrEmpty] |
| 40 | + public string ResourceGroupName { get; set; } |
| 41 | + |
| 42 | + [Parameter( |
| 43 | + Position = 0, |
| 44 | + Mandatory = true, |
| 45 | + ParameterSetName = StorageKeyParameterSet, |
| 46 | + HelpMessage = "Key of the storage account")] |
| 47 | + [ValidateNotNullOrEmpty] |
| 48 | + [Alias("sk")] |
| 49 | + public string StorageKey |
| 50 | + { |
| 51 | + get; |
| 52 | + set; |
| 53 | + } |
| 54 | + |
| 55 | + [Parameter( |
| 56 | + Position = 1, |
| 57 | + Mandatory = true, |
| 58 | + ValueFromPipelineByPropertyName = true, |
| 59 | + HelpMessage = "Uri to blob")] |
| 60 | + [ValidateNotNullOrEmpty] |
| 61 | + [Alias("src")] |
| 62 | + public Uri Source |
| 63 | + { |
| 64 | + get; |
| 65 | + set; |
| 66 | + } |
| 67 | + |
| 68 | + [Parameter( |
| 69 | + Position = 2, |
| 70 | + Mandatory = true, |
| 71 | + HelpMessage = "Local path of the vhd file")] |
| 72 | + [ValidateNotNullOrEmpty] |
| 73 | + [Alias("lf")] |
| 74 | + public FileInfo LocalFilePath |
| 75 | + { |
| 76 | + get; |
| 77 | + set; |
| 78 | + } |
| 79 | + |
| 80 | + private int numberOfThreads = DefaultNumberOfUploaderThreads; |
| 81 | + |
| 82 | + [Parameter( |
| 83 | + Position = 3, |
| 84 | + Mandatory = false, |
| 85 | + HelpMessage = "Number of downloader threads")] |
| 86 | + [ValidateNotNullOrEmpty] |
| 87 | + [ValidateRange(1, 64)] |
| 88 | + [Alias("th")] |
| 89 | + public int NumberOfThreads |
| 90 | + { |
| 91 | + get { return this.numberOfThreads; } |
| 92 | + set { this.numberOfThreads = value; } |
| 93 | + } |
| 94 | + |
| 95 | + [Parameter( |
| 96 | + Position = 4, |
| 97 | + Mandatory = false, |
| 98 | + HelpMessage = "Delete the local file if already exists")] |
| 99 | + [ValidateNotNullOrEmpty] |
| 100 | + [Alias("o")] |
| 101 | + public SwitchParameter OverWrite |
| 102 | + { |
| 103 | + get; |
| 104 | + set; |
| 105 | + } |
| 106 | + |
| 107 | + protected override void ProcessRecord() |
| 108 | + { |
| 109 | + BlobUri blobUri; |
| 110 | + if (!BlobUri.TryParseUri(Source, out blobUri)) |
| 111 | + { |
| 112 | + throw new ArgumentOutOfRangeException("Source", Source.ToString()); |
| 113 | + } |
| 114 | + |
| 115 | + var storageKey = this.StorageKey; |
| 116 | + if (this.StorageKey == null) |
| 117 | + { |
| 118 | + var storageClient = AzureSession.ClientFactory.CreateClient<StorageManagementClient>( |
| 119 | + DefaultProfile.Context, AzureEnvironment.Endpoint.ResourceManager); |
| 120 | + |
| 121 | + |
| 122 | + var storageService = storageClient.StorageAccounts.GetProperties(this.ResourceGroupName, blobUri.StorageAccountName); |
| 123 | + if (storageService != null) |
| 124 | + { |
| 125 | + var storageKeys = storageClient.StorageAccounts.ListKeys(this.ResourceGroupName, storageService.StorageAccount.Name); |
| 126 | + storageKey = storageKeys.StorageAccountKeys.Key1; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + var downloaderParameters = new DownloaderParameters |
| 131 | + { |
| 132 | + BlobUri = blobUri, |
| 133 | + LocalFilePath = LocalFilePath.FullName, |
| 134 | + ConnectionLimit = NumberOfThreads, |
| 135 | + StorageAccountKey = storageKey, |
| 136 | + ValidateFreeDiskSpace = true, |
| 137 | + OverWrite = OverWrite |
| 138 | + }; |
| 139 | + |
| 140 | + var vhdDownloadContext = VhdDownloaderModel.Download(downloaderParameters, this); |
| 141 | + WriteObject(vhdDownloadContext); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments