Skip to content

Commit d99bb3b

Browse files
Samuel Anudeepsiddharth7
authored andcommitted
Added Azure File Share support for Get-AzureRmRecoveryServicesBackupContainer. Manual testing succeeded. Automation tests still pending.
1 parent 7aab1ca commit d99bb3b

File tree

18 files changed

+479
-128
lines changed

18 files changed

+479
-128
lines changed

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Helpers/Conversions/ConversionHelpers.cs

Lines changed: 82 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -277,52 +277,96 @@ public static ItemBase GetItemModel(ServiceClientModel.ProtectedItemResource pro
277277
{
278278
if (protectedItem.Properties.GetType().IsSubclassOf(typeof(ServiceClientModel.AzureIaaSVMProtectedItem)))
279279
{
280-
string policyName = null;
281-
string policyId = ((ServiceClientModel.AzureIaaSVMProtectedItem)protectedItem.Properties).PolicyId;
282-
if (!string.IsNullOrEmpty(policyId))
283-
{
284-
Dictionary<UriEnums, string> keyValueDict =
285-
HelperUtils.ParseUri(policyId);
286-
policyName = HelperUtils.GetPolicyNameFromPolicyId(keyValueDict, policyId);
287-
}
288-
289-
string containerUri = HelperUtils.GetContainerUri(
290-
HelperUtils.ParseUri(protectedItem.Id),
291-
protectedItem.Id);
292-
293-
itemModel = new AzureVmItem(
294-
protectedItem,
295-
IdUtils.GetNameFromUri(containerUri),
296-
ContainerType.AzureVM,
297-
policyName);
280+
itemModel = GetAzureVmItemModel(protectedItem);
298281
}
299282

300283
if (protectedItem.Properties.GetType() ==
301284
typeof(ServiceClientModel.AzureSqlProtectedItem))
302285
{
303-
ServiceClientModel.AzureSqlProtectedItem azureSqlProtectedItem =
304-
(ServiceClientModel.AzureSqlProtectedItem)protectedItem.Properties;
305-
string policyName = null;
306-
string policyId = azureSqlProtectedItem.PolicyId;
307-
if (!string.IsNullOrEmpty(policyId))
308-
{
309-
Dictionary<UriEnums, string> keyVauleDict =
310-
HelperUtils.ParseUri(policyId);
311-
policyName = HelperUtils.GetPolicyNameFromPolicyId(keyVauleDict, policyId);
312-
}
313-
314-
string containerUri = HelperUtils.GetContainerUri(
315-
HelperUtils.ParseUri(protectedItem.Id),
316-
protectedItem.Id);
317-
318-
itemModel = new AzureSqlItem(
319-
protectedItem,
320-
IdUtils.GetNameFromUri(containerUri),
321-
ContainerType.AzureSQL,
322-
policyName);
286+
itemModel = GetAzureSqlItemModel(protectedItem);
323287
}
288+
289+
if (protectedItem.Properties.GetType() ==
290+
typeof(ServiceClientModel.AzureFileshareProtectedItem))
291+
{
292+
itemModel = GetAzureFileShareItemModel(protectedItem);
293+
}
294+
}
295+
296+
return itemModel;
297+
}
298+
299+
private static ItemBase GetAzureFileShareItemModel(ServiceClientModel.ProtectedItemResource protectedItem)
300+
{
301+
ItemBase itemModel;
302+
string policyName = null;
303+
string policyId = ((ServiceClientModel.AzureFileshareProtectedItem)protectedItem.Properties).PolicyId;
304+
if (!string.IsNullOrEmpty(policyId))
305+
{
306+
Dictionary<UriEnums, string> keyValueDict =
307+
HelperUtils.ParseUri(policyId);
308+
policyName = HelperUtils.GetPolicyNameFromPolicyId(keyValueDict, policyId);
309+
}
310+
311+
string containerUri = HelperUtils.GetContainerUri(
312+
HelperUtils.ParseUri(protectedItem.Id),
313+
protectedItem.Id);
314+
315+
itemModel = new AzureFileShareItem(
316+
protectedItem,
317+
IdUtils.GetNameFromUri(containerUri),
318+
ContainerType.AzureStorage,
319+
policyName);
320+
return itemModel;
321+
}
322+
323+
private static ItemBase GetAzureSqlItemModel(ServiceClientModel.ProtectedItemResource protectedItem)
324+
{
325+
ItemBase itemModel;
326+
ServiceClientModel.AzureSqlProtectedItem azureSqlProtectedItem =
327+
(ServiceClientModel.AzureSqlProtectedItem)protectedItem.Properties;
328+
string policyName = null;
329+
string policyId = azureSqlProtectedItem.PolicyId;
330+
if (!string.IsNullOrEmpty(policyId))
331+
{
332+
Dictionary<UriEnums, string> keyVauleDict =
333+
HelperUtils.ParseUri(policyId);
334+
policyName = HelperUtils.GetPolicyNameFromPolicyId(keyVauleDict, policyId);
335+
}
336+
337+
string containerUri = HelperUtils.GetContainerUri(
338+
HelperUtils.ParseUri(protectedItem.Id),
339+
protectedItem.Id);
340+
341+
itemModel = new AzureSqlItem(
342+
protectedItem,
343+
IdUtils.GetNameFromUri(containerUri),
344+
ContainerType.AzureSQL,
345+
policyName);
346+
return itemModel;
347+
}
348+
349+
private static ItemBase GetAzureVmItemModel(ServiceClientModel.ProtectedItemResource protectedItem)
350+
{
351+
ItemBase itemModel;
352+
string policyName = null;
353+
string policyId = ((ServiceClientModel.AzureIaaSVMProtectedItem)protectedItem.Properties).PolicyId;
354+
if (!string.IsNullOrEmpty(policyId))
355+
{
356+
Dictionary<UriEnums, string> keyValueDict =
357+
HelperUtils.ParseUri(policyId);
358+
policyName = HelperUtils.GetPolicyNameFromPolicyId(keyValueDict, policyId);
324359
}
325360

361+
string containerUri = HelperUtils.GetContainerUri(
362+
HelperUtils.ParseUri(protectedItem.Id),
363+
protectedItem.Id);
364+
365+
itemModel = new AzureVmItem(
366+
protectedItem,
367+
IdUtils.GetNameFromUri(containerUri),
368+
ContainerType.AzureVM,
369+
policyName);
326370
return itemModel;
327371
}
328372

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 Microsoft.Azure.Management.RecoveryServices.Backup.Models;
17+
18+
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models
19+
{
20+
/// <summary>
21+
/// Azure File Share Item Class
22+
/// </summary>
23+
public class AzureFileShareItem : ItemBase
24+
{
25+
public string VirtualMachineId { get; set; }
26+
27+
/// <summary>
28+
/// Protection Status of the item
29+
/// </summary>
30+
public ItemProtectionStatus ProtectionStatus { get; set; }
31+
32+
/// <summary>
33+
/// Protection State of the item
34+
/// </summary>
35+
public ItemProtectionState ProtectionState { get; set; }
36+
37+
/// <summary>
38+
/// Last Backup Status for the item
39+
/// </summary>
40+
public string LastBackupStatus { get; set; }
41+
42+
/// <summary>
43+
/// Last Backup Time for the item
44+
/// </summary>
45+
public DateTime? LastBackupTime { get; set; }
46+
47+
/// <summary>
48+
/// Protection Policy Name for the Item
49+
/// </summary>
50+
public string ProtectionPolicyName { get; set; }
51+
52+
/// <summary>
53+
/// ExtendedInfo for the Item
54+
/// </summary
55+
public AzureFileShareItemExtendedInfo ExtendedInfo { get; set; }
56+
57+
/// <summary>
58+
/// Constructor. Takes the service client object representing the protected item
59+
/// and converts it in to the PS protected item model
60+
/// </summary>
61+
/// <param name="protectedItemResource">Service client object representing the protected item resource</param>
62+
/// <param name="containerName">Name of the container associated with this protected item</param>
63+
/// <param name="containerType">Type of the container associated with this protected item</param>
64+
/// <param name="policyName">Name of the protection policy associated with this protected item</param>
65+
public AzureFileShareItem(ProtectedItemResource protectedItemResource,
66+
string containerName, ContainerType containerType, string policyName)
67+
: base(protectedItemResource, containerName, containerType)
68+
{
69+
AzureFileshareProtectedItem protectedItem = (AzureFileshareProtectedItem)protectedItemResource.Properties;
70+
LastBackupStatus = protectedItem.LastBackupStatus;
71+
LastBackupTime = protectedItem.LastBackupTime;
72+
ProtectionPolicyName = policyName;
73+
ProtectionState =
74+
EnumUtils.GetEnum<ItemProtectionState>(protectedItem.ProtectionState.ToString());
75+
ProtectionStatus = EnumUtils.GetEnum<ItemProtectionStatus>(protectedItem.ProtectionStatus);
76+
}
77+
}
78+
79+
/// <summary>
80+
/// Azure File Share Item ExtendedInfo Class
81+
/// </summary>
82+
public class AzureFileShareItemExtendedInfo : ItemExtendedInfoBase
83+
{
84+
/// <summary>
85+
/// Oldest Recovery Point for the Item
86+
/// </summary
87+
public DateTime? OldestRecoveryPoint { get; set; }
88+
89+
/// <summary>
90+
/// Recovery Points Count for the Item
91+
/// </summary
92+
public int RecoveryPointCount { get; set; }
93+
94+
/// <summary>
95+
/// PolicyState for the Item
96+
/// </summary
97+
public string PolicyState { get; set; }
98+
}
99+
}

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Models/CmdletParamEnums.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public enum PolicyParams
7171

