Skip to content

Commit cd4b52d

Browse files
committed
Merge pull request #203 from MabOneSdk/pragrawa-dev1
Enable Protection cmdlet implementationPart1
2 parents eb8a7e9 + 95fcfec commit cd4b52d

File tree

12 files changed

+538
-2
lines changed

12 files changed

+538
-2
lines changed

src/ResourceManager/RecoveryServices.Backup/CmdletParameterHelpMessages.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,13 @@ internal static class Job
5757
public const string WaitJobOrListFilter = "Job or List of jobs until end of which the cmdlet should wait.";
5858
public const string WaitJobTimeoutFilter = "Maximum time to wait before aborting wait in seconds.";
5959
}
60+
61+
internal static class Item
62+
{
63+
public const string AzureVMName = "Azure VM Name.";
64+
public const string AzureVMServiceName = "Cloud Service Name for Azure Classic Compute VM.";
65+
public const string AzureVMResourceGroupName = "Resource Group Name for Azure Compute VM .";
66+
public const string ProtectedItem = "Filter value for status of job.";
67+
}
6068
}
6169
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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.Linq;
17+
using System.Text;
18+
using System.Threading.Tasks;
19+
using System.Collections.Generic;
20+
using System.Management.Automation;
21+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
22+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ProviderModel;
23+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers;
24+
25+
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
26+
{
27+
/// <summary>
28+
/// Enable Azure Backup protection
29+
/// </summary>
30+
[Cmdlet(VerbsLifecycle.Enable, "AzureRmRecoveryServicesProtection", DefaultParameterSetName = ModifyProtectionParameterSet), OutputType(typeof(AzureRmRecoveryServicesJobBase))]
31+
public class EnableAzureRmRecoveryServicesProtection : RecoveryServicesBackupCmdletBase
32+
{
33+
internal const string AzureVMClassicComputeParameterSet = "AzureVMClassicCompute";
34+
internal const string AzureVMComputeParameterSet = "AzureVMCompute";
35+
internal const string ModifyProtectionParameterSet = "ModifyProtection";
36+
37+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = AzureVMClassicComputeParameterSet, HelpMessage = ParamHelpMsg.Item.AzureVMName)]
38+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = AzureVMComputeParameterSet, HelpMessage = ParamHelpMsg.Item.AzureVMName)]
39+
public string Name { get; set; }
40+
41+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = AzureVMClassicComputeParameterSet, HelpMessage = ParamHelpMsg.Item.AzureVMServiceName)]
42+
public string ServiceName { get; set; }
43+
44+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = AzureVMComputeParameterSet, HelpMessage = ParamHelpMsg.Item.AzureVMResourceGroupName)]
45+
public string ResourceGroupName { get; set; }
46+
47+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = AzureVMClassicComputeParameterSet, HelpMessage = ParamHelpMsg.Common.WorkloadType)]
48+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = AzureVMComputeParameterSet, HelpMessage = ParamHelpMsg.Common.WorkloadType)]
49+
public WorkloadType WorkLoadType { get; set; }
50+
51+
[Parameter(Mandatory = true, HelpMessage = ParamHelpMsg.Policy.ProtectionPolicy)]
52+
[ValidateNotNullOrEmpty]
53+
public AzureRmRecoveryServicesPolicyBase Policy { get; set; }
54+
55+
[Parameter(Mandatory = false, HelpMessage = ParamHelpMsg.Item.ProtectedItem, ValueFromPipeline = true)]
56+
[ValidateNotNullOrEmpty]
57+
public AzureRmRecoveryServicesItemBase Item { get; set; }
58+
59+
public override void ExecuteCmdlet()
60+
{
61+
ExecutionBlock(() =>
62+
{
63+
base.ExecuteCmdlet();
64+
65+
PsBackupProviderManager providerManager = new PsBackupProviderManager(new Dictionary<System.Enum, object>()
66+
{
67+
{ItemParams.AzureVMName, Name},
68+
{ItemParams.AzureVMCloudServiceName, ServiceName},
69+
{ItemParams.AzureVMResourceGroupName, ResourceGroupName},
70+
{ItemParams.WorkloadType, WorkLoadType},
71+
{ItemParams.Policy, Policy},
72+
{ItemParams.Item, Item},
73+
{ItemParams.ParameterSetName, this.ParameterSetName},
74+
}, HydraAdapter);
75+
76+
IPsBackupProvider psBackupProvider = (Item != null) ? providerManager.GetProviderInstance(WorkLoadType, Item.BackupManagementType)
77+
: providerManager.GetProviderInstance(WorkLoadType);
78+
79+
var jobResponse = psBackupProvider.EnableProtection();
80+
81+
// Track Response and display job details
82+
// -- TBD to move it to common helper and remove hard-coded vaules
83+
84+
var response = HydraAdapter.GetProtectedItemOperationStatusByURL(jobResponse.AzureAsyncOperation);
85+
while (response.OperationStatus.Status == "InProgress")
86+
{
87+
response = HydraAdapter.GetProtectedItemOperationStatusByURL(jobResponse.AzureAsyncOperation);
88+
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5));
89+
}
90+
91+
if(response.OperationStatus.Status == "Completed")
92+
{
93+
// TBD -- Hydra change to add jobId in OperationStatusExtendedInfo
94+
string jobId = ""; //response.OperationStatus.Properties.jobId;
95+
var job = HydraAdapter.GetJob("", "", jobId);
96+
//WriteObject(ConversionHelpers.GetJobModel(job));
97+
}
98+
99+
});
100+
}
101+
}
102+
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Cmdlets.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
</ItemGroup>
7070
<ItemGroup>
7171
<Compile Include="Cmdlets\Container\GetAzureRmRecoveryServicesContainer.cs" />
72+
<Compile Include="Cmdlets\Item\EnableAzureRmRecoveryServicesProtection.cs" />
7273
<Compile Include="Cmdlets\Jobs\GetAzureRmRecoveryServicesJob.cs" />
7374
<Compile Include="Cmdlets\Jobs\GetAzureRmRecoveryServicesJobDetails.cs" />
7475
<Compile Include="Cmdlets\Jobs\WaitAzureRmRecoveryServicesJob.cs" />

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Helpers/HydraHelpers.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,37 @@ public static void GetSkipTokenFromNextLink(string url, out string nextLink)
7474
nextLink = null;
7575
}
7676
}
77+
78+
public static string GetHydraContainerType(CmdletModel.ContainerType containerType)
79+
{
80+
string hydraContainerType = string.Empty;
81+
82+
switch (containerType)
83+
{
84+
case CmdletModel.ContainerType.AzureVM:
85+
hydraContainerType = ContainerType.IaasVMContainer.ToString();
86+
break;
87+
default:
88+
break;
89+
}
90+
91+
return hydraContainerType;
92+
}
93+
94+
public static string GetHydraWorkloadType(CmdletModel.WorkloadType workloadType)
95+
{
96+
string hydraWorkloadType = string.Empty;
97+
98+
switch (workloadType)
99+
{
100+
case CmdletModel.WorkloadType.AzureVM:
101+
hydraWorkloadType = WorkloadType.VM.ToString();
102+
break;
103+
default:
104+
break;
105+
}
106+
107+
return hydraWorkloadType;
108+
}
77109
}
78110
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.HydraAdapter/BMSAPIs/ItemAPIs.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,81 @@
1717
using System.Linq;
1818
using System.Text;
1919
using System.Threading.Tasks;
20+
using Microsoft.Azure.Management.RecoveryServices.Backup.Models;
2021

