12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
+ using System . Collections ;
16
+ using System . Collections . Generic ;
17
+ using System . Management . Automation ;
15
18
using Microsoft . Azure . Commands . Common . Strategies ;
16
19
using Microsoft . Azure . Commands . ResourceManager . Common . ArgumentCompleters ;
17
20
using Microsoft . Azure . Commands . SignalR . Models ;
18
- using Microsoft . Azure . Commands . SignalR . Strategies ;
21
+ using Microsoft . Azure . Management . Internal . Resources ;
22
+ using Microsoft . Azure . Management . SignalR ;
19
23
using Microsoft . Azure . Management . SignalR . Models ;
20
- using System . Collections . Generic ;
21
- using System . Management . Automation ;
22
- using System . Threading . Tasks ;
23
- using Microsoft . Azure . Commands . SignalR . Strategies . ResourceManager ;
24
- using Microsoft . Azure . Commands . SignalR . Strategies . SignalRRp ;
24
+ using Microsoft . WindowsAzure . Commands . Common ;
25
+ using Newtonsoft . Json ;
25
26
26
27
namespace Microsoft . Azure . Commands . SignalR . Cmdlets
27
28
{
@@ -30,6 +31,7 @@ namespace Microsoft.Azure.Commands.SignalR.Cmdlets
30
31
public sealed class NewAzureRmSignalR : SignalRCmdletBase
31
32
{
32
33
private const string DefaultSku = "Standard_S1" ;
34
+ private const int DefaultUnitCount = 1 ;
33
35
34
36
[ Parameter (
35
37
Mandatory = false ,
@@ -53,88 +55,86 @@ public sealed class NewAzureRmSignalR : SignalRCmdletBase
53
55
54
56
[ Parameter (
55
57
Mandatory = false ,
56
- HelpMessage = "The SignalR service SKU." ) ]
58
+ HelpMessage = "The SignalR service SKU. Default to \" Standard_S1 \" . " ) ]
57
59
[ PSArgumentCompleter ( "Free_F1" , "Standard_S1" ) ]
58
- public string Sku { get ; set ; } = DefaultSku ;
60
+ public string Sku { get ; set ; }
59
61
60
62
[ Parameter (
61
63
Mandatory = false ,
62
- HelpMessage = "The SignalR service unit count, from 1 to 10. Default to 1." ) ]
63
- [ PSArgumentCompleter ( "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" ) ]
64
- [ ValidateRange ( 1 , 10 ) ]
65
- public int UnitCount { get ; set ; } = 1 ;
64
+ HelpMessage = "The SignalR service unit count, value only from {1, 2, 5, 10, 20, 50, 100}. Default to 1." ) ]
65
+ [ PSArgumentCompleter ( "1" , "2" , "5" , "10" , "20" , "50" , "100" ) ]
66
+ public int UnitCount { get ; set ; } = DefaultUnitCount ;
66
67
67
68
[ Parameter (
68
69
Mandatory = false ,
69
70
HelpMessage = "The tags for the SignalR service." ) ]
70
71
public IDictionary < string , string > Tag { get ; set ; }
71
72
73
+ [ Parameter (
74
+ Mandatory = false ,
75
+ HelpMessage = "The service mode for the SignalR service." ) ]
76
+ [ PSArgumentCompleter ( "Default" , "Serverless" , "Classic" ) ]
77
+ public string ServiceMode { get ; set ; }
78
+
79
+ [ Parameter (
80
+ Mandatory = false ,
81
+ HelpMessage = "The allowed origins for the SignalR service. To allow all, use \" *\" and remove all other origins from the list. Slashes are not allowed as part of domain or after top-level domain" ) ]
82
+ public string [ ] AllowedOrigin { get ; set ; }
83
+
72
84
[ Parameter (
73
85
Mandatory = false ,
74
86
HelpMessage = "Run the cmdlet in background job." ) ]
75
87
public SwitchParameter AsJob { get ; set ; }
76
88
77
89
public override void ExecuteCmdlet ( )
78
- => this . StartAndWait ( SimpleExecuteCmdlet ) ;
79
-
80
- sealed class Parameters : IParameters < SignalRResource >
81
90
{
82
- public string Location
91
+ base . ExecuteCmdlet ( ) ;
92
+
93
+ RunCmdlet ( ( ) =>
83
94
{
84
- get
85
- {
86
- return _cmdlet . Location ;
87
- }
95
+ ResolveResourceGroupName ( required : false ) ;
96
+ ResourceGroupName = ResourceGroupName ?? Name ;
88
97
89
- set
98
+ if ( ShouldProcess ( $ "SignalR service { ResourceGroupName } / { Name } " , "new" ) )
90
99
{
91
- _cmdlet . Location = value ;
100
+ PromptParameter ( nameof ( ResourceGroupName ) , ResourceGroupName ) ;
101
+ PromptParameter ( nameof ( Name ) , Name ) ;
102
+
103
+ if ( Location == null )
104
+ {
105
+ Location = GetLocationFromResourceGroup ( ) ;
106
+ PromptParameter ( nameof ( Location ) , null , true , Location , "(from resource group location)" ) ;
107
+ }
108
+ else
109
+ {
110
+ PromptParameter ( nameof ( Location ) , Location ) ;
111
+ }
112
+
113
+ PromptParameter ( nameof ( Sku ) , Sku , true , DefaultSku ) ;
114
+ PromptParameter ( nameof ( UnitCount ) , UnitCount ) ;
115
+ PromptParameter ( nameof ( Tag ) , Tag == null ? null : JsonConvert . SerializeObject ( Tag ) ) ;
116
+ PromptParameter ( nameof ( ServiceMode ) , ServiceMode ) ;
117
+
118
+ IList < string > origins = ParseAndCheckAllowedOrigins ( AllowedOrigin ) ;
119
+ PromptParameter ( nameof ( AllowedOrigin ) , origins == null ? null : JsonConvert . SerializeObject ( origins ) ) ;
120
+
121
+ Sku = Sku ?? DefaultSku ;
122
+
123
+ IList < SignalRFeature > features = ServiceMode == null ? null : new List < SignalRFeature > { new SignalRFeature ( value : ServiceMode ) } ;
124
+ SignalRCorsSettings cors = AllowedOrigin == null ? null : new SignalRCorsSettings ( allowedOrigins : origins ) ;
125
+
126
+ var parameters = new SignalRCreateParameters (
127
+ location : Location ,
128
+ tags : Tag ,
129
+ sku : new ResourceSku ( name : Sku , capacity : UnitCount ) ,
130
+ properties : new SignalRCreateOrUpdateProperties ( features : features , cors : cors ) ) ;
131
+
132
+ Client . SignalR . CreateOrUpdate ( ResourceGroupName , Name , parameters ) ;
133
+
134
+ var signalr = Client . SignalR . Get ( ResourceGroupName , Name ) ;
135
+ WriteObject ( new PSSignalRResource ( signalr ) ) ;
92
136
}
93
- }
94
-
95
- public string DefaultLocation => "eastus" ;
96
-
97
- readonly NewAzureRmSignalR _cmdlet ;
98
-
99
- public Parameters ( NewAzureRmSignalR cmdlet )
100
- {
101
- _cmdlet = cmdlet ;
102
- }
103
-
104
- public Task < ResourceConfig < SignalRResource > > CreateConfigAsync ( )
105
- {
106
- _cmdlet . ResolveResourceGroupName ( required : false ) ;
107
- _cmdlet . ResourceGroupName = _cmdlet . ResourceGroupName ?? _cmdlet . Name ;
108
-
109
- var resourceGroup = ResourceGroupStrategy . CreateResourceGroupConfig (
110
- _cmdlet . ResourceGroupName ) ;
111
-
112
- var result = SignalRStrategy . Strategy . CreateResourceConfig (
113
- resourceGroup : resourceGroup ,
114
- name : _cmdlet . Name ,
115
- createModel : engine => new SignalRResource (
116
- tags : _cmdlet . Tag ,
117
- sku : new ResourceSku ( _cmdlet . Sku , capacity : _cmdlet . UnitCount ) ,
118
- hostNamePrefix : null /* _cmdlet.Name*/ ) ) ; // hostNamePrefix is just a placeholder and ignored in the resource provider.
119
-
120
- return Task . FromResult ( result ) ;
121
- }
122
- }
123
-
124
- async Task SimpleExecuteCmdlet ( IAsyncCmdlet asyncCmdlet )
125
- {
126
- var client = new Client ( DefaultProfile . DefaultContext ) ;
127
-
128
- var parameters = new Parameters ( this ) ;
129
-
130
- var result = await client . RunAsync (
131
- client . SubscriptionId , parameters , asyncCmdlet ) ;
132
-
133
- if ( result != null )
134
- {
135
- var psResult = new PSSignalRResource ( result ) ;
136
- asyncCmdlet . WriteObject ( psResult ) ;
137
- }
137
+ } ) ;
138
138
}
139
139
}
140
140
}
0 commit comments