コード例 #1
0
        internal AzFunctionInfo(RpcFunctionMetadata metadata)
        {
            FunctionName = metadata.Name;
            Directory    = metadata.Directory;
            EntryPoint   = metadata.EntryPoint;
            ScriptPath   = metadata.ScriptFile;

            AllBindings    = new MapField <string, BindingInfo>();
            OutputBindings = new MapField <string, BindingInfo>();

            foreach (var binding in metadata.Bindings)
            {
                string      bindingName = binding.Key;
                BindingInfo bindingInfo = binding.Value;

                AllBindings.Add(bindingName, bindingInfo);

                // PowerShell doesn't support the 'InOut' type binding
                if (bindingInfo.Direction == BindingInfo.Types.Direction.In)
                {
                    switch (bindingInfo.Type)
                    {
                    case OrchestrationTrigger:
                        Type = AzFunctionType.OrchestrationFunction;
                        break;

                    case ActivityTrigger:
                        Type = AzFunctionType.ActivityFunction;
                        break;

                    default:
                        Type = AzFunctionType.RegularFunction;
                        break;
                    }
                    continue;
                }

                if (bindingInfo.Direction == BindingInfo.Types.Direction.Out)
                {
                    OutputBindings.Add(bindingName, bindingInfo);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Construct an object of AzFunctionInfo from the 'RpcFunctionMetadata'.
        /// Necessary validations are done on the metadata and script.
        /// </summary>
        internal AzFunctionInfo(RpcFunctionMetadata metadata)
        {
            FuncName      = metadata.Name;
            FuncDirectory = metadata.Directory;
            EntryPoint    = metadata.EntryPoint;
            ScriptPath    = metadata.ScriptFile;

            // Support 'entryPoint' only if 'scriptFile' is a .psm1 file;
            // Support .psm1 'scriptFile' only if 'entryPoint' is specified.
            bool isScriptFilePsm1 = ScriptPath.EndsWith(".psm1", StringComparison.OrdinalIgnoreCase);

            if (string.IsNullOrEmpty(EntryPoint))
            {
                if (isScriptFilePsm1)
                {
                    throw new ArgumentException(PowerShellWorkerStrings.RequireEntryPointForScriptModule);
                }
            }
            else if (!isScriptFilePsm1)
            {
                throw new ArgumentException(PowerShellWorkerStrings.InvalidEntryPointForScriptFile);
            }

            // Get the parameter names of the script or function.
            var psScriptParams = GetParameters(ScriptPath, EntryPoint);

            FuncParameters = new ReadOnlyDictionary <string, PSScriptParamInfo>(psScriptParams);

            var parametersCopy = new Dictionary <string, PSScriptParamInfo>(psScriptParams, StringComparer.OrdinalIgnoreCase);

            HasTriggerMetadataParam = parametersCopy.Remove(TriggerMetadata);

            var allBindings    = new Dictionary <string, ReadOnlyBindingInfo>(StringComparer.OrdinalIgnoreCase);
            var inputBindings  = new Dictionary <string, ReadOnlyBindingInfo>(StringComparer.OrdinalIgnoreCase);
            var outputBindings = new Dictionary <string, ReadOnlyBindingInfo>(StringComparer.OrdinalIgnoreCase);

            var inputsMissingFromParams = new List <string>();

            foreach (var binding in metadata.Bindings)
            {
                string bindingName = binding.Key;
                var    bindingInfo = new ReadOnlyBindingInfo(binding.Value);

                allBindings.Add(bindingName, bindingInfo);

                if (bindingInfo.Direction == BindingInfo.Types.Direction.In)
                {
                    Type = GetAzFunctionType(bindingInfo);
                    inputBindings.Add(bindingName, bindingInfo);

                    // If the input binding name is in the set, we remove it;
                    // otherwise, the binding name is missing from the params.
                    if (!parametersCopy.Remove(bindingName))
                    {
                        inputsMissingFromParams.Add(bindingName);
                    }
                }
                else if (bindingInfo.Direction == BindingInfo.Types.Direction.Out)
                {
                    outputBindings.Add(bindingName, bindingInfo);
                }
                else
                {
                    // PowerShell doesn't support the 'InOut' type binding
                    throw new InvalidOperationException(string.Format(PowerShellWorkerStrings.InOutBindingNotSupported, bindingName));
                }
            }

            if (inputsMissingFromParams.Count != 0 || parametersCopy.Count != 0)
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (string inputBindingName in inputsMissingFromParams)
                {
                    stringBuilder.AppendFormat(PowerShellWorkerStrings.MissingParameter, inputBindingName).AppendLine();
                }

                foreach (string param in parametersCopy.Keys)
                {
                    stringBuilder.AppendFormat(PowerShellWorkerStrings.UnknownParameter, param).AppendLine();
                }

                string errorMsg = stringBuilder.ToString();
                throw new InvalidOperationException(errorMsg);
            }

            AllBindings    = new ReadOnlyDictionary <string, ReadOnlyBindingInfo>(allBindings);
            InputBindings  = new ReadOnlyDictionary <string, ReadOnlyBindingInfo>(inputBindings);
            OutputBindings = new ReadOnlyDictionary <string, ReadOnlyBindingInfo>(outputBindings);
        }
コード例 #3
0
        /// <summary>
        /// Construct an object of AzFunctionInfo from the 'RpcFunctionMetadata'.
        /// Necessary validations are done on the metadata and script.
        /// </summary>
        internal AzFunctionInfo(RpcFunctionMetadata metadata)
        {
            FuncName      = metadata.Name;
            FuncDirectory = metadata.Directory;
            EntryPoint    = metadata.EntryPoint;
            ScriptPath    = metadata.ScriptFile;

            // Support 'entryPoint' only if 'scriptFile' is a .psm1 file;
            // Support .psm1 'scriptFile' only if 'entryPoint' is specified.
            bool isScriptFilePsm1     = ScriptPath.EndsWith(".psm1", StringComparison.OrdinalIgnoreCase);
            bool entryPointNotDefined = string.IsNullOrEmpty(EntryPoint);

            if (entryPointNotDefined)
            {
                if (isScriptFilePsm1)
                {
                    throw new ArgumentException(PowerShellWorkerStrings.RequireEntryPointForScriptModule);
                }
            }
            else if (!isScriptFilePsm1)
            {
                throw new ArgumentException(PowerShellWorkerStrings.InvalidEntryPointForScriptFile);
            }

            // Get the parameter names of the script or function.
            var psScriptParams = GetParameters(ScriptPath, EntryPoint, out ScriptBlockAst scriptAst);

            FuncParameters = new ReadOnlyDictionary <string, PSScriptParamInfo>(psScriptParams);

            var parametersCopy = new Dictionary <string, PSScriptParamInfo>(psScriptParams, StringComparer.OrdinalIgnoreCase);

            HasTriggerMetadataParam = parametersCopy.Remove(TriggerMetadata);
            HasTraceContextParam    = parametersCopy.Remove(TraceContext);

            var allBindings    = new Dictionary <string, ReadOnlyBindingInfo>(StringComparer.OrdinalIgnoreCase);
            var inputBindings  = new Dictionary <string, ReadOnlyBindingInfo>(StringComparer.OrdinalIgnoreCase);
            var outputBindings = new Dictionary <string, ReadOnlyBindingInfo>(StringComparer.OrdinalIgnoreCase);

            var inputsMissingFromParams = new List <string>();

            foreach (var binding in metadata.Bindings)
            {
                string bindingName = binding.Key;
                var    bindingInfo = new ReadOnlyBindingInfo(binding.Value);

                allBindings.Add(bindingName, bindingInfo);

                if (bindingInfo.Direction == BindingInfo.Types.Direction.In)
                {
                    Type = GetAzFunctionType(bindingInfo);
                    inputBindings.Add(bindingName, bindingInfo);

                    // If the input binding name is in the set, we remove it;
                    // otherwise, the binding name is missing from the params.
                    if (!parametersCopy.Remove(bindingName))
                    {
                        inputsMissingFromParams.Add(bindingName);
                    }
                }
                else if (bindingInfo.Direction == BindingInfo.Types.Direction.Out)
                {
                    if (bindingInfo.Type == OrchestrationClient)
                    {
                        OrchestrationClientBindingName = bindingName;
                    }

                    outputBindings.Add(bindingName, bindingInfo);
                }
                else
                {
                    // PowerShell doesn't support the 'InOut' type binding
                    throw new InvalidOperationException(string.Format(PowerShellWorkerStrings.InOutBindingNotSupported, bindingName));
                }
            }

            if (inputsMissingFromParams.Count != 0 || parametersCopy.Count != 0)
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (string inputBindingName in inputsMissingFromParams)
                {
                    stringBuilder.AppendFormat(PowerShellWorkerStrings.MissingParameter, inputBindingName).AppendLine();
                }

                foreach (string param in parametersCopy.Keys)
                {
                    stringBuilder.AppendFormat(PowerShellWorkerStrings.UnknownParameter, param).AppendLine();
                }

                string errorMsg = stringBuilder.ToString();
                throw new InvalidOperationException(errorMsg);
            }

            if (entryPointNotDefined && scriptAst.ScriptRequirements == null)
            {
                // If the function script is a '.ps1' file that doesn't have '#requires' defined,
                // then we get the script block and will deploy it as a PowerShell function in the
                // global scope of each Runspace, so as to avoid hitting the disk every invocation.
                FuncScriptBlock    = scriptAst.GetScriptBlock();
                DeployedPSFuncName = $"_{FuncName}_";
            }

            AllBindings    = new ReadOnlyDictionary <string, ReadOnlyBindingInfo>(allBindings);
            InputBindings  = new ReadOnlyDictionary <string, ReadOnlyBindingInfo>(inputBindings);
            OutputBindings = new ReadOnlyDictionary <string, ReadOnlyBindingInfo>(outputBindings);
        }
コード例 #4
0
        /// <summary>
        /// Construct an object of AzFunctionInfo from the 'RpcFunctionMetadata'.
        /// Necessary validations are done on the metadata and script.
        /// </summary>
        internal AzFunctionInfo(RpcFunctionMetadata metadata)
        {
            FuncName      = metadata.Name;
            FuncDirectory = metadata.Directory;
            EntryPoint    = metadata.EntryPoint;
            ScriptPath    = metadata.ScriptFile;

            // Support 'entryPoint' only if 'scriptFile' is a .psm1 file;
            // Support .psm1 'scriptFile' only if 'entryPoint' is specified.
            bool isScriptFilePsm1 = ScriptPath.EndsWith(".psm1", StringComparison.OrdinalIgnoreCase);

            if (string.IsNullOrEmpty(EntryPoint))
            {
                if (isScriptFilePsm1)
                {
                    throw new ArgumentException($"The 'entryPoint' property needs to be specified when 'scriptFile' points to a PowerShell module script file (.psm1).");
                }
            }
            else if (!isScriptFilePsm1)
            {
                throw new ArgumentException($"The 'entryPoint' property is supported only if 'scriptFile' points to a PowerShell module script file (.psm1).");
            }

            // Get the parameter names of the script or function.
            FuncParameters = GetParameters(ScriptPath, EntryPoint);
            var parametersCopy = new HashSet <string>(FuncParameters, StringComparer.OrdinalIgnoreCase);

            parametersCopy.Remove(TriggerMetadata);

            var allBindings    = new Dictionary <string, ReadOnlyBindingInfo>();
            var inputBindings  = new Dictionary <string, ReadOnlyBindingInfo>();
            var outputBindings = new Dictionary <string, ReadOnlyBindingInfo>();

            var inputsMissingFromParams = new List <string>();

            foreach (var binding in metadata.Bindings)
            {
                string bindingName = binding.Key;
                var    bindingInfo = new ReadOnlyBindingInfo(binding.Value);

                allBindings.Add(bindingName, bindingInfo);

                if (bindingInfo.Direction == BindingInfo.Types.Direction.In)
                {
                    Type = GetAzFunctionType(bindingInfo);
                    inputBindings.Add(bindingName, bindingInfo);

                    // If the input binding name is in the set, we remove it;
                    // otherwise, the binding name is missing from the params.
                    if (!parametersCopy.Remove(bindingName))
                    {
                        inputsMissingFromParams.Add(bindingName);
                    }
                }
                else if (bindingInfo.Direction == BindingInfo.Types.Direction.Out)
                {
                    outputBindings.Add(bindingName, bindingInfo);
                }
                else
                {
                    // PowerShell doesn't support the 'InOut' type binding
                    throw new InvalidOperationException($"The binding '{bindingName}' is declared with 'InOut' direction, which is not supported by PowerShell functions.");
                }
            }

            if (inputsMissingFromParams.Count != 0 || parametersCopy.Count != 0)
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (string inputBindingName in inputsMissingFromParams)
                {
                    stringBuilder.AppendLine($"No parameter defined in the script or function for the input binding '{inputBindingName}'.");
                }

                foreach (string param in parametersCopy)
                {
                    stringBuilder.AppendLine($"No input binding defined for the parameter '{param}' that is declared in the script or function.");
                }

                string errorMsg = stringBuilder.ToString();
                throw new InvalidOperationException(errorMsg);
            }

            AllBindings    = new ReadOnlyDictionary <string, ReadOnlyBindingInfo>(allBindings);
            InputBindings  = new ReadOnlyDictionary <string, ReadOnlyBindingInfo>(inputBindings);
            OutputBindings = new ReadOnlyDictionary <string, ReadOnlyBindingInfo>(outputBindings);
        }