public override void ExecuteCmdlet() { ExecutionBlock(() => { base.ExecuteCmdlet(); ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId); string vaultName = resourceIdentifier.ResourceName; string resourceGroupName = resourceIdentifier.ResourceGroupName; string backupManagementType = ""; string workloadType = ""; string containerName = ""; if (ParameterSetName == IdParamSet) { Dictionary <UriEnums, string> keyValueDict = HelperUtils.ParseUri(ParentID); containerName = HelperUtils.GetContainerUri(keyValueDict, ParentID); if (containerName.Split(new string[] { ";" }, System.StringSplitOptions.None)[0].ToLower() == "vmappcontainer") { backupManagementType = ServiceClientModel.BackupManagementType.AzureWorkload; } string protectableItem = HelperUtils.GetProtectableItemUri(keyValueDict, ParentID); if (protectableItem.Split(new string[] { ";" }, System.StringSplitOptions.None)[0].ToLower() == "sqlinstance" || protectableItem.Split(new string[] { ";" }, System.StringSplitOptions.None)[0].ToLower() == "sqlavailabilitygroupcontainer") { workloadType = ServiceClientModel.WorkloadType.SQLDataBase; } } else { backupManagementType = Container.BackupManagementType.ToString(); workloadType = ConversionUtils.GetServiceClientWorkloadType(WorkloadType.ToString()); containerName = Container.Name; } ODataQuery <BMSPOQueryObject> queryParam = new ODataQuery <BMSPOQueryObject>( q => q.BackupManagementType == backupManagementType && q.WorkloadType == workloadType && q.ContainerName == containerName); WriteDebug("going to query service to get list of protectable items"); List <WorkloadProtectableItemResource> protectableItems = ServiceClientAdapter.ListProtectableItem( queryParam, vaultName: vaultName, resourceGroupName: resourceGroupName); WriteDebug("Successfully got response from service"); List <ProtectableItemBase> itemModels = ConversionHelpers.GetProtectableItemModelList(protectableItems); if (ParameterSetName == FilterParamSet) { string protectableItemType = ItemType.ToString(); itemModels = itemModels.Where(itemModel => { return(((AzureWorkloadProtectableItem)itemModel).ProtectableItemType == protectableItemType); }).ToList(); } WriteObject(itemModels, enumerateCollection: true); }); }
public void FetchNodesListAndAutoProtectionPolicy(List <ProtectableItemBase> itemModels, string vaultName, string resourceGroupName) { foreach (var itemModel in itemModels) { AzureWorkloadProtectableItem protectableItem = ((AzureWorkloadProtectableItem)itemModel); string itemType = ""; string itemName = ""; string containerUri = ""; string backupManagementType = ""; Dictionary <UriEnums, string> keyValueDict = HelperUtils.ParseUri(protectableItem.Id); itemType = HelperUtils.GetProtectableItemUri(keyValueDict, protectableItem.Id).Split(';')[0]; itemName = HelperUtils.GetProtectableItemUri(keyValueDict, protectableItem.Id).Split(';')[1]; containerUri = HelperUtils.GetContainerUri(keyValueDict, protectableItem.Id); // fetch AutoProtectionPolicy for non DBs if (protectableItem.ProtectableItemType != "SQLDataBase") { // fetch the policy using backup intent ODataQuery <ServiceClientModel.ProtectionIntentQueryObject> queryParams = null; backupManagementType = ServiceClientModel.BackupManagementType.AzureWorkload; queryParams = new ODataQuery <ServiceClientModel.ProtectionIntentQueryObject>( q => q.ItemType == itemType && q.ItemName == itemName && q.ParentName == containerUri && q.BackupManagementType == backupManagementType); var intentList = ServiceClientAdapter.ListProtectionIntent( queryParams, vaultName: vaultName, resourceGroupName: resourceGroupName); foreach (var intent in intentList) { protectableItem.AutoProtectionPolicy = intent.Properties.PolicyId; } } // fetch Nodelist for SQLAGs if (protectableItem.ProtectableItemType == "SQLAvailabilityGroup") { // add the NodeList ProtectionContainerResource cont = ServiceClientAdapter.GetContainer(vaultName, resourceGroupName, containerUri); AzureSQLAGWorkloadContainerProtectionContainer protectionContainer = (AzureSQLAGWorkloadContainerProtectionContainer)cont.Properties; if (protectionContainer.ExtendedInfo != null) { protectableItem.NodesList = protectionContainer.ExtendedInfo.NodesList; } } } }
public override void ExecuteCmdlet() { ExecutionBlock(() => { base.ExecuteCmdlet(); ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId); string vaultName = resourceIdentifier.ResourceName; string resourceGroupName = resourceIdentifier.ResourceGroupName; string shouldProcessName = InputItem.Id; string protectedItemUri = ""; if (ShouldProcess(shouldProcessName, VerbsLifecycle.Enable)) { Dictionary <UriEnums, string> keyValueDict = HelperUtils.ParseUri(InputItem.Id); protectedItemUri = HelperUtils.GetProtectableItemUri( keyValueDict, InputItem.Id); AzureRecoveryServiceVaultProtectionIntent properties = new AzureRecoveryServiceVaultProtectionIntent(); properties.BackupManagementType = ServiceClientModel.BackupManagementType.AzureWorkload; properties.ItemId = InputItem.Id; properties.PolicyId = Policy.Id; ProtectionIntentResource serviceClientRequest = new ProtectionIntentResource() { Properties = properties }; bool isAutoProtectionSuccessful = false; try { var itemResponse = ServiceClientAdapter.CreateOrUpdateProtectionIntent( GetGuid ?? Guid.NewGuid().ToString(), serviceClientRequest, vaultName: vaultName, resourceGroupName: resourceGroupName); isAutoProtectionSuccessful = true; } catch { } if (PassThru.IsPresent) { WriteObject(isAutoProtectionSuccessful); } } }); }
/// <summary> /// Triggers the enable protection operation for the given item /// </summary> /// <returns>The job response returned from the service</returns> public BaseRecoveryServicesJobResponse 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; if (itemBase == null) { isComputeAzureVM = string.IsNullOrEmpty(azureVMCloudServiceName) ? true : false; string azureVMRGName = (isComputeAzureVM) ? azureVMResourceGroupName : azureVMCloudServiceName; ValidateAzureVMWorkloadType(policy.WorkloadType); ValidateAzureVMEnableProtectionRequest( azureVMName, azureVMCloudServiceName, azureVMResourceGroupName, policy); ProtectableObjectResource 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); } 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); } // construct Service Client protectedItem request AzureIaaSVMProtectedItem properties; if (isComputeAzureVM == false) { properties = new AzureIaaSClassicComputeVMProtectedItem(); } else { properties = new AzureIaaSComputeVMProtectedItem(); } properties.PolicyId = policy.Id; ProtectedItemCreateOrUpdateRequest serviceClientRequest = new ProtectedItemCreateOrUpdateRequest() { Item = new ProtectedItemResource() { Properties = properties, } }; return(ServiceClientAdapter.CreateOrUpdateProtectedItem( containerUri, protectedItemUri, serviceClientRequest)); }
private RestAzureNS.AzureOperationResponse <ProtectedItemResource> EnableOrModifyProtection(bool disableWithRetentionData = false) { string vaultName = (string)ProviderData[VaultParams.VaultName]; string vaultResourceGroupName = (string)ProviderData[VaultParams.ResourceGroupName]; PolicyBase policy = ProviderData.ContainsKey(ItemParams.Policy) ? (PolicyBase)ProviderData[ItemParams.Policy] : null; ProtectableItemBase protectableItemBase = ProviderData.ContainsKey(ItemParams.ProtectableItem) ? (ProtectableItemBase)ProviderData[ItemParams.ProtectableItem] : null; AzureWorkloadProtectableItem protectableItem = ProviderData.ContainsKey(ItemParams.ProtectableItem) ? (AzureWorkloadProtectableItem)ProviderData[ItemParams.ProtectableItem] : null; ItemBase itemBase = ProviderData.ContainsKey(ItemParams.Item) ? (ItemBase)ProviderData[ItemParams.Item] : null; AzureWorkloadSQLDatabaseProtectedItem item = ProviderData.ContainsKey(ItemParams.Item) ? (AzureWorkloadSQLDatabaseProtectedItem)ProviderData[ItemParams.Item] : null; AzureVmWorkloadSQLDatabaseProtectedItem properties = new AzureVmWorkloadSQLDatabaseProtectedItem(); string containerUri = ""; string protectedItemUri = ""; if (disableWithRetentionData) { //Disable protection while retaining backup data ValidateAzureWorkloadDisableProtectionRequest(itemBase); Dictionary <UriEnums, string> keyValueDict = HelperUtils.ParseUri(item.Id); containerUri = HelperUtils.GetContainerUri(keyValueDict, item.Id); protectedItemUri = HelperUtils.GetProtectedItemUri(keyValueDict, item.Id); properties.PolicyId = string.Empty; properties.ProtectionState = ProtectionState.ProtectionStopped; properties.SourceResourceId = item.SourceResourceId; } else { if (protectableItem != null) { Dictionary <UriEnums, string> keyValueDict = HelperUtils.ParseUri(protectableItem.Id); containerUri = HelperUtils.GetContainerUri( keyValueDict, protectableItem.Id); protectedItemUri = HelperUtils.GetProtectableItemUri( keyValueDict, protectableItem.Id); properties.PolicyId = policy.Id; } else if (item != null) { Dictionary <UriEnums, string> keyValueDict = HelperUtils.ParseUri(item.Id); containerUri = HelperUtils.GetContainerUri( keyValueDict, item.Id); protectedItemUri = HelperUtils.GetProtectedItemUri( keyValueDict, item.Id); properties.PolicyId = policy.Id; } } ProtectedItemResource serviceClientRequest = new ProtectedItemResource() { Properties = properties }; return(ServiceClientAdapter.CreateOrUpdateProtectedItem( containerUri, protectedItemUri, serviceClientRequest, vaultName: vaultName, resourceGroupName: vaultResourceGroupName)); }
private RestAzureNS.AzureOperationResponse <ProtectedItemResource> EnableOrModifyProtection(bool disableWithRetentionData = false) { string vaultName = (string)ProviderData[VaultParams.VaultName]; string vaultResourceGroupName = (string)ProviderData[VaultParams.ResourceGroupName]; string azureFileShareName = ProviderData.ContainsKey(ItemParams.ItemName) ? (string)ProviderData[ItemParams.ItemName] : null; string storageAccountName = ProviderData.ContainsKey(ItemParams.StorageAccountName) ? (string)ProviderData[ItemParams.StorageAccountName] : null; string parameterSetName = ProviderData.ContainsKey(ItemParams.ParameterSetName) ? (string)ProviderData[ItemParams.ParameterSetName] : null; PolicyBase policy = ProviderData.ContainsKey(ItemParams.Policy) ? (PolicyBase)ProviderData[ItemParams.Policy] : null; ItemBase itemBase = (ItemBase)ProviderData[ItemParams.Item]; AzureFileShareItem item = (AzureFileShareItem)ProviderData[ItemParams.Item]; string containerUri = ""; string protectedItemUri = ""; string sourceResourceId = null; AzureFileshareProtectedItem properties = new AzureFileshareProtectedItem(); if (itemBase == null) { //Enable protection ValidateAzureFilesWorkloadType(policy.WorkloadType); ValidateFileShareEnableProtectionRequest( azureFileShareName, storageAccountName); WorkloadProtectableItemResource protectableObjectResource = GetAzureFileShareProtectableObject( azureFileShareName, storageAccountName, vaultName: vaultName, vaultResourceGroupName: vaultResourceGroupName); Dictionary <UriEnums, string> keyValueDict = HelperUtils.ParseUri(protectableObjectResource.Id); containerUri = HelperUtils.GetContainerUri( keyValueDict, protectableObjectResource.Id); protectedItemUri = HelperUtils.GetProtectableItemUri( keyValueDict, protectableObjectResource.Id); AzureFileShareProtectableItem azureFileShareProtectableItem = (AzureFileShareProtectableItem)protectableObjectResource.Properties; if (azureFileShareProtectableItem != null) { sourceResourceId = azureFileShareProtectableItem.ParentContainerFabricId; } // construct Service Client protectedItem request properties.PolicyId = policy.Id; properties.SourceResourceId = sourceResourceId; } else if (itemBase != null && !disableWithRetentionData) { //modify policy with new policy ValidateAzureFilesWorkloadType(item.WorkloadType, policy.WorkloadType); ValidateAzureFilesModifyProtectionRequest(itemBase, policy); 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 properties.PolicyId = policy.Id; properties.SourceResourceId = sourceResourceId; } else if (disableWithRetentionData) { //Disable protection while retaining backup data ValidateAzureFileShareDisableProtectionRequest(itemBase); Dictionary <UriEnums, string> keyValueDict = HelperUtils.ParseUri(item.Id); containerUri = HelperUtils.GetContainerUri(keyValueDict, item.Id); protectedItemUri = HelperUtils.GetProtectedItemUri(keyValueDict, item.Id); properties.PolicyId = string.Empty; properties.ProtectionState = ProtectionState.ProtectionStopped; properties.SourceResourceId = item.SourceResourceId; } ProtectedItemResource serviceClientRequest = new ProtectedItemResource() { Properties = properties }; return(ServiceClientAdapter.CreateOrUpdateProtectedItem( containerUri, protectedItemUri, serviceClientRequest, vaultName: vaultName, resourceGroupName: vaultResourceGroupName)); }
public override void ExecuteCmdlet() { ExecutionBlock(() => { base.ExecuteCmdlet(); ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId); string vaultName = resourceIdentifier.ResourceName; string resourceGroupName = resourceIdentifier.ResourceGroupName; string shouldProcessName = InputItem.Id; string itemType = ""; string itemName = ""; string containerUri = ""; if (ShouldProcess(shouldProcessName, VerbsLifecycle.Disable)) { Dictionary <UriEnums, string> keyValueDict = HelperUtils.ParseUri(InputItem.Id); itemType = HelperUtils.GetProtectableItemUri( keyValueDict, InputItem.Id).Split(';')[0]; itemName = HelperUtils.GetProtectableItemUri( keyValueDict, InputItem.Id).Split(';')[1]; containerUri = HelperUtils.GetContainerUri( keyValueDict, InputItem.Id); bool isDisableAutoProtectionSuccessful = false; try { ODataQuery <ServiceClientModel.ProtectionIntentQueryObject> queryParams = null; string backupManagementType = ServiceClientModel.BackupManagementType.AzureWorkload; queryParams = new ODataQuery <ServiceClientModel.ProtectionIntentQueryObject>( q => q.ItemType == itemType && q.ItemName == itemName && q.ParentName == containerUri && q.BackupManagementType == backupManagementType); var itemResponses = ServiceClientAdapter.ListProtectionIntent( queryParams, vaultName: vaultName, resourceGroupName: resourceGroupName); string intentName = null; foreach (var itemResponse in itemResponses) { string itemNameResponse = ""; string containerNameResponse = ""; Dictionary <UriEnums, string> keyValueDictResponse = HelperUtils.ParseUri(itemResponse.Properties.ItemId); itemNameResponse = HelperUtils.GetProtectableItemUri( keyValueDictResponse, itemResponse.Properties.ItemId).ToLower(); containerNameResponse = HelperUtils.GetContainerUri( keyValueDictResponse, itemResponse.Properties.ItemId); if (String.Compare(itemNameResponse, itemName, true) == 0 && String.Compare(containerUri.Split(';')[3], containerNameResponse.Split(';')[2], true) == 0) { intentName = itemResponse.Name; break; } } var deleteResponse = ServiceClientAdapter.DeleteProtectionIntent( intentName, vaultName: vaultName, resourceGroupName: resourceGroupName); isDisableAutoProtectionSuccessful = true; } catch { } if (PassThru.IsPresent) { WriteObject(isDisableAutoProtectionSuccessful); } } }); }