Skip to content

Commit 0555e2d

Browse files
author
Hovsep Mkrtchyan
committed
Fixed Resource and RoleDefinition cmdlet screen outputs.
1 parent edb0832 commit 0555e2d

12 files changed

+225
-30
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Configuration>
3+
<ViewDefinitions>
4+
<View>
5+
<Name>Microsoft.Azure.Commands.ResourceManager.Cmdlets.Models.PSResourceObject</Name>
6+
<ViewSelectedBy>
7+
<TypeName>Microsoft.Azure.Commands.ResourceManager.Cmdlets.Models.PSResourceObject</TypeName>
8+
</ViewSelectedBy>
9+
<ListControl>
10+
<ListEntries>
11+
<ListEntry>
12+
<ListItems>
13+
<ListItem>
14+
<Label>Name</Label>
15+
<PropertyName>Name</PropertyName>
16+
</ListItem>
17+
<ListItem>
18+
<Label>ResourceId</Label>
19+
<PropertyName>ResourceId</PropertyName>
20+
</ListItem>
21+
<ListItem>
22+
<Label>ResourceName</Label>
23+
<PropertyName>ResourceName</PropertyName>
24+
</ListItem>
25+
<ListItem>
26+
<Label>ResourceType</Label>
27+
<PropertyName>ResourceType</PropertyName>
28+
</ListItem>
29+
<ListItem>
30+
<Label>ResourceGroupName</Label>
31+
<PropertyName>ResourceGroupName</PropertyName>
32+
</ListItem>
33+
<ListItem>
34+
<Label>Location</Label>
35+
<PropertyName>Location</PropertyName>
36+
</ListItem>
37+
<ListItem>
38+
<Label>SubscriptionId</Label>
39+
<PropertyName>SubscriptionId</PropertyName>
40+
</ListItem>
41+
<ListItem>
42+
<Label>Properties</Label>
43+
<PropertyName>PropertiesText</PropertyName>
44+
</ListItem>
45+
</ListItems>
46+
</ListEntry>
47+
</ListEntries>
48+
</ListControl>
49+
</View>
50+
</ViewDefinitions>
51+
</Configuration>

