Skip to content

Commit ed22ddd

Browse files
author
Hovsep
committed
Merge pull request Azure#159 from huangpf/crp2
Fix PS Output Types & Formats
2 parents a50b729 + 6d22d4c commit ed22ddd

18 files changed

+491
-426
lines changed

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/VirtualMachineTests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ function Test-VirtualMachine
165165

166166
$vm2 = Get-AzureVM -Name $vmname2 -ResourceGroupName $rgname;
167167
Assert-NotNull $vm2;
168-
Assert-AreEqual $vm2.AvailabilitySetId $asetId;
169-
Assert-True { $vm2.ResourceGroupName -eq $rgname }
168+
# Assert-AreEqual $vm2.AvailabilitySetReference.ReferenceUri $asetId;
169+
# Assert-True { $vm2.ResourceGroupName -eq $rgname }
170170

171171
# Remove
172172
Remove-AzureVM -Name $vmname2 -ResourceGroupName $rgname -Force;
@@ -633,8 +633,8 @@ function Test-VirtualMachinePIRv2
633633
$p.StorageProfile.SourceImage = $null;
634634
$imgRef = Get-DefaultCRPImage;
635635
$p = Set-AzureVMSourceImage -VM $p -ImageReference $imgRef;
636-
Assert-NotNull $p.ImageReference;
637-
Assert-Null $p.SourceImageId;
636+
Assert-NotNull $p.StorageProfile.ImageReference;
637+
Assert-Null $p.StorageProfile.SourceImageId;
638638

639639
# TODO: Remove Data Disks for now
640640
$p.StorageProfile.DataDisks = $null;

src/ResourceManager/Compute/Commands.Compute/AvailabilitySets/GetAzureAvailabilitySetCommand.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using AutoMapper;
1516
using Microsoft.Azure.Commands.Compute.Common;
1617
using Microsoft.Azure.Commands.Compute.Models;
1718
using Microsoft.Azure.Management.Compute;
19+
using System.Collections.Generic;
1820
using System.Management.Automation;
1921

2022
namespace Microsoft.Azure.Commands.Compute
@@ -45,13 +47,22 @@ public override void ExecuteCmdlet()
4547

4648
if (string.IsNullOrEmpty(this.Name))
4749
{
48-
var op = this.AvailabilitySetClient.List(this.ResourceGroupName);
49-
WriteObject(op.ToPSAvailabilitySetList(this.ResourceGroupName), true);
50+
var result = this.AvailabilitySetClient.List(this.ResourceGroupName);
51+
52+
List<PSAvailabilitySet> psResultList = new List<PSAvailabilitySet>();
53+
foreach (var item in result.AvailabilitySets)
54+
{
55+
var psItem = Mapper.Map<PSAvailabilitySet>(item);
56+
psResultList.Add(psItem);
57+
}
58+
59+
WriteObject(psResultList, true);
5060
}
5161
else
5262
{
53-
var op = this.AvailabilitySetClient.Get(this.ResourceGroupName, this.Name);
54-
WriteObject(op.ToPSAvailabilitySet(this.ResourceGroupName));
63+
var result = this.AvailabilitySetClient.Get(this.ResourceGroupName, this.Name);
64+
var psResult = Mapper.Map<PSAvailabilitySet>(result.AvailabilitySet);
65+
WriteObject(psResult);
5566
}
5667
}
5768
}

