From 18606b08635e2cb3d36e889dc47c297a626d6568 Mon Sep 17 00:00:00 2001 From: Hyonho Lee Date: Mon, 21 Sep 2015 11:21:27 -0700 Subject: [PATCH 1/9] Adding format file to PS auto generator. --- .../Commands.Compute/Commands.Compute.csproj | 3 + ...Compute.Automation.format.generated.ps1xml | 245 ++++++++++++++++++ .../AzureResourceManager.psd1 | 1 + .../Automation/RunCodeGeneration.ps1 | 146 +++++++++++ 4 files changed, 395 insertions(+) create mode 100644 src/ResourceManager/Compute/Commands.Compute/Microsoft.Azure.Commands.Compute.Automation.format.generated.ps1xml diff --git a/src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj b/src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj index 259397014261..3b818fbe0c9d 100644 --- a/src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj +++ b/src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj @@ -360,6 +360,9 @@ + + Always + Always diff --git a/src/ResourceManager/Compute/Commands.Compute/Microsoft.Azure.Commands.Compute.Automation.format.generated.ps1xml b/src/ResourceManager/Compute/Commands.Compute/Microsoft.Azure.Commands.Compute.Automation.format.generated.ps1xml new file mode 100644 index 000000000000..28ff27a9dc25 --- /dev/null +++ b/src/ResourceManager/Compute/Commands.Compute/Microsoft.Azure.Commands.Compute.Automation.format.generated.ps1xml @@ -0,0 +1,245 @@ + + + + + + Microsoft.Azure.Management.Compute.Models.DeleteOperationResponse + + Microsoft.Azure.Management.Compute.Models.DeleteOperationResponse + + + + + + + + EndTime + + + + [Newtonsoft.Json.JsonConvert]::SerializeObject($_.Error, [Newtonsoft.Json.Formatting]::Indented) + + + + StartTime + + + + [Newtonsoft.Json.JsonConvert]::SerializeObject($_.Status, [Newtonsoft.Json.Formatting]::Indented) + + + + TrackingOperationId + + + + AzureAsyncOperation + + + + RequestId + + + + StatusCode + + + + + + + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetGetResponse + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetGetResponse + + + + + + + + [Newtonsoft.Json.JsonConvert]::SerializeObject($_.VirtualMachineScaleSet, [Newtonsoft.Json.Formatting]::Indented) + + + + RequestId + + + + StatusCode + + + + + + + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetListResponse + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetListResponse + + + + + + + + NextLink + + + + if ($_.VirtualMachineScaleSets -eq $null) { 0 } else { $_.VirtualMachineScaleSets.Count } + + + + foreach ($item in $_.VirtualMachineScaleSets) { [Newtonsoft.Json.JsonConvert]::SerializeObject($item, [Newtonsoft.Json.Formatting]::Indented) } + + + + RequestId + + + + StatusCode + + + + + + + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetListSkusResponse + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetListSkusResponse + + + + + + + + if ($_.VirtualMachineScaleSetAvailableSkus -eq $null) { 0 } else { $_.VirtualMachineScaleSetAvailableSkus.Count } + + + + foreach ($item in $_.VirtualMachineScaleSetAvailableSkus) { [Newtonsoft.Json.JsonConvert]::SerializeObject($item, [Newtonsoft.Json.Formatting]::Indented) } + + + + RequestId + + + + StatusCode + + + + + + + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetVMGetResponse + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetVMGetResponse + + + + + + + + [Newtonsoft.Json.JsonConvert]::SerializeObject($_.VirtualMachineScaleSetVM, [Newtonsoft.Json.Formatting]::Indented) + + + + RequestId + + + + StatusCode + + + + + + + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetVMGetInstanceViewResponse + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetVMGetInstanceViewResponse + + + + + + + + [Newtonsoft.Json.JsonConvert]::SerializeObject($_.VirtualMachineScaleSetVMInstanceView, [Newtonsoft.Json.Formatting]::Indented) + + + + RequestId + + + + StatusCode + + + + + + + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetVMListResponse + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetVMListResponse + + + + + + + + if ($_.VirtualMachineScaleSetVMs -eq $null) { 0 } else { $_.VirtualMachineScaleSetVMs.Count } + + + + foreach ($item in $_.VirtualMachineScaleSetVMs) { [Newtonsoft.Json.JsonConvert]::SerializeObject($item, [Newtonsoft.Json.Formatting]::Indented) } + + + + RequestId + + + + StatusCode + + + + + + + + diff --git a/src/ResourceManager/Resources/Commands.Resources/AzureResourceManager.psd1 b/src/ResourceManager/Resources/Commands.Resources/AzureResourceManager.psd1 index a8b9f265813a..5c5b4b471b0b 100644 --- a/src/ResourceManager/Resources/Commands.Resources/AzureResourceManager.psd1 +++ b/src/ResourceManager/Resources/Commands.Resources/AzureResourceManager.psd1 @@ -71,6 +71,7 @@ FormatsToProcess = @( '.\StreamAnalytics\Microsoft.Azure.Commands.StreamAnalytics.format.ps1xml', '.\Compute\Microsoft.Azure.Commands.Compute.format.ps1xml', '.\Compute\Microsoft.Azure.Commands.Compute.format.generated.ps1xml', + '.\Compute\Microsoft.Azure.Commands.Compute.Automation.format.generated.ps1xml', '.\Network\Microsoft.Azure.Commands.Network.format.ps1xml', '.\Storage\Microsoft.WindowsAzure.Commands.Storage.format.ps1xml', '.\StorageManagement\Microsoft.Azure.Commands.Management.Storage.format.ps1xml', diff --git a/src/ServiceManagement/Compute/Commands.ServiceManagement.Preview/Automation/RunCodeGeneration.ps1 b/src/ServiceManagement/Compute/Commands.ServiceManagement.Preview/Automation/RunCodeGeneration.ps1 index 27f69e51cdd5..56d28cd4de65 100644 --- a/src/ServiceManagement/Compute/Commands.ServiceManagement.Preview/Automation/RunCodeGeneration.ps1 +++ b/src/ServiceManagement/Compute/Commands.ServiceManagement.Preview/Automation/RunCodeGeneration.ps1 @@ -1683,6 +1683,118 @@ function Get-SubComplexParameterList return Get-SubComplexParameterListFromType $param_info.ParameterType $client_name_space; } +# Get proper type name +function Get-ProperTypeName +{ + param([System.Type] $itemType) + + if ($itemType.IsGenericType -and ($itemType.Name.StartsWith('IList') -or $itemType.Name.StartsWith('List'))) + { + $typeStr = 'IList<' + $itemType.GenericTypeArguments[0].Name + '>'; + } + elseif ($itemType.IsGenericType -and ($itemType.Name.StartsWith('IDictionary') -or $itemType.Name.StartsWith('Dictionary'))) + { + $typeStr = 'IDictionary<' + $itemType.GenericTypeArguments[0].Name + ',' + $itemType.GenericTypeArguments[1].Name + '>'; + } + elseif ($itemType.IsGenericType -and $itemType.Name.StartsWith('Nullable')) + { + $typeStr = $itemType.GenericTypeArguments[0].Name + '?'; + } + else + { + $typeStr = $itemType.Name; + } + + $typeStr = $typeStr.Replace("System.String", "string"); + $typeStr = $typeStr.Replace("String", "string"); + $typeStr = $typeStr.Replace("System.Boolean", "bool"); + $typeStr = $typeStr.Replace("Boolean", "bool"); + $typeStr = $typeStr.Replace("System.UInt32", "uint"); + $typeStr = $typeStr.Replace("UInt32", "uint"); + $typeStr = $typeStr.Replace("System.Int32", "int"); + $typeStr = $typeStr.Replace("Int32", "int"); + + return $typeStr; +} + +# Process the return type +function Process-ReturnType +{ + param([Type] $rt) + + $result = $null; + + if ($rt -eq $null) + { + return $result; + } + + + if ($rt.Name -like '*LongRunning*' -or $rt.Name -like '*computeoperationresponse*' -or $rt.Name -like '*AzureOperationResponse*') + { + return $result; + } + + $xml = '' + $rt.FullName + ''; + $xml += '' + $rt.FullName + '' + [System.Environment]::NewLine; + $xml += '' + [System.Environment]::NewLine; + + $props = $rt.GetProperties([System.Reflection.BindingFlags]::Public -bor [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::Static); + + foreach ($pr1 in $props) + { + $typeStr = Get-ProperTypeName $pr1.PropertyType; + $itemLabel = $itemName = $pr1.Name; + + if ($typeStr -eq 'string' ` + -or $typeStr -eq 'string[]' ` + -or $typeStr -eq 'uint' ` + -or $typeStr -eq 'uint?' ` + -or $typeStr -eq 'int' ` + -or $typeStr -eq 'int?' ` + -or $typeStr -eq 'bool' ` + -or $typeStr -eq 'bool?' ` + -or $typeStr -eq 'DateTime' ` + -or $typeStr -eq 'DateTime?' ` + -or $typeStr -eq 'DateTimeOffset' ` + -or $typeStr -eq 'DateTimeOffset?' ` + -or $typeStr -eq 'HttpStatusCode' ) + { + $xml += "${itemName}" + [System.Environment]::NewLine; + } + elseif ($typeStr.StartsWith('IList') ` + -or $typeStr.StartsWith('IDictionary')) + { + $xml += " if (" + "$" + "_.${itemName} -eq $" + "null) { 0 } else { $" + "_.${itemName}.Count }" + [System.Environment]::NewLine; + $xml += " foreach ($" + "item in $" + "_.${itemName}) { [Newtonsoft.Json.JsonConvert]::SerializeObject(" + "$" + "item, [Newtonsoft.Json.Formatting]::Indented) } " + [System.Environment]::NewLine; + } + else + { + $xml += "[Newtonsoft.Json.JsonConvert]::SerializeObject(" + "$" + "_." + ${itemName} + ", [Newtonsoft.Json.Formatting]::Indented)" + [System.Environment]::NewLine; + } + } + + $xml += '' + [System.Environment]::NewLine; + $xml = '' + [System.Environment]::NewLine + $xml + '' + [System.Environment]::NewLine; + + Write-Verbose ("Xml: " + $xml); + + return $xml; +} + +# Get proper type name +function Format-XML ([xml]$xml, $indent = 2) +{ + $StringWriter = New-Object System.IO.StringWriter; + $XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter; + $xmlWriter.Formatting = "indented"; + $xmlWriter.Indentation = $Indent; + $xml.WriteContentTo($XmlWriter); + $XmlWriter.Flush(); + $StringWriter.Flush(); + Write-Output $StringWriter.ToString(); +} + # Sample: NewAzureVirtualMachineCreateParameters.cs function Write-ParameterCmdletFile { @@ -1839,6 +1951,8 @@ else $dynamic_param_method_code = @(); $invoke_cmdlet_method_code = @(); $parameter_cmdlet_method_code = @(); + $all_return_type_names = @(); + $formatXml = ""; # Write Operation Cmdlet Files foreach ($ft in $filtered_types) @@ -1902,6 +2016,18 @@ else } } } + + $rt = $mt.ReturnType.GenericTypeArguments[0]; + + if ($all_return_type_names.Contains($rt.Name)) + { + Write-Verbose("Already Exists: " + $rt.Name); + } + else + { + $all_return_type_names += $rt.Name; + $formatXml += Process-ReturnType -rt $mt.ReturnType.GenericTypeArguments[0]; + } } Write-InvokeCmdletFile $invoke_cmdlet_file_name $invoke_cmdlet_class_name $auto_base_cmdlet_name $clientClassType $filtered_types $invoke_cmdlet_method_code $dynamic_param_method_code; @@ -1912,4 +2038,24 @@ else Write-Output "============================================="; Write-Output "Finished."; Write-Output "============================================="; + + $outFolder = $outFolder.Replace('/Generated', ""); + $xmlFilePath = ($outFolder + '\' + $code_common_namespace + '.format.generated.ps1xml'); + + Write-Host ('Writing XML Format File: ' + $xmlFilePath); + + $xmlCommentHeader = '' + [System.Environment]::NewLine; + + $xmlContent = [xml]($xmlCommentHeader + '' + [System.Environment]::NewLine + $formatXml + '' + [System.Environment]::NewLine); + $node = $xmlContent.CreateXmlDeclaration('1.0', 'UTF-8', $null); + $xmlContent.InsertBefore($node, $xmlContent.ChildNodes[0]); + + $formattedXmlContent = Format-XML $xmlContent.OuterXml; + Set-Content -Force -Path $xmlFilePath -Value $formattedXmlContent; + Write-Verbose($formattedXmlContent); } From e7865164e5bfb57589a8d0cd81ffc141e650aaa5 Mon Sep 17 00:00:00 2001 From: Hyonho Lee Date: Mon, 21 Sep 2015 14:21:17 -0700 Subject: [PATCH 2/9] Add tests for format files. --- .../VirtualMachineScaleSetTests.ps1 | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.ps1 b/src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.ps1 index c54fc51b9b24..d33a05baba09 100644 --- a/src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.ps1 +++ b/src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.ps1 @@ -120,34 +120,64 @@ function Test-VirtualMachineScaleSet $vmss.VirtualMachineProfile.StorageProfile.OSDisk.VirtualHardDiskContainers.Add($vhdContainer); $st = New-AzureVirtualMachineScaleSet -ResourceGroupName $rgname -VirtualMachineScaleSetCreateOrUpdateParameters $vmss; + + Write-Verbose ('Running Command : ' + 'Get-AzureVirtualMachineScaleSet'); $vmssResult = Get-AzureVirtualMachineScaleSet -ResourceGroupName $rgname -VMScaleSetName $vmss.Name; Assert-True { $vmss.Name -eq $vmssResult.VirtualMachineScaleSet.Name }; + $output = $vmssResult | Out-String; + Assert-True { $output.Contains("VirtualMachineScaleSet") }; + Write-Verbose ($output); # List All + Write-Verbose ('Running Command : ' + 'Get-AzureVirtualMachineScaleSetAllList'); $vmssList = Get-AzureVirtualMachineScaleSetAllList -VirtualMachineScaleSetListAllParameters $null; Assert-True { ($vmssList.VirtualMachineScaleSets | select -ExpandProperty Name) -contains $vmss.Name }; + $output = $vmssList | Out-String; + Assert-True { $output.Contains("VirtualMachineScaleSets") }; + Write-Verbose ($output); # List from RG + Write-Verbose ('Running Command : ' + 'Get-AzureVirtualMachineScaleSetList'); $vmssList = Get-AzureVirtualMachineScaleSetList -ResourceGroupName $rgname; Assert-True { ($vmssList.VirtualMachineScaleSets | select -ExpandProperty Name) -contains $vmss.Name }; + $output = $vmssList | Out-String; + Assert-True { $output.Contains("VirtualMachineScaleSets") }; + Write-Verbose ($output); # List Skus + Write-Verbose ('Running Command : ' + 'Get-AzureVirtualMachineScaleSetSkusList'); $skuList = Get-AzureVirtualMachineScaleSetSkusList -ResourceGroupName $rgname -VMScaleSetName $vmss.Name; + $output = $skuList | Out-String; + Write-Verbose ($output); # List All VMs $vmListParams = New-AzureComputeParameterObject -FriendlyName VirtualMachineScaleSetVMListParameters; $vmListParams.ResourceGroupName = $rgname; $vmListParams.VirtualMachineScaleSetName = $vmss.Name; + + Write-Verbose ('Running Command : ' + 'Get-AzureVirtualMachineScaleSetVMList'); $vmListResult = Get-AzureVirtualMachineScaleSetVMList -VirtualMachineScaleSetVMListParameters $vmListParams; + $output = $vmListResult | Out-String; + Write-Verbose ($output); + $vmList = $vmListResult.VirtualMachineScaleSetVMs; # List each VM for ($i = 0; $i -lt $vmList.Count; $i++) { + Write-Verbose ('Running Command : ' + 'Get-AzureVirtualMachineScaleSetVM'); $vm = Get-AzureVirtualMachineScaleSetVM -ResourceGroupName $rgname -VMScaleSetName $vmss.Name -InstanceId $i; Assert-NotNull $vm.VirtualMachineScaleSetVM; + $output = $vm | Out-String; + Assert-True { $output.Contains("VirtualMachineScaleSetVM") }; + Write-Verbose ($output); + + Write-Verbose ('Running Command : ' + 'Get-AzureVirtualMachineScaleSetVMInstanceView'); $vmInstance = Get-AzureVirtualMachineScaleSetVMInstanceView -ResourceGroupName $rgname -VMScaleSetName $vmss.Name -InstanceId $i; Assert-NotNull $vmInstance.VirtualMachineScaleSetVMInstanceView; + $output = $vmInstance | Out-String; + Assert-True { $output.Contains("VirtualMachineScaleSetVMInstanceView") }; + Write-Verbose($output); } # List Next (negative test) From c3482f5437a9de1b43002b486d897635cae8e391 Mon Sep 17 00:00:00 2001 From: Hyonho Lee Date: Mon, 21 Sep 2015 16:08:37 -0700 Subject: [PATCH 3/9] Fix format file generator and tests --- .../VirtualMachineScaleSetTests.ps1 | 2 + ...Compute.Automation.format.generated.ps1xml | 54 +++++++++++++++++++ .../Automation/RunCodeGeneration.ps1 | 39 ++++++++++++++ 3 files changed, 95 insertions(+) diff --git a/src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.ps1 b/src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.ps1 index d33a05baba09..9e2a6d5b9618 100644 --- a/src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.ps1 +++ b/src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/VirtualMachineScaleSetTests.ps1 @@ -148,6 +148,7 @@ function Test-VirtualMachineScaleSet Write-Verbose ('Running Command : ' + 'Get-AzureVirtualMachineScaleSetSkusList'); $skuList = Get-AzureVirtualMachineScaleSetSkusList -ResourceGroupName $rgname -VMScaleSetName $vmss.Name; $output = $skuList | Out-String; + Assert-True { $output.Contains("VirtualMachineScaleSetSku") }; Write-Verbose ($output); # List All VMs @@ -158,6 +159,7 @@ function Test-VirtualMachineScaleSet Write-Verbose ('Running Command : ' + 'Get-AzureVirtualMachineScaleSetVMList'); $vmListResult = Get-AzureVirtualMachineScaleSetVMList -VirtualMachineScaleSetVMListParameters $vmListParams; $output = $vmListResult | Out-String; + Assert-True { $output.Contains("VirtualMachineScaleSetVM") }; Write-Verbose ($output); $vmList = $vmListResult.VirtualMachineScaleSetVMs; diff --git a/src/ResourceManager/Compute/Commands.Compute/Microsoft.Azure.Commands.Compute.Automation.format.generated.ps1xml b/src/ResourceManager/Compute/Commands.Compute/Microsoft.Azure.Commands.Compute.Automation.format.generated.ps1xml index 28ff27a9dc25..b8dd744e83f2 100644 --- a/src/ResourceManager/Compute/Commands.Compute/Microsoft.Azure.Commands.Compute.Automation.format.generated.ps1xml +++ b/src/ResourceManager/Compute/Commands.Compute/Microsoft.Azure.Commands.Compute.Automation.format.generated.ps1xml @@ -129,6 +129,24 @@ + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSet + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSet + + + + + + + + [Newtonsoft.Json.JsonConvert]::SerializeObject($_, [Newtonsoft.Json.Formatting]::Indented) + + + + + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetListSkusResponse @@ -159,6 +177,24 @@ + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetSku + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetSku + + + + + + + + [Newtonsoft.Json.JsonConvert]::SerializeObject($_, [Newtonsoft.Json.Formatting]::Indented) + + + + + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetVMGetResponse @@ -241,5 +277,23 @@ + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetVM + + Microsoft.Azure.Management.Compute.Models.VirtualMachineScaleSetVM + + + + + + + + [Newtonsoft.Json.JsonConvert]::SerializeObject($_, [Newtonsoft.Json.Formatting]::Indented) + + + + + + diff --git a/src/ServiceManagement/Compute/Commands.ServiceManagement.Preview/Automation/RunCodeGeneration.ps1 b/src/ServiceManagement/Compute/Commands.ServiceManagement.Preview/Automation/RunCodeGeneration.ps1 index eb90d224bf93..66e16ca742f4 100644 --- a/src/ServiceManagement/Compute/Commands.ServiceManagement.Preview/Automation/RunCodeGeneration.ps1 +++ b/src/ServiceManagement/Compute/Commands.ServiceManagement.Preview/Automation/RunCodeGeneration.ps1 @@ -1788,6 +1788,33 @@ function Get-ProperTypeName return $typeStr; } + +# Process the list return type +function Process-ListType +{ + param([Type] $rt, [System.String] $name) + + $result = $null; + + if ($rt -eq $null) + { + return $result; + } + + $xml = '' + $rt.FullName + ''; + $xml += '' + $rt.FullName + '' + [System.Environment]::NewLine; + $xml += '' + [System.Environment]::NewLine; + + $itemLabel = $itemName = $rt.Name; + $xml += "[Newtonsoft.Json.JsonConvert]::SerializeObject(" + "$" + "_, [Newtonsoft.Json.Formatting]::Indented)" + [System.Environment]::NewLine; + $xml += '' + [System.Environment]::NewLine; + $xml = '' + [System.Environment]::NewLine + $xml + '' + [System.Environment]::NewLine; + + Write-Verbose ("Xml: " + $xml); + + return $xml; +} + # Process the return type function Process-ReturnType { @@ -1836,6 +1863,13 @@ function Process-ReturnType elseif ($typeStr.StartsWith('IList') ` -or $typeStr.StartsWith('IDictionary')) { + $elementType = $pr1.PropertyType.GenericTypeArguments[0]; + + if (-not $elementType.FullName.Contains("String")) + { + $addxml = Process-ListType -rt $pr1.PropertyType.GenericTypeArguments[0] -name ${itemName}; + } + $xml += " if (" + "$" + "_.${itemName} -eq $" + "null) { 0 } else { $" + "_.${itemName}.Count }" + [System.Environment]::NewLine; $xml += " foreach ($" + "item in $" + "_.${itemName}) { [Newtonsoft.Json.JsonConvert]::SerializeObject(" + "$" + "item, [Newtonsoft.Json.Formatting]::Indented) } " + [System.Environment]::NewLine; } @@ -1848,6 +1882,11 @@ function Process-ReturnType $xml += '' + [System.Environment]::NewLine; $xml = '' + [System.Environment]::NewLine + $xml + '' + [System.Environment]::NewLine; + if (-not [System.String]::IsNullOrEmpty($addxml)) + { + $xml += $addxml; + } + Write-Verbose ("Xml: " + $xml); return $xml; From 1cfc10773f88004b9f2b466ea73ebbc3e7067040 Mon Sep 17 00:00:00 2001 From: Hyonho Lee Date: Mon, 21 Sep 2015 16:41:04 -0700 Subject: [PATCH 4/9] Dev code for Save-AzureRMVhd cmdlet --- .../Commands.Compute/Commands.Compute.csproj | 3 + .../Models/VhdDownloadContext.cs | 25 +++ .../Models/VhdDownloaderModel.cs | 40 +++++ .../StorageServices/AddAzureVhdCommand.cs | 4 +- .../StorageServices/SaveAzureVhdCommand.cs | 144 ++++++++++++++++++ 5 files changed, 214 insertions(+), 2 deletions(-) create mode 100644 src/ResourceManager/Compute/Commands.Compute/Models/VhdDownloadContext.cs create mode 100644 src/ResourceManager/Compute/Commands.Compute/Models/VhdDownloaderModel.cs create mode 100644 src/ResourceManager/Compute/Commands.Compute/StorageServices/SaveAzureVhdCommand.cs diff --git a/src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj b/src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj index 78e5da5cee9a..5f0b4dbdd78c 100644 --- a/src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj +++ b/src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj @@ -264,12 +264,15 @@ + + + diff --git a/src/ResourceManager/Compute/Commands.Compute/Models/VhdDownloadContext.cs b/src/ResourceManager/Compute/Commands.Compute/Models/VhdDownloadContext.cs new file mode 100644 index 000000000000..6b460684400e --- /dev/null +++ b/src/ResourceManager/Compute/Commands.Compute/Models/VhdDownloadContext.cs @@ -0,0 +1,25 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using System; +using System.IO; + +namespace Microsoft.Azure.Commands.Compute.Models +{ + public class VhdDownloadContext + { + public FileInfo LocalFilePath { get; set; } + public Uri Source { get; set; } + } +} diff --git a/src/ResourceManager/Compute/Commands.Compute/Models/VhdDownloaderModel.cs b/src/ResourceManager/Compute/Commands.Compute/Models/VhdDownloaderModel.cs new file mode 100644 index 000000000000..5ab1443c3087 --- /dev/null +++ b/src/ResourceManager/Compute/Commands.Compute/Models/VhdDownloaderModel.cs @@ -0,0 +1,40 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using Microsoft.WindowsAzure.Commands.Sync; +using Microsoft.WindowsAzure.Commands.Sync.Download; +using System.IO; + +namespace Microsoft.Azure.Commands.Compute.Models +{ + public class VhdDownloaderModel + { + public static VhdDownloadContext Download(DownloaderParameters downloadParameters, ComputeClientBaseCmdlet cmdlet) + { + Program.SyncOutput = new PSSyncOutputEvents(cmdlet); + + downloadParameters.ProgressDownloadComplete = Program.SyncOutput.ProgressDownloadComplete; + downloadParameters.ProgressDownloadStatus = Program.SyncOutput.ProgressDownloadStatus; + + var downloader = new Downloader(downloadParameters); + downloader.Download(); + + return new VhdDownloadContext + { + LocalFilePath = new FileInfo(downloadParameters.LocalFilePath), + Source = downloadParameters.BlobUri.Uri + }; + } + } +} diff --git a/src/ResourceManager/Compute/Commands.Compute/StorageServices/AddAzureVhdCommand.cs b/src/ResourceManager/Compute/Commands.Compute/StorageServices/AddAzureVhdCommand.cs index 77d355dc62c7..c01f610f2236 100644 --- a/src/ResourceManager/Compute/Commands.Compute/StorageServices/AddAzureVhdCommand.cs +++ b/src/ResourceManager/Compute/Commands.Compute/StorageServices/AddAzureVhdCommand.cs @@ -131,7 +131,7 @@ public UploadParameters ValidateParameters() } } - var storageCredentialsFactory = CreateStorageCredentialsFactory(destinationUri); + var storageCredentialsFactory = CreateStorageCredentialsFactory(); PathIntrinsics currentPath = SessionState.Path; var filePath = new FileInfo(currentPath.GetUnresolvedProviderPathFromPSPath(LocalFilePath.ToString())); @@ -147,7 +147,7 @@ public UploadParameters ValidateParameters() return parameters; } - private StorageCredentialsFactory CreateStorageCredentialsFactory(BlobUri destinationUri) + private StorageCredentialsFactory CreateStorageCredentialsFactory() { StorageCredentialsFactory storageCredentialsFactory; diff --git a/src/ResourceManager/Compute/Commands.Compute/StorageServices/SaveAzureVhdCommand.cs b/src/ResourceManager/Compute/Commands.Compute/StorageServices/SaveAzureVhdCommand.cs new file mode 100644 index 000000000000..87fd946dc2ea --- /dev/null +++ b/src/ResourceManager/Compute/Commands.Compute/StorageServices/SaveAzureVhdCommand.cs @@ -0,0 +1,144 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +using Microsoft.Azure.Commands.Compute.Common; +using Microsoft.Azure.Commands.Compute.Models; +using Microsoft.Azure.Common.Authentication; +using Microsoft.Azure.Common.Authentication.Models; +using Microsoft.Azure.Management.Storage; +using Microsoft.WindowsAzure.Commands.Sync.Download; +using System; +using System.IO; +using System.Management.Automation; + +namespace Microsoft.Azure.Commands.Compute.StorageServices +{ + [Cmdlet(VerbsData.Save, ProfileNouns.Vhd), OutputType(typeof(VhdDownloadContext))] + public class SaveAzureVhdCommand : ComputeClientBaseCmdlet + { + private const int DefaultNumberOfUploaderThreads = 8; + private const string ResourceGroupParameterSet = "ResourceGroupParameterSetName"; + private const string StorageKeyParameterSet = "StorageKeyParameterSetName"; + + [Parameter( + Position = 0, + Mandatory = true, + ParameterSetName = ResourceGroupParameterSet, + ValueFromPipelineByPropertyName = true)] + [ValidateNotNullOrEmpty] + public string ResourceGroupName { get; set; } + + [Parameter( + Position = 0, + Mandatory = true, + ParameterSetName = StorageKeyParameterSet, + HelpMessage = "Key of the storage account")] + [ValidateNotNullOrEmpty] + [Alias("sk")] + public string StorageKey + { + get; + set; + } + + [Parameter( + Position = 1, + Mandatory = true, + ValueFromPipelineByPropertyName = true, + HelpMessage = "Uri to blob")] + [ValidateNotNullOrEmpty] + [Alias("src")] + public Uri Source + { + get; + set; + } + + [Parameter( + Position = 2, + Mandatory = true, + HelpMessage = "Local path of the vhd file")] + [ValidateNotNullOrEmpty] + [Alias("lf")] + public FileInfo LocalFilePath + { + get; + set; + } + + private int numberOfThreads = DefaultNumberOfUploaderThreads; + + [Parameter( + Position = 3, + Mandatory = false, + HelpMessage = "Number of downloader threads")] + [ValidateNotNullOrEmpty] + [ValidateRange(1, 64)] + [Alias("th")] + public int NumberOfThreads + { + get { return this.numberOfThreads; } + set { this.numberOfThreads = value; } + } + + [Parameter( + Position = 4, + Mandatory = false, + HelpMessage = "Delete the local file if already exists")] + [ValidateNotNullOrEmpty] + [Alias("o")] + public SwitchParameter OverWrite + { + get; + set; + } + + protected override void ProcessRecord() + { + BlobUri blobUri; + if (!BlobUri.TryParseUri(Source, out blobUri)) + { + throw new ArgumentOutOfRangeException("Source", Source.ToString()); + } + + var storageKey = this.StorageKey; + if (this.StorageKey == null) + { + var storageClient = AzureSession.ClientFactory.CreateClient( + DefaultProfile.Context, AzureEnvironment.Endpoint.ResourceManager); + + + var storageService = storageClient.StorageAccounts.GetProperties(this.ResourceGroupName, blobUri.StorageAccountName); + if (storageService != null) + { + var storageKeys = storageClient.StorageAccounts.ListKeys(this.ResourceGroupName, storageService.StorageAccount.Name); + storageKey = storageKeys.StorageAccountKeys.Key1; + } + } + + var downloaderParameters = new DownloaderParameters + { + BlobUri = blobUri, + LocalFilePath = LocalFilePath.FullName, + ConnectionLimit = NumberOfThreads, + StorageAccountKey = storageKey, + ValidateFreeDiskSpace = true, + OverWrite = OverWrite + }; + + var vhdDownloadContext = VhdDownloaderModel.Download(downloaderParameters, this); + WriteObject(vhdDownloadContext); + } + } +} From 64bfa3142335b2214c3ceaba429bb9179e38cb0a Mon Sep 17 00:00:00 2001 From: Hyonho Lee Date: Mon, 21 Sep 2015 16:46:41 -0700 Subject: [PATCH 5/9] Add "None" value for Disk Caching. --- .../Compute/Commands.Compute/Common/ConstantStringTypes.cs | 1 + .../VirtualMachine/Config/AddAzureVMDataDiskCommand.cs | 2 +- .../VirtualMachine/Config/SetAzureVMDataDiskCommand.cs | 2 +- .../VirtualMachine/Config/SetAzureVMOSDiskCommand.cs | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ResourceManager/Compute/Commands.Compute/Common/ConstantStringTypes.cs b/src/ResourceManager/Compute/Commands.Compute/Common/ConstantStringTypes.cs index 2f5eabcae1dc..078d8629c9d4 100644 --- a/src/ResourceManager/Compute/Commands.Compute/Common/ConstantStringTypes.cs +++ b/src/ResourceManager/Compute/Commands.Compute/Common/ConstantStringTypes.cs @@ -47,6 +47,7 @@ public static class ValidateSetValues { public const string ReadOnly = "ReadOnly"; public const string ReadWrite = "ReadWrite"; + public const string None = "None"; } public static class ProfileNouns diff --git a/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Config/AddAzureVMDataDiskCommand.cs b/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Config/AddAzureVMDataDiskCommand.cs index 5737db843271..59d83e64c760 100644 --- a/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Config/AddAzureVMDataDiskCommand.cs +++ b/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Config/AddAzureVMDataDiskCommand.cs @@ -60,7 +60,7 @@ public class AddAzureVMDataDiskCommand : Microsoft.Azure.Commands.ResourceManage ValueFromPipelineByPropertyName = true, HelpMessage = HelpMessages.VMDataDiskCaching)] [ValidateNotNullOrEmpty] - [ValidateSet(ValidateSetValues.ReadOnly, ValidateSetValues.ReadWrite)] + [ValidateSet(ValidateSetValues.ReadOnly, ValidateSetValues.ReadWrite, ValidateSetValues.None)] public string Caching { get; set; } [Parameter( diff --git a/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Config/SetAzureVMDataDiskCommand.cs b/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Config/SetAzureVMDataDiskCommand.cs index 0b330cc5dc4d..83403242bd2a 100644 --- a/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Config/SetAzureVMDataDiskCommand.cs +++ b/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Config/SetAzureVMDataDiskCommand.cs @@ -68,7 +68,7 @@ public class SetAzureVMDataDiskCommand : Microsoft.Azure.Commands.ResourceManage ValueFromPipelineByPropertyName = true, HelpMessage = HelpMessages.VMDataDiskCaching)] [ValidateNotNullOrEmpty] - [ValidateSet(ValidateSetValues.ReadOnly, ValidateSetValues.ReadWrite)] + [ValidateSet(ValidateSetValues.ReadOnly, ValidateSetValues.ReadWrite, ValidateSetValues.None)] public string Caching { get; set; } [Parameter( diff --git a/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Config/SetAzureVMOSDiskCommand.cs b/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Config/SetAzureVMOSDiskCommand.cs index 1226f5500a93..b29fd2277b6b 100644 --- a/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Config/SetAzureVMOSDiskCommand.cs +++ b/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Config/SetAzureVMOSDiskCommand.cs @@ -65,7 +65,7 @@ public class SetAzureVMOSDiskCommand : Microsoft.Azure.Commands.ResourceManager. ValueFromPipelineByPropertyName = true, HelpMessage = HelpMessages.VMOSDiskCaching)] [ValidateNotNullOrEmpty] - [ValidateSet(ValidateSetValues.ReadOnly, ValidateSetValues.ReadWrite)] + [ValidateSet(ValidateSetValues.ReadOnly, ValidateSetValues.ReadWrite, ValidateSetValues.None)] public string Caching { get; set; } [Alias("SourceImage")] From faef27a9129e7eecc63483ddded0ca08de7dfe51 Mon Sep 17 00:00:00 2001 From: Hyonho Lee Date: Mon, 21 Sep 2015 16:52:51 -0700 Subject: [PATCH 6/9] Fix the output of New-AzureRMVM and Update-AzureRMVM --- .../VirtualMachine/Operation/NewAzureVMCommand.cs | 6 ++++-- .../VirtualMachine/Operation/UpdateAzureVMCommand.cs | 8 +++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Operation/NewAzureVMCommand.cs b/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Operation/NewAzureVMCommand.cs index 59f4bffb3244..310a285af452 100644 --- a/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Operation/NewAzureVMCommand.cs +++ b/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Operation/NewAzureVMCommand.cs @@ -12,9 +12,9 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using AutoMapper; using Microsoft.Azure.Commands.Compute.Common; using Microsoft.Azure.Commands.Compute.Models; -using Microsoft.Azure.Commands.Tags.Model; using Microsoft.Azure.Management.Compute; using Microsoft.Azure.Management.Compute.Models; using System.Collections; @@ -23,6 +23,7 @@ namespace Microsoft.Azure.Commands.Compute { [Cmdlet(VerbsCommon.New, ProfileNouns.VirtualMachine)] + [OutputType(typeof(PSComputeLongRunningOperation))] public class NewAzureVMCommand : VirtualMachineBaseCmdlet { [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true)] @@ -61,7 +62,8 @@ protected override void ProcessRecord() }; var op = this.VirtualMachineClient.CreateOrUpdate(this.ResourceGroupName, parameters); - WriteObject(op); + var result = Mapper.Map(op); + WriteObject(result); }); } } diff --git a/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Operation/UpdateAzureVMCommand.cs b/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Operation/UpdateAzureVMCommand.cs index 7f2f5bfe3f9e..51c184741750 100644 --- a/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Operation/UpdateAzureVMCommand.cs +++ b/src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Operation/UpdateAzureVMCommand.cs @@ -12,7 +12,7 @@ // limitations under the License. // ---------------------------------------------------------------------------------- - +using AutoMapper; using Microsoft.Azure.Commands.Compute.Common; using Microsoft.Azure.Commands.Compute.Models; using Microsoft.Azure.Management.Compute; @@ -22,7 +22,8 @@ namespace Microsoft.Azure.Commands.Compute { - [Cmdlet(VerbsData.Update, ProfileNouns.VirtualMachine, DefaultParameterSetName = ResourceGroupNameParameterSet)] + [Cmdlet(VerbsData.Update, ProfileNouns.VirtualMachine, DefaultParameterSetName = ResourceGroupNameParameterSet)] + [OutputType(typeof(PSComputeLongRunningOperation))] public class UpdateAzureVMCommand : VirtualMachineActionBaseCmdlet { [Alias("VMProfile")] @@ -53,7 +54,8 @@ protected override void ProcessRecord() }; var op = this.VirtualMachineClient.CreateOrUpdate(this.ResourceGroupName, parameters); - WriteObject(op); + var result = Mapper.Map(op); + WriteObject(result); }); } } From d01407dfa26c267d95f867a4ca5f7299555a4519 Mon Sep 17 00:00:00 2001 From: Hyonho Lee Date: Mon, 21 Sep 2015 19:53:13 -0700 Subject: [PATCH 7/9] Fix AzureRT test settings --- AzurePowershell.Test.targets | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/AzurePowershell.Test.targets b/AzurePowershell.Test.targets index 62daee482588..5c14f4877bd2 100644 --- a/AzurePowershell.Test.targets +++ b/AzurePowershell.Test.targets @@ -205,7 +205,7 @@ - + - + - + - + - + - + - + - + @@ -326,7 +326,7 @@ ContinueOnError="false" /> - + @@ -335,7 +335,7 @@ ContinueOnError="true" /> - + @@ -344,7 +344,7 @@ ContinueOnError="false" /> - + @@ -353,7 +353,7 @@ ContinueOnError="false" /> - + @@ -362,7 +362,7 @@ ContinueOnError="false" /> - + @@ -371,7 +371,7 @@ ContinueOnError="false" /> - + @@ -380,7 +380,7 @@ ContinueOnError="false" /> - + @@ -392,7 +392,7 @@ ContinueOnError="false" /> - + - + - + Date: Mon, 21 Sep 2015 20:48:51 -0700 Subject: [PATCH 8/9] Add -WhatIf to Remove-AzureVM --- .../FunctionalTests/BVTTest.cs | 3 + .../IaasCmdletInfo/RemoveAzureVMCmdletInfo.cs | 7 ++- .../ServiceManagementCmdletTestHelper.cs | 4 +- .../IaaS/PersistentVMs/RemoveAzureVM.cs | 57 ++++++++++--------- .../Properties/Resources.Designer.cs | 11 +++- .../Properties/Resources.resx | 3 + 6 files changed, 54 insertions(+), 31 deletions(-) diff --git a/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/BVTTest.cs b/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/BVTTest.cs index 520fc058609e..9e891962b837 100644 --- a/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/BVTTest.cs +++ b/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/BVTTest.cs @@ -379,6 +379,9 @@ public void AzureIaaSBVT() // // Remove-AzureVM // + vmPowershellCmdlets.RemoveAzureVM(newAzureVMName, serviceName, false, true); + Assert.AreNotEqual(null, vmPowershellCmdlets.GetAzureVM(newAzureVMName, serviceName)); + vmPowershellCmdlets.RemoveAzureVM(newAzureVMName, serviceName); RecordTimeTaken(ref prevTime); diff --git a/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/IaasCmdletInfo/RemoveAzureVMCmdletInfo.cs b/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/IaasCmdletInfo/RemoveAzureVMCmdletInfo.cs index 8048c1ad0736..39632cbd2990 100644 --- a/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/IaasCmdletInfo/RemoveAzureVMCmdletInfo.cs +++ b/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/IaasCmdletInfo/RemoveAzureVMCmdletInfo.cs @@ -19,7 +19,7 @@ namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests { public class RemoveAzureVMCmdletInfo : CmdletsInfo { - public RemoveAzureVMCmdletInfo(string vmName, string serviceName, bool deleteVhd) + public RemoveAzureVMCmdletInfo(string vmName, string serviceName, bool deleteVhd, bool whatif) { this.cmdletName = Utilities.RemoveAzureVMCmdletName; this.cmdletParams.Add(new CmdletParam("Name", vmName)); @@ -29,6 +29,11 @@ public RemoveAzureVMCmdletInfo(string vmName, string serviceName, bool deleteVhd { this.cmdletParams.Add(new CmdletParam("DeleteVhd")); } + + if (whatif) + { + this.cmdletParams.Add(new CmdletParam("WhatIf")); + } } } } diff --git a/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/ServiceManagementCmdletTestHelper.cs b/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/ServiceManagementCmdletTestHelper.cs index bddb03530864..94e98fc4047b 100644 --- a/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/ServiceManagementCmdletTestHelper.cs +++ b/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/ServiceManagementCmdletTestHelper.cs @@ -1446,9 +1446,9 @@ public SM.PersistentVMRoleContext GetAzureVM(string vmName, string serviceName) return RunPSCmdletAndReturnFirst(new GetAzureVMCmdletInfo(vmName, serviceName)); } - public ManagementOperationContext RemoveAzureVM(string vmName, string serviceName, bool deleteVhd = false) + public ManagementOperationContext RemoveAzureVM(string vmName, string serviceName, bool deleteVhd = false, bool whatif = false) { - return RunPSCmdletAndReturnFirst(new RemoveAzureVMCmdletInfo(vmName, serviceName, deleteVhd)); + return RunPSCmdletAndReturnFirst(new RemoveAzureVMCmdletInfo(vmName, serviceName, deleteVhd, whatif)); } public ManagementOperationContext StartAzureVM(string vmName, string serviceName) diff --git a/src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/PersistentVMs/RemoveAzureVM.cs b/src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/PersistentVMs/RemoveAzureVM.cs index 5784f1d24125..c5a8d5df9322 100644 --- a/src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/PersistentVMs/RemoveAzureVM.cs +++ b/src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/PersistentVMs/RemoveAzureVM.cs @@ -23,7 +23,7 @@ namespace Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS { - [Cmdlet(VerbsCommon.Remove, "AzureVM"), OutputType(typeof(ManagementOperationContext))] + [Cmdlet(VerbsCommon.Remove, "AzureVM", SupportsShouldProcess = true), OutputType(typeof(ManagementOperationContext))] public class RemoveAzureVMCommand : IaaSDeploymentManagementCmdletBase { [Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of the role to remove.")] @@ -43,38 +43,41 @@ public SwitchParameter DeleteVHD protected override void ExecuteCommand() { - ServiceManagementProfile.Initialize(); - - base.ExecuteCommand(); - if (CurrentDeploymentNewSM == null) + if (this.ShouldProcess(String.Format("Service: {0}, VM: {1}", this.ServiceName, this.Name), Resources.RemoveAzureVMShouldProcessAction)) { - return; - } + ServiceManagementProfile.Initialize(); - DeploymentGetResponse deploymentGetResponse = this.ComputeClient.Deployments.GetBySlot(this.ServiceName, DeploymentSlot.Production); - if (deploymentGetResponse.Roles.FirstOrDefault(r => r.RoleName.Equals(Name, StringComparison.InvariantCultureIgnoreCase)) == null) - { - throw new ArgumentOutOfRangeException(String.Format(Resources.RoleInstanceCanNotBeFoundWithName, Name)); - } + base.ExecuteCommand(); + if (CurrentDeploymentNewSM == null) + { + return; + } - if (deploymentGetResponse.RoleInstances.Count > 1) - { - ExecuteClientActionNewSM( - null, - CommandRuntime.ToString(), - () => this.ComputeClient.VirtualMachines.Delete(this.ServiceName, CurrentDeploymentNewSM.Name, Name, DeleteVHD.IsPresent)); - } - else - { - if (deploymentGetResponse != null && !string.IsNullOrEmpty(deploymentGetResponse.ReservedIPName)) + DeploymentGetResponse deploymentGetResponse = this.ComputeClient.Deployments.GetBySlot(this.ServiceName, DeploymentSlot.Production); + if (deploymentGetResponse.Roles.FirstOrDefault(r => r.RoleName.Equals(Name, StringComparison.InvariantCultureIgnoreCase)) == null) { - WriteVerboseWithTimestamp(string.Format(Resources.ReservedIPNameNoLongerInUseByDeletingLastVMButStillBeingReserved, deploymentGetResponse.ReservedIPName)); + throw new ArgumentOutOfRangeException(String.Format(Resources.RoleInstanceCanNotBeFoundWithName, Name)); } - ExecuteClientActionNewSM( - null, - CommandRuntime.ToString(), - () => this.ComputeClient.Deployments.DeleteByName(this.ServiceName, CurrentDeploymentNewSM.Name, DeleteVHD.IsPresent)); + if (deploymentGetResponse.RoleInstances.Count > 1) + { + ExecuteClientActionNewSM( + null, + CommandRuntime.ToString(), + () => this.ComputeClient.VirtualMachines.Delete(this.ServiceName, CurrentDeploymentNewSM.Name, Name, DeleteVHD.IsPresent)); + } + else + { + if (deploymentGetResponse != null && !string.IsNullOrEmpty(deploymentGetResponse.ReservedIPName)) + { + WriteVerboseWithTimestamp(string.Format(Resources.ReservedIPNameNoLongerInUseByDeletingLastVMButStillBeingReserved, deploymentGetResponse.ReservedIPName)); + } + + ExecuteClientActionNewSM( + null, + CommandRuntime.ToString(), + () => this.ComputeClient.Deployments.DeleteByName(this.ServiceName, CurrentDeploymentNewSM.Name, DeleteVHD.IsPresent)); + } } } } diff --git a/src/ServiceManagement/Compute/Commands.ServiceManagement/Properties/Resources.Designer.cs b/src/ServiceManagement/Compute/Commands.ServiceManagement/Properties/Resources.Designer.cs index f3cb57a9fed3..61587543ea21 100644 --- a/src/ServiceManagement/Compute/Commands.ServiceManagement/Properties/Resources.Designer.cs +++ b/src/ServiceManagement/Compute/Commands.ServiceManagement/Properties/Resources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.34014 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -1429,6 +1429,15 @@ public static string RemoveAzureDnsServerWarning { } } + /// + /// Looks up a localized string similar to Deleting VM. + /// + public static string RemoveAzureVMShouldProcessAction { + get { + return ResourceManager.GetString("RemoveAzureVMShouldProcessAction", resourceCulture); + } + } + /// /// Looks up a localized string similar to Remove Reserved IP Association. /// diff --git a/src/ServiceManagement/Compute/Commands.ServiceManagement/Properties/Resources.resx b/src/ServiceManagement/Compute/Commands.ServiceManagement/Properties/Resources.resx index 143942ad19e9..8a6ca4ac560c 100644 --- a/src/ServiceManagement/Compute/Commands.ServiceManagement/Properties/Resources.resx +++ b/src/ServiceManagement/Compute/Commands.ServiceManagement/Properties/Resources.resx @@ -753,4 +753,7 @@ Upload '{0}' {0} is the name of an storage blob + + Deleting VM + \ No newline at end of file From 94916b9d30d4b1ad9efdf3ef3b8567a45c099fe6 Mon Sep 17 00:00:00 2001 From: Hyonho Lee Date: Tue, 22 Sep 2015 11:32:22 -0700 Subject: [PATCH 9/9] Fix AzureRT test issues due to the storage cmdlet name changes --- .../FunctionalTests/CredentialHelper.cs | 22 ++++++++++++------- .../FunctionalTestCommonVhd.cs | 12 +++++----- .../ServiceManagementCmdletTestHelper.cs | 4 +++- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/CredentialHelper.cs b/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/CredentialHelper.cs index d7d2bcb8296b..c04882e8c968 100644 --- a/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/CredentialHelper.cs +++ b/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/CredentialHelper.cs @@ -12,15 +12,17 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.Properties; +using Microsoft.WindowsAzure.Commands.Storage.Common; +using Microsoft.WindowsAzure.Storage.Auth; +using Microsoft.WindowsAzure.Storage.Blob; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Diagnostics; using System.IO; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.Properties; -using Microsoft.WindowsAzure.Storage.Auth; -using Microsoft.WindowsAzure.Storage.Blob; +using System.Management.Automation; namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests { @@ -263,12 +265,14 @@ public static void CopyTestData(string srcContainer, string srcBlob, string dest // Create a container try { - vmPowershellCmdlets.RunPSScript("Get-AzureStorageContainer -Name " + destContainer); + vmPowershellCmdlets.RunPSScript(String.Format("{0}-{1} -Name {2}", + VerbsCommon.Get, StorageNouns.Container, destContainer)); } catch { // Create a container. - vmPowershellCmdlets.RunPSScript("New-AzureStorageContainer -Name " + destContainer); + vmPowershellCmdlets.RunPSScript(String.Format("{0}-{1} -Name {2}", + VerbsCommon.New, StorageNouns.Container, destContainer)); } // Make SAS Uri for the source blob. @@ -276,12 +280,14 @@ public static void CopyTestData(string srcContainer, string srcBlob, string dest if (string.IsNullOrEmpty(destBlob)) { - vmPowershellCmdlets.RunPSScript(string.Format("Start-AzureStorageBlobCopy -SrcContainer {0} -SrcBlob {1} -DestContainer {2} -Force", srcContainer, srcBlob, destContainer)); + vmPowershellCmdlets.RunPSScript(string.Format("{0}-{1} -SrcContainer {2} -SrcBlob {3} -DestContainer {4} -Force", + VerbsLifecycle.Start, StorageNouns.CopyBlob, srcContainer, srcBlob, destContainer)); destBlob = srcBlob; } else { - vmPowershellCmdlets.RunPSScript(string.Format("Start-AzureStorageBlobCopy -SrcUri \"{0}\" -DestContainer {1} -DestBlob {2} -Force", srcSasUri, destContainer, destBlob)); + vmPowershellCmdlets.RunPSScript(string.Format("{0}-{1} -SrcUri \"{2}\" -DestContainer {3} -DestBlob {4} -Force", + VerbsLifecycle.Start, StorageNouns.CopyBlob, srcSasUri, destContainer, destBlob)); } for (int i = 0; i < 60; i++) diff --git a/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/FunctionalTestCommonVhd.cs b/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/FunctionalTestCommonVhd.cs index f7c661787d10..9863677cd6f6 100644 --- a/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/FunctionalTestCommonVhd.cs +++ b/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/FunctionalTestCommonVhd.cs @@ -12,14 +12,15 @@ // limitations under the License. // ---------------------------------------------------------------------------------- - +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.WindowsAzure.Commands.ServiceManagement.Model; +using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.ConfigDataInfo; +using Microsoft.WindowsAzure.Commands.Storage.Common; using System; using System.IO; using System.Linq; +using System.Management.Automation; using System.Reflection; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Microsoft.WindowsAzure.Commands.ServiceManagement.Model; -using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.ConfigDataInfo; namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests { @@ -166,7 +167,8 @@ public void AzureDiskTest() private void CopyCommonVhd(string vhdContainerName, string vhdName, string myVhdName) { - vmPowershellCmdlets.RunPSScript(string.Format("Start-AzureStorageBlobCopy -SrcContainer {0} -SrcBlob {1} -DestContainer {2} -DestBlob {3}", vhdContainerName, vhdName, vhdContainerName, myVhdName)); + vmPowershellCmdlets.RunPSScript(string.Format("{0}-{1} -SrcContainer {2} -SrcBlob {3} -DestContainer {4} -DestBlob {5}", + VerbsLifecycle.Start, StorageNouns.CopyBlob, vhdContainerName, vhdName, vhdContainerName, myVhdName)); } [TestMethod(), TestCategory(Category.Functional), TestCategory(Category.BVT), TestProperty("Feature", "IAAS"), Priority(1), Owner("hylee"), Description("Test the cmdlet ((Add,Get,Save,Update,Remove)-AzureVMImage)")] diff --git a/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/ServiceManagementCmdletTestHelper.cs b/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/ServiceManagementCmdletTestHelper.cs index 94e98fc4047b..affe8d845584 100644 --- a/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/ServiceManagementCmdletTestHelper.cs +++ b/src/ServiceManagement/Compute/Commands.ServiceManagement.Test/FunctionalTests/ServiceManagementCmdletTestHelper.cs @@ -33,6 +33,7 @@ using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.PIRCmdletInfo; using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.NetworkCmdletInfo; using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.SubscriptionCmdletInfo; +using Microsoft.WindowsAzure.Commands.Storage.Common; using Microsoft.WindowsAzure.Commands.Utilities.Common; using Microsoft.WindowsAzure.Management.Network.Models; using Microsoft.WindowsAzure.Storage.Blob; @@ -176,7 +177,8 @@ private Collection RunPSCmdletAndReturnAllHelper(PowershellCore.CmdletsInf public CopyState CheckCopyBlobStatus(string destContainer, string destBlob, bool debug = false) { List st = new List(); - st.Add(string.Format("Get-AzureStorageBlobCopyState -Container {0} -Blob {1}", destContainer, destBlob)); + st.Add(string.Format("{0}-{1} -Container {2} -Blob {3}", + VerbsCommon.Get, StorageNouns.CopyBlobStatus, destContainer, destBlob)); WindowsAzurePowershellScript azurePowershellCmdlet = new WindowsAzurePowershellScript(st); return (CopyState)azurePowershellCmdlet.Run(debug)[0].BaseObject;