|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.Azure.Management.SiteRecovery; |
| 6 | +using Microsoft.Azure.Management.SiteRecovery.Models; |
| 7 | + |
| 8 | +namespace Microsoft.Azure.Commands.SiteRecovery |
| 9 | +{ |
| 10 | + public partial class PSRecoveryServicesClient |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// Gets all storage classifications associated with a vault. |
| 14 | + /// </summary> |
| 15 | + /// <param name="callback">Callback to execute on the result.</param> |
| 16 | + /// <returns>Task object tracking async operation.</returns> |
| 17 | + public Task EnumerateStorageClassificationsAsync(Action<IEnumerable<StorageClassification>> callback) |
| 18 | + { |
| 19 | + CancellationToken cancellationToken = new CancellationToken(); |
| 20 | + |
| 21 | + Task backgroundTask = new Task(new Action(() => |
| 22 | + { |
| 23 | + Task<StorageClassificationListResponse> storageTask = |
| 24 | + this.GetSiteRecoveryClient().StorageClassification.ListAllAsync( |
| 25 | + this.GetRequestHeaders(), |
| 26 | + cancellationToken); |
| 27 | + |
| 28 | + Task.WaitAll(storageTask); |
| 29 | + |
| 30 | + callback(storageTask.Result.StorageClassifications); |
| 31 | + |
| 32 | + while (!string.IsNullOrEmpty(storageTask.Result.NextLink)) |
| 33 | + { |
| 34 | + storageTask = |
| 35 | + this.GetSiteRecoveryClient().StorageClassification.ListNextAsync( |
| 36 | + storageTask.Result.NextLink, |
| 37 | + this.GetRequestHeaders(), |
| 38 | + cancellationToken); |
| 39 | + |
| 40 | + Task.WaitAll(storageTask); |
| 41 | + |
| 42 | + callback(storageTask.Result.StorageClassifications); |
| 43 | + } |
| 44 | + })); |
| 45 | + |
| 46 | + backgroundTask.Start(); |
| 47 | + return backgroundTask; |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Gets all storage classifications associated with a vault. |
| 52 | + /// </summary> |
| 53 | + /// <param name="callback">Callback to execute on the result.</param> |
| 54 | + /// <returns>Task object tracking async operation.</returns> |
| 55 | + public Task EnumerateStorageClassificationMappingsAsync(Action<IEnumerable<StorageClassificationMapping>> callback) |
| 56 | + { |
| 57 | + CancellationToken cancellationToken = new CancellationToken(); |
| 58 | + |
| 59 | + Task backgroundTask = new Task(new Action(() => |
| 60 | + { |
| 61 | + Task<StorageClassificationMappingListResponse> storageTask = |
| 62 | + this.GetSiteRecoveryClient().StorageClassificationMapping.ListAllAsync( |
| 63 | + this.GetRequestHeaders(), |
| 64 | + cancellationToken); |
| 65 | + |
| 66 | + Task.WaitAll(storageTask); |
| 67 | + |
| 68 | + callback(storageTask.Result.StorageClassificationMappings); |
| 69 | + |
| 70 | + while (!string.IsNullOrEmpty(storageTask.Result.NextLink)) |
| 71 | + { |
| 72 | + storageTask = |
| 73 | + this.GetSiteRecoveryClient().StorageClassificationMapping.ListNextAsync( |
| 74 | + storageTask.Result.NextLink, |
| 75 | + this.GetRequestHeaders(), |
| 76 | + cancellationToken); |
| 77 | + |
| 78 | + Task.WaitAll(storageTask); |
| 79 | + |
| 80 | + callback(storageTask.Result.StorageClassificationMappings); |
| 81 | + } |
| 82 | + })); |
| 83 | + |
| 84 | + backgroundTask.Start(); |
| 85 | + return backgroundTask; |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Starts job for unmapping storage classifications. |
| 90 | + /// </summary> |
| 91 | + /// <param name="mapping">Classification mapping.</param> |
| 92 | + /// <returns>Job object.</returns> |
| 93 | + public ASRJob UnmapStorageClassifications(StorageClassificationMapping mapping) |
| 94 | + { |
| 95 | + string[] tokens = mapping.Id.UnFormatArmId( |
| 96 | + ARMResourceIdPaths.StorageClassificationMappingResourceIdPath); |
| 97 | + LongRunningOperationResponse operationResponse = |
| 98 | + this.GetSiteRecoveryClient().StorageClassificationMapping |
| 99 | + .BeginUnpairStorageClassification( |
| 100 | + tokens[0], |
| 101 | + tokens[1], |
| 102 | + tokens[2], |
| 103 | + this.GetRequestHeaders()); |
| 104 | + |
| 105 | + JobResponse jobResponse = |
| 106 | + this.GetAzureSiteRecoveryJobDetails( |
| 107 | + PSRecoveryServicesClient.GetJobIdFromReponseLocation(operationResponse.Location)); |
| 108 | + |
| 109 | + return new ASRJob(jobResponse.Job); |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Starts job for mapping storage classification. |
| 114 | + /// </summary> |
| 115 | + /// <param name="primaryClassification">Primary classification.</param> |
| 116 | + /// <param name="recoveryClassification">Recovery classification.</param> |
| 117 | + /// <param name="armName">Optional. ARM name of the mapping.</param> |
| 118 | + /// <returns>Job object.</returns> |
| 119 | + public ASRJob MapStorageClassification( |
| 120 | + ASRStorageClassification primaryClassification, |
| 121 | + ASRStorageClassification recoveryClassification, |
| 122 | + string armName = null) |
| 123 | + { |
| 124 | + string[] tokens = primaryClassification.StorageClassificationId.UnFormatArmId( |
| 125 | + ARMResourceIdPaths.StorageClassificationResourceIdPath); |
| 126 | + |
| 127 | + if (string.IsNullOrEmpty(armName)) |
| 128 | + { |
| 129 | + armName = string.Format( |
| 130 | + "StrgMap_{0}_{1}", |
| 131 | + primaryClassification.StorageClassificationFriendlyName, |
| 132 | + recoveryClassification.StorageClassificationFriendlyName); |
| 133 | + } |
| 134 | + |
| 135 | + var props = new StorageClassificationMappingInputProperties() |
| 136 | + { |
| 137 | + TargetStorageClassificationId = recoveryClassification.StorageClassificationId |
| 138 | + }; |
| 139 | + |
| 140 | + var input = new StorageClassificationMappingInput() |
| 141 | + { |
| 142 | + Properties = props |
| 143 | + }; |
| 144 | + |
| 145 | + LongRunningOperationResponse operationResponse = |
| 146 | + this.GetSiteRecoveryClient().StorageClassificationMapping |
| 147 | + .BeginPairStorageClassification( |
| 148 | + tokens[0], |
| 149 | + tokens[1], |
| 150 | + armName, |
| 151 | + input, |
| 152 | + this.GetRequestHeaders()); |
| 153 | + |
| 154 | + JobResponse jobResponse = |
| 155 | + this.GetAzureSiteRecoveryJobDetails( |
| 156 | + PSRecoveryServicesClient.GetJobIdFromReponseLocation(operationResponse.Location)); |
| 157 | + |
| 158 | + return new ASRJob(jobResponse.Job); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /// <summary> |
| 163 | + /// Extension methods for Storage classification. |
| 164 | + /// </summary> |
| 165 | + public static class StorageClassificationExtensions |
| 166 | + { |
| 167 | + /// <summary> |
| 168 | + /// Gets primary storage classification ARM Id. |
| 169 | + /// </summary> |
| 170 | + /// <param name="mapping">Storage classification mapping input.</param> |
| 171 | + /// <returns>ARM Id of the primary storage classification.</returns> |
| 172 | + public static string GetPrimaryStorageClassificationId( |
| 173 | + this StorageClassificationMapping mapping) |
| 174 | + { |
| 175 | + string[] tokens = mapping.Id.UnFormatArmId( |
| 176 | + ARMResourceIdPaths.StorageClassificationMappingResourceIdPath); |
| 177 | + |
| 178 | + string vaultId = mapping.Id.GetVaultArmId(); |
| 179 | + |
| 180 | + return vaultId + "/" + string.Format( |
| 181 | + ARMResourceIdPaths.StorageClassificationResourceIdPath, |
| 182 | + tokens[0], |
| 183 | + tokens[1]); |
| 184 | + } |
| 185 | + |
| 186 | + /// <summary> |
| 187 | + /// Gets fabric Id from classification ARM Id. |
| 188 | + /// </summary> |
| 189 | + /// <param name="classification">Storage classification.</param> |
| 190 | + /// <returns>ARM Id of the fabric.</returns> |
| 191 | + public static string GetFabricId( |
| 192 | + this StorageClassification classification) |
| 193 | + { |
| 194 | + string[] tokens = classification.Id.UnFormatArmId( |
| 195 | + ARMResourceIdPaths.StorageClassificationResourceIdPath); |
| 196 | + |
| 197 | + string vaultId = classification.Id.GetVaultArmId(); |
| 198 | + |
| 199 | + return vaultId + "/" + string.Format( |
| 200 | + ARMResourceIdPaths.FabricResourceIdPath, |
| 201 | + tokens[0]); |
| 202 | + } |
| 203 | + |
| 204 | + /// <summary> |
| 205 | + /// Gets powershell object from Storage classification object. |
| 206 | + /// </summary> |
| 207 | + /// <param name="classification">Classification to process.</param> |
| 208 | + /// <param name="classificationMap">Dictionary of all possible classifications.</param> |
| 209 | + /// <param name="fabricMap">Dictionary of list of fabrics.</param> |
| 210 | + /// <param name="mappingsDict">Dictionary of mapping objects.</param> |
| 211 | + /// <returns>Powershell representation of storage classification.</returns> |
| 212 | + public static ASRStorageClassification GetPSObject( |
| 213 | + this StorageClassification classification, |
| 214 | + Dictionary<string, StorageClassification> classificationMap, |
| 215 | + Dictionary<string, Fabric> fabricMap, |
| 216 | + Dictionary<string, List<StorageClassificationMapping>> mappingsDict) |
| 217 | + { |
| 218 | + var fabric = fabricMap[classification.GetFabricId()]; |
| 219 | + List<StorageClassificationMapping> targetClassifications; |
| 220 | + |
| 221 | + return new ASRStorageClassification() |
| 222 | + { |
| 223 | + FabricFriendlyName = fabric.Properties.FriendlyName, |
| 224 | + FabricId = fabric.Id, |
| 225 | + StorageClassificationFriendlyName = |
| 226 | + classification.Properties.FriendlyName, |
| 227 | + StorageClassificationId = classification.Id, |
| 228 | + TargetClassifications = |
| 229 | + mappingsDict.TryGetValue( |
| 230 | + classification.Id, |
| 231 | + out targetClassifications) ? |
| 232 | + targetClassifications.ConvertAll(item => |
| 233 | + classificationMap[item.Properties.TargetStorageClassificationId] |
| 234 | + .GetPSObject( |
| 235 | + classificationMap, |
| 236 | + fabricMap, |
| 237 | + mappingsDict)) : |
| 238 | + new List<ASRStorageClassification>() |
| 239 | + }; |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments