Skip to content

Commit 65465a4

Browse files
committed
Validations for create protection profile object cmdlet added
Customer storage account validation added
1 parent 203532f commit 65465a4

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/PSRecoveryServicesClient/PSRecoveryServicesClientHelper.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414

1515
using System;
1616
using System.Collections.Generic;
17+
using System.Collections.ObjectModel;
18+
using System.Management.Automation;
1719
using Microsoft.WindowsAzure;
1820
using Microsoft.WindowsAzure.Commands.Common;
1921
using Microsoft.WindowsAzure.Commands.Common.Models;
22+
using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
2023
using Microsoft.WindowsAzure.Management.Storage;
2124
using Microsoft.WindowsAzure.Management.Storage.Models;
2225

@@ -78,17 +81,22 @@ public static void ValidateStorageAccountAssociation(string azureStorageAccount)
7881

7982
bool associatedAccount = false;
8083

81-
SubscriptionCloudCredentials creds
82-
= AzureSession.AuthenticationFactory.GetSubscriptionCloudCredentials(AzureSession.CurrentContext);
83-
StorageManagementClient storageManagementClient = new StorageManagementClient(creds);
84-
85-
StorageAccountListResponse storageAccounts = storageManagementClient.StorageAccounts.List();
86-
foreach (StorageAccount storage in storageAccounts)
84+
using (System.Management.Automation.PowerShell powerShellInstance = System.Management.Automation.PowerShell.Create())
8785
{
88-
if (azureStorageAccount.Equals(storage.Name, StringComparison.OrdinalIgnoreCase))
86+
powerShellInstance.AddCommand("Get-AzureStorageAccount");
87+
Collection<PSObject> powershellOutput = powerShellInstance.Invoke();
88+
89+
foreach (var storage in powershellOutput)
8990
{
90-
associatedAccount = true;
91-
break;
91+
if (storage.BaseObject is StorageServicePropertiesOperationContext)
92+
{
93+
StorageServicePropertiesOperationContext storageAccount = (StorageServicePropertiesOperationContext)storage.BaseObject;
94+
if (azureStorageAccount.Equals(storageAccount.StorageAccountName, StringComparison.OrdinalIgnoreCase))
95+
{
96+
associatedAccount = true;
97+
break;
98+
}
99+
}
92100
}
93101
}
94102

@@ -108,6 +116,11 @@ SubscriptionCloudCredentials creds
108116
/// <returns>A UShort corresponding to the value.</returns>
109117
public static ushort ConvertReplicationFrequencyToUshort(string replicationFrequencyString)
110118
{
119+
if (replicationFrequencyString == null)
120+
{
121+
return 0;
122+
}
123+
111124
ushort replicationFrequency;
112125

113126
if (!ushort.TryParse(replicationFrequencyString, out replicationFrequency))

src/ServiceManagement/RecoveryServices/Commands.RecoveryServices/Service/CreateAzureSiteRecoveryProtectionProfileObject.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ public override void ExecuteCmdlet()
152152
{
153153
try
154154
{
155-
switch (this.ReplicationProvider)
155+
switch (this.ParameterSetName)
156156
{
157-
case Constants.HyperVReplica:
157+
case ASRParameterSets.EnterpriseToEnterprise:
158158
this.EnterpriseToEnterpriseProtectionProfileObject();
159159
break;
160-
case Constants.HyperVReplicaAzure:
160+
case ASRParameterSets.EnterpriseToAzure:
161161
this.EnterpriseToAzureProtectionProfileObject();
162162
break;
163163
}
@@ -183,12 +183,20 @@ protected override void StopProcessing()
183183
/// </summary>
184184
private void EnterpriseToAzureProtectionProfileObject()
185185
{
186-
//// Verify whether the storage account is associated with the account or not.
187-
//// PSRecoveryServicesClientHelper.ValidateStorageAccountAssociation(this.RecoveryAzureStorageAccount);
186+
if (string.Compare(this.ReplicationProvider, Constants.HyperVReplicaAzure, StringComparison.OrdinalIgnoreCase) != 0)
187+
{
188+
throw new InvalidOperationException(
189+
string.Format(
190+
Properties.Resources.IncorrectReplicationProvider,
191+
this.ReplicationProvider));
192+
}
188193

189194
// Verify whether the subscription is associated with the account or not.
190195
PSRecoveryServicesClientHelper.ValidateSubscriptionAccountAssociation(this.RecoveryAzureSubscription);
191196

197+
// Verify whether the storage account is associated with the account or not.
198+
PSRecoveryServicesClientHelper.ValidateStorageAccountAssociation(this.RecoveryAzureStorageAccount);
199+
192200
PSRecoveryServicesClientHelper.ValidateReplicationStartTime(this.ReplicationStartTime);
193201

194202
ushort replicationFrequencyInSeconds = PSRecoveryServicesClientHelper.ConvertReplicationFrequencyToUshort(this.ReplicationFrequencyInSeconds);
@@ -217,6 +225,14 @@ private void EnterpriseToAzureProtectionProfileObject()
217225
/// </summary>
218226
private void EnterpriseToEnterpriseProtectionProfileObject()
219227
{
228+
if (string.Compare(this.ReplicationProvider, Constants.HyperVReplica, StringComparison.OrdinalIgnoreCase) != 0)
229+
{
230+
throw new InvalidOperationException(
231+
string.Format(
232+
Properties.Resources.IncorrectReplicationProvider,
233+
this.ReplicationProvider));
234+
}
235+
220236
PSRecoveryServicesClientHelper.ValidateReplicationStartTime(this.ReplicationStartTime);
221237

222238
ushort replicationFrequencyInSeconds = PSRecoveryServicesClientHelper.ConvertReplicationFrequencyToUshort(this.ReplicationFrequencyInSeconds);

0 commit comments

Comments
 (0)