|
21 | 21 |
|
22 | 22 | namespace Microsoft.Azure.Commands.Compute
|
23 | 23 | {
|
24 |
| - [Cmdlet(VerbsCommon.Get, ProfileNouns.VirtualMachineImage)] |
25 |
| - [OutputType(typeof(PSVirtualMachineImage))] |
| 24 | + [Cmdlet(VerbsCommon.Get, |
| 25 | + ProfileNouns.VirtualMachineImage, |
| 26 | + DefaultParameterSetName = ListVMImageParamSetName)] |
| 27 | + [OutputType(typeof(PSVirtualMachineImage), |
| 28 | + ParameterSetName = new [] {ListVMImageParamSetName})] |
| 29 | + [OutputType(typeof(PSVirtualMachineImageDetail), |
| 30 | + ParameterSetName = new [] {GetVMImageDetailParamSetName})] |
26 | 31 | public class GetAzureVMImageCommand : VirtualMachineImageBaseCmdlet
|
27 | 32 | {
|
28 |
| - [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true), ValidateNotNullOrEmpty] |
| 33 | + protected const string ListVMImageParamSetName = "ListVMImage"; |
| 34 | + protected const string GetVMImageDetailParamSetName = "GetVMImageDetail"; |
| 35 | + |
| 36 | + [Parameter(ParameterSetName = ListVMImageParamSetName, |
| 37 | + Mandatory = true, |
| 38 | + ValueFromPipelineByPropertyName = true)] |
| 39 | + [Parameter(ParameterSetName = GetVMImageDetailParamSetName, |
| 40 | + Mandatory = true, |
| 41 | + ValueFromPipelineByPropertyName = true)] |
| 42 | + [ValidateNotNullOrEmpty] |
29 | 43 | public string Location { get; set; }
|
30 | 44 |
|
31 |
| - [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true), ValidateNotNullOrEmpty] |
| 45 | + [Parameter(ParameterSetName = ListVMImageParamSetName, |
| 46 | + Mandatory = true, |
| 47 | + ValueFromPipelineByPropertyName = true)] |
| 48 | + [Parameter(ParameterSetName = GetVMImageDetailParamSetName, |
| 49 | + Mandatory = true, |
| 50 | + ValueFromPipelineByPropertyName = true)] |
| 51 | + [ValidateNotNullOrEmpty] |
32 | 52 | public string PublisherName { get; set; }
|
33 | 53 |
|
34 |
| - [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true), ValidateNotNullOrEmpty] |
| 54 | + [Parameter(ParameterSetName = ListVMImageParamSetName, |
| 55 | + Mandatory = true, |
| 56 | + ValueFromPipelineByPropertyName = true)] |
| 57 | + [Parameter(ParameterSetName = GetVMImageDetailParamSetName, |
| 58 | + Mandatory = true, |
| 59 | + ValueFromPipelineByPropertyName = true)] |
| 60 | + [ValidateNotNullOrEmpty] |
35 | 61 | public string Offer { get; set; }
|
36 | 62 |
|
37 |
| - [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true), ValidateNotNullOrEmpty] |
| 63 | + [Parameter(ParameterSetName = ListVMImageParamSetName, |
| 64 | + Mandatory = true, |
| 65 | + ValueFromPipelineByPropertyName = true)] |
| 66 | + [Parameter(ParameterSetName = GetVMImageDetailParamSetName, |
| 67 | + Mandatory = true, |
| 68 | + ValueFromPipelineByPropertyName = true)] |
| 69 | + [ValidateNotNullOrEmpty] |
38 | 70 | public string Skus { get; set; }
|
39 | 71 |
|
40 |
| - [Parameter, ValidateNotNullOrEmpty] |
| 72 | + [Parameter(ParameterSetName = ListVMImageParamSetName, |
| 73 | + ValueFromPipelineByPropertyName = true), |
| 74 | + ValidateNotNullOrEmpty] |
41 | 75 | public string FilterExpression { get; set; }
|
42 | 76 |
|
| 77 | + [Parameter(ParameterSetName = GetVMImageDetailParamSetName, |
| 78 | + Mandatory = true, |
| 79 | + ValueFromPipelineByPropertyName = true), |
| 80 | + ValidateNotNullOrEmpty] |
| 81 | + public string Version { get; set; } |
| 82 | + |
43 | 83 | public override void ExecuteCmdlet()
|
44 | 84 | {
|
45 | 85 | base.ExecuteCmdlet();
|
46 | 86 |
|
47 |
| - var parameters = new VirtualMachineImageListParameters |
| 87 | + if (this.ParameterSetName.Equals(ListVMImageParamSetName)) |
48 | 88 | {
|
49 |
| - Location = Location.Canonicalize(), |
50 |
| - Offer = Offer, |
51 |
| - PublisherName = PublisherName, |
52 |
| - Skus = Skus, |
53 |
| - FilterExpression = FilterExpression |
54 |
| - }; |
55 |
| - |
56 |
| - VirtualMachineImageResourceList result = this.VirtualMachineImageClient.List(parameters); |
57 |
| - |
58 |
| - var images = from r in result.Resources |
59 |
| - select new PSVirtualMachineImage |
60 |
| - { |
61 |
| - RequestId = result.RequestId, |
62 |
| - StatusCode = result.StatusCode, |
63 |
| - Id = r.Id, |
64 |
| - Location = r.Location, |
65 |
| - Version = r.Name, |
66 |
| - PublisherName = this.PublisherName, |
67 |
| - Offer = this.Offer, |
68 |
| - Skus = this.Skus, |
69 |
| - FilterExpression = this.FilterExpression |
70 |
| - }; |
71 |
| - |
72 |
| - WriteObject(images, true); |
| 89 | + var parameters = new VirtualMachineImageListParameters |
| 90 | + { |
| 91 | + Location = Location.Canonicalize(), |
| 92 | + Offer = Offer, |
| 93 | + PublisherName = PublisherName, |
| 94 | + Skus = Skus, |
| 95 | + FilterExpression = FilterExpression |
| 96 | + }; |
| 97 | + |
| 98 | + VirtualMachineImageResourceList result = this.VirtualMachineImageClient.List(parameters); |
| 99 | + |
| 100 | + var images = from r in result.Resources |
| 101 | + select new PSVirtualMachineImage |
| 102 | + { |
| 103 | + RequestId = result.RequestId, |
| 104 | + StatusCode = result.StatusCode, |
| 105 | + Id = r.Id, |
| 106 | + Location = r.Location, |
| 107 | + Version = r.Name, |
| 108 | + PublisherName = this.PublisherName, |
| 109 | + Offer = this.Offer, |
| 110 | + Skus = this.Skus, |
| 111 | + FilterExpression = this.FilterExpression |
| 112 | + }; |
| 113 | + |
| 114 | + WriteObject(images, true); |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + var parameters = new VirtualMachineImageGetParameters |
| 119 | + { |
| 120 | + Location = Location.Canonicalize(), |
| 121 | + PublisherName = PublisherName, |
| 122 | + Offer = Offer, |
| 123 | + Skus = Skus, |
| 124 | + Version = Version |
| 125 | + }; |
| 126 | + |
| 127 | + VirtualMachineImageGetResponse response = this.VirtualMachineImageClient.Get(parameters); |
| 128 | + |
| 129 | + var image = new PSVirtualMachineImageDetail |
| 130 | + { |
| 131 | + RequestId = response.RequestId, |
| 132 | + StatusCode = response.StatusCode, |
| 133 | + Id = response.VirtualMachineImage.Id, |
| 134 | + Location = response.VirtualMachineImage.Location, |
| 135 | + Version = response.VirtualMachineImage.Name, |
| 136 | + PublisherName = this.PublisherName, |
| 137 | + Offer = this.Offer, |
| 138 | + Skus = this.Skus, |
| 139 | + OSDiskImage = response.VirtualMachineImage.OSDiskImage, |
| 140 | + DataDiskImages = response.VirtualMachineImage.DataDiskImages, |
| 141 | + PurchasePlan = response.VirtualMachineImage.PurchasePlan, |
| 142 | + }; |
| 143 | + |
| 144 | + WriteObject(image); |
| 145 | + } |
73 | 146 | }
|
74 | 147 | }
|
75 | 148 | }
|
0 commit comments