Skip to content

Commit 751c9bb

Browse files
Merge pull request #8349 from MabOneSdk/mercury_dev_test
Backup Support for SQL Server in Azure VM
2 parents 9b01fb9 + e012566 commit 751c9bb

File tree

103 files changed

+2483490
-389
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+2483490
-389
lines changed

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

Lines changed: 213 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System;
16-
using System.Collections.Generic;
1715
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
1816
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
17+
using System;
18+
using System.Collections.Generic;
1919
using ServiceClientModel = Microsoft.Azure.Management.RecoveryServices.Backup.Models;
2020

2121
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers
@@ -55,6 +55,11 @@ public static ContainerBase GetContainerModel(ServiceClientModel.ProtectionConta
5555
{
5656
containerModel = new AzureFileShareContainer(protectionContainer);
5757
}
58+
else if (protectionContainer.Properties.GetType() ==
59+
typeof(ServiceClientModel.AzureVMAppContainerProtectionContainer))
60+
{
61+
containerModel = new AzureVmWorkloadContainer(protectionContainer);
62+
}
5863
}
5964

6065
return containerModel;
@@ -190,7 +195,7 @@ public static PolicyBase GetPolicyModelForAzureSql(ServiceClientModel.Protection
190195
ServiceClientModel.SimpleRetentionPolicy azureSqlRetentionPolicy =
191196
(ServiceClientModel.SimpleRetentionPolicy)azureSqlPolicy.RetentionPolicy;
192197
sqlPolicyModel.RetentionPolicy =
193-
PolicyHelpers.GetPSSimpleRetentionPolicy(azureSqlRetentionPolicy, null);
198+
PolicyHelpers.GetPSSimpleRetentionPolicy(azureSqlRetentionPolicy, null, "AzureSql");
194199
return policyModel;
195200
}
196201

@@ -236,6 +241,86 @@ public static PolicyBase GetPolicyModelForAzureFileShare(ServiceClientModel.Prot
236241
return policyModel;
237242
}
238243

