public void GetsDynamicParametersForTemplateFile() { RuntimeDefinedParameterDictionary result = TemplateUtility.GetTemplateParametersFromFile( templateFile, null, null, new[] { "TestPS" }); Assert.Equal(7, result.Count); Assert.Equal("string", result["string"].Name); Assert.Equal(typeof(string), result["String"].ParameterType); Assert.Equal("int", result["int"].Name); Assert.Equal(typeof(int), result["int"].ParameterType); Assert.Equal("securestring", result["securestring"].Name); Assert.Equal(typeof(SecureString), result["securestring"].ParameterType); Assert.Equal("bool", result["bool"].Name); Assert.Equal(typeof(bool), result["bool"].ParameterType); Assert.Equal("object", result["object"].Name); Assert.Equal(typeof(Hashtable), result["object"].ParameterType); Assert.Equal("secureObject", result["secureObject"].Name); Assert.Equal(typeof(Hashtable), result["secureObject"].ParameterType); Assert.Equal("array", result["array"].Name); Assert.Equal(typeof(object[]), result["array"].ParameterType); }
public void GetTemplateParametersFromObject() { Hashtable templateParameterObject = new Hashtable(); templateParameterObject["string"] = "myvalue"; templateParameterObject["int"] = 12; templateParameterObject["bool"] = true; templateParameterObject["object"] = new Hashtable() { { "code", "F1" }, { "name", "Free" } }; templateParameterObject["array"] = new object[] { "A", "D", "F" }; RuntimeDefinedParameterDictionary result = TemplateUtility.GetTemplateParametersFromFile( templateFile, templateParameterObject, null, new[] { "TestPS" }); Assert.Equal(7, result.Count); Assert.Equal("string", result["string"].Name); Assert.Equal(typeof(string), result["string"].ParameterType); Assert.Equal("myvalue", result["string"].Value); Assert.Equal("int", result["int"].Name); Assert.Equal(typeof(int), result["int"].ParameterType); Assert.Equal(12, result["int"].Value); Assert.Equal("bool", result["bool"].Name); Assert.Equal(typeof(bool), result["bool"].ParameterType); Assert.True(result["bool"].Value as bool?); Assert.Equal("object", result["object"].Name); Assert.Equal(typeof(Hashtable), result["object"].ParameterType); Hashtable objectValue = result["object"].Value as Hashtable; Assert.Equal(2, objectValue.Count); Assert.Equal("F1", objectValue["code"]); Assert.Equal("Free", objectValue["name"]); Assert.Equal("array", result["array"].Name); Assert.Equal(typeof(object[]), result["array"].ParameterType); var arrayValue = result["array"].Value as object[]; Assert.Equal(3, arrayValue.Length); Assert.Equal("A", arrayValue[0]); Assert.Equal("F", arrayValue[2]); }
public void HandlesInvalidTemplateFiles() { Hashtable hashtable = new Hashtable(); hashtable["Bool"] = true; hashtable["Foo"] = "bar"; RuntimeDefinedParameterDictionary result = TemplateUtility.GetTemplateParametersFromFile( invalidTemplateFile, null, templateParameterFileSchema1, new[] { "TestPS" }); Assert.Empty(result); }
public object GetDynamicParameters() { if (!string.IsNullOrEmpty(TemplateFile) && !TemplateFile.Equals(templateFile, StringComparison.OrdinalIgnoreCase)) { templateFile = TemplateFile; if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( this.ResolvePath(TemplateFile), TemplateParameterObject, this.ResolvePath(TemplateParameterFile), MyInvocation.MyCommand.Parameters.Keys.ToArray()); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( this.ResolvePath(TemplateFile), TemplateParameterObject, TemplateParameterUri, MyInvocation.MyCommand.Parameters.Keys.ToArray()); } } else if (!string.IsNullOrEmpty(TemplateUri) && !TemplateUri.Equals(templateUri, StringComparison.OrdinalIgnoreCase)) { templateUri = TemplateUri; if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( TemplateUri, TemplateParameterObject, this.ResolvePath(TemplateParameterFile), MyInvocation.MyCommand.Parameters.Keys.ToArray()); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( TemplateUri, TemplateParameterObject, TemplateParameterUri, MyInvocation.MyCommand.Parameters.Keys.ToArray()); } } RegisterDynamicParameters(dynamicParameters); return(dynamicParameters); }
public void GetTemplateParametersFromFileWithSchema2MergesObjects() { Hashtable hashtable = new Hashtable(); hashtable["Bool"] = true; hashtable["Foo"] = "bar"; RuntimeDefinedParameterDictionary result = TemplateUtility.GetTemplateParametersFromFile( templateFile, null, templateParameterFileSchema2, new[] { "TestPS" }); Assert.Equal(7, result.Count); Assert.Equal("string", result["string"].Name); Assert.Equal(typeof(string), result["string"].ParameterType); Assert.Equal("myvalue", result["string"].Value); Assert.Equal("int", result["int"].Name); Assert.Equal(typeof(int), result["int"].ParameterType); Assert.Equal("12", result["int"].Value); Assert.Equal("bool", result["bool"].Name); Assert.Equal(typeof(bool), result["bool"].ParameterType); Assert.Equal("True", result["bool"].Value); Assert.Equal("object", result["object"].Name); Assert.Equal(typeof(Hashtable), result["object"].ParameterType); JObject objectValue = result["object"].Value as JObject; Assert.Equal(2, objectValue.Count); Assert.Equal("F1", objectValue["code"].ToObject <string>()); Assert.Equal("Free", objectValue["name"].ToObject <string>()); Assert.Equal("array", result["array"].Name); Assert.Equal(typeof(object[]), result["array"].ParameterType); var arrayValue = result["array"].Value as JArray; Assert.Equal(3, arrayValue.Count); Assert.Equal("A", arrayValue[0].ToObject <string>()); Assert.Equal("F", arrayValue[2].ToObject <string>()); }
public virtual object GetDynamicParameters() { if (!this.IsParameterBound(c => c.SkipTemplateParameterPrompt)) { // Resolve the static parameter names for this cmdlet: string[] staticParameterNames = this.GetStaticParameterNames(); if (TemplateObject != null && TemplateObject != templateObject) { templateObject = TemplateObject; if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( TemplateObject, TemplateParameterObject, this.ResolvePath(TemplateParameterFile), staticParameterNames); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( TemplateObject, TemplateParameterObject, TemplateParameterUri, staticParameterNames); } } else if (!string.IsNullOrEmpty(TemplateFile) && !TemplateFile.Equals(templateFile, StringComparison.OrdinalIgnoreCase)) { templateFile = TemplateFile; if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( this.ResolvePath(TemplateFile), TemplateParameterObject, this.ResolvePath(TemplateParameterFile), staticParameterNames); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( this.ResolvePath(TemplateFile), TemplateParameterObject, TemplateParameterUri, staticParameterNames); } } else if (!string.IsNullOrEmpty(TemplateUri) && !TemplateUri.Equals(templateUri, StringComparison.OrdinalIgnoreCase)) { if (string.IsNullOrEmpty(protectedTemplateUri)) { templateUri = TemplateUri; } else { templateUri = protectedTemplateUri; } if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( templateUri, TemplateParameterObject, this.ResolvePath(TemplateParameterFile), staticParameterNames); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( templateUri, TemplateParameterObject, TemplateParameterUri, staticParameterNames); } } else if (!string.IsNullOrEmpty(TemplateSpecId) && !TemplateSpecId.Equals(templateSpecId, StringComparison.OrdinalIgnoreCase)) { templateSpecId = TemplateSpecId; ResourceIdentifier resourceIdentifier = new ResourceIdentifier(templateSpecId); if (!resourceIdentifier.ResourceType.Equals("Microsoft.Resources/templateSpecs/versions", StringComparison.OrdinalIgnoreCase)) { throw new PSArgumentException("No version found in Resource ID"); } if (!string.IsNullOrEmpty(resourceIdentifier.Subscription) && TemplateSpecsClient.SubscriptionId != resourceIdentifier.Subscription) { // The template spec is in a different subscription than our default // context. Force the client to use that subscription: TemplateSpecsClient.SubscriptionId = resourceIdentifier.Subscription; } var templateSpecVersion = TemplateSpecsClient.TemplateSpecVersions.Get( ResourceIdUtility.GetResourceGroupName(templateSpecId), ResourceIdUtility.GetResourceName(templateSpecId).Split('/')[0], resourceIdentifier.ResourceName); if (!(templateSpecVersion.Template is JObject)) { throw new InvalidOperationException("Unexpected type."); // Sanity check } JObject templateObj = (JObject)templateSpecVersion.Template; if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( templateObj, TemplateParameterObject, this.ResolvePath(TemplateParameterFile), staticParameterNames); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( templateObj, TemplateParameterObject, TemplateParameterUri, staticParameterNames); } } } RegisterDynamicParameters(dynamicParameters); return(dynamicParameters); }
public virtual object GetDynamicParameters() { if (BicepUtility.IsBicepFile(TemplateFile)) { BuildAndUseBicepTemplate(); } if (!this.IsParameterBound(c => c.SkipTemplateParameterPrompt)) { // Resolve the static parameter names for this cmdlet: string[] staticParameterNames = this.GetStaticParameterNames(); if (TemplateObject != null && TemplateObject != templateObject) { templateObject = TemplateObject; if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( TemplateObject, TemplateParameterObject, this.ResolvePath(TemplateParameterFile), staticParameterNames); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( TemplateObject, TemplateParameterObject, TemplateParameterUri, staticParameterNames); } } else if (!string.IsNullOrEmpty(TemplateFile) && !TemplateFile.Equals(templateFile, StringComparison.OrdinalIgnoreCase)) { templateFile = TemplateFile; if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( this.ResolvePath(TemplateFile), TemplateParameterObject, this.ResolvePath(TemplateParameterFile), staticParameterNames); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( this.ResolvePath(TemplateFile), TemplateParameterObject, TemplateParameterUri, staticParameterNames); } } else if (!string.IsNullOrEmpty(TemplateUri) && !TemplateUri.Equals(templateUri, StringComparison.OrdinalIgnoreCase)) { if (string.IsNullOrEmpty(protectedTemplateUri)) { templateUri = TemplateUri; } else { templateUri = protectedTemplateUri; } if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( templateUri, TemplateParameterObject, this.ResolvePath(TemplateParameterFile), staticParameterNames); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( templateUri, TemplateParameterObject, TemplateParameterUri, staticParameterNames); } } else if (!string.IsNullOrEmpty(TemplateSpecId) && !TemplateSpecId.Equals(templateSpecId, StringComparison.OrdinalIgnoreCase)) { templateSpecId = TemplateSpecId; ResourceIdentifier resourceIdentifier = new ResourceIdentifier(templateSpecId); if (!resourceIdentifier.ResourceType.Equals("Microsoft.Resources/templateSpecs/versions", StringComparison.OrdinalIgnoreCase)) { throw new PSArgumentException("No version found in Resource ID"); } if (!string.IsNullOrEmpty(resourceIdentifier.Subscription) && TemplateSpecsClient.SubscriptionId != resourceIdentifier.Subscription) { // The template spec is in a different subscription than our default // context. Force the client to use that subscription: TemplateSpecsClient.SubscriptionId = resourceIdentifier.Subscription; } JObject templateObj = (JObject)null; try { var templateSpecVersion = TemplateSpecsClient.TemplateSpecVersions.Get( ResourceIdUtility.GetResourceGroupName(templateSpecId), ResourceIdUtility.GetResourceName(templateSpecId).Split('/')[0], resourceIdentifier.ResourceName); if (!(templateSpecVersion.Template is JObject)) { throw new InvalidOperationException("Unexpected type."); // Sanity check } templateObj = (JObject)templateSpecVersion.Template; } catch (TemplateSpecsErrorException e) { //If the templateSpec resourceID is pointing to a non existant resource if (e.Response.StatusCode.Equals(HttpStatusCode.NotFound)) { //By returning null, we are introducing parity in the way templateURI and templateSpecId are validated. Gives a cleaner error message in line with the error message for invalid templateURI return(null); } //Throw for any other error that is not due to a 404 for the template resource. throw; } if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( templateObj, TemplateParameterObject, this.ResolvePath(TemplateParameterFile), staticParameterNames); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( templateObj, TemplateParameterObject, TemplateParameterUri, staticParameterNames); } } } RegisterDynamicParameters(dynamicParameters); return(dynamicParameters); }
public object GetDynamicParameters() { if (!this.IsParameterBound(c => c.SkipTemplateParameterPrompt)) { if (TemplateObject != null && TemplateObject != templateObject) { templateObject = TemplateObject; if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( TemplateObject, TemplateParameterObject, this.ResolvePath(TemplateParameterFile), MyInvocation.MyCommand.Parameters.Keys.ToArray()); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( TemplateObject, TemplateParameterObject, TemplateParameterUri, MyInvocation.MyCommand.Parameters.Keys.ToArray()); } } else if (!string.IsNullOrEmpty(TemplateFile) && !TemplateFile.Equals(templateFile, StringComparison.OrdinalIgnoreCase)) { templateFile = TemplateFile; if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( this.ResolvePath(TemplateFile), TemplateParameterObject, this.ResolvePath(TemplateParameterFile), MyInvocation.MyCommand.Parameters.Keys.ToArray()); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( this.ResolvePath(TemplateFile), TemplateParameterObject, TemplateParameterUri, MyInvocation.MyCommand.Parameters.Keys.ToArray()); } } else if (!string.IsNullOrEmpty(TemplateUri) && !TemplateUri.Equals(templateUri, StringComparison.OrdinalIgnoreCase)) { templateUri = TemplateUri; if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( TemplateUri, TemplateParameterObject, this.ResolvePath(TemplateParameterFile), MyInvocation.MyCommand.Parameters.Keys.ToArray()); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( TemplateUri, TemplateParameterObject, TemplateParameterUri, MyInvocation.MyCommand.Parameters.Keys.ToArray()); } } else if (!string.IsNullOrEmpty(TemplateSpecId) && !TemplateSpecId.Equals(templateSpecId, StringComparison.OrdinalIgnoreCase)) { templateSpecId = TemplateSpecId; ResourceIdentifier resourceIdentifier = new ResourceIdentifier(templateSpecId); if (!resourceIdentifier.ResourceType.Equals("Microsoft.Resources/templateSpecs/versions", StringComparison.OrdinalIgnoreCase)) { throw new PSArgumentException("No version found in Resource ID"); } var templateSpecVersion = TemplateSpecsSdkClient.GetTemplateSpec( ResourceIdUtility.GetResourceName(templateSpecId).Split('/')[0], ResourceIdUtility.GetResourceGroupName(templateSpecId), resourceIdentifier.ResourceName).Versions.Single(); var templateObj = JObject.Parse(templateSpecVersion.Template); if (string.IsNullOrEmpty(TemplateParameterUri)) { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( templateObj, TemplateParameterObject, this.ResolvePath(TemplateParameterFile), MyInvocation.MyCommand.Parameters.Keys.ToArray()); } else { dynamicParameters = TemplateUtility.GetTemplateParametersFromFile( templateObj, TemplateParameterObject, TemplateParameterUri, MyInvocation.MyCommand.Parameters.Keys.ToArray()); } } } RegisterDynamicParameters(dynamicParameters); return(dynamicParameters); }