Skip to content

Clu #317

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 6 commits into from
Jan 12, 2016
Merged

Clu #317

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
3 changes: 3 additions & 0 deletions src/CLU/Commands.Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System.Linq;
using System.Threading;
using Microsoft.Azure.Commands.Common.Authentication.Factories;
using Microsoft.Rest;

namespace Microsoft.Azure.Commands.Utilities.Common
{
Expand Down Expand Up @@ -299,6 +300,8 @@ protected override void BeginProcessing()
ModuleName, string.Format("v{0}", ModuleVersion));
ClientFactory.UserAgents.Add(userAgentValue);
ClientFactory.AddHandler(new CmdletInfoHandler(this.CommandRuntime.ToString(), this.ParameterSetName));
ServiceClientTracing.AddTracingInterceptor(_adalListener);
ServiceClientTracing.IsEnabled = true;
base.BeginProcessing();
}

Expand Down
43 changes: 42 additions & 1 deletion src/CLU/Commands.Common/DebugStreamTraceListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Utilities.Common;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;

namespace Microsoft.Azure.Commands.Common
{
public class DebugStreamTraceListener : TraceListener
public class DebugStreamTraceListener : TraceListener, IServiceClientTracingInterceptor
{
public DebugStreamTraceListener(ConcurrentQueue<string> queue)
{
Expand Down Expand Up @@ -46,5 +52,40 @@ public static void RemoveAdalTracing(DebugStreamTraceListener listener)
{
//AdalTrace.TraceSource.Listeners.Remove(listener);
}

public void Configuration(string source, string name, string value)
{
}

public void ExitMethod(string invocationId, object returnValue)
{
WriteLine($"{invocationId}: End method with return value {returnValue}");
}

public void Information(string message)
{
}

public void TraceError(string invocationId, Exception exception)
{
WriteLine($"{invocationId}: Exception {exception}");
}

public void SendRequest(string invocationId, HttpRequestMessage request)
{
WriteLine($"{invocationId} REQUEST:");
WriteLine($"{GeneralUtilities.GetLog(request)}");
}

public void ReceiveResponse(string invocationId, HttpResponseMessage response)
{
WriteLine($"{invocationId} RESPONSE:");
WriteLine($"{GeneralUtilities.GetLog(response)}");
}

public void EnterMethod(string invocationId, object instance, string method, IDictionary<string, object> parameters)
{
WriteLine($"{invocationId}: Start method {method} with return value");
}
}
}
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
@@ -0,0 +1,2 @@
new: create
StorageAccountNameAvailability: storage dns name
Loading