7272
public enum ItemParams
7373
{
74-
AzureVMName,
74+
ItemName,
7575
AzureVMCloudServiceName,
7676
AzureVMResourceGroupName,
7777
WorkloadType,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
</ItemGroup>
4949
<ItemGroup>
5050
<Compile Include="AzureFileShareModels\AzureFileSharePolicy.cs" />
51+
<Compile Include="AzureFileShareModels\AzureFileShareItem.cs" />
5152
<Compile Include="AzureSqlModels\AzureSqlContainer.cs" />
5253
<Compile Include="AzureSqlModels\AzureSqlItem.cs" />
5354
<Compile Include="AzureSqlModels\AzureSqlPolicy.cs" />

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Models/CommonModels/Enums.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public enum BackupManagementType
6565
/// </summary>
6666
AzureBackupServer,
6767
AzureSQL,
68+
69+
/// <summary>
70+
/// Represents Azure File Storage. https://docs.microsoft.com/en-in/azure/storage/files/storage-files-introduction
71+
/// </summary>
6872
AzureStorage,
6973
}
7074

@@ -94,6 +98,10 @@ public enum WorkloadType
9498
/// </summary>
9599
AzureVM = 1,
96100
AzureSQLDatabase,
101+
102+
/// <summary>
103+
/// Represents Azure File https://docs.microsoft.com/en-in/azure/storage/files/storage-files-introduction
104+
/// </summary>
97105
AzureFiles,
98106
}
99107

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
16+
using System.Collections.Generic;
17+
using System.Linq;
1518
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ServiceClientAdapterNS;
1619
using Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers;
1720
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
21+
using Microsoft.Rest.Azure.OData;
22+
using CmdletModel = Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
23+
using ServiceClientModel = Microsoft.Azure.Management.RecoveryServices.Backup.Models;
1824
using SystemNet = System.Net;
1925

