private static ControlResult ExtractMultiToken <TV, TM>(ResourceControl control, JObject resource, out IEnumerable <TV> actual, out TM match) { IEnumerable <JToken> tokens = null; foreach (var jsonPath in control.JsonPath) { tokens = resource.SelectTokens(jsonPath); if (tokens != null) { break; } } var tokenNotFound = tokens == null; var result = ControlResult.Build(control, resource, tokens, tokenNotFound, VerificationResult.Failed); if (tokenNotFound) { result.IsTokenNotValid = true; } try { actual = tokenNotFound ? default(IEnumerable <TV>) : tokens.Values <TV>(); } catch (Exception) { actual = default(IEnumerable <TV>); result.IsTokenNotValid = true; } match = control.Data.ToObject <TM>(); return(result); }
private static ControlResult ExtractSingleToken <TV, TM>(ResourceControl control, JObject resource, out TV actual, out TM match) { JToken token = null; foreach (var jsonPath in control.JsonPath) { token = resource.SelectToken(jsonPath); if (token != null) { break; } } var tokenNotFound = token == null; var result = ControlResult.Build(control, resource, token, tokenNotFound, VerificationResult.Failed); if (tokenNotFound) { result.IsTokenNotValid = true; } try { actual = tokenNotFound ? default(TV) : token.Value <TV>(); } catch (Exception) { actual = default(TV); result.IsTokenNotValid = true; } match = control.Data.ToObject <TM>(); return(result); }
private static ControlResult ExtractMultiToken <TV, TM>(ResourceControl control, JObject resource, out IEnumerable <TV> actual, out TM match) { IEnumerable <JToken> tokens = null; foreach (var jsonPath in control.JsonPath) { tokens = resource.SelectTokens(jsonPath); if (tokens != null) { break; } } var tokenNotFound = tokens == null; var result = ControlResult.Build(control, resource, tokens, tokenNotFound, VerificationResult.Failed); if (tokenNotFound) { result.IsTokenNotValid = true; } try { if (tokenNotFound) { actual = default(IEnumerable <TV>); } else { var tokenValues = default(IEnumerable <TV>); string ARMtemplateFunctionType = null; if (tokens.Values <TV>().FirstOrDefault() != null) { ARMtemplateFunctionType = tokens.Values <TV>().First().ToString().GetFunctionType(); } switch (ARMtemplateFunctionType) { case "parameters": tokenValues = functionMultiParameters(tokens, tokenValues); break; case "variables": tokenValues = functionMultiVariables(tokens, tokenValues); break; default: tokenValues = tokens.Values <TV>(); break; } // Check if current token is parameter actual = tokenValues; } //actual = tokenNotFound ? default(IEnumerable<TV>) : tokens.Values<TV>(); } catch (Exception) { actual = default(IEnumerable <TV>); result.IsTokenNotValid = true; } match = control.Data.ToObject <TM>(); return(result); }
private static ControlResult ExtractSingleToken <TV, TM>(ResourceControl control, JObject resource, out TV actual, out TM match, bool validateParameters = true) { JToken token = null; foreach (var jsonPath in control.JsonPath) { token = resource.SelectToken(jsonPath); if (token != null) { break; } } var tokenNotFound = token == null; var result = ControlResult.Build(control, resource, token, tokenNotFound, VerificationResult.Failed); if (tokenNotFound) { result.IsTokenNotValid = true; } try { if (tokenNotFound) { actual = default(TV); } else { var tokenValue = default(TV); string ARMtemplateFunctionType = token.Value <String>().GetFunctionType(); // Switch Case to call particular function on the basis of its function type which may be parameters(), variables(),concat() and Substring(). switch (ARMtemplateFunctionType) { case "parameters": tokenValue = functionParameters(validateParameters, token, tokenValue); break; case "variables": tokenValue = functionVariables(validateParameters, token, tokenValue); break; case "concat": tokenValue = functionConcat(validateParameters, token, tokenValue); break; case "substring": tokenValue = functionSubString(validateParameters, token, tokenValue); break; default: tokenValue = token.Value <TV>(); break; } actual = tokenValue; } } catch (Exception) { actual = default(TV); result.IsTokenNotValid = true; } match = control.Data.ToObject <TM>(); return(result); }
private static ControlResult ExtractAllSingleToken <TV, TM>(ResourceControl control, JObject resource, out List <TV> actual, out TM match, bool validateParameters = true) { bool tokenNotFound = false; List <JToken> tokens = new List <JToken>(); foreach (var jsonPath in control.JsonPath) { if (resource.SelectToken(jsonPath) != null) { tokens.Add(resource.SelectToken(jsonPath)); } else { tokenNotFound = true; break; } } var result = ControlResult.Build(control, resource, tokens, tokenNotFound, VerificationResult.Failed); if (tokenNotFound) { result.IsTokenNotValid = true; } actual = new List <TV>(); try { if (tokenNotFound) { actual = null; } else { List <TV> tokenValues = new List <TV>(); for (int i = 0; i < tokens.Count; i++) { string ARMtemplateFunctionType = tokens[i].Value <String>().GetFunctionType(); switch (ARMtemplateFunctionType) { case "parameters": tokenValues = functionParameters(validateParameters, tokens[i], tokenValues); break; case "variables": tokenValues = functionVariables(validateParameters, tokens[i], tokenValues); break; case "concat": tokenValues = functionConcat(validateParameters, tokens[i], tokenValues); break; case "substring": tokenValues = functionSubString(validateParameters, tokens[i], tokenValues); break; default: tokenValues.Add(tokens[i].Value <TV>()); break; } actual = tokenValues; } } } catch (Exception) { actual = null; result.IsTokenNotValid = true; } match = control.Data.ToObject <TM>(); return(result); }
private static ControlResult ExtractMultiToken <TV, TM>(ResourceControl control, JObject resource, out IEnumerable <TV> actual, out TM match) { IEnumerable <JToken> tokens = null; foreach (var jsonPath in control.JsonPath) { tokens = resource.SelectTokens(jsonPath); if (tokens != null) { break; } } var tokenNotFound = tokens == null; var result = ControlResult.Build(control, resource, tokens, tokenNotFound, VerificationResult.Failed); if (tokenNotFound) { result.IsTokenNotValid = true; } try { if (tokenNotFound) { actual = default(IEnumerable <TV>); } else { var tokenValues = default(IEnumerable <TV>); bool paramterValueFound = false; // Check if current token is parameter if (tokens.Values <TV>().FirstOrDefault() != null && tokens.Values <TV>().First().ToString().CheckIsParameter()) { var parameterKey = tokens.Values <String>().First().GetParameterKey(); if (parameterKey != null) { // Check if parameter value is present in external parameter file if (_externalParametersDict.ContainsKey("parameters")) { JObject externalParameters = _externalParametersDict["parameters"].Value <JObject>(); var externalParamValue = externalParameters.Properties().Where(p => p.Name == parameterKey).Select(p => p.Value["value"].Values <TV>()); if (externalParamValue != null && externalParamValue.Count() > 0) { paramterValueFound = true; tokenValues = externalParamValue.First(); } } // If parameter value is not present in external parameter file, check for default value if (!paramterValueFound) { JObject innerParameters = _armTemplate["parameters"].Value <JObject>(); tokenValues = innerParameters.Properties().Where(p => p.Name == parameterKey).Select(p => p.Value["defaultValue"].Values <TV>()).FirstOrDefault(); } } } else { tokenValues = tokens.Values <TV>(); } actual = tokenValues; } //actual = tokenNotFound ? default(IEnumerable<TV>) : tokens.Values<TV>(); } catch (Exception) { actual = default(IEnumerable <TV>); result.IsTokenNotValid = true; } match = control.Data.ToObject <TM>(); return(result); }