2122
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.HydraAdapter
2223
{
2324
public partial class HydraAdapter
2425
{
26+
public BaseRecoveryServicesJobResponse CreateOrUpdateProtectedItem(
27+
string containerName,
28+
string protectedItemName,
29+
ProtectedItemCreateOrUpdateRequest request)
30+
{
31+
string resourceName = BmsAdapter.GetResourceName();
32+
string resourceGroupName = BmsAdapter.GetResourceName();
33+
34+
return BmsAdapter.Client.ProtectedItem.CreateOrUpdateProtectedItemAsync(
35+
resourceGroupName,
36+
resourceName,
37+
AzureFabricName,
38+
containerName,
39+
protectedItemName,
40+
request,
41+
BmsAdapter.GetCustomRequestHeaders(),
42+
BmsAdapter.CmdletCancellationToken).Result;
43+
}
44+
45+
public BaseRecoveryServicesJobResponse DeleteProtectedItem(
46+
string containerName,
47+
string protectedItemName)
48+
{
49+
string resourceName = BmsAdapter.GetResourceName();
50+
string resourceGroupName = BmsAdapter.GetResourceName();
51+
52+
return BmsAdapter.Client.ProtectedItem.DeleteProtectedItemAsync(
53+
resourceGroupName,
54+
resourceName,
55+
AzureFabricName,
56+
containerName,
57+
protectedItemName,
58+
BmsAdapter.GetCustomRequestHeaders(),
59+
BmsAdapter.CmdletCancellationToken).Result;
60+
}
61+
62+
public ProtectedItemResponse GetProtectedItem(
63+
string containerName,
64+
string protectedItemName,
65+
GetProtectedItemQueryParam queryFilter)
66+
{
67+
string resourceName = BmsAdapter.GetResourceName();
68+
string resourceGroupName = BmsAdapter.GetResourceName();
69+
70+
return BmsAdapter.Client.ProtectedItem.GetAsync(
71+
resourceGroupName,
72+
resourceName,
73+
AzureFabricName,
74+
containerName,
75+
protectedItemName,
76+
queryFilter,
77+
BmsAdapter.GetCustomRequestHeaders(),
78+
BmsAdapter.CmdletCancellationToken).Result;
79+
}
80+
81+
public ProtectedItemListResponse ListProtectedItem(
82+
ProtectedItemListQueryParam queryFilter,
83+
PaginationRequest paginationParams)
84+
{
85+
string resourceName = BmsAdapter.GetResourceName();
86+
string resourceGroupName = BmsAdapter.GetResourceName();
87+
88+
return BmsAdapter.Client.ProtectedItem.ListAsync(
89+
resourceGroupName,
90+
resourceName,
91+
queryFilter,
92+
paginationParams,
93+
BmsAdapter.GetCustomRequestHeaders(),
94+
BmsAdapter.CmdletCancellationToken).Result;
95+
}
2596
}
2697
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.HydraAdapter/BMSAPIs/OperationStatusAPIs.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,34 @@
1717
using System.Linq;
1818
using System.Text;
1919
using System.Threading.Tasks;
20+
using Microsoft.Azure.Management.RecoveryServices.Backup.Models;
2021

2122
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.HydraAdapter
2223
{
2324
public partial class HydraAdapter
2425
{
26+
public BaseRecoveryServicesJobResponse GetRefreshContainerOperationResultByURL(
27+
string operationResultLink)
28+
{
29+
string resourceName = BmsAdapter.GetResourceName();
30+
string resourceGroupName = BmsAdapter.GetResourceName();
31+
32+
return BmsAdapter.Client.Container.GetRefreshOperationResultByURLAsync(
33+
operationResultLink,
34+
BmsAdapter.GetCustomRequestHeaders(),
35+
BmsAdapter.CmdletCancellationToken).Result;
36+
}
37+
38+
public BackUpOperationStatusResponse GetProtectedItemOperationStatusByURL(
39+
string operationResultLink)
40+
{
41+
string resourceName = BmsAdapter.GetResourceName();
42+
string resourceGroupName = BmsAdapter.GetResourceName();
43+
44+
return BmsAdapter.Client.ProtectedItem.GetOperationStatusByURLAsync(
45+
operationResultLink,
46+
BmsAdapter.GetCustomRequestHeaders(),
47+
BmsAdapter.CmdletCancellationToken).Result;
48+
}
2549
}
2650
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.Collections.Generic;
17+
using System.Linq;
18+
using System.Text;
19+
using System.Threading.Tasks;
20+
using Microsoft.Azure.Management.RecoveryServices.Backup.Models;
21+
22+
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.HydraAdapter
23+
{
24+
public partial class HydraAdapter
25+
{
26+
public ProtectableObjectListResponse ListProtectableItem(
27+
ProtectableObjectListQueryParameters queryFilter)
28+
{
29+
string resourceName = BmsAdapter.GetResourceName();
30+
string resourceGroupName = BmsAdapter.GetResourceName();
31+
32+
return BmsAdapter.Client.ProtectableObject.ListAsync(
33+
resourceGroupName,
34+
resourceName,
35+
queryFilter,
36+
BmsAdapter.GetCustomRequestHeaders(),
37+
BmsAdapter.CmdletCancellationToken).Result;
38+
}
39+
}
40+
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.HydraAdapter/Commands.RecoveryServices.Backup.HydraAdapter.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<Compile Include="BMSAPIs\JobAPIs.cs" />
6363
<Compile Include="BMSAPIs\ItemAPIs.cs" />
6464
<Compile Include="BMSAPIs\ContainerAPIs.cs" />
65+
<Compile Include="BMSAPIs\ProtectableItemAPI.cs" />
6566
<Compile Include="ClientProxy.cs" />
6667
<Compile Include="ClientProxyBase.cs" />
6768
<Compile Include="CommonHelpers.cs" />

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Models/CmdletParamEnums.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ public enum PolicyParams
4040
RetentionPolicy,
4141
ProtectionPolicy,
4242
ResourceGroupName,
43-
ResourceName
43+
ResourceName
44+
}
45+
46+
public enum ItemParams
47+
{
48+
AzureVMName,
49+
AzureVMCloudServiceName,
50+
AzureVMResourceGroupName,
51+
WorkloadType,
52+
Policy,
53+
Item,
54+
ParameterSetName
4455
}
4556
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Providers/Commands.RecoveryServices.Backup.Providers.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
</Reference>
4545
<Reference Include="System" />
4646
<Reference Include="System.Core" />
47+
<Reference Include="System.Net" />
4748
<Reference Include="System.Xml.Linq" />
4849
<Reference Include="System.Data.DataSetExtensions" />
4950
<Reference Include="Microsoft.CSharp" />

0 commit comments

Comments
 (0)