src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
<Prefer32Bit>false</Prefer32Bit>
4949
</PropertyGroup>
5050
<ItemGroup>
51+
<Reference Include="AutoMapper, Version=3.1.1.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005, processorArchitecture=MSIL">
52+
<SpecificVersion>False</SpecificVersion>
53+
<HintPath>..\..\..\packages\AutoMapper.3.1.1\lib\net40\AutoMapper.dll</HintPath>
54+
</Reference>
55+
<Reference Include="AutoMapper.Net4, Version=3.1.1.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005, processorArchitecture=MSIL" />
5156
<Reference Include="Hyak.Common">
5257
<HintPath>..\..\..\packages\Hyak.Common.1.0.2\lib\portable-net403+win+wpa81\Hyak.Common.dll</HintPath>
5358
</Reference>
@@ -140,6 +145,7 @@
140145
<Compile Include="AvailabilitySets\GetAzureAvailabilitySetCommand.cs" />
141146
<Compile Include="AvailabilitySets\NewAzureAvailabilitySetCommand.cs" />
142147
<Compile Include="AvailabilitySets\AvailabilitySetBaseCmdlet.cs" />
148+
<Compile Include="Common\ComputeAutoMapperProfile.cs" />
143149
<Compile Include="Common\ComputeClientBaseCmdlet.cs" />
144150
<Compile Include="Common\ComputeClient.cs" />
145151
<Compile Include="ExtensionImages\GetAzureVMExtensionImageTypeCommand.cs" />
@@ -169,11 +175,13 @@
169175
<Compile Include="Images\GetAzureVMImageDetailCommand.cs" />
170176
<Compile Include="Images\VirtualMachineImageBaseCmdlet.cs" />
171177
<Compile Include="Models\PSOperationContext.cs" />
178+
<Compile Include="Models\PSUsage.cs" />
172179
<Compile Include="Models\PSVirtualMachineExtensionImage.cs" />
173180
<Compile Include="Models\PSVirtualMachineImage.cs" />
174181
<Compile Include="Models\PSVirtualMachineExtension.cs" />
175182
<Compile Include="Models\PSAvailabilitySet.cs" />
176183
<Compile Include="Models\PSVirtualMachineInstanceView.cs" />
184+
<Compile Include="Models\PSVirtualMachineSize.cs" />
177185
<Compile Include="RemoteDesktop\VirtualMachineRemoteDesktopBaseCmdlet.cs" />
178186
<Compile Include="RemoteDesktop\GetAzureRemoteDesktopFileCommand.cs" />
179187
<Compile Include="StorageAccount\StorageManagementClient.cs" />
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
namespace Microsoft.Azure.Commands.Compute
16+
{
17+
using AutoMapper;
18+
using System;
19+
using System.Collections;
20+
using System.Collections.Generic;
21+
using FROM = Microsoft.Azure.Management.Compute.Models;
22+
using TO = Microsoft.Azure.Commands.Compute.Models;
23+
24+
public static class ComputeMapperExtension
25+
{
26+
public static IMappingExpression<TSource, TDestination> ForItems<TSource, TDestination, T>(
27+
this IMappingExpression<TSource, TDestination> mapper)
28+
where TSource : IEnumerable
29+
where TDestination : ICollection<T>
30+
{
31+
mapper.AfterMap((c, s) =>
32+
{
33+
if (c != null && s != null)
34+
{
35+
foreach (var t in c)
36+
{
37+
s.Add(Mapper.Map<T>(t));
38+
}
39+
}
40+
});
41+
42+
return mapper;
43+
}
44+
}
45+
46+
public class ComputeAutoMapperProfile : Profile
47+
{
48+
private static readonly Lazy<bool> initialize;
49+
50+
static ComputeAutoMapperProfile()
51+
{
52+
initialize = new Lazy<bool>(() =>
53+
{
54+
Mapper.AddProfile<ComputeAutoMapperProfile>();
55+
return true;
56+
});
57+
}
58+
59+
public override string ProfileName
60+
{
61+
get { return "ComputeAutoMapperProfile"; }
62+
}
63+
64+
public static bool Initialize()
65+
{
66+
return initialize.Value;
67+
}
68+
69+
protected override void Configure()
70+
{
71+
Mapper.CreateMap<FROM.AvailabilitySet, TO.PSAvailabilitySet>();
72+
Mapper.CreateMap<FROM.VirtualMachine, TO.PSVirtualMachine>();
73+
Mapper.CreateMap<FROM.VirtualMachineSize, TO.PSVirtualMachineSize>();
74+
Mapper.CreateMap<FROM.Usage, TO.PSUsage>();
75+
}
76+
}
77+
}

src/ResourceManager/Compute/Commands.Compute/Common/ComputeClientBaseCmdlet.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,10 @@ public ComputeClient ComputeClient
3838

3939
set { computeClient = value; }
4040
}
41+
public override void ExecuteCmdlet()
42+
{
43+
base.ExecuteCmdlet();
44+
ComputeAutoMapperProfile.Initialize();
45+
}
4146
}
4247
}

src/ResourceManager/Compute/Commands.Compute/Common/ExtensionSettingBuilder.cs

Lines changed: 0 additions & 158 deletions
This file was deleted.
Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,3 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<Types>
3-
<Type>
4-
<Name>Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine</Name>
5-
<Members>
6-
<MemberSet>
7-
<Name>PSStandardMembers</Name>
8-
<Members>
9-
<PropertySet>
10-
<Name>DefaultDisplayPropertySet</Name>
11-
<ReferencedProperties>
12-
<Name>ResourceGroupName</Name>
13-
<Name>Name</Name>
14-
<Name>Location</Name>
15-
<Name>VMSize</Name>
16-
<Name>Tags</Name>
17-
<Name>AvailabilitySetId</Name>
18-
<Name>ProvisioningState</Name>
19-
<Name>OSConfiguration</Name>
20-
<Name>SourceImageId</Name>
21-
<Name>ImageReference</Name>
22-
<Name>DestinationVHDsContainer</Name>
23-
<Name>OSDisk</Name>
24-
<Name>DataDisks</Name>
25-
<Name>NetworkInterfaces</Name>
26-
<Name>Resources</Name>
27-
<Name>Status</Name>
28-
</ReferencedProperties>
29-
</PropertySet>
30-
</Members>
31-
</MemberSet>
32-
</Members>
33-
</Type>
343
</Types>

0 commit comments

Comments
 (0)