Skip to content

Commit baa08e2

Browse files
committed
DisableProtection cmdlet implementation
1 parent ffabeff commit baa08e2

File tree

12 files changed

+267
-7
lines changed

12 files changed

+267
-7
lines changed

src/ResourceManager/RecoveryServices.Backup/CmdletParameterHelpMessages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ internal static class Item
7272
public const string ProtectionStatus = "Protection status of Item";
7373
public const string Status = "Status of the data source";
7474
public const string Container = "Container where the item resides";
75+
public const string RemoveProtectionOption = "If this option is used, all the backup data for this item will also be deleted and restoring data will not be possible.";
7576
}
7677
}
7778
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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.Properties;
24+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers;
25+
using HydraModel = Microsoft.Azure.Management.RecoveryServices.Backup.Models;
26+
27+
28+
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
29+
{
30+
/// <summary>
31+
/// Enable Azure Backup protection
32+
/// </summary>
33+
[Cmdlet(VerbsLifecycle.Disable, "AzureRmRecoveryServicesProtection"), OutputType(typeof(AzureRmRecoveryServicesJobBase))]
34+
public class DisableAzureRmRecoveryServicesProtection : RecoveryServicesBackupCmdletBase
35+
{
36+
[Parameter(Mandatory = true, HelpMessage = ParamHelpMsg.Item.ProtectedItem, ValueFromPipeline = true)]
37+
[ValidateNotNullOrEmpty]
38+
public AzureRmRecoveryServicesItemBase Item { get; set; }
39+
40+
[Parameter(Position = 1, Mandatory = false, HelpMessage = ParamHelpMsg.Item.RemoveProtectionOption)]
41+
public SwitchParameter RemoveRecoveryPoints
42+
{
43+
get { return DeleteBackupData; }
44+
set { DeleteBackupData = value; }
45+
}
46+
47+
[Parameter(Mandatory = false, HelpMessage = "Don't ask for confirmation.")]
48+
public SwitchParameter Force { get; set; }
49+
50+
private bool DeleteBackupData;
51+
52+
public override void ExecuteCmdlet()
53+
{
54+
ConfirmAction(
55+
Force.IsPresent,
56+
string.Format(Resources.DisableProtectionWarning, Item.Name),
57+
Resources.DisableProtectionMessage,
58+
Item.Name, () =>
59+
{
60+
ExecutionBlock(() =>
61+
{
62+
base.ExecuteCmdlet();
63+
64+
PsBackupProviderManager providerManager = new PsBackupProviderManager(new Dictionary<System.Enum, object>()
65+
{
66+
67+
{ItemParams.Item, Item},
68+
{ItemParams.DeleteBackupData, this.DeleteBackupData},
69+
}, HydraAdapter);
70+
71+
IPsBackupProvider psBackupProvider = providerManager.GetProviderInstance(Item.WorkloadType, Item.BackupManagementType);
72+
73+
var jobResponse = psBackupProvider.DisableProtection();
74+
75+
// Track Response and display job details
76+
77+
var response = OperationStatusHelper.TrackOperationStatus(jobResponse, HydraAdapter);
78+
79+
if (response.OperationStatus.Status == HydraModel.OperationStatusValues.Succeeded)
80+
{
81+
var jobStatusResponse = (HydraModel.OperationStatusJobExtendedInfo)response.OperationStatus.Properties;
82+
string jobId = jobStatusResponse.JobId;
83+
var job = HydraAdapter.GetJob(jobId);
84+
WriteObject(JobConversions.GetPSJob(job));
85+
}
86+
else if(response.OperationStatus.Status == HydraModel.OperationStatusValues.Failed)
87+
{
88+
var jobStatusResponse = (HydraModel.OperationStatusJobExtendedInfo)response.OperationStatus.Properties;
89+
if(jobStatusResponse != null || !string.IsNullOrEmpty(jobStatusResponse.JobId))
90+
{
91+
string jobId = jobStatusResponse.JobId;
92+
var job = HydraAdapter.GetJob(jobId);
93+
WriteObject(JobConversions.GetPSJob(job));
94+
}
95+
96+
var errorMessage = string.Format(Resources.DisableProtectionOperationFailed,
97+
response.OperationStatus.OperationStatusError.Code,
98+
response.OperationStatus.OperationStatusError.Message);
99+
100+
throw new Exception(errorMessage);
101+
}
102+
});
103+
});
104+
105+
}
106+
}
107+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Compile Include="Cmdlets\Container\GetAzureRmRecoveryServicesContainer.cs" />
7979
<Compile Include="Cmdlets\Container\UnregisterAzureRmBackupManagementServer.cs" />
8080
<Compile Include="Cmdlets\Container\UnregisterAzureRmRecoveryServicesBackupContainer.cs" />
81+
<Compile Include="Cmdlets\Item\DisableAzureRmRecoveryServicesProtection.cs" />
8182
<Compile Include="Cmdlets\Item\EnableAzureRmRecoveryServicesProtection.cs" />
8283
<Compile Include="Cmdlets\Item\GetAzureRmRecoveryServicesItem.cs" />
8384
<Compile Include="Cmdlets\Jobs\GetAzureRmRecoveryServicesJob.cs" />

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Helpers/Conversions/ConversionHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public static AzureRmRecoveryServicesItemBase GetItemModel(ProtectedItemResource
150150
{
151151
if (protectedItem.Properties.GetType().IsSubclassOf(typeof(AzureIaaSVMProtectedItem)))
152152
{
153-
itemModel = new AzureRmRecoveryServicesIaasVmItem((AzureIaaSVMProtectedItem)protectedItem.Properties, container);
153+
itemModel = new AzureRmRecoveryServicesIaasVmItem(protectedItem, container);
154154
}
155155
}
156156

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Logger/Logger.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Text;
1818
using System.Threading.Tasks;
1919
using System.Collections.Generic;
20+
using System.Management.Automation;
2021

2122
namespace Microsoft.Azure.Commands.RecoveryServices.Backup
2223
{
@@ -28,15 +29,19 @@ public class Logger
2829

2930
private Action<string> writeVerboseAction;
3031

32+
private Action<ErrorRecord> throwTerminatingErrorAction;
33+
3134
public static Logger Instance { get; set; }
3235

3336
public Logger(Action<string> writeWarning,
3437
Action<string> writeDebug,
35-
Action<string> writeVerbose)
38+
Action<string> writeVerbose,
39+
Action<ErrorRecord> throwTerminatingError)
3640
{
3741
writeWarningAction = writeWarning;
3842
writeDebugAction = writeDebug;
3943
writeVerboseAction = writeVerbose;
44+
throwTerminatingErrorAction = throwTerminatingError;
4045
}
4146

4247
public void WriteVerbose(string text)
@@ -53,5 +58,10 @@ public void WriteWarning(string text)
5358
{
5459
writeWarningAction(text);
5560
}
61+
62+
public void ThrowTerminatingError(ErrorRecord errorRecord)
63+
{
64+
throwTerminatingErrorAction(errorRecord);
65+
}
5666
}
5767
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Models/AzureVmModels/AzureRmRecoveryServicesAzureVmItem.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ public class AzureRmRecoveryServicesIaasVmItem : AzureRmRecoveryServicesItemBase
5858
/// </summary
5959
public AzureRmRecoveryServicesIaasVmItemExtendedInfo ExtendedInfo { get; set; }
6060

61-
public AzureRmRecoveryServicesIaasVmItem(AzureIaaSVMProtectedItem protectedItem,
61+
public AzureRmRecoveryServicesIaasVmItem(ProtectedItemResource protectedItemResource,
6262
AzureRmRecoveryServicesContainerBase container)
63-
: base(protectedItem, container)
63+
: base(protectedItemResource, container)
6464
{
65+
AzureIaaSVMProtectedItem protectedItem = (AzureIaaSVMProtectedItem)protectedItemResource.Properties;
6566
LastBackupStatus = protectedItem.LastBackupStatus;
6667
ProtectionPolicyName = protectedItem.PolicyName;
6768
ProtectionState = EnumUtils.GetEnum<ItemStatus>(protectedItem.ProtectionState);

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,28 @@ public AzureRmRecoveryServicesItemContext(ProtectedItem protectedItem,
101101
/// </summary>
102102
public class AzureRmRecoveryServicesItemBase : AzureRmRecoveryServicesItemContext
103103
{
104+
/// <summary>
105+
/// Name of the item
106+
/// </summary>
107+
public string Name { get; set; }
108+
109+
/// <summary>
110+
/// Id of the item
111+
/// </summary>
112+
public string Id { get; set; }
113+
104114
/// <summary>
105115
/// Last Recovery Point for the item
106116
/// </summary>
107117
public DateTime? LastRecoveryPoint { get; set; }
108118

109-
public AzureRmRecoveryServicesItemBase(ProtectedItem protectedItem,
119+
public AzureRmRecoveryServicesItemBase(ProtectedItemResource protectedItemResource,
110120
AzureRmRecoveryServicesContainerBase container)
111-
: base(protectedItem, container)
121+
: base((ProtectedItem)protectedItemResource.Properties, container)
112122
{
123+
ProtectedItem protectedItem = (ProtectedItem)protectedItemResource.Properties;
124+
Name = protectedItemResource.Name;
125+
Id = protectedItemResource.Id;
113126
LastRecoveryPoint = protectedItem.LastRecoveryPoint;
114127
}
115128
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,6 @@ public enum ItemParams
7070
Container,
7171
ProtectionStatus,
7272
Status,
73+
DeleteBackupData
7374
}
7475
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Models/CommonModels/Enums.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public enum ItemStatus
6464
IRPending = 1,
6565
ProtectionError,
6666
Protected,
67+
ProtectionStopped
6768
}
6869

6970
#region policy

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Models/Properties/Resources.Designer.cs

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Models/Properties/Resources.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@
195195
<data name="UnExpectedWorkLoadTypeException" xml:space="preserve">
196196
<value>Unexpected WorkloadType - expected:{0}, current:{1}</value>
197197
</data>
198+
<data name="UnExpectedContainerTypeException" xml:space="preserve">
199+
<value>Unexpected ContainerType - expected:{0}, current:{1}</value>
200+
</data>
198201
<data name="AllRetentionSchedulesEmptyException" xml:space="preserve">
199202
<value>All retention schedules are Empty in retentionPolicy. Alteast one is expected.</value>
200203
</data>
@@ -338,4 +341,13 @@ Please contact Microsoft for further assistant.</value>
338341
<data name="ProtectionPolicyDeleted" xml:space="preserve">
339342
<value>Successfully deleted policy</value>
340343
</data>
344+
<data name="DisableProtectionMessage" xml:space="preserve">
345+
<value>Disabling protection for the item</value>
346+
</data>
347+
<data name="DisableProtectionWarning" xml:space="preserve">
348+
<value>Are you sure you want to disable protection for the item '{0}'</value>
349+
</data>
350+
<data name="DisableProtectionOperationFailed" xml:space="preserve">
351+
<value>Disable Protection OperationFailed with error code {0} , and error Message {1}</value>
352+
</data>
341353
</root>

0 commit comments

Comments
 (0)