Skip to content

Commit 1a12910

Browse files
committed
Get-AzureSiteRecoveryVault optimising code.
1 parent 8a088a6 commit 1a12910

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

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

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,7 @@ public override void ExecuteCmdlet()
6464
/// </summary>
6565
private void GetByDefault()
6666
{
67-
IEnumerable<CloudService> cloudServiceList = RecoveryServicesClient.GetCloudServices();
68-
69-
List<ASRVault> vaultList = new List<ASRVault>();
70-
foreach (var cloudService in cloudServiceList)
71-
{
72-
foreach (var vault in cloudService.Resources)
73-
{
74-
if (vault.Type.Equals(Constants.ASRVaultType, StringComparison.InvariantCultureIgnoreCase))
75-
{
76-
vaultList.Add(new ASRVault(cloudService, vault));
77-
}
78-
}
79-
}
67+
List<ASRVault> vaultList = this.GetVaults();
8068

8169
this.WriteVaults(vaultList);
8270
}
@@ -88,6 +76,38 @@ private void GetByName()
8876
{
8977
bool vaultFound = false;
9078

79+
List<ASRVault> vaultList = this.GetVaults(out vaultFound);
80+
81+
if (!vaultFound)
82+
{
83+
throw new InvalidOperationException(
84+
string.Format(
85+
Properties.Resources.VaultNotFound,
86+
this.Name));
87+
}
88+
89+
this.WriteVaults(vaultList);
90+
}
91+
92+
/// <summary>
93+
/// Overloaded GetVaults method so as to pass the out variable
94+
/// </summary>
95+
/// <returns>List of ASR Vaults</returns>
96+
private List<ASRVault> GetVaults()
97+
{
98+
bool temp = false;
99+
return this.GetVaults(out temp);
100+
}
101+
102+
/// <summary>
103+
/// Gets the vaults in the cloud service.
104+
/// </summary>
105+
/// <param name="vaultFound">Out variable to indicate if the vault was found</param>
106+
/// <returns>List of ASR Vaults</returns>
107+
private List<ASRVault> GetVaults(out bool vaultFound)
108+
{
109+
vaultFound = false;
110+
91111
IEnumerable<CloudService> cloudServiceList = RecoveryServicesClient.GetCloudServices();
92112

93113
List<ASRVault> vaultList = new List<ASRVault>();
@@ -97,22 +117,23 @@ private void GetByName()
97117
{
98118
if (vault.Type.Equals(Constants.ASRVaultType, StringComparison.InvariantCultureIgnoreCase))
99119
{
100-
if (string.Compare(this.Name, vault.Name, StringComparison.OrdinalIgnoreCase) == 0)
120+
if (string.Compare(this.ParameterSetName, ASRParameterSets.ByName, StringComparison.OrdinalIgnoreCase) == 0)
121+
{
122+
if (string.Compare(this.Name, vault.Name, StringComparison.OrdinalIgnoreCase) == 0)
123+
{
124+
vaultFound = true;
125+
vaultList.Add(new ASRVault(cloudService, vault));
126+
}
127+
}
128+
else
101129
{
102-
vaultFound = true;
103-
this.WriteVault(new ASRVault(cloudService, vault));
130+
vaultList.Add(new ASRVault(cloudService, vault));
104131
}
105132
}
106133
}
107134
}
108135

109-
if (!vaultFound)
110-
{
111-
throw new InvalidOperationException(
112-
string.Format(
113-
Properties.Resources.VaultNotFound,
114-
this.Name));
115-
}
136+
return vaultList;
116137
}
117138

118139
/// <summary>

0 commit comments

Comments
 (0)