|
| 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 | +} |
0 commit comments