Skip to content

Commit

Permalink
Merge pull request #12 from ArieShout/menxiao/signalr
Browse files Browse the repository at this point in the history
SignalR Service PowerShell module continued
  • Loading branch information
sergey-shandar authored Apr 19, 2018
2 parents 740b137 + daae454 commit 28bfbba
Show file tree
Hide file tree
Showing 22 changed files with 1,131 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# RootModule = ''

# Version number of this module.
ModuleVersion = '0.0.1'
ModuleVersion = '0.1.0'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,24 @@ namespace Microsoft.Azure.Commands.SignalR
{
[Cmdlet(VerbsCommon.Get, SignalRNoun, DefaultParameterSetName = ListSignalRServiceParameterSet)]
[OutputType(typeof(PSSignalRResource))]
public class GetAzureRmSignalR : SignalRCmdletBase
public class GetAzureRmSignalR : SignalRCmdletBase, IWithResourceId
{
[Parameter(Position = 0,
Mandatory = false,
ParameterSetName = ListSignalRServiceParameterSet,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Resource group name.")]
HelpMessage = "The resource group name.")]
[Parameter(Position = 0,
Mandatory = true,
Mandatory = false,
ParameterSetName = ResourceGroupParameterSet,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Resource group name.")]
HelpMessage = "The resource group name. The default one will be used if not specified.")]
[ResourceGroupCompleter()]
[ValidateNotNullOrEmpty]
public string ResourceGroupName { get; set; }
public override string ResourceGroupName { get; set; }

[Parameter(Position = 1,
Mandatory = true,
ValueFromPipelineByPropertyName = true,
ParameterSetName = ResourceGroupParameterSet,
HelpMessage = "SignalR service name.")]
HelpMessage = "The SignalR service name.")]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

Expand Down Expand Up @@ -58,15 +55,17 @@ public override void ExecuteCmdlet()
WriteObject(new PSSignalRResource(s));
}
break;
case ResourceIdParameterSet:
this.LoadFromResourceId();
var signalrById = Client.Signalr.Get(ResourceGroupName, Name);
WriteObject(new PSSignalRResource(signalrById));
break;
case ResourceGroupParameterSet:
ResolveResourceGroupName();
var signalr = Client.Signalr.Get(ResourceGroupName, Name);
WriteObject(new PSSignalRResource(signalr));
break;
case ResourceIdParameterSet:
var resource = new ResourceIdentifier(ResourceId);
var idSignalR = Client.Signalr.Get(resource.ResourceGroupName, resource.ResourceName);
WriteObject(new PSSignalRResource(idSignalR));
break;

default:
throw new ArgumentException(Resources.ParameterSetError);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
using System;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.SignalR.Cmdlets
namespace Microsoft.Azure.Commands.SignalR
{
[Cmdlet(VerbsCommon.Get, SignalRKeyNoun, DefaultParameterSetName = ResourceGroupParameterSet)]
[OutputType(typeof(PSSignalRKeys))]
public class GetAzureRmSignalRKey : SignalRCmdletBase
public class GetAzureRmSignalRKey : SignalRCmdletBase, IWithInputObject, IWithResourceId
{
[Parameter(Position = 0,
Mandatory = true,
Mandatory = false,
ParameterSetName = ResourceGroupParameterSet,
HelpMessage = "Resource group name.")]
HelpMessage = "The resource group name. The default one will be used if not specified.")]
[ResourceGroupCompleter()]
[ValidateNotNullOrEmpty]
public string ResourceGroupName { get; set; }
public override string ResourceGroupName { get; set; }

[Parameter(Position = 1,
Mandatory = true,
ParameterSetName = ResourceGroupParameterSet,
HelpMessage = "SignalR service name.")]
HelpMessage = "The SignalR service name.")]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

Expand All @@ -47,25 +47,20 @@ public override void ExecuteCmdlet()

