|
| 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 System; |
| 16 | +using System.ComponentModel; |
| 17 | +using System.Management.Automation; |
| 18 | +using Microsoft.Azure.Management.SiteRecovery.Models; |
| 19 | +using Microsoft.Azure.Portal.RecoveryServices.Models.Common; |
| 20 | +using Properties = Microsoft.Azure.Commands.SiteRecovery.Properties; |
| 21 | +using System.Collections.Generic; |
| 22 | + |
| 23 | +namespace Microsoft.Azure.Commands.SiteRecovery |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// Updates Azure Site Recovery Policy. |
| 27 | + /// </summary> |
| 28 | + [Cmdlet(VerbsData.Update, "AzureRmSiteRecoveryPolicy", DefaultParameterSetName = ASRParameterSets.EnterpriseToAzure)] |
| 29 | + public class UpdateAzureRmSiteRecoveryPolicy : SiteRecoveryCmdletBase |
| 30 | + { |
| 31 | + |
| 32 | + #region Parameters |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets or sets Name of the Policy. |
| 36 | + /// </summary> |
| 37 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToEnterprise, Mandatory = true)] |
| 38 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToAzure, Mandatory = true)] |
| 39 | + public ASRPolicy Policy { get; set; } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Gets or sets a value for Replication Method of the Policy. |
| 43 | + /// </summary> |
| 44 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToEnterprise)] |
| 45 | + [ValidateNotNullOrEmpty] |
| 46 | + [ValidateSet( |
| 47 | + Constants.OnlineReplicationMethod, |
| 48 | + Constants.OfflineReplicationMethod)] |
| 49 | + public string ReplicationMethod { get; set; } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets or sets Replication Frequency of the Policy in seconds. |
| 53 | + /// </summary> |
| 54 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToEnterprise, Mandatory = true)] |
| 55 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToAzure, Mandatory = true)] |
| 56 | + [ValidateNotNullOrEmpty] |
| 57 | + [ValidateSet( |
| 58 | + Constants.Thirty, |
| 59 | + Constants.ThreeHundred, |
| 60 | + Constants.NineHundred)] |
| 61 | + public string ReplicationFrequencyInSeconds { get; set; } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Gets or sets Recovery Points of the Policy. |
| 65 | + /// </summary> |
| 66 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToEnterprise)] |
| 67 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToAzure)] |
| 68 | + [ValidateNotNullOrEmpty] |
| 69 | + [DefaultValue(0)] |
| 70 | + public int RecoveryPoints { get; set; } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Gets or sets Application Consistent Snapshot Frequency of the Policy in hours. |
| 74 | + /// </summary> |
| 75 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToEnterprise)] |
| 76 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToAzure)] |
| 77 | + [ValidateNotNullOrEmpty] |
| 78 | + [DefaultValue(0)] |
| 79 | + public int ApplicationConsistentSnapshotFrequencyInHours { get; set; } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Gets or sets a value indicating whether Compression needs to be Enabled on the Policy. |
| 83 | + /// </summary> |
| 84 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToEnterprise)] |
| 85 | + [DefaultValue(false)] |
| 86 | + public SwitchParameter CompressionEnabled { get; set; } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Gets or sets the Replication Port of the Policy. |
| 90 | + /// </summary> |
| 91 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToEnterprise, Mandatory = true)] |
| 92 | + [ValidateNotNullOrEmpty] |
| 93 | + public ushort ReplicationPort { get; set; } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Gets or sets the Replication Port of the Policy. |
| 97 | + /// </summary> |
| 98 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToEnterprise)] |
| 99 | + [ValidateNotNullOrEmpty] |
| 100 | + [ValidateSet( |
| 101 | + Constants.AuthenticationTypeCertificate, |
| 102 | + Constants.AuthenticationTypeKerberos)] |
| 103 | + public string Authentication { get; set; } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Gets or sets Replication Start time of the Policy. |
| 107 | + /// </summary> |
| 108 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToEnterprise)] |
| 109 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToAzure)] |
| 110 | + [ValidateNotNullOrEmpty] |
| 111 | + public TimeSpan? ReplicationStartTime { get; set; } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Gets or sets a value indicating whether Replica should be Deleted on |
| 115 | + /// disabling protection of a protection entity protected by the Policy. |
| 116 | + /// </summary> |
| 117 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToEnterprise)] |
| 118 | + [DefaultValue(false)] |
| 119 | + public SwitchParameter AllowReplicaDeletion { get; set; } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Gets or sets Recovery Azure Storage Account Name of the Policy for E2A scenarios. |
| 123 | + /// </summary> |
| 124 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToAzure, Mandatory = false)] |
| 125 | + [ValidateNotNullOrEmpty] |
| 126 | + public string RecoveryAzureStorageAccountId { get; set; } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Gets or sets Encrypt parameter. On passing, data will be encrypted. |
| 130 | + /// </summary> |
| 131 | + [Parameter(ParameterSetName = ASRParameterSets.EnterpriseToAzure)] |
| 132 | + public SwitchParameter Encrypt { get; set; } |
| 133 | + |
| 134 | + #endregion Parameters |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// ProcessRecord of the command. |
| 138 | + /// </summary> |
| 139 | + public override void ExecuteSiteRecoveryCmdlet() |
| 140 | + { |
| 141 | + base.ExecuteSiteRecoveryCmdlet(); |
| 142 | + |
| 143 | + switch (this.ParameterSetName) |
| 144 | + { |
| 145 | + case ASRParameterSets.EnterpriseToEnterprise: |
| 146 | + this.EnterpriseToEnterprisePolicyObject(); |
| 147 | + break; |
| 148 | + case ASRParameterSets.EnterpriseToAzure: |
| 149 | + this.EnterpriseToAzurePolicyObject(); |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// Creates an E2E Policy object |
| 156 | + /// </summary> |
| 157 | + private void EnterpriseToEnterprisePolicyObject() |
| 158 | + { |
| 159 | + if (string.Compare(this.Policy.ReplicationProvider, Constants.HyperVReplica2012, StringComparison.OrdinalIgnoreCase) != 0 && string.Compare(this.Policy.ReplicationProvider, Constants.HyperVReplica2012R2, StringComparison.OrdinalIgnoreCase) != 0) |
| 160 | + { |
| 161 | + throw new InvalidOperationException( |
| 162 | + string.Format( |
| 163 | + Properties.Resources.IncorrectReplicationProvider, |
| 164 | + this.Policy.ReplicationProvider)); |
| 165 | + } |
| 166 | + |
| 167 | + PSRecoveryServicesClient.ValidateReplicationStartTime(this.ReplicationStartTime); |
| 168 | + |
| 169 | + ushort replicationFrequencyInSeconds = |
| 170 | + PSRecoveryServicesClient.ConvertReplicationFrequencyToUshort(this.ReplicationFrequencyInSeconds); |
| 171 | + |
| 172 | + var updatePolicyProperties = new UpdatePolicyProperties(); |
| 173 | + |
| 174 | + if (string.Compare(this.Policy.ReplicationProvider, Constants.HyperVReplica2012, StringComparison.OrdinalIgnoreCase) == 0) |
| 175 | + { |
| 176 | + updatePolicyProperties.ReplicationProviderSettings = new HyperVReplica2012PolicyInput() |
| 177 | + { |
| 178 | + AllowedAuthenticationType = |
| 179 | + (ushort)((string.Compare(this.Authentication, Constants.AuthenticationTypeKerberos, StringComparison.OrdinalIgnoreCase) == 0) ? 1 : 2), |
| 180 | + ApplicationConsistentSnapshotFrequencyInHours = this.ApplicationConsistentSnapshotFrequencyInHours, |
| 181 | + Compression = this.CompressionEnabled == true ? "Enable" : "Disable", |
| 182 | + InitialReplicationMethod = |
| 183 | + (string.Compare(this.ReplicationMethod, Constants.OnlineReplicationMethod, StringComparison.OrdinalIgnoreCase) == 0) ? "OverNetwork" : "Offline", |
| 184 | + OnlineReplicationStartTime = this.ReplicationStartTime, |
| 185 | + RecoveryPoints = this.RecoveryPoints, |
| 186 | + ReplicaDeletion = this.AllowReplicaDeletion == true ? "Required" : "NotRequired", |
| 187 | + ReplicationPort = this.ReplicationPort |
| 188 | + }; |
| 189 | + |
| 190 | + } |
| 191 | + else |
| 192 | + { |
| 193 | + updatePolicyProperties.ReplicationProviderSettings = new HyperVReplica2012R2PolicyInput() |
| 194 | + { |
| 195 | + AllowedAuthenticationType = |
| 196 | + (ushort)((string.Compare(this.Authentication, Constants.AuthenticationTypeKerberos, StringComparison.OrdinalIgnoreCase) == 0) ? 1 : 2), |
| 197 | + ApplicationConsistentSnapshotFrequencyInHours = this.ApplicationConsistentSnapshotFrequencyInHours, |
| 198 | + Compression = this.CompressionEnabled == true ? "Enable" : "Disable", |
| 199 | + InitialReplicationMethod = |
| 200 | + (string.Compare(this.ReplicationMethod, Constants.OnlineReplicationMethod, StringComparison.OrdinalIgnoreCase) == 0) ? "OverNetwork" : "Offline", |
| 201 | + OnlineReplicationStartTime = this.ReplicationStartTime, |
| 202 | + RecoveryPoints = this.RecoveryPoints, |
| 203 | + ReplicaDeletion = this.AllowReplicaDeletion == true ? "Required" : "NotRequired", |
| 204 | + ReplicationFrequencyInSeconds = replicationFrequencyInSeconds, |
| 205 | + ReplicationPort = this.ReplicationPort |
| 206 | + }; |
| 207 | + } |
| 208 | + |
| 209 | + var updatePolicyInput = new UpdatePolicyInput() |
| 210 | + { |
| 211 | + Properties = updatePolicyProperties |
| 212 | + }; |
| 213 | + |
| 214 | + LongRunningOperationResponse responseBlue = |
| 215 | + RecoveryServicesClient.UpdatePolicy(this.Policy.Name, updatePolicyInput); |
| 216 | + |
| 217 | + JobResponse jobResponseBlue = |
| 218 | + RecoveryServicesClient |
| 219 | + .GetAzureSiteRecoveryJobDetails(PSRecoveryServicesClient.GetJobIdFromReponseLocation(responseBlue.Location)); |
| 220 | + |
| 221 | + WriteObject(new ASRJob(jobResponseBlue.Job)); |
| 222 | + } |
| 223 | + |
| 224 | + /// <summary> |
| 225 | + /// Creates an E2A Policy Object |
| 226 | + /// </summary> |
| 227 | + private void EnterpriseToAzurePolicyObject() |
| 228 | + { |
| 229 | + if (string.Compare(this.Policy.ReplicationProvider, Constants.HyperVReplicaAzure, StringComparison.OrdinalIgnoreCase) != 0) |
| 230 | + { |
| 231 | + throw new InvalidOperationException( |
| 232 | + string.Format( |
| 233 | + Properties.Resources.IncorrectReplicationProvider, |
| 234 | + this.Policy.ReplicationProvider)); |
| 235 | + } |
| 236 | + |
| 237 | + PSRecoveryServicesClient.ValidateReplicationStartTime(this.ReplicationStartTime); |
| 238 | + |
| 239 | + ushort replicationFrequencyInSeconds = |
| 240 | + PSRecoveryServicesClient.ConvertReplicationFrequencyToUshort(this.ReplicationFrequencyInSeconds); |
| 241 | + |
| 242 | + var hyperVReplicaAzurePolicyInput = new HyperVReplicaAzurePolicyInput() |
| 243 | + { |
| 244 | + ApplicationConsistentSnapshotFrequencyInHours = |
| 245 | + this.ApplicationConsistentSnapshotFrequencyInHours, |
| 246 | + Encryption = this.Encrypt ? "Enable" : "Disable", |
| 247 | + OnlineIrStartTime = this.ReplicationStartTime, |
| 248 | + RecoveryPointHistoryDuration = this.RecoveryPoints, |
| 249 | + ReplicationInterval = replicationFrequencyInSeconds, |
| 250 | + |
| 251 | + }; |
| 252 | + |
| 253 | + hyperVReplicaAzurePolicyInput.StorageAccounts = |
| 254 | + new System.Collections.Generic.List<string>(); |
| 255 | + |
| 256 | + if (RecoveryAzureStorageAccountId != null) |
| 257 | + { |
| 258 | + string storageAccount = this.RecoveryAzureStorageAccountId; |
| 259 | + hyperVReplicaAzurePolicyInput.StorageAccounts.Add(storageAccount); |
| 260 | + } |
| 261 | + |
| 262 | + var updatePolicyProperties = new UpdatePolicyProperties() |
| 263 | + { |
| 264 | + ReplicationProviderSettings = hyperVReplicaAzurePolicyInput |
| 265 | + }; |
| 266 | + |
| 267 | + var updatePolicyInput = new UpdatePolicyInput() |
| 268 | + { |
| 269 | + Properties = updatePolicyProperties |
| 270 | + }; |
| 271 | + |
| 272 | + LongRunningOperationResponse response = |
| 273 | + RecoveryServicesClient.UpdatePolicy(this.Policy.Name, updatePolicyInput); |
| 274 | + |
| 275 | + JobResponse jobResponse = |
| 276 | + RecoveryServicesClient |
| 277 | + .GetAzureSiteRecoveryJobDetails(PSRecoveryServicesClient.GetJobIdFromReponseLocation(response.Location)); |
| 278 | + |
| 279 | + WriteObject(new ASRJob(jobResponse.Job)); |
| 280 | + } |
| 281 | + } |
| 282 | +} |
0 commit comments