Skip to content

Commit 33c5f43

Browse files
committed
Merge pull request #21 from MabOneSdk/mkherani
Mkherani
2 parents 0121f42 + cc47e8a commit 33c5f43

File tree

10 files changed

+492
-43
lines changed

10 files changed

+492
-43
lines changed

src/ResourceManager/AzureBackup/Commands.AzureBackup/AzureBackupCmdletHelpMessage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ internal static class AzureBackupCmdletHelpMessage
2626
public const string ContainerId = "The container ID.";
2727
public const string ContainerRegistrationStatus = "The container registration status.";
2828
public const string ContainerType = "The container type.";
29+
public const string VirtualMachine = "Virtual Machine.";
2930
public const string ContainerResourceGroupName = "The container resource group name.";
3031
public const string ProtectionStatus = "Protection Status of the azure backup item.";
3132
public const string AzureBackUpItem = "Azure BackUp Item.";

src/ResourceManager/AzureBackup/Commands.AzureBackup/Cmdlets/Container/GetAzureBackupContainer.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ public class GetAzureBackupContainer : AzureBackupVaultCmdletBase
4040

4141
[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ContainerRegistrationStatus)]
4242
[ValidateNotNullOrEmpty]
43-
public AzureBackupContainerStatus Status { get; set; }
43+
public AzureBackupContainerStatusInput Status { get; set; }
4444

4545
[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.ContainerType)]
4646
[ValidateNotNullOrEmpty]
47-
public AzureBackupContainerType Type { get; set; }
47+
public AzureBackupContainerTypeInput Type { get; set; }
4848