RunCmdlet(() =>
{
ResourceIdentifier resourceId = null;
switch (ParameterSetName)
{
case ResourceGroupParameterSet:
ResolveResourceGroupName();
break;
case ResourceIdParameterSet:
resourceId = new ResourceIdentifier(ResourceId);
this.LoadFromResourceId();
break;
case InputObjectParameterSet:
resourceId = new ResourceIdentifier(InputObject.Id);
this.LoadFromInputObject();
break;
default:
throw new ArgumentException(Resources.ParameterSetError);
}
if (resourceId != null)
{
ResourceGroupName = resourceId.ResourceGroupName;
Name = resourceId.ResourceName;
}

var keys = Client.Signalr.ListKeys(ResourceGroupName, Name);
WriteObject(new PSSignalRKeys(Name, keys));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.Azure.Commands.SignalR.Models;
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.SignalR
{
public interface IWithInputObject : IWithResourceGroupAndName
{
PSSignalRResource InputObject { get; }
}

public static class IWithInputObjectExtensions
{
public static void LoadFromInputObject(this IWithInputObject cmdlet)
{
var resourceId = new ResourceIdentifier(cmdlet.InputObject.Id);
cmdlet.ResourceGroupName = resourceId.ResourceGroupName;
cmdlet.Name = resourceId.ResourceName;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.SignalR
{
public interface IWithResourceGroupAndName
{
string ResourceGroupName { get; set; }
string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Azure.Commands.SignalR
{
public interface IWithResourceId : IWithResourceGroupAndName
{
string ResourceId { get; }
}

public static class IWithResourceIdExtensions
{
public static void LoadFromResourceId(this IWithResourceId cmdlet)
{
var resourceId = new ResourceIdentifier(cmdlet.ResourceId);
cmdlet.ResourceGroupName = resourceId.ResourceGroupName;
cmdlet.Name = resourceId.ResourceName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,55 +27,48 @@

namespace Microsoft.Azure.Commands.SignalR
{
[Cmdlet(VerbsCommon.New, "AzureRmSignalR", SupportsShouldProcess = true)]
[Cmdlet(VerbsCommon.New, SignalRNoun, SupportsShouldProcess = true)]
[OutputType(typeof(PSSignalRResource))]
public sealed class NewAzureRmSignalR : AzureRMCmdlet
public sealed class NewAzureRmSignalR : SignalRCmdletBase
{
/// <summary>
/// TODO: smart command with default resource group name
/// </summary>
private const string DefaultSku = "Basic_DS2";

[Parameter(
Mandatory = false,
Position = 0)]
Position = 0,
HelpMessage = "The resource group name. The default one will be used if not specified.")]
[ValidateNotNullOrEmpty()]
public string ResourceGroupName { get; set; }
public override string ResourceGroupName { get; set; }

[Parameter(
Mandatory = true,
Position = 1)]
Position = 1,
HelpMessage = "The SignalR service name.")]
[ValidateNotNullOrEmpty()]
public string Name { get; set; }

/// <summary>
/// TODO: smart command with default location based on ResourceGroupName
/// </summary>
[Parameter(
Mandatory = false,
Position = 2)]
[LocationCompleter("Microsoft.SignalR/signalRs")]
Position = 2,
HelpMessage = "The SignalR service location. The resource group location will be used if not specified.")]
[LocationCompleter("Microsoft.SignalR/SignalR")]
[ValidateNotNullOrEmpty()]
public string Location { get; set; }

/// <summary>
/// TODO:
/// - Assign default value.
/// - validation set or tab completion
/// </summary>
[Parameter(Mandatory = false)]
public string Sku { get; set; }

/// <summary>
/// TODO:
/// - Default host name prefix.
/// - alias `DomainNameLabel`
/// </summary>
[Parameter(Mandatory = false)]
public string HostNamePrefix { get; set; }

[Parameter(Mandatory = false)]
[Parameter(
Mandatory = false,
HelpMessage = "The SignalR service SKU.")]
[PSArgumentCompleter("Basic_DS2")]
public string Sku { get; set; } = DefaultSku;

[Parameter(
Mandatory = false,
HelpMessage = "The tags for the SignalR service.")]
public IDictionary<string, string> Tag { get; set; }

[Parameter(Mandatory = false)]
[Parameter(
Mandatory = false,
HelpMessage = "Run the cmdlet in background job.")]
public SwitchParameter AsJob { get; set; }

public override void ExecuteCmdlet()
Expand Down Expand Up @@ -106,6 +99,7 @@ public Parameters(NewAzureRmSignalR cmdlet)
public async Task<ResourceConfig<SignalRResource>> CreateConfigAsync()
{
_cmdlet.ResourceGroupName = _cmdlet.ResourceGroupName ?? _cmdlet.Name;
_cmdlet.ResolveResourceGroupName();

var resourceGroup = ResourceGroupStrategy.CreateResourceGroupConfig(
_cmdlet.ResourceGroupName);
Expand All @@ -115,8 +109,8 @@ public async Task<ResourceConfig<SignalRResource>> CreateConfigAsync()
name: _cmdlet.Name,
createModel: engine => new SignalRResource(
tags: _cmdlet.Tag,
signalrsku: _cmdlet.Sku == null ? null : new ResourceSku(name: _cmdlet.Sku),
hostNamePrefix: _cmdlet.HostNamePrefix));
signalrsku: new ResourceSku(_cmdlet.Sku, capacity: 1), // we only allow capacity 1 in public preview, this may be a parameter in future.
hostNamePrefix: _cmdlet.Name)); // hostNamePrefix is just a placeholder and ignored in the resource provider.
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
using System;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.SignalR.Cmdlets
namespace Microsoft.Azure.Commands.SignalR
{
[Cmdlet(VerbsCommon.New, SignalRKeyNoun, SupportsShouldProcess = true, DefaultParameterSetName = ResourceGroupParameterSet)]
[OutputType(typeof(PSSignalRKeys))]
public class NewAzureRmSignalRKey : SignalRCmdletBase
[OutputType(typeof(bool))]
public class NewAzureRmSignalRKey : SignalRCmdletBase, IWithInputObject, IWithResourceId
{
[Parameter(Position = 0,
Mandatory = true,
Mandatory = false,
ParameterSetName = ResourceGroupParameterSet,
HelpMessage = "Resource group name.")]
HelpMessage = "The resource group name. The default one will be used if not specified.")]
[ResourceGroupCompleter()]
[ValidateNotNullOrEmpty]
public string ResourceGroupName { get; set; }
public override string ResourceGroupName { get; set; }

[Parameter(Position = 1,
Mandatory = true,
ParameterSetName = ResourceGroupParameterSet,
HelpMessage = "SignalR service name.")]
HelpMessage = "The SignalR service name.")]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

Expand All @@ -47,36 +47,38 @@ public class NewAzureRmSignalRKey : SignalRCmdletBase
[ValidateSet("Primary", "Secondary", IgnoreCase = true)]
public string KeyType { get; set; }

[Parameter(Mandatory = false)]
public SwitchParameter PassThru { get; set; }

public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();

RunCmdlet(() =>
{
ResourceIdentifier resourceId = null;
switch (ParameterSetName)
{
case ResourceGroupParameterSet:
ResolveResourceGroupName();
break;
case ResourceIdParameterSet:
resourceId = new ResourceIdentifier(ResourceId);
this.LoadFromResourceId();
break;
case InputObjectParameterSet:
resourceId = new ResourceIdentifier(InputObject.Id);
this.LoadFromInputObject();
break;
default:
throw new ArgumentException(Resources.ParameterSetError);
}
if (resourceId != null)
{
ResourceGroupName = resourceId.ResourceGroupName;
Name = resourceId.ResourceName;
}

if (ShouldProcess($"{KeyType} key for {ResourceGroupName}/{Name}", "regenerate"))
{
var keys = Client.Signalr.RegenerateKey(ResourceGroupName, Name, new RegenerateKeyParameters(KeyType));
WriteObject(new PSSignalRKeys(Name, keys));
Client.Signalr.RegenerateKey(ResourceGroupName, Name, new RegenerateKeyParameters(KeyType));

if (PassThru)
{
WriteObject(true);
}
}
});
}
Expand Down
Loading

0 comments on commit 28bfbba

Please sign in to comment.