|
| 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; |
| 17 | +using Microsoft.Azure; |
| 18 | +using Microsoft.WindowsAzure.Commands.ServiceManagement.Model; |
| 19 | +using Microsoft.WindowsAzure.Commands.Utilities.Common; |
| 20 | +using Microsoft.WindowsAzure.Management.Compute; |
| 21 | +using Microsoft.WindowsAzure.Management.Compute.Models; |
| 22 | + |
| 23 | +namespace Microsoft.WindowsAzure.Commands.ServiceManagement.HostedServices |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// Migrate ASM deployment to ARM |
| 27 | + /// </summary> |
| 28 | + [Cmdlet(VerbsCommon.Move, "AzureService"), OutputType(typeof(OperationStatusResponse))] |
| 29 | + public class MoveAzureServiceCommand : ServiceManagementBaseCmdlet |
| 30 | + { |
| 31 | + private const string AbortParameterSetName = "AbortMigrationParameterSet"; |
| 32 | + private const string CommitParameterSetName = "CommitMigrationParameterSet"; |
| 33 | + private const string PrepareDefaultParameterSetName = "PrepareDefaultMigrationParameterSet"; |
| 34 | + private const string PrepareNewParameterSetName = "PrepareNewMigrationParameterSet"; |
| 35 | + private const string PrepareExistingParameterSetName = "PrepareExistingMigrationParameterSet"; |
| 36 | + |
| 37 | + private string DestinationVNetType; |
| 38 | + |
| 39 | + [Parameter( |
| 40 | + Position =0, |
| 41 | + Mandatory = true, |
| 42 | + ParameterSetName = AbortParameterSetName, |
| 43 | + HelpMessage = "Abort migration")] |
| 44 | + public SwitchParameter Abort |
| 45 | + { |
| 46 | + get; |
| 47 | + set; |
| 48 | + } |
| 49 | + |
| 50 | + [Parameter( |
| 51 | + Position = 0, |
| 52 | + Mandatory = true, |
| 53 | + ParameterSetName = CommitParameterSetName, |
| 54 | + HelpMessage = "Commit migration")] |
| 55 | + public SwitchParameter Commit |
| 56 | + { |
| 57 | + get; |
| 58 | + set; |
| 59 | + } |
| 60 | + |
| 61 | + [Parameter( |
| 62 | + Position = 0, |
| 63 | + Mandatory = true, |
| 64 | + ParameterSetName = PrepareDefaultParameterSetName, |
| 65 | + HelpMessage = "Prepare migration")] |
| 66 | + public SwitchParameter PrepareDefault |
| 67 | + { |
| 68 | + get; |
| 69 | + set; |
| 70 | + } |
| 71 | + |
| 72 | + [Parameter( |
| 73 | + Position = 0, |
| 74 | + Mandatory = true, |
| 75 | + ParameterSetName = PrepareNewParameterSetName, |
| 76 | + HelpMessage = "Prepare migration")] |
| 77 | + public SwitchParameter PrepareNew |
| 78 | + { |
| 79 | + get; |
| 80 | + set; |
| 81 | + } |
| 82 | + |
| 83 | + [Parameter( |
| 84 | + Position = 0, |
| 85 | + Mandatory = true, |
| 86 | + ParameterSetName = PrepareExistingParameterSetName, |
| 87 | + HelpMessage = "Prepare migration")] |
| 88 | + public SwitchParameter PrepareExistingDestinationVNet |
| 89 | + { |
| 90 | + get; |
| 91 | + set; |
| 92 | + } |
| 93 | + |
| 94 | + [Parameter( |
| 95 | + Position = 1, |
| 96 | + Mandatory = true, |
| 97 | + ValueFromPipelineByPropertyName = true, |
| 98 | + HelpMessage = "Service name to be migrated")] |
| 99 | + [ValidateNotNullOrEmpty] |
| 100 | + public string ServiceName |
| 101 | + { |
| 102 | + get; |
| 103 | + set; |
| 104 | + } |
| 105 | + |
| 106 | + [Parameter( |
| 107 | + Position = 2, |
| 108 | + Mandatory = true, |
| 109 | + HelpMessage = "Deployment name to be migrated")] |
| 110 | + [ValidateNotNullOrEmpty] |
| 111 | + public string DeploymentName |
| 112 | + { |
| 113 | + get; |
| 114 | + set; |
| 115 | + } |
| 116 | + |
| 117 | + [Parameter( |
| 118 | + Position = 3, |
| 119 | + Mandatory = true, |
| 120 | + ParameterSetName = PrepareExistingParameterSetName, |
| 121 | + HelpMessage = "Resource group name for migration")] |
| 122 | + [ValidateNotNullOrEmpty] |
| 123 | + public string ResourceGroupName |
| 124 | + { |
| 125 | + get; |
| 126 | + set; |
| 127 | + } |
| 128 | + |
| 129 | + [Parameter( |
| 130 | + Position = 4, |
| 131 | + Mandatory = true, |
| 132 | + ParameterSetName = PrepareExistingParameterSetName, |
| 133 | + HelpMessage = "Virtual network name for migration")] |
| 134 | + [ValidateNotNullOrEmpty] |
| 135 | + public string VirtualNetworkName |
| 136 | + { |
| 137 | + get; |
| 138 | + set; |
| 139 | + } |
| 140 | + |
| 141 | + [Parameter( |
| 142 | + Position = 5, |
| 143 | + Mandatory = true, |
| 144 | + ParameterSetName = PrepareExistingParameterSetName, |
| 145 | + HelpMessage = "Subnet Name for migration")] |
| 146 | + [ValidateNotNullOrEmpty] |
| 147 | + public string SubnetName |
| 148 | + { |
| 149 | + get; |
| 150 | + set; |
| 151 | + } |
| 152 | + |
| 153 | + protected override void OnProcessRecord() |
| 154 | + { |
| 155 | + ServiceManagementProfile.Initialize(); |
| 156 | + |
| 157 | + if (this.Abort.IsPresent) |
| 158 | + { |
| 159 | + ExecuteClientActionNewSM( |
| 160 | + null, |
| 161 | + CommandRuntime.ToString(), |
| 162 | + () => this.ComputeClient.Deployments.AbortMigration(this.ServiceName, DeploymentName)); |
| 163 | + } |
| 164 | + else if (this.Commit.IsPresent) |
| 165 | + { |
| 166 | + ExecuteClientActionNewSM( |
| 167 | + null, |
| 168 | + CommandRuntime.ToString(), |
| 169 | + () => this.ComputeClient.Deployments.CommitMigration(this.ServiceName, DeploymentName)); |
| 170 | + } |
| 171 | + else |
| 172 | + { |
| 173 | + if (this.PrepareDefault.IsPresent) |
| 174 | + { |
| 175 | + DestinationVNetType = DestinationVirtualNetwork.Default; |
| 176 | + } |
| 177 | + else if (this.PrepareNew.IsPresent) |
| 178 | + { |
| 179 | + DestinationVNetType = DestinationVirtualNetwork.New; |
| 180 | + } |
| 181 | + else |
| 182 | + { |
| 183 | + DestinationVNetType = DestinationVirtualNetwork.Existing; |
| 184 | + } |
| 185 | + |
| 186 | + var parameter = (this.ParameterSetName == PrepareExistingParameterSetName) |
| 187 | + ? new PrepareDeploymentMigrationParameters |
| 188 | + { |
| 189 | + DestinationVirtualNetwork = this.DestinationVNetType, |
| 190 | + ResourceGroupName = this.ResourceGroupName, |
| 191 | + SubNetName = this.SubnetName, |
| 192 | + VirtualNetworkName = this.VirtualNetworkName |
| 193 | + } |
| 194 | + : new PrepareDeploymentMigrationParameters |
| 195 | + { |
| 196 | + DestinationVirtualNetwork = this.DestinationVNetType, |
| 197 | + ResourceGroupName = string.Empty, |
| 198 | + SubNetName = string.Empty, |
| 199 | + VirtualNetworkName = string.Empty |
| 200 | + }; |
| 201 | + |
| 202 | + ExecuteClientActionNewSM( |
| 203 | + null, |
| 204 | + CommandRuntime.ToString(), |
| 205 | + () => this.ComputeClient.Deployments.PrepareMigration(this.ServiceName, DeploymentName, parameter)); |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments