|
1 |
| -using System; |
| 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.Management.Automation; |
2 | 17 | using System.Collections.Generic;
|
| 18 | +using System.Xml; |
3 | 19 | using System.Linq;
|
4 |
| -using System.Text; |
5 |
| -using System.Threading.Tasks; |
| 20 | +using Microsoft.Azure.Management.BackupServices.Models; |
| 21 | +using MBS = Microsoft.Azure.Management.BackupServices; |
| 22 | +using System.Runtime.Serialization; |
| 23 | +using Microsoft.Azure.Management.BackupServices; |
6 | 24 |
|
7 |
| -namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets.DataSource |
| 25 | +namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets |
8 | 26 | {
|
9 |
| - class Enable_AzureBackupProtection |
| 27 | + // ToDo: |
| 28 | + // Correct the Commandlet |
| 29 | + // Correct the OperationResponse |
| 30 | + // Get Tracking API from Piyush and Get JobResponse |
| 31 | + // Get JobResponse Object from Aditya |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Enable Azure Backup protection |
| 35 | + /// </summary> |
| 36 | + [Cmdlet(VerbsLifecycle.Enable, "AzureBackupProtection"), OutputType(typeof(OperationResponse))] |
| 37 | + public class EnableAzureBackupProtection : AzureBackupItemCmdletBase |
10 | 38 | {
|
| 39 | + [Parameter(Position = 1, Mandatory = true, HelpMessage = AzureBackupCmdletHelpMessage.PolicyName)] |
| 40 | + [ValidateNotNullOrEmpty] |
| 41 | + public AzureBackupProtectionPolicy Policy { get; set; } |
| 42 | + public override void ExecuteCmdlet() |
| 43 | + { |
| 44 | + base.ExecuteCmdlet(); |
| 45 | + |
| 46 | + ExecutionBlock(() => |
| 47 | + { |
| 48 | + WriteVerbose("Making client call"); |
| 49 | + SetProtectionRequestInput input = new SetProtectionRequestInput(); |
| 50 | + input.PolicyId = Policy.InstanceId; |
| 51 | + if (Item.GetType() == ((AzureBackupItem)Item).GetType()) |
| 52 | + { |
| 53 | + input.ProtectableObjectType = (Item as AzureBackupItem).Type; |
| 54 | + input.ProtectableObjects.Add((Item as AzureBackupItem).Name); |
| 55 | + } |
| 56 | + else if (Item.GetType() == ((AzureBackupContainer)Item).GetType()) |
| 57 | + { |
| 58 | + if((Item as AzureBackupContainer).ContainerType == ContainerType.IaasVMContainer.ToString()) |
| 59 | + { |
| 60 | + input.ProtectableObjectType = DataSourceType.VM.ToString(); |
| 61 | + input.ProtectableObjects.Add((Item as AzureBackupContainer).ContainerUniqueName); |
| 62 | + } |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + throw new Exception("Uknown item type"); |
| 67 | + } |
| 68 | + |
| 69 | + var enableAzureBackupProtection = AzureBackupClient.DataSource.EnableProtectionAsync(GetCustomRequestHeaders(), input, CmdletCancellationToken).Result; |
| 70 | + |
| 71 | + WriteVerbose("Received policy response"); |
| 72 | + WriteVerbose("Converting response"); |
| 73 | + WriteAzureBackupProtectionPolicy(enableAzureBackupProtection); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + public void WriteAzureBackupProtectionPolicy(OperationResponse sourceOperationResponse) |
| 78 | + { |
| 79 | + // this needs to be uncommented once we have proper constructor |
| 80 | + //this.WriteObject(new AzureBackupRecoveryPoint(ResourceGroupName, ResourceName, sourceOperationResponse)); |
| 81 | + } |
| 82 | + |
| 83 | + public void WriteAzureBackupProtectionPolicy(IEnumerable<OperationResponse> sourceOperationResponseList) |
| 84 | + { |
| 85 | + List<OperationResponse> targetList = new List<OperationResponse>(); |
| 86 | + |
| 87 | + foreach (var sourceOperationResponse in sourceOperationResponseList) |
| 88 | + { |
| 89 | + // this needs to be uncommented once we have proper constructor |
| 90 | + targetList.Add(sourceOperationResponse); |
| 91 | + } |
| 92 | + |
| 93 | + this.WriteObject(targetList, true); |
| 94 | + } |
| 95 | + public enum ContainerType |
| 96 | + { |
| 97 | + [EnumMember] |
| 98 | + Invalid = 0, |
| 99 | + |
| 100 | + [EnumMember] |
| 101 | + Unknown, |
| 102 | + |
| 103 | + // used by fabric adapter to populate discovered VMs |
| 104 | + [EnumMember] |
| 105 | + IaasVMContainer, |
| 106 | + |
| 107 | + // used by fabric adapter to populate discovered services |
| 108 | + // VMs are child containers of services they belong to |
| 109 | + [EnumMember] |
| 110 | + IaasVMServiceContainer |
| 111 | + } |
| 112 | + public enum DataSourceType |
| 113 | + { |
| 114 | + [EnumMember] |
| 115 | + Invalid = 0, |
| 116 | + |
| 117 | + [EnumMember] |
| 118 | + VM |
| 119 | + |
| 120 | + // TODO: fix GetJobTypes() in PortalMetadataInternalEP.cs |
| 121 | + // [EnumMember] |
| 122 | + // AzureStorageAccount |
| 123 | + } |
11 | 124 | }
|
12 | 125 | }
|
0 commit comments