Skip to content

[REBASE] AFS Get-Item #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -277,52 +277,96 @@ public static ItemBase GetItemModel(ServiceClientModel.ProtectedItemResource pro
{
if (protectedItem.Properties.GetType().IsSubclassOf(typeof(ServiceClientModel.AzureIaaSVMProtectedItem)))
{
string policyName = null;
string policyId = ((ServiceClientModel.AzureIaaSVMProtectedItem)protectedItem.Properties).PolicyId;
if (!string.IsNullOrEmpty(policyId))
{
Dictionary<UriEnums, string> keyValueDict =
HelperUtils.ParseUri(policyId);
policyName = HelperUtils.GetPolicyNameFromPolicyId(keyValueDict, policyId);
}

string containerUri = HelperUtils.GetContainerUri(
HelperUtils.ParseUri(protectedItem.Id),
protectedItem.Id);

itemModel = new AzureVmItem(
protectedItem,
IdUtils.GetNameFromUri(containerUri),
ContainerType.AzureVM,
policyName);
itemModel = GetAzureVmItemModel(protectedItem);
}

if (protectedItem.Properties.GetType() ==
typeof(ServiceClientModel.AzureSqlProtectedItem))
{
ServiceClientModel.AzureSqlProtectedItem azureSqlProtectedItem =
(ServiceClientModel.AzureSqlProtectedItem)protectedItem.Properties;
string policyName = null;
string policyId = azureSqlProtectedItem.PolicyId;
if (!string.IsNullOrEmpty(policyId))
{
Dictionary<UriEnums, string> keyVauleDict =
HelperUtils.ParseUri(policyId);
policyName = HelperUtils.GetPolicyNameFromPolicyId(keyVauleDict, policyId);
}

string containerUri = HelperUtils.GetContainerUri(
HelperUtils.ParseUri(protectedItem.Id),
protectedItem.Id);

itemModel = new AzureSqlItem(
protectedItem,
IdUtils.GetNameFromUri(containerUri),
ContainerType.AzureSQL,
policyName);
itemModel = GetAzureSqlItemModel(protectedItem);
}

if (protectedItem.Properties.GetType() ==
typeof(ServiceClientModel.AzureFileshareProtectedItem))
{
itemModel = GetAzureFileShareItemModel(protectedItem);
}
}

return itemModel;
}

private static ItemBase GetAzureFileShareItemModel(ServiceClientModel.ProtectedItemResource protectedItem)
{
ItemBase itemModel;
string policyName = null;
string policyId = ((ServiceClientModel.AzureFileshareProtectedItem)protectedItem.Properties).PolicyId;
if (!string.IsNullOrEmpty(policyId))
{
Dictionary<UriEnums, string> keyValueDict =
HelperUtils.ParseUri(policyId);
policyName = HelperUtils.GetPolicyNameFromPolicyId(keyValueDict, policyId);
}

string containerUri = HelperUtils.GetContainerUri(
HelperUtils.ParseUri(protectedItem.Id),
protectedItem.Id);

itemModel = new AzureFileShareItem(
protectedItem,
IdUtils.GetNameFromUri(containerUri),
ContainerType.AzureStorage,
policyName);
return itemModel;
}

private static ItemBase GetAzureSqlItemModel(ServiceClientModel.ProtectedItemResource protectedItem)
{
ItemBase itemModel;
ServiceClientModel.AzureSqlProtectedItem azureSqlProtectedItem =
(ServiceClientModel.AzureSqlProtectedItem)protectedItem.Properties;
string policyName = null;
string policyId = azureSqlProtectedItem.PolicyId;
if (!string.IsNullOrEmpty(policyId))
{
Dictionary<UriEnums, string> keyVauleDict =
HelperUtils.ParseUri(policyId);
policyName = HelperUtils.GetPolicyNameFromPolicyId(keyVauleDict, policyId);
}

string containerUri = HelperUtils.GetContainerUri(
HelperUtils.ParseUri(protectedItem.Id),
protectedItem.Id);

itemModel = new AzureSqlItem(
protectedItem,
IdUtils.GetNameFromUri(containerUri),
ContainerType.AzureSQL,
policyName);
return itemModel;
}

