Skip to content

Commit 64f8240

Browse files
Anthony StaplesFrancisco-Gamino
authored andcommitted
New-PM Spike
1 parent 565ea8b commit 64f8240

File tree

6 files changed

+211
-0
lines changed

6 files changed

+211
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using CSharpx;
7+
using System;
8+
using System.Collections;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
using System.Management.Automation;
12+
using System.Management.Automation.Runspaces;
13+
14+
namespace Microsoft.Azure.Functions.PowerShellWorker.Commands
15+
{
16+
/// <summary>
17+
/// Registers an Azure Functions with the new Powershell programming model
18+
/// </summary>
19+
/// <remarks>
20+
/// </remarks>
21+
[Cmdlet(VerbsLifecycle.Register, "AzureFunction")]
22+
public sealed class RegisterAzureFunctionCommand : PSCmdlet
23+
{
24+
/// <summary>
25+
/// The name of the Azure Function to register
26+
/// </summary>
27+
[Parameter(Mandatory = true, Position = 0)]
28+
public string Name { get; set; }
29+
30+
/// <summary>
31+
/// The names of the Bindings you want to associate with this Function
32+
/// </summary>
33+
[Parameter(Mandatory = true, Position = 1)]
34+
public object Bindings { get; set; }
35+
36+
/// <summary>
37+
/// ProcessRecord override.
38+
/// </summary>
39+
protected override void ProcessRecord()
40+
{
41+
WorkerIndexingHelper.RegisterFunction(Name, ProcessBindings(Bindings));
42+
}
43+
44+
private List<string> ProcessBindings(object bindings)
45+
{
46+
bool isBindingsEnumerable = LanguagePrimitives.IsObjectEnumerable(Bindings);
47+
if (isBindingsEnumerable)
48+
{
49+
return (Bindings as List<object>).Select(x => x.ToString()).ToList();
50+
}
51+
else
52+
{
53+
return new List<string> { bindings.ToString() };
54+
}
55+
}
56+
}
57+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
7+
using System;
8+
using System.Collections;
9+
using System.Management.Automation;
10+
11+
namespace Microsoft.Azure.Functions.PowerShellWorker.Commands
12+
{
13+
/// <summary>
14+
/// Registers an Azure Functions with the new Powershell programming model
15+
/// </summary>
16+
/// <remarks>
17+
/// </remarks>
18+
[Cmdlet(VerbsLifecycle.Register, "Binding")]
19+
public sealed class RegisterBindingCommand : PSCmdlet
20+
{
21+
/// <summary>
22+
/// The name of the Azure Functions Binding to register
23+
/// </summary>
24+
[Parameter(Mandatory = true, Position = 0)]
25+
public string Name { get; set; }
26+
27+
/// <summary>
28+
/// The type of this binding
29+
/// </summary>
30+
[Parameter()]
31+
public string Type { get; set; }
32+
33+
/// <summary>
34+
/// The direction of this binding
35+
/// </summary>
36+
[Parameter()]
37+
public string Direction { get; set; }
38+
39+
/// <summary>
40+
/// The data type of this binding
41+
/// </summary>
42+
[Parameter()]
43+
public string DataType { get; set; }
44+
45+
/// <summary>
46+
/// The auth level of this binding
47+
/// </summary>
48+
[Parameter()]
49+
public object OtherInfo { get; set; }
50+
51+
/// <summary>
52+
/// ProcessRecord override.
53+
/// </summary>
54+
protected override void ProcessRecord()
55+
{
56+
BindingInfo info = new BindingInfo();
57+
info.Type = Type;
58+
info.Direction = ParseDirection(Direction);
59+
info.DataType = ParseDataType(DataType);
60+
61+
WorkerIndexingHelper.RegisterBinding(Name, info);
62+
}
63+
64+
private BindingInfo.Types.DataType ParseDataType(string dataType)
65+
{
66+
switch (dataType)
67+
{
68+
case "string":
69+
return BindingInfo.Types.DataType.String;
70+
case "binary":
71+
return BindingInfo.Types.DataType.Binary;
72+
case "stream":
73+
return BindingInfo.Types.DataType.Stream;
74+
default:
75+
return BindingInfo.Types.DataType.Undefined;
76+
}
77+
}
78+
79+
private BindingInfo.Types.Direction ParseDirection(string direction)
80+
{
81+
switch(direction)
82+
{
83+
case "in":
84+
return BindingInfo.Types.Direction.In;
85+
case "out":
86+
return BindingInfo.Types.Direction.Out;
87+
case "inout":
88+
return BindingInfo.Types.Direction.Inout;
89+
default:
90+
string errorMsg = string.Format(PowerShellWorkerStrings.InvalidBindingDirection, direction);
91+
ErrorRecord er = new ErrorRecord(
92+
new InvalidOperationException(errorMsg),
93+
nameof(PowerShellWorkerStrings.BindingNameNotExist),
94+
ErrorCategory.InvalidOperation,
95+
targetObject: direction);
96+
97+
this.ThrowTerminatingError(er);
98+
break;
99+
}
100+
return BindingInfo.Types.Direction.Inout;
101+
}
102+
}
103+
}

src/RequestProcessor.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ internal RequestProcessor(MessagingStream msgStream, System.Management.Automatio
6767
_requestHandlers.Add(StreamingMessage.ContentOneofCase.InvocationCancel, ProcessInvocationCancelRequest);
6868

6969
_requestHandlers.Add(StreamingMessage.ContentOneofCase.FunctionEnvironmentReloadRequest, ProcessFunctionEnvironmentReloadRequest);
70+
71+
_requestHandlers.Add(StreamingMessage.ContentOneofCase.FunctionsMetadataRequest, ProcessFunctionMetadataRequest);
7072
}
7173

7274
internal async Task ProcessRequestLoop()
@@ -366,6 +368,18 @@ internal StreamingMessage ProcessFunctionEnvironmentReloadRequest(StreamingMessa
366368
return response;
367369
}
368370

371+
internal StreamingMessage ProcessFunctionMetadataRequest(StreamingMessage request)
372+
{
373+
//_ = FunctionLoader.ParseFunctions(request.FunctionsMetadataRequest.FunctionAppDirectory);
374+
375+
StreamingMessage response = NewStreamingMessageTemplate(
376+
request.RequestId,
377+
StreamingMessage.ContentOneofCase.FunctionMetadataResponses,
378+
out StatusResult status);
379+
380+
return response;
381+
}
382+
369383
#region Helper_Methods
370384

371385
/// <summary>
@@ -394,6 +408,9 @@ private StreamingMessage NewStreamingMessageTemplate(string requestId, Streaming
394408
case StreamingMessage.ContentOneofCase.FunctionEnvironmentReloadResponse:
395409
response.FunctionEnvironmentReloadResponse = new FunctionEnvironmentReloadResponse() { Result = status };
396410
break;
411+
case StreamingMessage.ContentOneofCase.FunctionMetadataResponses:
412+
response.FunctionMetadataResponses = new FunctionMetadataResponses() { Result = status };
413+
break;
397414
default:
398415
throw new InvalidOperationException("Unreachable code.");
399416
}

src/WorkerIndexingHelper.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Microsoft.Azure.Functions.PowerShellWorker
9+
{
10+
internal static class WorkerIndexingHelper
11+
{
12+
private static Dictionary<string, List<string>> functions = new Dictionary<string, List<string>>();
13+
private static Dictionary<string, BindingInfo> bindings = new Dictionary<string, BindingInfo>();
14+
15+
internal static void RegisterFunction(string functionName, List<String> bindingNames)
16+
{
17+
functions[functionName] = bindingNames;
18+
}
19+
20+
internal static void RegisterBinding(string bindingName, BindingInfo binding)
21+
{
22+
bindings[bindingName] = binding;
23+
}
24+
25+
internal static FunctionMetadataResponses FormatMetadata()
26+
{
27+
28+
}
29+
}
30+
}

src/resources/PowerShellWorkerStrings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@
217217
<data name="BindingNameNotExist" xml:space="preserve">
218218
<value>The specified name '{0}' cannot be resolved to a valid output binding of this function.</value>
219219
</data>
220+
<data name="InvalidBindingDirection" xml:space="preserve">
221+
<value>The specified binding direction '{0}' cannot be resolved to a valid binding direction.</value>
222+
</data>
220223
<data name="UnrecognizedBehavior" xml:space="preserve">
221224
<value>Unrecognized data collecting behavior '{0}'.</value>
222225
</data>

src/worker.config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"extensions":[".ps1", ".psm1"],
55
"defaultExecutablePath":"dotnet",
66
"defaultWorkerPath":"%FUNCTIONS_WORKER_RUNTIME_VERSION%/Microsoft.Azure.Functions.PowerShellWorker.dll",
7+
"AzureWebjobsFeatureFlags":"EnableWorkerIndexing",
78
"supportedRuntimeVersions":["7", "7.2"],
89
"defaultRuntimeVersion": "7",
910
"sanitizeRuntimeVersionRegex":"\\d+\\.?\\d*"

0 commit comments

Comments
 (0)