src/CLU/Commands.ResourceManager.Cmdlets/Extensions/ResourceExtensions.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions
1717
using Commands.Utilities.Common;
1818
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
20+
using Models;
2021
using Newtonsoft.Json.Linq;
2122
using System.Collections.Generic;
2223
using System.Linq;
@@ -26,11 +27,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions
2627
/// </summary>
2728
internal static class ResourceExtensions
2829
{
29-
/// <summary>
30-
/// Converts a <see cref="Resource{JToken}"/> object into a <see cref="PSObject"/> object.
31-
/// </summary>
32-
/// <param name="resource">The <see cref="Resource{JToken}"/> object.</param>
33-
internal static PSObject ToPsObject(this Resource<JToken> resource)
30+
internal static PSResourceObject ToPSResourceObject(this Resource<JToken> resource)
3431
{
3532
var resourceType = string.IsNullOrEmpty(resource.Id)
3633
? null
@@ -42,28 +39,34 @@ internal static PSObject ToPsObject(this Resource<JToken> resource)
4239

4340
var objectDefinition = new Dictionary<string, object>
4441
{
45-
{ "Name", resource.Name },
46-
{ "ResourceId", string.IsNullOrEmpty(resource.Id) ? null : resource.Id },
47-
{ "ResourceName", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceName(resource.Id) },
48-
{ "ResourceType", resourceType },
4942
{ "ExtensionResourceName", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetExtensionResourceName(resource.Id) },
5043
{ "ExtensionResourceType", extensionResourceType },
5144
{ "Kind", resource.Kind },
52-
{ "ResourceGroupName", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceGroupName(resource.Id) },
53-
{ "Location", resource.Location },
54-
{ "SubscriptionId", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetSubscriptionId(resource.Id) },
5545
{ "Tags", TagsHelper.GetTagsHashtables(resource.Tags) },
56-
{ "Plan", resource.Plan == null ? null : resource.Plan.ToJToken().ToPsObject() },
46+
{ "Plan", resource.Plan == null ? null : resource.Plan.ToJToken() },
5747
{ "Properties", ResourceExtensions.GetProperties(resource) },
5848
{ "CreatedTime", resource.CreatedTime },
5949
{ "ChangedTime", resource.ChangedTime },
6050
{ "ETag", resource.ETag },
61-
{ "Sku", resource.Sku == null ? null : resource.Sku.ToJToken().ToPsObject() },
51+
{ "Sku", resource.Sku == null ? null : resource.Sku.ToJToken() },
6252
};
6353

64-
var psObject =
65-
PowerShellUtilities.ConstructPSObject(
66-
objectDefinition.Where(kvp => kvp.Value != null).SelectManyArray(kvp => new[] { kvp.Key, kvp.Value }));
54+
var psObject = new PSResourceObject();
55+
psObject.Name = resource.Name;
56+
psObject.ResourceId = string.IsNullOrEmpty(resource.Id) ? null : resource.Id;
57+
psObject.ResourceName = string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceName(resource.Id);
58+
psObject.ResourceType = resourceType;
59+
psObject.ResourceGroupName = string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceGroupName(resource.Id);
60+
psObject.Location = resource.Location;
61+
psObject.SubscriptionId = string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetSubscriptionId(resource.Id);
62+
63+
64+
var objectProperties = objectDefinition.Where(kvp => kvp.Value != null).SelectManyArray(kvp => new[] { kvp.Key, kvp.Value });
65+
66+
for(int i=0; i< objectProperties.Length; i+=2)
67+
{
68+
psObject.Properties.Add(objectProperties[i].ToString(), objectProperties[i + 1].ToString());
69+
}
6770

6871
return psObject;
6972
}

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/GetAzureResourceGroupDeploymentOperationCmdlet.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2121
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
2222
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2323
using Newtonsoft.Json.Linq;
24-
24+
using Models;
2525
/// <summary>
2626
/// Gets the deployment operation.
2727
/// </summary>
28-
[Cmdlet(VerbsCommon.Get, "AzureRmResourceGroupDeploymentOperation"), OutputType(typeof(Resource<JToken>))]
28+
[Cmdlet(VerbsCommon.Get, "AzureRmResourceGroupDeploymentOperation"), OutputType(typeof(PSResourceObject))]
2929
public class GetAzureResourceGroupDeploymentOperationCmdlet : ResourceManagerCmdletBase
3030
{
3131
/// <summary>
@@ -81,7 +81,9 @@ private void RunCmdlet()
8181
getFirstPage: () => this.GetResources(),
8282
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
8383
cancellationToken: this.CancellationToken,
84-
action: resources => this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(resource => resource.ToResource()), enumerateCollection: true));
84+
action: resources => this.WriteObject(sendToPipeline: resources.CoalesceEnumerable()
85+
.SelectArray(resource => resource.ToResource().ToPSResourceObject()),
86+
enumerateCollection: true));
8587
}
8688