244+
public static PolicyBase GetPolicyModelForAzureVmWorkload(ServiceClientModel.ProtectionPolicyResource serviceClientResponse,
245+
PolicyBase policyModel)
246+
{
247+
ServiceClientModel.AzureVmWorkloadProtectionPolicy azureVmWorkloadPolicy =
248+
(ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties;
249+
250+
foreach (var policy in azureVmWorkloadPolicy.SubProtectionPolicy)
251+
{
252+
if (string.Compare(policy.PolicyType, "Full") == 0)
253+
{
254+
if (policy.SchedulePolicy.GetType() !=
255+
typeof(ServiceClientModel.SimpleSchedulePolicy))
256+
{
257+
Logger.Instance.WriteDebug("Unknown Schedule object received: " +
258+
policy.SchedulePolicy.GetType());
259+
Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
260+
return null;
261+
}
262+
if (policy.RetentionPolicy.GetType() !=
263+
typeof(ServiceClientModel.LongTermRetentionPolicy))
264+
{
265+
Logger.Instance.WriteDebug("Unknown RetentionPolicy object received: " +
266+
policy.RetentionPolicy.GetType());
267+
Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
268+
return null;
269+
}
270+
}
271+
else if (string.Compare(policy.PolicyType, "Differential") == 0)
272+
{
273+
if (policy.SchedulePolicy.GetType() !=
274+
typeof(ServiceClientModel.SimpleSchedulePolicy))
275+
{
276+
Logger.Instance.WriteDebug("Unknown Schedule object received: " +
277+
policy.SchedulePolicy.GetType());
278+
Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
279+
return null;
280+
}
281+
if (policy.RetentionPolicy.GetType() !=
282+
typeof(ServiceClientModel.SimpleRetentionPolicy))
283+
{
284+
Logger.Instance.WriteDebug("Unknown RetentionPolicy object received: " +
285+
policy.RetentionPolicy.GetType());
286+
Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
287+
return null;
288+
}
289+
}
290+
else if (string.Compare(policy.PolicyType, "Log") == 0)
291+
{
292+
if (policy.SchedulePolicy.GetType() !=
293+
typeof(ServiceClientModel.LogSchedulePolicy))
294+
{
295+
Logger.Instance.WriteDebug("Unknown Schedule object received: " +
296+
policy.SchedulePolicy.GetType());
297+
Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
298+
return null;
299+
}
300+
if (policy.RetentionPolicy.GetType() !=
301+
typeof(ServiceClientModel.SimpleRetentionPolicy))
302+
{
303+
Logger.Instance.WriteDebug("Unknown RetentionPolicy object received: " +
304+
policy.RetentionPolicy.GetType());
305+
Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
306+
return null;
307+
}
308+
}
309+
}
310+
311+
policyModel = new AzureVmWorkloadPolicy();
312+
AzureVmWorkloadPolicy azureVmWorkloadPolicyModel = policyModel as AzureVmWorkloadPolicy;
313+
azureVmWorkloadPolicyModel.WorkloadType = WorkloadType.MSSQL;
314+
azureVmWorkloadPolicyModel.BackupManagementType = BackupManagementType.AzureWorkload;
315+
azureVmWorkloadPolicyModel.IsCompression =
316+
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.IsCompression;
317+
azureVmWorkloadPolicyModel.IsDifferentialBackupEnabled = false;
318+
azureVmWorkloadPolicyModel.IsLogBackupEnabled = false;
319+
GetPSSubProtectionPolicy(azureVmWorkloadPolicyModel, serviceClientResponse,
320+
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.TimeZone);
321+
return policyModel;
322+
}
323+
239324
/// <summary>
240325
/// Helper function to convert ps backup policy model from service response.
241326
/// </summary>
@@ -263,6 +348,11 @@ public static PolicyBase GetPolicyModel(ServiceClientModel.ProtectionPolicyResou
263348
{
264349
policyModel = GetPolicyModelForAzureFileShare(serviceClientResponse, policyModel);
265350
}
351+
else if (serviceClientResponse.Properties.GetType() ==
352+
typeof(ServiceClientModel.AzureVmWorkloadProtectionPolicy))
353+
{
354+
policyModel = GetPolicyModelForAzureVmWorkload(serviceClientResponse, policyModel);
355+
}
266356
else
267357
{
268358
// we will enter this case when service supports new workload and customer
@@ -338,8 +428,38 @@ public static ItemBase GetItemModel(ServiceClientModel.ProtectedItemResource pro
338428
{
339429
itemModel = GetAzureFileShareItemModel(protectedItem);
340430
}
431+
432+
if (protectedItem.Properties.GetType() ==
433+
typeof(ServiceClientModel.AzureVmWorkloadSQLDatabaseProtectedItem))
434+
{
435+
itemModel = GetAzureVmWorkloadItemModel(protectedItem);
436+
}
437+
}
438+
439+
return itemModel;
440+
}
441+
442+
private static ItemBase GetAzureVmWorkloadItemModel(ServiceClientModel.ProtectedItemResource protectedItem)
443+
{
444+
ItemBase itemModel;
445+
string policyName = null;
446+
string policyId = ((ServiceClientModel.AzureVmWorkloadSQLDatabaseProtectedItem)protectedItem.Properties).PolicyId;
447+
if (!string.IsNullOrEmpty(policyId))
448+
{
449+
Dictionary<UriEnums, string> keyValueDict =
450+
HelperUtils.ParseUri(policyId);
451+
policyName = HelperUtils.GetPolicyNameFromPolicyId(keyValueDict, policyId);
341452
}
342453

454+
string containerUri = HelperUtils.GetContainerUri(
455+
HelperUtils.ParseUri(protectedItem.Id),
456+
protectedItem.Id);
457+
458+
itemModel = new AzureWorkloadSQLDatabaseProtectedItem(
459+
protectedItem,
460+
containerUri,
461+
ContainerType.AzureVMAppContainer,
462+
policyName);
343463
return itemModel;
344464
}
345465

@@ -418,7 +538,42 @@ private static ItemBase GetAzureVmItemModel(ServiceClientModel.ProtectedItemReso
418538
}
419539

420540
/// <summary>
421-
/// Helper function to convert ps backup policy item list from service response.
541+
/// Helper function to convert ps protectable item from service response.
542+
/// </summary>
543+
public static ProtectableItemBase GetProtectableItemModel(ServiceClientModel.WorkloadProtectableItemResource protectableItem)
544+
{
545+
ProtectableItemBase itemModel = null;
546+
547+
if (protectableItem != null &&
548+
protectableItem.Properties != null)
549+
{
550+
if (protectableItem.Properties.GetType().IsSubclassOf(typeof(ServiceClientModel.AzureVmWorkloadProtectableItem)))
551+
{
552+
itemModel = GetAzureWorkloadProtectableItemModel(protectableItem);
553+
}
554+
}
555+
556+
return itemModel;
557+
}
558+
559+
private static ProtectableItemBase GetAzureWorkloadProtectableItemModel(ServiceClientModel.WorkloadProtectableItemResource protectableItem)
560+
{
561+
ProtectableItemBase itemModel;
562+
563+
string containerUri = HelperUtils.GetContainerUri(
564+
HelperUtils.ParseUri(protectableItem.Id),
565+
protectableItem.Id);
566+
567+
itemModel = new AzureWorkloadProtectableItem(
568+
protectableItem,
569+
containerUri,
570+
ContainerType.AzureVMAppContainer);
571+
572+
return itemModel;
573+
}
574+
575+
/// <summary>
576+
/// Helper function to convert ps item list from service response.
422577
/// </summary>
423578
public static List<ItemBase> GetItemModelList(IEnumerable<ServiceClientModel.ProtectedItemResource> protectedItems)
424579
{
@@ -431,6 +586,60 @@ public static List<ItemBase> GetItemModelList(IEnumerable<ServiceClientModel.Pro
431586

432587
return itemModels;
433588
}
589+
590+
public static void GetPSSubProtectionPolicy(AzureVmWorkloadPolicy azureVmWorkloadPolicyModel,
591+
ServiceClientModel.ProtectionPolicyResource serviceClientResponse, string timeZone)
592+
{
593+
foreach (var subProtectionPolicy in
594+
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).SubProtectionPolicy)
595+
{
596+
if (string.Compare(subProtectionPolicy.PolicyType, "Full") == 0)
597+
{
598+
azureVmWorkloadPolicyModel.FullBackupSchedulePolicy = PolicyHelpers.GetPSSimpleSchedulePolicy(
599+
(ServiceClientModel.SimpleSchedulePolicy)subProtectionPolicy.SchedulePolicy,
600+
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.TimeZone);
601+
602+
azureVmWorkloadPolicyModel.FullBackupRetentionPolicy = PolicyHelpers.GetPSLongTermRetentionPolicy(
603+
(ServiceClientModel.LongTermRetentionPolicy)subProtectionPolicy.RetentionPolicy,
604+
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.TimeZone);
605+
}
606+
else if (string.Compare(subProtectionPolicy.PolicyType, "Differential") == 0)
607+
{
608+
azureVmWorkloadPolicyModel.DifferentialBackupSchedulePolicy = PolicyHelpers.GetPSSimpleSchedulePolicy(
609+
(ServiceClientModel.SimpleSchedulePolicy)subProtectionPolicy.SchedulePolicy,
610+
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.TimeZone);
611+
azureVmWorkloadPolicyModel.DifferentialBackupRetentionPolicy = PolicyHelpers.GetPSSimpleRetentionPolicy(
612+
(ServiceClientModel.SimpleRetentionPolicy)subProtectionPolicy.RetentionPolicy,
613+
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.TimeZone, "AzureWorkload");
614+
azureVmWorkloadPolicyModel.IsDifferentialBackupEnabled = true;
615+
}
616+
else if (string.Compare(subProtectionPolicy.PolicyType, "Log") == 0)
617+
{
618+
azureVmWorkloadPolicyModel.LogBackupSchedulePolicy = PolicyHelpers.GetPSLogSchedulePolicy((ServiceClientModel.LogSchedulePolicy)
619+
subProtectionPolicy.SchedulePolicy,
620+
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.TimeZone);
621+
azureVmWorkloadPolicyModel.LogBackupRetentionPolicy = PolicyHelpers.GetPSSimpleRetentionPolicy((ServiceClientModel.SimpleRetentionPolicy)
622+
subProtectionPolicy.RetentionPolicy,
623+
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.TimeZone, "AzureWorkload");
624+
azureVmWorkloadPolicyModel.IsLogBackupEnabled = true;
625+
}
626+
}
627+
}
628+
629+
/// <summary>
630+
/// Helper function to convert ps protectable item list from service response.
631+
/// </summary>
632+
public static List<ProtectableItemBase> GetProtectableItemModelList(IEnumerable<ServiceClientModel.WorkloadProtectableItemResource> protectableItems)
633+
{
634+
List<ProtectableItemBase> itemModels = new List<ProtectableItemBase>();
635+
636+
foreach (var protectableItem in protectableItems)
637+
{
638+
itemModels.Add(GetProtectableItemModel(protectableItem));
639+
}
640+
641+
return itemModels;
642+
}
434643
#endregion
435644
}
436645
}

