forked from Azure/azure-powershell
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Mkherani #21
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
202 changes: 202 additions & 0 deletions
202
...anager/AzureBackup/Commands.AzureBackup/Cmdlets/Container/RegisterAzureBackupContainer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...ager/AzureBackup/Commands.AzureBackup/Cmdlets/Container/UnregisterAzureBackupContainer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...zureBackup/Commands.AzureBackup/Cmdlets/RegisterContainer/RegisterAzureBackupContainer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
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 | ||
{ | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?