8789
/// <summary>

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/InvokeAzureResourceActionCmdlet.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2020
using Newtonsoft.Json.Linq;
2121
using Commands.Common;
22+
using Models;
23+
2224
/// <summary>
2325
/// A cmdlet that invokes a resource action.
2426
/// </summary>
25-
[Cmdlet(VerbsLifecycle.Invoke, "AzureRmResourceAction", SupportsShouldProcess = true, DefaultParameterSetName = ResourceManipulationCmdletBase.ResourceIdParameterSet), OutputType(typeof(PSObject))]
27+
[Cmdlet(VerbsLifecycle.Invoke, "AzureRmResourceAction", SupportsShouldProcess = true, DefaultParameterSetName = ResourceManipulationCmdletBase.ResourceIdParameterSet),
28+
OutputType(typeof(PSResourceObject))]
2629
public sealed class InvokAzureResourceActionCmdlet : ResourceManipulationCmdletBase
2730
{
2831
/// <summary>

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/Resource/FindAzureResourceCmdlet.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2525
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
2626
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2727
using Newtonsoft.Json.Linq;
28+
using Models;
2829

2930
/// <summary>
3031
/// Cmdlet to get existing resources from ARM cache.
3132
/// </summary>
32-
[Cmdlet(VerbsCommon.Find, "AzureRmResource", DefaultParameterSetName = FindAzureResourceCmdlet.ListResourcesParameterSet), OutputType(typeof(Resource<JToken>))]
33+
[Cmdlet(VerbsCommon.Find, "AzureRmResource", DefaultParameterSetName = FindAzureResourceCmdlet.ListResourcesParameterSet), OutputType(typeof(PSResourceObject))]
3334
public sealed class FindAzureResourceCmdlet : ResourceManagerCmdletBase
3435
{
3536
/// <summary>
@@ -191,14 +192,14 @@ private void RunCmdlet()
191192
items = this.GetPopulatedResource(batch).Result;
192193
}
193194

194-
var powerShellObjects = items.SelectArray(genericResource => genericResource);
195+
var powerShellObjects = items.SelectArray(genericResource => genericResource.ToPSResourceObject());
195196

196197
this.WriteObject(sendToPipeline: powerShellObjects, enumerateCollection: true);
197198
}
198199
}
199200
else
200201
{
201-
this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(res => res.ToResource()), enumerateCollection: true);
202+
this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(res => res.ToResource().ToPSResourceObject()), enumerateCollection: true);
202203
}
203204
});
204205

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/Resource/GetAzureResourceCmdlet.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2525
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
2626
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2727
using Newtonsoft.Json.Linq;
28+
using Models;
2829

2930
/// <summary>
3031
/// Cmdlet to get existing resources.
3132
/// </summary>
32-
[Cmdlet(VerbsCommon.Get, "AzureRmResource", DefaultParameterSetName = GetAzureResourceCmdlet.ParameterlessSet), OutputType(typeof(Resource<JToken>))]
33+
[Cmdlet(VerbsCommon.Get, "AzureRmResource", DefaultParameterSetName = GetAzureResourceCmdlet.ParameterlessSet), OutputType(typeof(PSResourceObject))]
3334
public sealed class GetAzureResourceCmdlet : ResourceManagerCmdletBase
3435
{
3536
/// <summary>
@@ -228,14 +229,14 @@ private void RunCmdlet()
228229
items = this.GetPopulatedResource(batch).Result;
229230
}
230231

231-
var powerShellObjects = items.SelectArray(genericResource => genericResource);
232+
var powerShellObjects = items.SelectArray(genericResource => genericResource.ToPSResourceObject());
232233

