Skip to content

[#110362894]: Ensure correct screen formatting for Resource cmdlet output #1625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<View>
<Name>Microsoft.Azure.Commands.ResourceManager.Cmdlets.Models.PSResourceObject</Name>
<ViewSelectedBy>
<TypeName>Microsoft.Azure.Commands.ResourceManager.Cmdlets.Models.PSResourceObject</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<Label>Name</Label>
<PropertyName>Name</PropertyName>
</ListItem>
<ListItem>
<Label>ResourceId</Label>
<PropertyName>ResourceId</PropertyName>
</ListItem>
<ListItem>
<Label>ResourceName</Label>
<PropertyName>ResourceName</PropertyName>
</ListItem>
<ListItem>
<Label>ResourceType</Label>
<PropertyName>ResourceType</PropertyName>
</ListItem>
<ListItem>
<Label>ResourceGroupName</Label>
<PropertyName>ResourceGroupName</PropertyName>
</ListItem>
<ListItem>
<Label>Location</Label>
<PropertyName>Location</PropertyName>
</ListItem>
<ListItem>
<Label>SubscriptionId</Label>
<PropertyName>SubscriptionId</PropertyName>
</ListItem>
<ListItem>
<Label>Properties</Label>
<PropertyName>PropertiesText</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
</ViewDefinitions>
</Configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions
using Commands.Utilities.Common;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
using Models;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -26,11 +27,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions
/// </summary>
internal static class ResourceExtensions
{
/// <summary>
/// Converts a <see cref="Resource{JToken}"/> object into a <see cref="PSObject"/> object.
/// </summary>
/// <param name="resource">The <see cref="Resource{JToken}"/> object.</param>
internal static PSObject ToPsObject(this Resource<JToken> resource)
internal static PSResourceObject ToPSResourceObject(this Resource<JToken> resource)
{
var resourceType = string.IsNullOrEmpty(resource.Id)
? null
Expand All @@ -42,28 +39,34 @@ internal static PSObject ToPsObject(this Resource<JToken> resource)

var objectDefinition = new Dictionary<string, object>
{
{ "Name", resource.Name },
{ "ResourceId", string.IsNullOrEmpty(resource.Id) ? null : resource.Id },
{ "ResourceName", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceName(resource.Id) },
{ "ResourceType", resourceType },
{ "ExtensionResourceName", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetExtensionResourceName(resource.Id) },
{ "ExtensionResourceType", extensionResourceType },
{ "Kind", resource.Kind },
{ "ResourceGroupName", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceGroupName(resource.Id) },
{ "Location", resource.Location },
{ "SubscriptionId", string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetSubscriptionId(resource.Id) },
{ "Tags", TagsHelper.GetTagsHashtables(resource.Tags) },
{ "Plan", resource.Plan == null ? null : resource.Plan.ToJToken().ToPsObject() },
{ "Plan", resource.Plan == null ? null : resource.Plan.ToJToken() },
{ "Properties", ResourceExtensions.GetProperties(resource) },
{ "CreatedTime", resource.CreatedTime },
{ "ChangedTime", resource.ChangedTime },
{ "ETag", resource.ETag },
{ "Sku", resource.Sku == null ? null : resource.Sku.ToJToken().ToPsObject() },
{ "Sku", resource.Sku == null ? null : resource.Sku.ToJToken() },
};

var psObject =
PowerShellUtilities.ConstructPSObject(
objectDefinition.Where(kvp => kvp.Value != null).SelectManyArray(kvp => new[] { kvp.Key, kvp.Value }));
var psObject = new PSResourceObject();
psObject.Name = resource.Name;
psObject.ResourceId = string.IsNullOrEmpty(resource.Id) ? null : resource.Id;
psObject.ResourceName = string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceName(resource.Id);
psObject.ResourceType = resourceType;
psObject.ResourceGroupName = string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetResourceGroupName(resource.Id);
psObject.Location = resource.Location;
psObject.SubscriptionId = string.IsNullOrEmpty(resource.Id) ? null : ResourceIdUtility.GetSubscriptionId(resource.Id);


var objectProperties = objectDefinition.Where(kvp => kvp.Value != null).SelectManyArray(kvp => new[] { kvp.Key, kvp.Value });

for(int i=0; i< objectProperties.Length; i+=2)
{
psObject.Properties.Add(objectProperties[i].ToString(), objectProperties[i + 1].ToString());
}

return psObject;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Newtonsoft.Json.Linq;

using Models;
/// <summary>
/// Gets the deployment operation.
/// </summary>
[Cmdlet(VerbsCommon.Get, "AzureRmResourceGroupDeploymentOperation"), OutputType(typeof(Resource<JToken>))]
[Cmdlet(VerbsCommon.Get, "AzureRmResourceGroupDeploymentOperation"), OutputType(typeof(PSResourceObject))]
public class GetAzureResourceGroupDeploymentOperationCmdlet : ResourceManagerCmdletBase
{
/// <summary>
Expand Down Expand Up @@ -81,7 +81,9 @@ private void RunCmdlet()
getFirstPage: () => this.GetResources(),
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
cancellationToken: this.CancellationToken,
action: resources => this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(resource => resource.ToResource()), enumerateCollection: true));
action: resources => this.WriteObject(sendToPipeline: resources.CoalesceEnumerable()
.SelectArray(resource => resource.ToResource().ToPSResourceObject()),
enumerateCollection: true));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Newtonsoft.Json.Linq;
using Commands.Common;
using Models;

/// <summary>
/// A cmdlet that invokes a resource action.
/// </summary>
[Cmdlet(VerbsLifecycle.Invoke, "AzureRmResourceAction", SupportsShouldProcess = true, DefaultParameterSetName = ResourceManipulationCmdletBase.ResourceIdParameterSet), OutputType(typeof(PSObject))]
[Cmdlet(VerbsLifecycle.Invoke, "AzureRmResourceAction", SupportsShouldProcess = true, DefaultParameterSetName = ResourceManipulationCmdletBase.ResourceIdParameterSet),
OutputType(typeof(PSResourceObject))]
public sealed class InvokAzureResourceActionCmdlet : ResourceManipulationCmdletBase
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Newtonsoft.Json.Linq;
using Models;

/// <summary>
/// Cmdlet to get existing resources from ARM cache.
/// </summary>
[Cmdlet(VerbsCommon.Find, "AzureRmResource", DefaultParameterSetName = FindAzureResourceCmdlet.ListResourcesParameterSet), OutputType(typeof(Resource<JToken>))]
[Cmdlet(VerbsCommon.Find, "AzureRmResource", DefaultParameterSetName = FindAzureResourceCmdlet.ListResourcesParameterSet), OutputType(typeof(PSResourceObject))]
public sealed class FindAzureResourceCmdlet : ResourceManagerCmdletBase
{
/// <summary>
Expand Down Expand Up @@ -191,14 +192,14 @@ private void RunCmdlet()
items = this.GetPopulatedResource(batch).Result;
}

var powerShellObjects = items.SelectArray(genericResource => genericResource);
var powerShellObjects = items.SelectArray(genericResource => genericResource.ToPSResourceObject());

this.WriteObject(sendToPipeline: powerShellObjects, enumerateCollection: true);
}
}
else
{
this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(res => res.ToResource()), enumerateCollection: true);
this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(res => res.ToResource().ToPSResourceObject()), enumerateCollection: true);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Newtonsoft.Json.Linq;
using Models;

