Skip to content

Commit

Permalink
Merge pull request #11 from MabOneSdk/pikumar3
Browse files Browse the repository at this point in the history
Added Get-AzureBackupItem cmdlet
  • Loading branch information
pikumarmsft16 committed Jun 4, 2015
2 parents d61bcc4 + b782f4f commit 7605325
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// ----------------------------------------------------------------------------------
//
// 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 System.Management.Automation;
using System.Collections.Generic;
using System.Xml;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Microsoft.Azure.Common.Authentication;
using Microsoft.Azure.Common.Authentication.Models;
using System.Threading;
using Hyak.Common;
using Microsoft.Azure.Commands.AzureBackup.Properties;
using System.Net;

namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
{
public abstract class AzureBackupContainerCmdletBase : AzureBackupCmdletBase
{
// ToDO:
// Correct Help message and other attributes related to paameters
[Parameter(Position = 0, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.ResourceGroupName, ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
public AzureBackupContainer AzureBackupContainer { get; set; }

public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();

WriteDebug(String.Format("Cmdlet called for ResourceGroupName: {0}, ResourceName: {1}", AzureBackupContainer.ResourceGroupName, AzureBackupContainer.ResourceName));

InitializeAzureBackupCmdlet(AzureBackupContainer.ResourceGroupName, AzureBackupContainer.ResourceName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets.DataSource
{
class Disable_AzureBackupProtection
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets.DataSource
{
class Enable_AzureBackupProtection
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// ----------------------------------------------------------------------------------
//
// 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 System.Management.Automation;
using System.Collections.Generic;
using System.Xml;
using System.Linq;
using Microsoft.Azure.Management.BackupServices.Models;

namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
{
/// <summary>
/// Get list of containers
/// </summary>
[Cmdlet(VerbsCommon.Get, "AzureBackupItem"), OutputType(typeof(AzureBackupItem), typeof(List<AzureBackupItem>))]
public class GetAzureBackupItem : AzureBackupContainerCmdletBase
{
[Parameter(Position = 2, Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)]
[ValidateNotNullOrEmpty]
public string Id { get; set; }

public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();

ExecutionBlock(() =>
{
WriteVerbose("Making client call");

var azureBackupDatasourceListResponse = AzureBackupClient.DataSource.ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
var azureBackupPOListResponse = AzureBackupClient.ProtectableObject. ListAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;

WriteVerbose("Received policy response");
WriteVerbose("Received policy response2");
IEnumerable<DataSourceInfo> azureBackupDatasourceObjects = null;
IEnumerable<ProtectableObjectInfo> azureBackupPOObjects = null;

if (Id != null)
{
azureBackupDatasourceObjects = azureBackupDatasourceListResponse.DataSources.Objects.Where(x => x.InstanceId.Equals(Id, System.StringComparison.InvariantCultureIgnoreCase));
azureBackupPOObjects = azureBackupPOListResponse.ProtectableObject.Objects.Where(x => x.InstanceId.Equals(Id, System.StringComparison.InvariantCultureIgnoreCase));
}
else
{
azureBackupDatasourceObjects = azureBackupDatasourceListResponse.DataSources.Objects;
azureBackupPOObjects = azureBackupPOListResponse.ProtectableObject.Objects;
}

WriteVerbose("Converting response");
WriteAzureBackupProtectionPolicy(azureBackupDatasourceObjects, azureBackupPOObjects, AzureBackupContainer);
});
}

public void WriteAzureBackupProtectionPolicy(DataSourceInfo sourceItem, AzureBackupContainer azureBackupItem)
{
this.WriteObject(new AzureBackupItem(sourceItem, azureBackupItem));
}

public void WriteAzureBackupProtectionPolicy(IEnumerable<DataSourceInfo> sourceDataSourceList,IEnumerable<ProtectableObjectInfo> sourcePOList, AzureBackupContainer azureBackupContainer)
{
List<AzureBackupItem> targetList = new List<AzureBackupItem>();

foreach (var item in sourceDataSourceList)
{
targetList.Add(new AzureBackupItem(item, azureBackupContainer));
}

foreach (var item in sourcePOList)
{
targetList.Add(new AzureBackupItem(item, azureBackupContainer));
}

this.WriteObject(targetList, true);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Management.BackupServices.Models;
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
{
public class AzureBackupVaultContextObject
Expand Down Expand Up @@ -66,6 +67,13 @@ public AzureBackupContainerContextObject(AzureBackupContainerContextObject azure
ContainerName = azureBackupContainerContextObject.ContainerName;
ContainerId = azureBackupContainerContextObject.ContainerId;
}
public AzureBackupContainerContextObject(AzureBackupContainer azureBackupContainer)
: base(azureBackupContainer.ResourceGroupName, azureBackupContainer.ResourceName)
{
ContainerType = azureBackupContainer.ContainerType;
ContainerName = azureBackupContainer.ContainerName;
ContainerId = azureBackupContainer.ContainerId;
}
}

public class AzureBackupItemContextObject : AzureBackupContainerContextObject
Expand All @@ -91,5 +99,18 @@ public AzureBackupItemContextObject(AzureBackupItemContextObject azureBackupItem
DataSourceId = azureBackupItemContextObject.DataSourceId;
DataSourceType = azureBackupItemContextObject.DataSourceType;
}

public AzureBackupItemContextObject(DataSourceInfo item, AzureBackupContainer azureBackupContainer)
: base(azureBackupContainer)
{
DataSourceId = item.InstanceId;
DataSourceType = item.Type;
}

public AzureBackupItemContextObject(ProtectableObjectInfo item, AzureBackupContainer azureBackupContainer)
: base(azureBackupContainer)
{
DataSourceType = item.Type;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
/// <summary>
/// Represents Azure Backup Container
/// </summary>
public class AzureBackupContainer : AzureBackupVaultContextObject
public class AzureBackupContainer : AzureBackupContainerContextObject
{
/// <summary>
/// Type of the Azure Backup container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Management.BackupServices.Models;

namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
{
Expand Down Expand Up @@ -79,5 +80,33 @@ public class AzureBackupItem : AzureBackupItemContextObject
/// Last Backup Job Id for the Azure Backup Item
/// </summary>
public string LastBackupJobId { get; set; }

public AzureBackupItem()
: base()
{
}

public AzureBackupItem(DataSourceInfo datasource, AzureBackupContainer azureBackupContainer)
: base(datasource, azureBackupContainer)
{
Status = datasource.Status;
ProtectionStatus = datasource.ProtectionStatus;
ProtectableObjectName = datasource.ProtectableObjectName;
ProtectionPolicyName = datasource.ProtectionPolicyName;
ProtectionPolicyId = datasource.ProtectionPolicyId;
PolicyInconsistent = datasource.PolicyInconsistent;
RecoveryPointsCount = datasource.RecoveryPointsCount;
LastRecoveryPoint = datasource.LastRecoveryPoint;
LastBackupTime = datasource.LastBackupTime;
LastBackupStatus = datasource.LastBackupStatus;
LastBackupJobId = datasource.LastBackupJobId;
}

public AzureBackupItem(ProtectableObjectInfo pPOItem, AzureBackupContainer azureBackupContainer)
: base(pPOItem, azureBackupContainer)
{
ProtectionStatus = pPOItem.ProtectionStatus;
ProtectableObjectName = pPOItem.Name;
}
}
}

0 comments on commit 7605325

Please sign in to comment.