src/RecoveryServices/RecoveryServices.Backup.Helpers/Conversions/JobConversions.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public static CmdletModel.JobBase GetPSJob(JobResource serviceClientJob)
4747
{
4848
response = GetPSAzureFileShareJob(serviceClientJob);
4949
}
50+
else if (serviceClientJob.Properties.GetType() == typeof(AzureWorkloadJob))
51+
{
52+
response = GetPSAzureWorkloadJob(serviceClientJob);
53+
}
5054

5155
return response;
5256
}
@@ -234,6 +238,88 @@ private static CmdletModel.AzureJobErrorInfo GetPSAzureFileShareErrorInfo(AzureS
234238
return psErrorInfo;
235239
}
236240

241+
private static CmdletModel.JobBase GetPSAzureWorkloadJob(JobResource serviceClientJob)
242+
{
243+
CmdletModel.AzureVmWorkloadJob response;
244+
245+
AzureWorkloadJob workloadJob = serviceClientJob.Properties as AzureWorkloadJob;
246+
247+
if (workloadJob.ExtendedInfo != null)
248+
{
249+
response = new CmdletModel.AzureVmWorkloadJobDetails();
250+
}
251+
else
252+
{
253+
response = new CmdletModel.AzureVmWorkloadJob();
254+
}
255+
256+
response.JobId = GetLastIdFromFullId(serviceClientJob.Id);
257+
response.StartTime = GetJobStartTime(workloadJob.StartTime);
258+
response.EndTime = workloadJob.EndTime;
259+
response.Duration = GetJobDuration(workloadJob.Duration);
260+
response.Status = workloadJob.Status;
261+
response.WorkloadName = workloadJob.EntityFriendlyName;
262+
response.ActivityId = workloadJob.ActivityId;
263+
response.BackupManagementType =
264+
CmdletModel.ConversionUtils.GetPsBackupManagementType(workloadJob.BackupManagementType);
265+
response.Operation = workloadJob.Operation;
266+
267+
if (workloadJob.ErrorDetails != null)
268+
{
269+
response.ErrorDetails = new List<CmdletModel.AzureJobErrorInfo>();
270+
foreach (var workloadError in workloadJob.ErrorDetails)
271+
{
272+
response.ErrorDetails.Add(GetPSAzureWorkloadErrorInfo(workloadError));
273+
}
274+
}
275+
276+
// fill extended info if present
277+
if (workloadJob.ExtendedInfo != null)
278+
{
279+
CmdletModel.AzureVmWorkloadJobDetails detailedResponse =
280+
response as CmdletModel.AzureVmWorkloadJobDetails;
281+
282+
detailedResponse.DynamicErrorMessage = workloadJob.ExtendedInfo.DynamicErrorMessage;
283+
if (workloadJob.ExtendedInfo.PropertyBag != null)
284+
{
285+
detailedResponse.Properties = new Dictionary<string, string>();
286+
foreach (var key in workloadJob.ExtendedInfo.PropertyBag.Keys)
287+
{
288+
detailedResponse.Properties.Add(key, workloadJob.ExtendedInfo.PropertyBag[key]);
289+
}
290+
}
291+
292+
if (workloadJob.ExtendedInfo.TasksList != null)
293+
{
294+
detailedResponse.SubTasks = new List<CmdletModel.AzureVmWorkloadJobSubTask>();
295+
foreach (var workloadJobTask in workloadJob.ExtendedInfo.TasksList)
296+
{
297+
detailedResponse.SubTasks.Add(new CmdletModel.AzureVmWorkloadJobSubTask()
298+
{
299+
Name = workloadJobTask.TaskId,
300+
Status = workloadJobTask.Status
301+
});
302+
}
303+
}
304+
}
305+
306+
return response;
307+
}
308+
309+
private static CmdletModel.AzureJobErrorInfo GetPSAzureWorkloadErrorInfo(AzureWorkloadErrorInfo workloadError)
310+
{
311+
CmdletModel.AzureVmWorkloadJobErrorInfo psErrorInfo = new CmdletModel.AzureVmWorkloadJobErrorInfo();
312+
psErrorInfo.ErrorCode = GetJobErrorCode(workloadError.ErrorCode);
313+
psErrorInfo.ErrorMessage = workloadError.ErrorString;
314+
if (workloadError.Recommendations != null)
315+
{
316+
psErrorInfo.Recommendations = new List<string>();
317+
psErrorInfo.Recommendations.AddRange(workloadError.Recommendations);
318+
}
319+
320+
return psErrorInfo;
321+
}
322+
237323
private static int GetJobErrorCode(int? errorCode)
238324
{
239325
return errorCode ?? default(int);

0 commit comments

Comments
 (0)