Skip to content

Commit 2bc59e4

Browse files
committed
Merge pull request #234 from MabOneSdk/pragrawa-dev1
Adding URI parsing from resource Id
2 parents 45ad5d4 + 21270ac commit 2bc59e4

File tree

6 files changed

+151
-31
lines changed

6 files changed

+151
-31
lines changed

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Helpers/Utils.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using System.Text.RegularExpressions;
56
using System.Threading.Tasks;
7+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
8+
using CmdletModel = Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
69

710
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers
811
{
@@ -39,5 +42,75 @@ public static List<string> GetStringListFromEnumList<T>(IList<T> enumList)
3942

4043
return ret;
4144
}
45+
46+
/// <summary>
47+
/// Helper function to parse resource id which in format of "[\{Key}\{value}]*"
48+
/// </summary>
49+
/// <param name="id">Id of the resource</param>
50+
/// <returns>dictionary of UriEnum as key and value as value of corresponding URI enum</returns>
51+
public static Dictionary<CmdletModel.UriEnums, string> ParseUri(string id)
52+
{
53+
Dictionary<CmdletModel.UriEnums, string> keyValuePairDict = new Dictionary<CmdletModel.UriEnums, string>();
54+
if (!string.IsNullOrEmpty(id))
55+
{
56+
string pattern = @"/[a-zA-Z]*/[a-zA-Z0-9-;.]*";
57+
Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
58+
59+
// Match the regular expression pattern against a uri string.
60+
Match match = reg.Match(id);
61+
62+
while (match.Success)
63+
{
64+
Console.WriteLine(match.Value);
65+
string pat = @"/";
66+
string[] keyValuePair = match.Value.Split(new string[] { pat }, StringSplitOptions.RemoveEmptyEntries);
67+
CmdletModel.UriEnums key;
68+
CmdletModel.UriEnums value;
69+
if (keyValuePair.Length == 2)
70+
{
71+
if (Enum.TryParse<CmdletModel.UriEnums>(keyValuePair[0], true, out key) &&
72+
Enum.TryParse<CmdletModel.UriEnums>(keyValuePair[1], true, out value))
73+
{
74+
keyValuePairDict.Add(key, keyValuePair[1]);
75+
}
76+
}
77+
match = match.NextMatch();
78+
}
79+
}
80+
return keyValuePairDict;
81+
}
82+
83+
public static string GetContainerUri(Dictionary<CmdletModel.UriEnums, string> keyValuePairDict, string id)
84+
{
85+
string containerUri = string.Empty;
86+
87+
if (keyValuePairDict.ContainsKey(CmdletModel.UriEnums.ProtectionContainers))
88+
{
89+
containerUri = keyValuePairDict[CmdletModel.UriEnums.ProtectionContainers];
90+
}
91+
else
92+
{
93+
throw new ArgumentException(string.Format(Resources.URIValueNotFound,
94+
CmdletModel.UriEnums.ProtectionContainers.ToString(), id));
95+
}
96+
return containerUri;
97+
}
98+
99+
public static string GetProtectedItemUri(Dictionary<CmdletModel.UriEnums, string> keyValuePairDict, string id)
100+
{
101+
string itemUri = string.Empty;
102+
103+
if (keyValuePairDict.ContainsKey(CmdletModel.UriEnums.ProtectedItems))
104+
{
105+
itemUri = keyValuePairDict[CmdletModel.UriEnums.ProtectedItems];
106+
}
107+
else
108+
{
109+
throw new ArgumentException(string.Format(Resources.URIValueNotFound,
110+
CmdletModel.UriEnums.ProtectedItems.ToString(), id));
111+
}
112+
return itemUri;
113+
}
114+
42115
}
43116
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Models/Commands.RecoveryServices.Backup.Models.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<AutoGen>True</AutoGen>
6262
<DesignTime>True</DesignTime>
6363
</Compile>
64+
<Compile Include="UriEnums.cs" />
6465
</ItemGroup>
6566
<ItemGroup>
6667
<EmbeddedResource Include="Properties\Resources.resx">

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Models/Properties/Resources.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.Collections.Generic;
17+
using System.Linq;
18+
using System.Text;
19+
using System.Threading.Tasks;
20+
21+
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models
22+
{
23+
public enum UriEnums
24+
{
25+
Subscriptions,
26+
ResourceGroups,
27+
Providers,
28+
Vaults,
29+
BackupFabrics,
30+
ProtectionContainers,
31+
ProtectedItems,
32+
RecoveryPoints,
33+
BackupJobs,
34+
OperationResults,
35+
BackupPolicies,
36+
Operations,
37+
OperationsStatus
38+
}
39+
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Providers/Providers/IaasVmPsBackupProvider.cs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public BaseRecoveryServicesJobResponse EnableProtection()
6464
AzureRmRecoveryServicesIaasVmItem item = (AzureRmRecoveryServicesIaasVmItem)
6565
ProviderData.ProviderParameters[ItemParams.Item];
6666
// do validations
67-
67+
6868
string containerUri = "";
6969
string protectedItemUri = "";
7070
bool isComputeAzureVM = false;
@@ -78,22 +78,21 @@ public BaseRecoveryServicesJobResponse EnableProtection()
7878

7979
ValidateAzureVMEnableProtectionRequest(azureVMName, azureVMCloudServiceName, azureVMResourceGroupName, policy);
8080

81-
AzureIaaSVMProtectableItem protectableObject = GetAzureVMProtectableObject(azureVMName, azureVMRGName, isComputeAzureVM);
81+
ProtectableObjectResource protectableObjectResource = GetAzureVMProtectableObject(azureVMName, azureVMRGName, isComputeAzureVM);
8282

83-
containerUri = protectableObject.ContainerUri;
84-
protectedItemUri = protectableObject.ProtectableObjectUri;
83+
Dictionary<UriEnums, string> keyValueDict = HelperUtils.ParseUri(protectableObjectResource.Id);
84+
containerUri = HelperUtils.GetContainerUri(keyValueDict, item.Id);
85+
protectedItemUri = HelperUtils.GetProtectedItemUri(keyValueDict, item.Id);
8586
}
8687
else
8788
{
8889
ValidateAzureVMWorkloadType(item.WorkloadType, policy.WorkloadType);
8990
ValidateAzureVMModifyProtectionRequest(itemBase, policy);
9091

91-
isComputeAzureVM = IsComputeAzureVM(item.VirtualMachineId);
92-
93-
string containerType = HydraHelpers.GetHydraContainerType(item.ContainerType);
94-
string vmType = HydraHelpers.GetHydraWorkloadType(item.WorkloadType);
95-
containerUri = string.Join(separator, new string[] { containerType, item.ContainerName });
96-
protectedItemUri = string.Join(separator, new string[] { vmType, item.Name });
92+
isComputeAzureVM = IsComputeAzureVM(item.VirtualMachineId);
93+
Dictionary<UriEnums, string> keyValueDict = HelperUtils.ParseUri(item.Id);
94+
containerUri = HelperUtils.GetContainerUri(keyValueDict, item.Id);
95+
protectedItemUri = HelperUtils.GetProtectedItemUri(keyValueDict, item.Id);
9796
}
9897

