private bool IsDiscoveryNeeded(string vmName, string rgName, bool isComputeAzureVM, out WorkloadProtectableItemResource protectableObjectResource) { bool isDiscoveryNeed = true; protectableObjectResource = null; string vmVersion = string.Empty; vmVersion = (isComputeAzureVM) == true ? computeAzureVMVersion : classicComputeAzureVMVersion; string virtualMachineId = GetAzureIaasVirtualMachineId(rgName, vmVersion, vmName); ODataQuery <BMSPOQueryObject> queryParam = new ODataQuery <BMSPOQueryObject>( q => q.BackupManagementType == ServiceClientModel.BackupManagementType.AzureIaasVM); var protectableItemList = ServiceClientAdapter.ListProtectableItem(queryParam); Logger.Instance.WriteDebug(string.Format(Resources.ContainerCountAfterFilter, protectableItemList.Count())); if (protectableItemList.Count() == 0) { //Container is not discovered Logger.Instance.WriteDebug(Resources.ContainerNotDiscovered); isDiscoveryNeed = true; } else { foreach (var protectableItem in protectableItemList) { IaaSVMProtectableItem iaaSVMProtectableItem = (IaaSVMProtectableItem)protectableItem.Properties; if (iaaSVMProtectableItem != null && string.Compare(iaaSVMProtectableItem.FriendlyName, vmName, true) == 0 && iaaSVMProtectableItem.VirtualMachineId.IndexOf(virtualMachineId, StringComparison.InvariantCultureIgnoreCase) >= 0) { protectableObjectResource = protectableItem; isDiscoveryNeed = false; break; } } } return(isDiscoveryNeed); }
/// <summary> /// Triggers the enable protection operation for the given item /// </summary> /// <returns>The job response returned from the service</returns> public RestAzureNS.AzureOperationResponse EnableProtection() { string azureVMName = (string)ProviderData[ItemParams.AzureVMName]; string azureVMCloudServiceName = (string)ProviderData[ItemParams.AzureVMCloudServiceName]; string azureVMResourceGroupName = (string)ProviderData[ItemParams.AzureVMResourceGroupName]; string parameterSetName = (string)ProviderData[ItemParams.ParameterSetName]; PolicyBase policy = (PolicyBase)ProviderData[ItemParams.Policy]; ItemBase itemBase = (ItemBase)ProviderData[ItemParams.Item]; AzureVmItem item = (AzureVmItem)ProviderData[ItemParams.Item]; // do validations string containerUri = ""; string protectedItemUri = ""; bool isComputeAzureVM = false; string sourceResourceId = null; if (itemBase == null) { isComputeAzureVM = string.IsNullOrEmpty(azureVMCloudServiceName) ? true : false; string azureVMRGName = (isComputeAzureVM) ? azureVMResourceGroupName : azureVMCloudServiceName; ValidateAzureVMWorkloadType(policy.WorkloadType); ValidateAzureVMEnableProtectionRequest( azureVMName, azureVMCloudServiceName, azureVMResourceGroupName, policy); WorkloadProtectableItemResource protectableObjectResource = GetAzureVMProtectableObject(azureVMName, azureVMRGName, isComputeAzureVM); Dictionary <UriEnums, string> keyValueDict = HelperUtils.ParseUri(protectableObjectResource.Id); containerUri = HelperUtils.GetContainerUri( keyValueDict, protectableObjectResource.Id); protectedItemUri = HelperUtils.GetProtectableItemUri( keyValueDict, protectableObjectResource.Id); IaaSVMProtectableItem iaasVmProtectableItem = (IaaSVMProtectableItem)protectableObjectResource.Properties; if (iaasVmProtectableItem != null) { sourceResourceId = iaasVmProtectableItem.VirtualMachineId; } } else { ValidateAzureVMWorkloadType(item.WorkloadType, policy.WorkloadType); ValidateAzureVMModifyProtectionRequest(itemBase, policy); isComputeAzureVM = IsComputeAzureVM(item.VirtualMachineId); Dictionary <UriEnums, string> keyValueDict = HelperUtils.ParseUri(item.Id); containerUri = HelperUtils.GetContainerUri(keyValueDict, item.Id); protectedItemUri = HelperUtils.GetProtectedItemUri(keyValueDict, item.Id); sourceResourceId = item.SourceResourceId; } // construct Service Client protectedItem request AzureIaaSVMProtectedItem properties; if (isComputeAzureVM == false) { properties = new AzureIaaSClassicComputeVMProtectedItem(); } else { properties = new AzureIaaSComputeVMProtectedItem(); } properties.PolicyId = policy.Id; properties.SourceResourceId = sourceResourceId; ProtectedItemResource serviceClientRequest = new ProtectedItemResource() { Properties = properties }; return(ServiceClientAdapter.CreateOrUpdateProtectedItem( containerUri, protectedItemUri, serviceClientRequest)); }