233234
this.WriteObject(sendToPipeline: powerShellObjects, enumerateCollection: true);
234235
}
235236
}
236237
else
237238
{
238-
this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(res => res.ToResource()), enumerateCollection: true);
239+
this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(res => res.ToResource().ToPSResourceObject()), enumerateCollection: true);
239240
}
240241
});
241242

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/Resource/NewAzureResourceCmdlet.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1818
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
2020
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
21+
using Models;
2122
using Newtonsoft.Json.Linq;
2223
using System.Collections;
2324
using System.Linq;
@@ -30,7 +31,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
3031
"AzureRmResource",
3132
SupportsShouldProcess = true,
3233
DefaultParameterSetName = ResourceManipulationCmdletBase.ResourceIdParameterSet),
33-
OutputType(typeof(Resource<JToken>))]
34+
OutputType(typeof(PSResourceObject))]
3435
public sealed class NewAzureResourceCmdlet : ResourceManipulationCmdletBase
3536
{
3637
/// <summary>

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/Resource/SetAzureResourceCmdlet.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1818
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
2020
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
21+
using Models;
2122
using Newtonsoft.Json.Linq;
2223
using System.Collections;
2324
using System.Linq;
@@ -28,7 +29,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2829
/// A cmdlet that creates a new azure resource.
2930
/// </summary>
3031
[Cmdlet(VerbsCommon.Set, "AzureRmResource", SupportsShouldProcess = true, DefaultParameterSetName = ResourceManipulationCmdletBase.ResourceIdParameterSet),
31-
OutputType(typeof(Resource<JToken>))]
32+
OutputType(typeof(PSResourceObject))]
3233
public sealed class SetAzureResourceCmdlet : ResourceManipulationCmdletBase
3334
{
3435
/// <summary>

src/CLU/Commands.ResourceManager.Cmdlets/Implementation/ResourceManagerCmdletBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ protected void TryConvertToResourceAndWriteObject(string resultString)
301301
Resource<JToken> resultResource;
302302
if (resultString.TryConvertTo<Resource<JToken>>(out resultResource))
303303
{
304-
this.WriteObject(resultResource);
304+
this.WriteObject(resultResource.ToPSResourceObject());
305305
}
306306
else
307307
{
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.Azure.Commands.Utilities.Common;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Models
10+
{
11+
public class PSResourceObject
12+
{
13+
public string Name { get; set; }
14+
15+
public string ResourceId { get; set; }
16+
17+
public string ResourceName { get; set; }
18+
19+
public string ResourceType { get; set; }
20+
21+
public string ResourceGroupName { get; set; }
22+
23+
public string Location { get; set; }
24+
25+
public string SubscriptionId { get; set; }
26+
27+
public Dictionary<string,string> Properties { get; private set; }
28+
29+
public string PropertiesText
30+
{
31+
get
32+
{
33+
var sb = new StringBuilder();
34+
if (Properties.Count > 0)
35+
{
36+
sb.AppendLine();
37+
}
38+
Properties.ForEach(kvp =>
39+
sb.AppendLine(string.Format(" {0}: {1}", kvp.Key, kvp.Value).Replace("\r\n", "")));
40+
41+
return sb.ToString();
42+
}
43+
}
44+
45+
public PSResourceObject()
46+
{
47+
Properties = new Dictionary<string, string>();
48+
}
49+
50+
}
51+
}

src/CLU/Microsoft.Azure.Commands.Resources/Microsoft.Azure.Commands.Resources.format.ps1xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,5 +370,43 @@
370370
</ListEntries>
371371
</ListControl>
372372
</View>
373+
<View>
374+
<Name>Microsoft.Azure.Commands.Resources.Models.Authorization.PSRoleDefinition</Name>
375+
<ViewSelectedBy>
376+
<TypeName>Microsoft.Azure.Commands.Resources.Models.Authorization.PSRoleDefinition</TypeName>
377+
</ViewSelectedBy>
378+
<ListControl>
379+
<ListEntries>
380+
<ListEntry>
381+
<ListItems>
382+
<ListItem>
383+
<Label>Id</Label>
384+
<PropertyName>Id</PropertyName>
385+
</ListItem>
386+
<ListItem>
387+
<Label>IsCustom</Label>
388+
<PropertyName>IsCustom</PropertyName>
389+
</ListItem>
390+
<ListItem>
391+
<Label>Description</Label>
392+
<PropertyName>Description</PropertyName>
393+
</ListItem>
394+
<ListItem>
395+
<Label>Actions</Label>
396+
<PropertyName>ActionsText</PropertyName>
397+
</ListItem>
398+
<ListItem>
399+
<Label>NotActions</Label>
400+
<PropertyName>NotActionsText</PropertyName>
401+
</ListItem>
402+
<ListItem>
403+
<Label>AssignableScopes</Label>
404+
<PropertyName>AssignableScopesText</PropertyName>
405+
</ListItem>
406+
</ListItems>
407+
</ListEntry>
408+
</ListEntries>
409+
</ListControl>
410+
</View>
373411
</ViewDefinitions>
374412
</Configuration>

0 commit comments

Comments
 (0)