9998
// construct Hydra protectedItem request
@@ -672,7 +671,6 @@ private void ValidateAzureVMRetentionPolicy(AzureRmRecoveryServicesRetentionPoli
672671
private void ValidateAzureVMEnableProtectionRequest(string vmName, string serviceName, string rgName,
673672
AzureRmRecoveryServicesPolicyBase policy)
674673
{
675-
ValidateAzureVMProtectionPolicy(policy);
676674
if (string.IsNullOrEmpty(vmName))
677675
{
678676
throw new ArgumentException(string.Format(Resources.InvalidAzureVMName));
@@ -716,19 +714,19 @@ private bool IsComputeAzureVM(string virtualMachineId)
716714
return isComputeAzureVM;
717715
}
718716

719-
private AzureIaaSVMProtectableItem GetAzureVMProtectableObject(string azureVMName, string azureVMRGName, bool isComputeAzureVM)
717+
private ProtectableObjectResource GetAzureVMProtectableObject(string azureVMName, string azureVMRGName, bool isComputeAzureVM)
720718
{
721719
//TriggerDiscovery if needed
722720

723721
bool isDiscoveryNeed = false;
724722

725-
AzureIaaSVMProtectableItem protectableObject = null;
726-
isDiscoveryNeed = IsDiscoveryNeeded(azureVMName, azureVMRGName, isComputeAzureVM, out protectableObject);
723+
ProtectableObjectResource protectableObjectResource = null;
724+
isDiscoveryNeed = IsDiscoveryNeeded(azureVMName, azureVMRGName, isComputeAzureVM, out protectableObjectResource);
727725
if (isDiscoveryNeed)
728726
{
729727
Logger.Instance.WriteDebug(String.Format(Resources.VMNotDiscovered, azureVMName));
730728
RefreshContainer();
731-
isDiscoveryNeed = IsDiscoveryNeeded(azureVMName, azureVMRGName, isComputeAzureVM, out protectableObject);
729+
isDiscoveryNeed = IsDiscoveryNeeded(azureVMName, azureVMRGName, isComputeAzureVM, out protectableObjectResource);
732730
if (isDiscoveryNeed == true)
733731
{
734732
// Container is not discovered. Throw exception
@@ -738,7 +736,7 @@ private AzureIaaSVMProtectableItem GetAzureVMProtectableObject(string azureVMNam
738736
Logger.Instance.ThrowTerminatingError(new ErrorRecord(new Exception(Resources.AzureVMNotFound), string.Empty, ErrorCategory.InvalidArgument, null));
739737
}
740738
}
741-
if (protectableObject == null)
739+
if (protectableObjectResource == null)
742740
{
743741
// Container is not discovered. Throw exception
744742
string vmversion = (isComputeAzureVM) ? computeAzureVMVersion : classicComputeAzureVMVersion;
@@ -747,15 +745,15 @@ private AzureIaaSVMProtectableItem GetAzureVMProtectableObject(string azureVMNam
747745
Logger.Instance.ThrowTerminatingError(new ErrorRecord(new Exception(Resources.AzureVMNotFound), string.Empty, ErrorCategory.InvalidArgument, null));
748746
}
749747

750-
return protectableObject;
748+
return protectableObjectResource;
751749

752750
}
753751

754752
private bool IsDiscoveryNeeded(string vmName, string rgName, bool isComputeAzureVM,
755-
out AzureIaaSVMProtectableItem protectableObject)
753+
out ProtectableObjectResource protectableObjectResource)
756754
{
757755
bool isDiscoveryNeed = true;
758-
protectableObject = null;
756+
protectableObjectResource = null;
759757
string vmVersion = string.Empty;
760758
vmVersion = (isComputeAzureVM) == true ? computeAzureVMVersion : classicComputeAzureVMVersion;
761759

@@ -786,7 +784,7 @@ private bool IsDiscoveryNeeded(string vmName, string rgName, bool isComputeAzure
786784
&& string.Compare(iaaSVMProtectableItem.ResourceGroup, rgName, true) == 0
787785
&& string.Compare(iaaSVMProtectableItem.VirtualMachineVersion, vmVersion, true) == 0)
788786
{
789-
protectableObject = iaaSVMProtectableItem;
787+
protectableObjectResource = protectableItem;
790788
isDiscoveryNeed = false;
791789
break;
792790
}
@@ -800,9 +798,9 @@ private void RefreshContainer()
800798
{
801799
bool isDiscoverySuccessful = false;
802800
string errorMessage = string.Empty;
803-
var refreshContainerJobResponse = HydraAdapter.RefreshContainers();
801+
var refreshContainerJobResponse = HydraAdapter.RefreshContainers();
804802

805-
//Now wait for the operation to Complete
803+
//Now wait for the operation to Complete
806804
WaitForDiscoveryToComplete(refreshContainerJobResponse.Location, out isDiscoverySuccessful, out errorMessage);
807805

808806
if (!isDiscoverySuccessful)

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Test/Commands.RecoveryServices.Backup.Test.csproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,19 @@
125125
<None Include="ScenarioTests\IaasVm\ContainerTests.ps1">
126126
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
127127
</None>
128-
<None Include="ScenarioTests\Mab\ContainerTests.ps1">
129-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
130-
</None>
128+
<None Include="ScenarioTests\Mab\ContainerTests.ps1">
129+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
130+
</None>
131131
<None Include="ScenarioTests\Dpm\ContainerTests.ps1">
132-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
133-
</None>
134-
<Compile Include="ScenarioTests\IaasVm\JobTests.cs" />
132+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
133+
</None>
134+
<Compile Include="ScenarioTests\IaasVm\JobTests.cs" />
135135
<Compile Include="ScenarioTests\IaasVm\ItemTests.cs" />
136136
<Compile Include="ScenarioTests\IaasVm\PolicyTests.cs" />
137137
<Compile Include="ScenarioTests\IaasVm\ContainerTests.cs" />
138138
<Compile Include="ScenarioTests\Mab\ContainerTests.cs" />
139-
<Compile Include="ScenarioTests\Dpm\ContainerTests.cs"/>
140-
<Compile Include="ScenarioTests\TestsBase.cs" />
139+
<Compile Include="ScenarioTests\Dpm\ContainerTests.cs"/>
140+
<Compile Include="ScenarioTests\TestsBase.cs" />
141141
<None Include="SessionRecords\Microsoft.Azure.Commands.RecoveryServices.Backup.Test.ScenarioTests.ContainerTests\TestGetContainerScenario.json">
142142
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
143143
</None>

0 commit comments

Comments
 (0)