2026
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ProviderModel
@@ -55,5 +61,100 @@ public void RefreshContainer(string vaultName = null, string resourceGroupName =
5561
Logger.Instance.WriteDebug(errorMessage);
5662
}
5763
}
64+
65+
public List<ServiceClientModel.ProtectedItemResource> ListProtectedItemsByContainer(
66+
string vaultName,
67+
string resourceGroupName,
68+
CmdletModel.ContainerBase container,
69+
CmdletModel.PolicyBase policy,
70+
string backupManagementType,
71+
string dataSourceType)
72+
{
73+
ODataQuery<ServiceClientModel.ProtectedItemQueryObject> queryParams = policy != null ?
74+
new ODataQuery<ServiceClientModel.ProtectedItemQueryObject>(
75+
q => q.BackupManagementType
76+
== backupManagementType &&
77+
q.ItemType == dataSourceType &&
78+
q.PolicyName == policy.Name) :
79+
new ODataQuery<ServiceClientModel.ProtectedItemQueryObject>(
80+
q => q.BackupManagementType
81+
== backupManagementType &&
82+
q.ItemType == dataSourceType);
83+
84+
List<ServiceClientModel.ProtectedItemResource> protectedItems = new List<ServiceClientModel.ProtectedItemResource>();
85+
string skipToken = null;
86+
var listResponse = ServiceClientAdapter.ListProtectedItem(
87+
queryParams,
88+
skipToken,
89+
vaultName: vaultName,
90+
resourceGroupName: resourceGroupName);
91+
protectedItems.AddRange(listResponse);
92+
93+
if (container != null)
94+
{
95+
protectedItems = protectedItems.Where(protectedItem =>
96+
{
97+
Dictionary<CmdletModel.UriEnums, string> dictionary = HelperUtils.ParseUri(protectedItem.Id);
98+
string containerUri = HelperUtils.GetContainerUri(dictionary, protectedItem.Id);
99+
100+
var delimIndex = containerUri.IndexOf(';');
101+
string containerName = containerUri.Substring(delimIndex + 1);
102+
return containerName.ToLower().Equals(container.Name.ToLower());
103+
}).ToList();
104+
}
105+
106+
return protectedItems;
107+
}
108+
109+
public List<CmdletModel.ItemBase> ListProtectedItemsByItemName(
110+
List<ServiceClientModel.ProtectedItemResource> protectedItems,
111+
string itemName,
112+
string vaultName,
113+
string resourceGroupName,
114+
Action<CmdletModel.ItemBase, ServiceClientModel.ProtectedItemResource> extendedInfoProcessor)
115+
{
116+
List<ServiceClientModel.ProtectedItemResource> protectedItemGetResponses =
117+
new List<ServiceClientModel.ProtectedItemResource>();
118+
119+
if (!string.IsNullOrEmpty(itemName))
120+
{
121+
protectedItems = protectedItems.Where(protectedItem =>
122+
{
123+
Dictionary<CmdletModel.UriEnums, string> dictionary = HelperUtils.ParseUri(protectedItem.Id);
124+
string protectedItemUri = HelperUtils.GetProtectedItemUri(dictionary, protectedItem.Id);
125+
return protectedItemUri.ToLower().Contains(itemName.ToLower());
126+
}).ToList();
127+
128+
ODataQuery<ServiceClientModel.GetProtectedItemQueryObject> getItemQueryParams =
129+
new ODataQuery<ServiceClientModel.GetProtectedItemQueryObject>(q => q.Expand == "extendedinfo");
130+
131+
for (int i = 0; i < protectedItems.Count; i++)
132+
{
133+
Dictionary<CmdletModel.UriEnums, string> dictionary = HelperUtils.ParseUri(protectedItems[i].Id);
134+
string containerUri = HelperUtils.GetContainerUri(dictionary, protectedItems[i].Id);
135+
string protectedItemUri = HelperUtils.GetProtectedItemUri(dictionary, protectedItems[i].Id);
136+
137+
var getResponse = ServiceClientAdapter.GetProtectedItem(
138+
containerUri,
139+
protectedItemUri,
140+
getItemQueryParams,
141+
vaultName: vaultName,
142+
resourceGroupName: resourceGroupName);
143+
protectedItemGetResponses.Add(getResponse.Body);
144+
}
145+
}
146+
147+
List<CmdletModel.ItemBase> itemModels = ConversionHelpers.GetItemModelList(protectedItems);
148+
149+
if (!string.IsNullOrEmpty(itemName))
150+
{
151+
for (int i = 0; i < itemModels.Count; i++)
152+
{
153+
extendedInfoProcessor(itemModels[i], protectedItemGetResponses[i]);
154+
}
155+
}
156+
157+
return itemModels;
158+
}
58159
}
59160
}

0 commit comments

Comments
 (0)