4949
public override void ExecuteCmdlet()
5050
{
@@ -98,7 +98,7 @@ private string ConstructQueryFilterString()
9898

9999
switch (Type)
100100
{
101-
case AzureBackupContainerType.AzureVirtualMachine:
101+
case AzureBackupContainerTypeInput.AzureVirtualMachine:
102102
containerQueryObject.Type = BCI.ContainerType.IaasVMContainer.ToString();
103103
break;
104104
default:
@@ -107,15 +107,12 @@ private string ConstructQueryFilterString()
107107

108108
switch (Status)
109109
{
110-
case AzureBackupContainerStatus.Registered:
110+
case AzureBackupContainerStatusInput.Registered:
111111
containerQueryObject.Status = BCI.RegistrationStatus.Registered.ToString();
112112
break;
113-
case AzureBackupContainerStatus.Registering:
113+
case AzureBackupContainerStatusInput.Registering:
114114
containerQueryObject.Status = BCI.RegistrationStatus.Registering.ToString();
115115
break;
116-
case AzureBackupContainerStatus.NotRegistered:
117-
containerQueryObject.Status = BCI.RegistrationStatus.NotRegistered.ToString();
118-
break;
119116
default:
120117
break;
121118
}
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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 System.Web;
17+
using System.Collections.Generic;
18+
using System.Collections.Specialized;
19+
using System.Linq;
20+
using System.Management.Automation;
21+
using System.Text;
22+
using System.Threading.Tasks;
23+
using Microsoft.Azure.Commands.Compute;
24+
using Microsoft.Azure.Management.BackupServices.Models;
25+
using MBS = Microsoft.Azure.Management.BackupServices;
26+
using Microsoft.Azure.Commands.Compute.Models;
27+
28+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
29+
{
30+
/// <summary>
31+
/// Get list of containers
32+
/// </summary>
33+
[Cmdlet(VerbsLifecycle.Register, "AzureBackupContainer"), OutputType(typeof(Guid))]
34+
public class RegisterAzureBackupContainer : AzureBackupVaultCmdletBase
35+
{
36+
//[Parameter(Position = 2, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.VirtualMachine)]
37+
//[ValidateNotNullOrEmpty]
38+
//public PSVirtualMachineInstanceView VirtualMachine { get; set; }
39+
[Parameter(Position = 2, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.VirtualMachine)]
40+
[ValidateNotNullOrEmpty]
41+
public string VirtualMachineName { get; set; }
42+
43+
[Parameter(Position = 3, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.VirtualMachine)]
44+
[ValidateNotNullOrEmpty]
45+
public string VirtualMachineRGName { get; set; }
46+
47+
public override void ExecuteCmdlet()
48+
{
49+
base.ExecuteCmdlet();
50+
51+
ExecutionBlock(() =>
52+
{
53+
//string vmName = VirtualMachine.Name;
54+
//string rgName = VirtualMachine.ResourceGroupName;
55+
string vmName = VirtualMachineName;
56+
string rgName = VirtualMachineRGName;
57+
Guid jobId = Guid.Empty;
58+
bool isDiscoveryNeed = false;
59+
MBS.OperationResponse operationResponse;
60+
61+
ContainerInfo container = null;
62+
isDiscoveryNeed = IsDiscoveryNeeded(vmName, rgName, out container);
63+
if(isDiscoveryNeed)
64+
{
65+
RefreshContainer();
66+
isDiscoveryNeed = IsDiscoveryNeeded(vmName, rgName, out container);
67+
if ((isDiscoveryNeed == true) || (container == null))
68+
{
69+
//Container is not discovered. Throw exception
70+
throw new NotImplementedException();
71+
}
72+
}
73+
74+
//Container is discovered. Register the container
75+
List<string> containerNameList = new List<string>();
76+
containerNameList.Add(container.Name);
77+
RegisterContainerRequestInput registrationRequest = new RegisterContainerRequestInput(containerNameList, AzureBackupContainerType.IaasVMContainer.ToString());
78+
operationResponse = AzureBackupClient.Container.RegisterAsync(registrationRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
79+
80+
//TODO fix the OperationResponse to JobID conversion
81+
jobId = operationResponse.OperationId;
82+
WriteObject(jobId);
83+
});
84+
}
85+
86+
private void RefreshContainer()
87+
{
88+
bool isRetyNeeded = true;
89+
int retryCount = 1;
90+
bool isDiscoverySuccessful = false;
91+
while (isRetyNeeded && retryCount <= 3)
92+
{
93+
MBS.OperationResponse opResponse =
94+
AzureBackupClient.Container.RefreshAsync(GetCustomRequestHeaders(), CmdletCancellationToken).Result;
95+
96+
//Now wait for the operation to Complete
97+
isRetyNeeded = WaitForDiscoveryToCOmplete(opResponse.OperationId.ToString(), out isDiscoverySuccessful);
98+
retryCount++;
99+
}
100+
101+
if (!isDiscoverySuccessful)
102+
{
103+
//Discovery failed
104+
throw new Exception(); //TODO:
105+
}
106+
}
107+
108+
private bool WaitForDiscoveryToCOmplete(string operationId, out bool isDiscoverySuccessful)
109+
{
110+
bool isRetryNeeded = false;
111+
112+
113+
BMSOperationStatusResponse status = new BMSOperationStatusResponse()
114+
{
115+
OperationStatus = AzureBackupOperationStatus.InProgress.ToString()
116+
};
117+
118+
while (status.OperationStatus != AzureBackupOperationStatus.Completed.ToString())
119+
{
120+
status = AzureBackupClient.OperationStatus.GetAsync(operationId, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
121+
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(15));
122+
}
123+
124+
isDiscoverySuccessful = true;
125+
//If operation fails check if retry is needed or not
126+
if (status.OperationResult != AzureBackupOperationResult.Succeeded.ToString())
127+
{
128+
isDiscoverySuccessful = false;
129+
if ((status.ErrorCode == AzureBackupOperationErrorCode.DiscoveryInProgress.ToString() ||
130+
(status.ErrorCode == AzureBackupOperationErrorCode.BMSUserErrorObjectLocked.ToString())))
131+
{
132+
//Need to retry for this errors
133+
isRetryNeeded = true;
134+
}
135+
}
136+
return isRetryNeeded;
137+
}
138+
139+
private bool IsDiscoveryNeeded(string vmName, string rgName, out ContainerInfo container)
140+
{
141+
bool isDiscoveryNeed = false;
142+
//First check if container is discoverd or not
143+
ListContainerQueryParameter queryParams = new ListContainerQueryParameter();
144+
queryParams.ContainerTypeField = AzureBackupContainerType.IaasVMContainer.ToString();
145+
queryParams.ContainerStatusField = String.Empty;
146+
queryParams.ContainerFriendlyNameField = vmName;
147+
string queryString = GetQueryFileter(queryParams);
148+
149+
ListContainerResponse containers = AzureBackupClient.Container.ListAsync(queryString,
150+
GetCustomRequestHeaders(), CmdletCancellationToken).Result;
151+
if (containers.Objects.Count() == 0)
152+
{
153+
//Container is not discover
154+
WriteVerbose("Container is not discovered");
155+
container = null;
156+
isDiscoveryNeed = true;
157+
}
158+
159+
else
160+
{
161+
//We can have multiple container with same friendly name.
162+
//Look for resourceGroup name in the container unoque name
163+
container = containers.Objects.Where(c => c.ParentContainerFriendlyName.ToLower().Equals(rgName.ToLower())).FirstOrDefault();
164+
if (container == null)
165+
{
166+
//Container is not in list of registered container
167+
isDiscoveryNeed = true;
168+
}
169+
}
170+
return isDiscoveryNeed;
171+
}
172+
173+
private string GetQueryFileter(ListContainerQueryParameter queryParams)
174+
{
175+
NameValueCollection collection = new NameValueCollection();
176+
if (!String.IsNullOrEmpty(queryParams.ContainerTypeField))
177+
{
178+
collection.Add("ContainerType", queryParams.ContainerTypeField);
179+
}
180+
181+
if (!String.IsNullOrEmpty(queryParams.ContainerStatusField))
182+
{
183+
collection.Add("ContainerStatus", queryParams.ContainerStatusField);
184+
}
185+
186+
if (!String.IsNullOrEmpty(queryParams.ContainerFriendlyNameField))
187+
{
188+
collection.Add("FriendlyName", queryParams.ContainerFriendlyNameField);
189+
}
190+
191+
if (collection == null || collection.Count == 0)
192+
{
193+
return String.Empty;
194+
}
195+
196+
var httpValueCollection = HttpUtility.ParseQueryString(String.Empty);
197+
httpValueCollection.Add(collection);
198+
199+
return "&" + httpValueCollection.ToString();
200+
}
201+
}
202+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 System.Web;
17+
using System.Collections.Generic;
18+
using System.Collections.Specialized;
19+
using System.Linq;
20+
using System.Management.Automation;
21+
using System.Text;
22+
using System.Threading.Tasks;
23+
using Microsoft.Azure.Commands.Compute;
24+
using Microsoft.Azure.Management.BackupServices.Models;
25+
using MBS = Microsoft.Azure.Management.BackupServices;
26+
using Microsoft.Azure.Commands.Compute.Models;
27+
28+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
29+
{
30+
/// <summary>
31+
/// Get list of containers
32+
/// </summary>
33+
[Cmdlet(VerbsLifecycle.Unregister, "AzureBackupContainer"), OutputType(typeof(Guid))]
34+
public class UnregisterAzureBackupContainer : AzureBackupVaultCmdletBase
35+
{
36+
[Parameter(Position = 2, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.VirtualMachine, ValueFromPipelineByPropertyName = true)]
37+
[ValidateNotNullOrEmpty]
38+
public string ContainerUniqueName { get; set; }
39+
40+
public override void ExecuteCmdlet()
41+
{
42+
base.ExecuteCmdlet();
43+
44+
ExecutionBlock(() =>
45+
{
46+
UnregisterContainerRequestInput unregRequest = new UnregisterContainerRequestInput(ContainerUniqueName, AzureBackupContainerType.IaasVMContainer.ToString());
47+
MBS.OperationResponse operationResponse = AzureBackupClient.Container.UnregisterAsync(unregRequest, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
48+
Guid jobId = operationResponse.OperationId; //TODO: Fix it once PiyushKa publish the rest APi to get jobId based on operationId
49+
50+
WriteObject(jobId);
51+
});
52+
}
53+
54+
private string GetQueryFileter(ListContainerQueryParameter queryParams)
55+
{
56+
NameValueCollection collection = new NameValueCollection();
57+
if (!String.IsNullOrEmpty(queryParams.ContainerTypeField))
58+
{
59+
collection.Add("ContainerType", queryParams.ContainerTypeField);
60+
}
61+
62+
if (!String.IsNullOrEmpty(queryParams.ContainerStatusField))
63+
{
64+
collection.Add("ContainerStatus", queryParams.ContainerStatusField);
65+
}
66+
67+
if (!String.IsNullOrEmpty(queryParams.ContainerFriendlyNameField))
68+
{
69+
collection.Add("FriendlyName", queryParams.ContainerFriendlyNameField);
70+
}
71+
72+
if (collection == null || collection.Count == 0)
73+
{
74+
return String.Empty;
75+
}
76+
77+
var httpValueCollection = HttpUtility.ParseQueryString(String.Empty);
78+
httpValueCollection.Add(collection);
79+
80+
return "&" + httpValueCollection.ToString();
81+
82+
}
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Management.Automation;
7+
8+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets.RegisterContainer
9+
{
10+
[Cmdlet(VerbsCommon.Get, ProfileNouns.VirtualMachine, DefaultParameterSetName = ListAllVirtualMachinesParamSet)]
11+
class RegisterAzureBackupContainer
12+
{
13+
}
14+
}

0 commit comments

Comments
 (0)