private static ItemBase GetAzureVmItemModel(ServiceClientModel.ProtectedItemResource protectedItem)
{
ItemBase itemModel;
string policyName = null;
string policyId = ((ServiceClientModel.AzureIaaSVMProtectedItem)protectedItem.Properties).PolicyId;
if (!string.IsNullOrEmpty(policyId))
{
Dictionary<UriEnums, string> keyValueDict =
HelperUtils.ParseUri(policyId);
policyName = HelperUtils.GetPolicyNameFromPolicyId(keyValueDict, policyId);
}

string containerUri = HelperUtils.GetContainerUri(
HelperUtils.ParseUri(protectedItem.Id),
protectedItem.Id);

itemModel = new AzureVmItem(
protectedItem,
IdUtils.GetNameFromUri(containerUri),
ContainerType.AzureVM,
policyName);
return itemModel;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using Microsoft.Azure.Management.RecoveryServices.Backup.Models;

namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models
{
/// <summary>
/// Azure File Share Item Class
/// </summary>
public class AzureFileShareItem : ItemBase
{
/// <summary>
/// Protection Status of the item
/// </summary>
public ItemProtectionStatus ProtectionStatus { get; set; }

/// <summary>
/// Protection State of the item
/// </summary>
public ItemProtectionState ProtectionState { get; set; }

/// <summary>
/// Last Backup Status for the item
/// </summary>
public string LastBackupStatus { get; set; }

/// <summary>
/// Last Backup Time for the item
/// </summary>
public DateTime? LastBackupTime { get; set; }

/// <summary>
/// Protection Policy Name for the Item
/// </summary>
public string ProtectionPolicyName { get; set; }

/// <summary>
/// ExtendedInfo for the Item
/// </summary
public AzureFileShareItemExtendedInfo ExtendedInfo { get; set; }

/// <summary>
/// Constructor. Takes the service client object representing the protected item
/// and converts it in to the PS protected item model
/// </summary>
/// <param name="protectedItemResource">Service client object representing the protected item resource</param>
/// <param name="containerName">Name of the container associated with this protected item</param>
/// <param name="containerType">Type of the container associated with this protected item</param>
/// <param name="policyName">Name of the protection policy associated with this protected item</param>
public AzureFileShareItem(ProtectedItemResource protectedItemResource,
string containerName, ContainerType containerType, string policyName)
: base(protectedItemResource, containerName, containerType)
{
AzureFileshareProtectedItem protectedItem = (AzureFileshareProtectedItem)protectedItemResource.Properties;
LastBackupStatus = protectedItem.LastBackupStatus;
LastBackupTime = protectedItem.LastBackupTime;
ProtectionPolicyName = policyName;
ProtectionState =
EnumUtils.GetEnum<ItemProtectionState>(protectedItem.ProtectionState.ToString());
ProtectionStatus = EnumUtils.GetEnum<ItemProtectionStatus>(protectedItem.ProtectionStatus);
}
}

/// <summary>
/// Azure File Share Item ExtendedInfo Class
/// </summary>
public class AzureFileShareItemExtendedInfo : ItemExtendedInfoBase
{
/// <summary>
/// Oldest Recovery Point for the Item
/// </summary
public DateTime? OldestRecoveryPoint { get; set; }

/// <summary>
/// Recovery Points Count for the Item
/// </summary
public int RecoveryPointCount { get; set; }

/// <summary>
/// PolicyState for the Item
/// </summary
public string PolicyState { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public enum PolicyParams

public enum ItemParams
{
AzureVMName,
ItemName,
AzureVMCloudServiceName,
AzureVMResourceGroupName,
WorkloadType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AzureFileShareModels\AzureFileSharePolicy.cs" />
<Compile Include="AzureFileShareModels\AzureFileShareItem.cs" />
<Compile Include="AzureSqlModels\AzureSqlContainer.cs" />
<Compile Include="AzureSqlModels\AzureSqlItem.cs" />
<Compile Include="AzureSqlModels\AzureSqlPolicy.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public enum BackupManagementType
/// </summary>
AzureBackupServer,
AzureSQL,

/// <summary>
/// Represents Azure File Storage. https://docs.microsoft.com/en-in/azure/storage/files/storage-files-introduction
/// </summary>
AzureStorage,
}

Expand Down Expand Up @@ -94,6 +98,10 @@ public enum WorkloadType
/// </summary>
AzureVM = 1,
AzureSQLDatabase,

/// <summary>
/// Represents Azure File https://docs.microsoft.com/en-in/azure/storage/files/storage-files-introduction
/// </summary>
AzureFiles,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ServiceClientAdapterNS;
using Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers;
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
using Microsoft.Rest.Azure.OData;
using CmdletModel = Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
using ServiceClientModel = Microsoft.Azure.Management.RecoveryServices.Backup.Models;
using SystemNet = System.Net;

namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ProviderModel
Expand Down Expand Up @@ -55,5 +61,100 @@ public void RefreshContainer(string vaultName = null, string resourceGroupName =
Logger.Instance.WriteDebug(errorMessage);
}
}

public List<ServiceClientModel.ProtectedItemResource> ListProtectedItemsByContainer(
string vaultName,
string resourceGroupName,
CmdletModel.ContainerBase container,
CmdletModel.PolicyBase policy,
string backupManagementType,
string dataSourceType)
{
ODataQuery<ServiceClientModel.ProtectedItemQueryObject> queryParams = policy != null ?
new ODataQuery<ServiceClientModel.ProtectedItemQueryObject>(
q => q.BackupManagementType
== backupManagementType &&
q.ItemType == dataSourceType &&
q.PolicyName == policy.Name) :
new ODataQuery<ServiceClientModel.ProtectedItemQueryObject>(
q => q.BackupManagementType
== backupManagementType &&
q.ItemType == dataSourceType);

List<ServiceClientModel.ProtectedItemResource> protectedItems = new List<ServiceClientModel.ProtectedItemResource>();
string skipToken = null;
var listResponse = ServiceClientAdapter.ListProtectedItem(
queryParams,
skipToken,
vaultName: vaultName,
resourceGroupName: resourceGroupName);
protectedItems.AddRange(listResponse);

if (container != null)
{
protectedItems = protectedItems.Where(protectedItem =>
{
Dictionary<CmdletModel.UriEnums, string> dictionary = HelperUtils.ParseUri(protectedItem.Id);
string containerUri = HelperUtils.GetContainerUri(dictionary, protectedItem.Id);

var delimIndex = containerUri.IndexOf(';');
string containerName = containerUri.Substring(delimIndex + 1);
return containerName.ToLower().Equals(container.Name.ToLower());
}).ToList();
}

return protectedItems;
}

public List<CmdletModel.ItemBase> ListProtectedItemsByItemName(
List<ServiceClientModel.ProtectedItemResource> protectedItems,
string itemName,
string vaultName,
string resourceGroupName,
Action<CmdletModel.ItemBase, ServiceClientModel.ProtectedItemResource> extendedInfoProcessor)
{
List<ServiceClientModel.ProtectedItemResource> protectedItemGetResponses =
new List<ServiceClientModel.ProtectedItemResource>();

if (!string.IsNullOrEmpty(itemName))
{
protectedItems = protectedItems.Where(protectedItem =>
{
Dictionary<CmdletModel.UriEnums, string> dictionary = HelperUtils.ParseUri(protectedItem.Id);
string protectedItemUri = HelperUtils.GetProtectedItemUri(dictionary, protectedItem.Id);
return protectedItemUri.ToLower().Contains(itemName.ToLower());
}).ToList();

ODataQuery<ServiceClientModel.GetProtectedItemQueryObject> getItemQueryParams =
new ODataQuery<ServiceClientModel.GetProtectedItemQueryObject>(q => q.Expand == "extendedinfo");

for (int i = 0; i < protectedItems.Count; i++)
{
Dictionary<CmdletModel.UriEnums, string> dictionary = HelperUtils.ParseUri(protectedItems[i].Id);
string containerUri = HelperUtils.GetContainerUri(dictionary, protectedItems[i].Id);
string protectedItemUri = HelperUtils.GetProtectedItemUri(dictionary, protectedItems[i].Id);

var getResponse = ServiceClientAdapter.GetProtectedItem(
containerUri,
protectedItemUri,
getItemQueryParams,
vaultName: vaultName,
resourceGroupName: resourceGroupName);
protectedItemGetResponses.Add(getResponse.Body);
}
}

List<CmdletModel.ItemBase> itemModels = ConversionHelpers.GetItemModelList(protectedItems);

if (!string.IsNullOrEmpty(itemName))
{
for (int i = 0; i < itemModels.Count; i++)
{
extendedInfoProcessor(itemModels[i], protectedItemGetResponses[i]);
}
}

return itemModels;
}
}
}
Loading