/// <summary>
/// Cmdlet to get existing resources.
/// </summary>
[Cmdlet(VerbsCommon.Get, "AzureRmResource", DefaultParameterSetName = GetAzureResourceCmdlet.ParameterlessSet), OutputType(typeof(Resource<JToken>))]
[Cmdlet(VerbsCommon.Get, "AzureRmResource", DefaultParameterSetName = GetAzureResourceCmdlet.ParameterlessSet), OutputType(typeof(PSResourceObject))]
public sealed class GetAzureResourceCmdlet : ResourceManagerCmdletBase
{
/// <summary>
Expand Down Expand Up @@ -228,14 +229,14 @@ private void RunCmdlet()
items = this.GetPopulatedResource(batch).Result;
}

var powerShellObjects = items.SelectArray(genericResource => genericResource);
var powerShellObjects = items.SelectArray(genericResource => genericResource.ToPSResourceObject());

this.WriteObject(sendToPipeline: powerShellObjects, enumerateCollection: true);
}
}
else
{
this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(res => res.ToResource()), enumerateCollection: true);
this.WriteObject(sendToPipeline: resources.CoalesceEnumerable().SelectArray(res => res.ToResource().ToPSResourceObject()), enumerateCollection: true);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Models;
using Newtonsoft.Json.Linq;
using System.Collections;
using System.Linq;
Expand All @@ -30,7 +31,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
"AzureRmResource",
SupportsShouldProcess = true,
DefaultParameterSetName = ResourceManipulationCmdletBase.ResourceIdParameterSet),
OutputType(typeof(Resource<JToken>))]
OutputType(typeof(PSResourceObject))]
public sealed class NewAzureResourceCmdlet : ResourceManipulationCmdletBase
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Models;
using Newtonsoft.Json.Linq;
using System.Collections;
using System.Linq;
Expand All @@ -28,7 +29,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
/// A cmdlet that creates a new azure resource.
/// </summary>
[Cmdlet(VerbsCommon.Set, "AzureRmResource", SupportsShouldProcess = true, DefaultParameterSetName = ResourceManipulationCmdletBase.ResourceIdParameterSet),
OutputType(typeof(Resource<JToken>))]
OutputType(typeof(PSResourceObject))]
public sealed class SetAzureResourceCmdlet : ResourceManipulationCmdletBase
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ protected void TryConvertToResourceAndWriteObject(string resultString)
Resource<JToken> resultResource;
if (resultString.TryConvertTo<Resource<JToken>>(out resultResource))
{
this.WriteObject(resultResource);
this.WriteObject(resultResource.ToPSResourceObject());
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.Azure.Commands.Utilities.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Models
{
public class PSResourceObject
{
public string Name { get; set; }

public string ResourceId { get; set; }

public string ResourceName { get; set; }

public string ResourceType { get; set; }

public string ResourceGroupName { get; set; }

public string Location { get; set; }

public string SubscriptionId { get; set; }

public Dictionary<string,string> Properties { get; private set; }

public string PropertiesText
{
get
{
var sb = new StringBuilder();
if (Properties.Count > 0)
{
sb.AppendLine();
}
Properties.ForEach(kvp =>
sb.AppendLine(string.Format(" {0}: {1}", kvp.Key, kvp.Value).Replace("\r\n", "")));

return sb.ToString();
}
}

public PSResourceObject()
{
Properties = new Dictionary<string, string>();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -370,5 +370,43 @@
</ListEntries>
</ListControl>
</View>
<View>
<Name>Microsoft.Azure.Commands.Resources.Models.Authorization.PSRoleDefinition</Name>
<ViewSelectedBy>
<TypeName>Microsoft.Azure.Commands.Resources.Models.Authorization.PSRoleDefinition</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<Label>Id</Label>
<PropertyName>Id</PropertyName>
</ListItem>
<ListItem>
<Label>IsCustom</Label>
<PropertyName>IsCustom</PropertyName>
</ListItem>
<ListItem>
<Label>Description</Label>
<PropertyName>Description</PropertyName>
</ListItem>
<ListItem>
<Label>Actions</Label>
<PropertyName>ActionsText</PropertyName>
</ListItem>
<ListItem>
<Label>NotActions</Label>
<PropertyName>NotActionsText</PropertyName>
</ListItem>
<ListItem>
<Label>AssignableScopes</Label>
<PropertyName>AssignableScopesText</PropertyName>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
</ViewDefinitions>
</Configuration>
Loading