Beispiel #1
0
        protected override void EndProcessing()
        {
            if (Body != null)
            {
                WriteArmArrayElement(Body);
                return;
            }

            var armArray = new ArmArray();

            for (int i = 0; i < Values.Length; i++)
            {
                object currVal = Values[i];
                if (!ArmElementConversion.TryConvertToArmElement(currVal, out ArmElement element))
                {
                    this.ThrowTerminatingError(
                        new InvalidCastException($"Unable to convert value '{currVal}' of type '{currVal.GetType()}' to type '{typeof(ArmElement)}'"),
                        "InvalidArmTypeCase",
                        ErrorCategory.InvalidArgument,
                        currVal);
                    return;
                }
                armArray.Add(element);
            }
            WriteObject(armArray);
        }
Beispiel #2
0
        private IReadOnlyDictionary <IArmString, ArmElement> GetTemplateParameters()
        {
            if (Parameters is null)
            {
                return(null);
            }

            var parameters = new Dictionary <IArmString, ArmElement>();

            foreach (DictionaryEntry entry in Parameters)
            {
                if (!ArmElementConversion.TryConvertToArmString(entry.Key, out IArmString key))
                {
                    throw new ArgumentException($"Cannot convert hashtable key '{entry.Key}' of type '{entry.Key.GetType()}' to ARM string");
                }

                if (!ArmElementConversion.TryConvertToArmElement(entry.Value, out ArmElement value))
                {
                    if (entry.Value is SecureString)
                    {
                        throw new ArgumentException($"Cannot provide secure string values for template publication. Provide these at template deployment time");
                    }

                    throw new ArgumentException($"Cannot convert hashtable value '{entry.Value}' of type '{entry.Value.GetType()}' to ARM element");
                }

                parameters[key] = value;
            }

            return(parameters);
        }
Beispiel #3
0
        private bool TryGetAllowedValues(ParameterAst parameter, out ArmArray allowedValues)
        {
            if (parameter.Attributes is null)
            {
                allowedValues = null;
                return(false);
            }

            foreach (AttributeBaseAst attributeBase in parameter.Attributes)
            {
                if (attributeBase is not AttributeAst attribute ||
                    attribute.TypeName.GetReflectionType() != typeof(ValidateSetAttribute) ||
                    attribute.PositionalArguments is null ||
                    attribute.PositionalArguments.Count == 0)
                {
                    continue;
                }

                allowedValues = new ArmArray();
                foreach (ExpressionAst allowedExpression in attribute.PositionalArguments)
                {
                    if (!ArmElementConversion.TryConvertToArmElement(allowedExpression.SafeGetValue(), out ArmElement allowedArmElement))
                    {
                        throw new ArgumentException($"ValidateSet value '{allowedExpression}' on parameter '{parameter.Name}' is not a value ARM value");
                    }

                    allowedValues.Add(allowedArmElement);
                }

                return(true);
            }

            allowedValues = null;
            return(false);
        }
        protected ArmElement GetParameterValue(ParameterAst parameter, List <PSVariable> variables)
        {
            if (parameter.DefaultValue is null)
            {
                return(null);
            }

            // We need to supply the predefined variables here, so must use the ScriptBlock.InvokeWithContext() method
            // This relies on this method being executed on the pipeline thread
            // So any attempt to make this asynchronous or parallelize it will lead to subtle bugs
            Collection <PSObject> result = ScriptBlock
                                           .Create(parameter.DefaultValue.Extent.Text)
                                           .InvokeWithContext(functionsToDefine: null, variables);

            object input = result.Count == 1 ? result[0] : result;

            // Define the variable for use in the next parameter evaluation
            variables.Add(new PSVariable(parameter.Name.VariablePath.UserPath, input));

            if (!ArmElementConversion.TryConvertToArmElement(input, out ArmElement armElement))
            {
                throw new ArgumentException($"Unable to convert value '{parameter.DefaultValue}' to ARM value");
            }

            return(armElement);
        }