Skip to content

Mkherani #21

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 5 commits into from
Jun 11, 2015
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 @@ -26,6 +26,7 @@ internal static class AzureBackupCmdletHelpMessage
public const string ContainerId = "The container ID.";
public const string ContainerRegistrationStatus = "The container registration status.";
public const string ContainerType = "The container type.";
public const string VirtualMachine = "Virtual Machine.";
public const string ContainerResourceGroupName = "The container resource group name.";
public const string ProtectionStatus = "Protection Status of the azure backup item.";
public const string AzureBackUpItem = "Azure BackUp Item.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public class GetAzureBackupContainer : AzureBackupVaultCmdletBase

[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ContainerRegistrationStatus)]
[ValidateNotNullOrEmpty]
public AzureBackupContainerStatus Status { get; set; }
public AzureBackupContainerStatusInput Status { get; set; }

[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ContainerType)]
[ValidateNotNullOrEmpty]
public AzureBackupContainerType Type { get; set; }
public AzureBackupContainerTypeInput Type { get; set; }

public override void ExecuteCmdlet()
{
Expand Down Expand Up @@ -98,7 +98,7 @@ private string ConstructQueryFilterString()

switch (Type)
{
case AzureBackupContainerType.AzureVirtualMachine:
case AzureBackupContainerTypeInput.AzureVirtualMachine:
containerQueryObject.Type = BCI.ContainerType.IaasVMContainer.ToString();
break;
default:
Expand All @@ -107,15 +107,12 @@ private string ConstructQueryFilterString()

switch (Status)
{
case AzureBackupContainerStatus.Registered:
case AzureBackupContainerStatusInput.Registered:
containerQueryObject.Status = BCI.RegistrationStatus.Registered.ToString();
break;
case AzureBackupContainerStatus.Registering:
case AzureBackupContainerStatusInput.Registering:
containerQueryObject.Status = BCI.RegistrationStatus.Registering.ToString();
break;
case AzureBackupContainerStatus.NotRegistered:
containerQueryObject.Status = BCI.RegistrationStatus.NotRegistered.ToString();
break;
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// ----------------------------------------------------------------------------------
//
// 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.Web;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.Compute;
using Microsoft.Azure.Management.BackupServices.Models;
using MBS = Microsoft.Azure.Management.BackupServices;
using Microsoft.Azure.Commands.Compute.Models;

namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
{
/// <summary>
/// Get list of containers
/// </summary>
[Cmdlet(VerbsLifecycle.Register, "AzureBackupContainer"), OutputType(typeof(Guid))]
public class RegisterAzureBackupContainer : AzureBackupVaultCmdletBase
{
//[Parameter(Position = 2, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.VirtualMachine)]
//[ValidateNotNullOrEmpty]
//public PSVirtualMachineInstanceView VirtualMachine { get; set; }
[Parameter(Position = 2, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.VirtualMachine)]
[ValidateNotNullOrEmpty]
public string VirtualMachineName { get; set; }

[Parameter(Position = 3, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.VirtualMachine)]
[ValidateNotNullOrEmpty]
public string VirtualMachineRGName { get; set; }

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

ExecutionBlock(() =>
{
//string vmName = VirtualMachine.Name;
//string rgName = VirtualMachine.ResourceGroupName;
string vmName = VirtualMachineName;
string rgName = VirtualMachineRGName;
Guid jobId = Guid.Empty;
bool isDiscoveryNeed = false;
MBS.OperationResponse operationResponse;

ContainerInfo container = null;
isDiscoveryNeed = IsDiscoveryNeeded(vmName, rgName, out container);
if(isDiscoveryNeed)
{
RefreshContainer();
isDiscoveryNeed = IsDiscoveryNeeded(vmName, rgName, out container);
if ((isDiscoveryNeed == true) || (container == null))
{
//Container is not discovered. Throw exception
throw new NotImplementedException();
}
}

//Container is discovered. Register the container
List<string> containerNameList = new List<string>();
containerNameList.Add(container.Name);
RegisterContainerRequestInput registrationRequest = new RegisterContainerRequestInput(containerNameList, AzureBackupContainerType.IaasVMContainer.ToString());
operationResponse = AzureBackupClient.Container.RegisterAsync(registrationRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result;

//TODO fix the OperationResponse to JobID conversion
jobId = operationResponse.OperationId;
WriteObject(jobId);
});
}

private void RefreshContainer()
{
bool isRetyNeeded = true;
int retryCount = 1;
bool isDiscoverySuccessful = false;
while (isRetyNeeded && retryCount <= 3)
{
MBS.OperationResponse opResponse =
AzureBackupClient.Container.RefreshAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;

//Now wait for the operation to Complete
isRetyNeeded = WaitForDiscoveryToCOmplete(opResponse.OperationId.ToString(), out isDiscoverySuccessful);
retryCount++;
}

if (!isDiscoverySuccessful)
{
//Discovery failed
throw new Exception(); //TODO:
}
}

private bool WaitForDiscoveryToCOmplete(string operationId, out bool isDiscoverySuccessful)
{
bool isRetryNeeded = false;


BMSOperationStatusResponse status = new BMSOperationStatusResponse()
{
OperationStatus = AzureBackupOperationStatus.InProgress.ToString()
};

while (status.OperationStatus != AzureBackupOperationStatus.Completed.ToString())
{
status = AzureBackupClient.OperationStatus.GetAsync(operationId, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(15));
}

isDiscoverySuccessful = true;
//If operation fails check if retry is needed or not
if (status.OperationResult != AzureBackupOperationResult.Succeeded.ToString())
{
isDiscoverySuccessful = false;
if ((status.ErrorCode == AzureBackupOperationErrorCode.DiscoveryInProgress.ToString() ||
(status.ErrorCode == AzureBackupOperationErrorCode.BMSUserErrorObjectLocked.ToString())))
{
//Need to retry for this errors
isRetryNeeded = true;
}
}
return isRetryNeeded;
}

private bool IsDiscoveryNeeded(string vmName, string rgName, out ContainerInfo container)
{
bool isDiscoveryNeed = false;
//First check if container is discoverd or not
ListContainerQueryParameter queryParams = new ListContainerQueryParameter();
queryParams.ContainerTypeField = AzureBackupContainerType.IaasVMContainer.ToString();
queryParams.ContainerStatusField = String.Empty;
queryParams.ContainerFriendlyNameField = vmName;
string queryString = GetQueryFileter(queryParams);

ListContainerResponse containers = AzureBackupClient.Container.ListAsync(queryString,
GetCustomRequestHeaders(), CmdletCancellationToken).Result;
if (containers.Objects.Count() == 0)
{
//Container is not discover
WriteVerbose("Container is not discovered");
container = null;
isDiscoveryNeed = true;
}

else
{
//We can have multiple container with same friendly name.
//Look for resourceGroup name in the container unoque name
container = containers.Objects.Where(c => c.ParentContainerFriendlyName.ToLower().Equals(rgName.ToLower())).FirstOrDefault();
if (container == null)
{
//Container is not in list of registered container
isDiscoveryNeed = true;
}
}
return isDiscoveryNeed;
}

private string GetQueryFileter(ListContainerQueryParameter queryParams)
{
NameValueCollection collection = new NameValueCollection();
if (!String.IsNullOrEmpty(queryParams.ContainerTypeField))
{
collection.Add("ContainerType", queryParams.ContainerTypeField);
}

if (!String.IsNullOrEmpty(queryParams.ContainerStatusField))
{
collection.Add("ContainerStatus", queryParams.ContainerStatusField);
}

if (!String.IsNullOrEmpty(queryParams.ContainerFriendlyNameField))
{
collection.Add("FriendlyName", queryParams.ContainerFriendlyNameField);
}

if (collection == null || collection.Count == 0)
{
return String.Empty;
}

var httpValueCollection = HttpUtility.ParseQueryString(String.Empty);
httpValueCollection.Add(collection);

return "&" + httpValueCollection.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// ----------------------------------------------------------------------------------
//
// 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.Web;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.Compute;
using Microsoft.Azure.Management.BackupServices.Models;
using MBS = Microsoft.Azure.Management.BackupServices;
using Microsoft.Azure.Commands.Compute.Models;

namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
{
/// <summary>
/// Get list of containers
/// </summary>
[Cmdlet(VerbsLifecycle.Unregister, "AzureBackupContainer"), OutputType(typeof(Guid))]
public class UnregisterAzureBackupContainer : AzureBackupVaultCmdletBase
{
[Parameter(Position = 2, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.VirtualMachine, ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
public string ContainerUniqueName { get; set; }

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

ExecutionBlock(() =>
{
UnregisterContainerRequestInput unregRequest = new UnregisterContainerRequestInput(ContainerUniqueName, AzureBackupContainerType.IaasVMContainer.ToString());
MBS.OperationResponse operationResponse = AzureBackupClient.Container.UnregisterAsync(unregRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
Guid jobId = operationResponse.OperationId; //TODO: Fix it once PiyushKa publish the rest APi to get jobId based on operationId

WriteObject(jobId);
});
}

private string GetQueryFileter(ListContainerQueryParameter queryParams)
{
NameValueCollection collection = new NameValueCollection();
if (!String.IsNullOrEmpty(queryParams.ContainerTypeField))
{
collection.Add("ContainerType", queryParams.ContainerTypeField);
}

if (!String.IsNullOrEmpty(queryParams.ContainerStatusField))
{
collection.Add("ContainerStatus", queryParams.ContainerStatusField);
}

if (!String.IsNullOrEmpty(queryParams.ContainerFriendlyNameField))
{
collection.Add("FriendlyName", queryParams.ContainerFriendlyNameField);
}

if (collection == null || collection.Count == 0)
{
return String.Empty;
}

var httpValueCollection = HttpUtility.ParseQueryString(String.Empty);
httpValueCollection.Add(collection);

return "&" + httpValueCollection.ToString();

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is not needed right?

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets.RegisterContainer
{
[Cmdlet(VerbsCommon.Get, ProfileNouns.VirtualMachine, DefaultParameterSetName = ListAllVirtualMachinesParamSet)]
class RegisterAzureBackupContainer
{
}
}
Loading