|
| 1 | +using Azure.Deployments.Core.Components; |
| 2 | +using Azure.Deployments.Core.Extensions; |
| 3 | +using Azure.Deployments.Core.Resources; |
| 4 | +using Azure.Deployments.Templates.Contracts; |
| 5 | +using Azure.Deployments.Templates.Export; |
| 6 | +using Azure.Deployments.Templates.Extensions; |
| 7 | +using Azure.Deployments.Templates.Helpers; |
| 8 | +using Azure.Deployments.TemplateSchemas; |
| 9 | +using Newtonsoft.Json; |
| 10 | +using Newtonsoft.Json.Linq; |
| 11 | +using System; |
| 12 | +using System.Collections.Generic; |
| 13 | +using System.IO; |
| 14 | +using System.Linq; |
| 15 | + |
| 16 | +namespace DeploymentsSchemaTests |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// Test schema cache |
| 20 | + /// </summary> |
| 21 | + internal class TestSchemaCache : INormalizedSchemaCache |
| 22 | + { |
| 23 | + /// <summary> |
| 24 | + /// Initializes the schema cache from a set of file paths. |
| 25 | + /// </summary> |
| 26 | + /// <param name="filePaths">The file paths to load.</param> |
| 27 | + public static INormalizedSchemaCache CreateFromFilePaths(IEnumerable<string> filePaths) |
| 28 | + { |
| 29 | + var schemaResults = TestSchemaCache.BuildSchemaCacheFromFilePaths(filePaths); |
| 30 | + |
| 31 | + return new TestSchemaCache(schemaResults); |
| 32 | + } |
| 33 | + |
| 34 | + private TestSchemaCache(ResultWithErrors<ResourceTypeSchema[]> schemaResults) |
| 35 | + { |
| 36 | + if (schemaResults.Errors.Any()) |
| 37 | + { |
| 38 | + var errors = schemaResults.Errors |
| 39 | + .Where(err => !SchemaLoader.CircularReferenceSchemas.Contains(err.Target)) |
| 40 | + .ToArray(); |
| 41 | + |
| 42 | + if (errors.Any()) |
| 43 | + { |
| 44 | + var errorsJson = JsonConvert.SerializeObject(errors, Formatting.Indented); |
| 45 | + throw new InvalidOperationException($"Found errors building normalized cache: {errorsJson}"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + this.schemaCache = schemaResults.Value |
| 50 | + .GroupByOrdinalInsensitively(schema => schema.ResourceProviderNamespace) |
| 51 | + .ToDictionary( |
| 52 | + keySelector: grouping => grouping.Key, |
| 53 | + elementSelector: grouping => grouping.ToLookupOrdinalInsensitively(schema => schema.FullyQualifiedResourceType), |
| 54 | + comparer: StringComparer.OrdinalIgnoreCase); |
| 55 | + } |
| 56 | + |
| 57 | + private IReadOnlyDictionary<string, ILookup<string, ResourceTypeSchema>> schemaCache; |
| 58 | + |
| 59 | + private class OfflineSchema : IResourceProviderSchema |
| 60 | + { |
| 61 | + public OfflineSchema(string providerNamespace, string apiVersion, JToken schemaContent) |
| 62 | + { |
| 63 | + this.ResourceProviderNamespace = providerNamespace; |
| 64 | + this.SchemaVersion = apiVersion; |
| 65 | + this.SchemaContent = schemaContent; |
| 66 | + } |
| 67 | + |
| 68 | + public string ResourceProviderNamespace { get; } |
| 69 | + |
| 70 | + public string SchemaVersion { get; } |
| 71 | + |
| 72 | + public JToken SchemaContent { get; } |
| 73 | + } |
| 74 | + |
| 75 | + private static readonly Uri SchemaBaseUri = new Uri("https://schema.management.azure.com/schemas/"); |
| 76 | + |
| 77 | + private static string GetRelativeSchemaPath(string schemaId) |
| 78 | + { |
| 79 | + var schemaUri = new Uri(new Uri(schemaId).GetLeftPart(UriPartial.Path)); |
| 80 | + if (!SchemaBaseUri.IsBaseOf(schemaUri)) |
| 81 | + { |
| 82 | + throw new ArgumentException($"Unable to process schema {schemaUri}"); |
| 83 | + } |
| 84 | + |
| 85 | + return SchemaBaseUri.MakeRelativeUri(schemaUri).ToString(); |
| 86 | + } |
| 87 | + |
| 88 | + private static ResultWithErrors<ResourceTypeSchema[]> BuildSchemaCacheFromFilePaths(IEnumerable<string> filePaths) |
| 89 | + { |
| 90 | + var schemasByPath = filePaths |
| 91 | + .Select(File.ReadAllText) |
| 92 | + .Select(JObject.Parse) |
| 93 | + .ToInsensitiveDictionary( |
| 94 | + keySelector: schema => GetRelativeSchemaPath(schema["id"].ToObject<string>()), |
| 95 | + elementSelector: schema => schema as JToken); |
| 96 | + |
| 97 | + var externalReferenceSchemasResult = SchemaUtils.GetExternalReferenceSchemas(schemasByPath, SchemaUtils.ExternalReferenceWhitelist.ToArray()); |
| 98 | + if (externalReferenceSchemasResult.Errors.Any()) |
| 99 | + { |
| 100 | + throw new InvalidOperationException($"Failed to initialize the offline schemas cache"); |
| 101 | + } |
| 102 | + |
| 103 | + var schemaRefsByFile = SchemaUtils.TopLevelReferenceSchemas |
| 104 | + .SelectMany(schemaPath => SchemaUtils.GetExternalReferences(schemasByPath[schemaPath])) |
| 105 | + .Select(property => property.Value.ToObject<string>()) |
| 106 | + .ToLookupOrdinalInsensitively(SchemaUtils.GetFilePathFromUri); |
| 107 | + |
| 108 | + var schemaKeysFound = schemaRefsByFile |
| 109 | + .Select(grouping => grouping.Key) |
| 110 | + .Where(filePath => !SchemaUtils.ExternalReferenceWhitelist.Contains(filePath)) |
| 111 | + .Where(filePath => schemasByPath.ContainsKey(filePath)) |
| 112 | + .ToArray(); |
| 113 | + |
| 114 | + var schemaGroups = schemaKeysFound.GroupByInsensitively(keySelector: schemaKey => SchemaUtils.GetSchemaGroupKey(schemaKey)); |
| 115 | + |
| 116 | + var schemaNormalizationResults = schemaGroups |
| 117 | + .Select(schemaGroup => schemaGroup.ToInsensitiveDictionary(keySelector: schemaKey => schemaKey, elementSelector: schemaKey => schemasByPath[schemaKey])) |
| 118 | + .SelectMany(schemaGroup => SchemaUtils.NormalizeAndFilterSchemaGroup( |
| 119 | + schemaGroup: schemaGroup, |
| 120 | + externalReferenceSchemas: externalReferenceSchemasResult.Value, |
| 121 | + schemaRefsByFile: schemaRefsByFile)) |
| 122 | + .ToInsensitiveDictionary( |
| 123 | + keySelector: normalizationResult => normalizationResult.Key, |
| 124 | + elementSelector: normalizationResult => normalizationResult.Value); |
| 125 | + |
| 126 | + var resourceTypeSchemasResults = schemaNormalizationResults |
| 127 | + .Where(kvp => kvp.Value.Value != null) |
| 128 | + .Select(kvp => new OfflineSchema( |
| 129 | + providerNamespace: SchemaUtils.GetProviderNamespaceFromSchemaKey(kvp.Key), |
| 130 | + apiVersion: SchemaUtils.GetVersionFromSchemaKey(kvp.Key), |
| 131 | + schemaContent: kvp.Value.Value)) |
| 132 | + .Select(schema => schema.GetResourceTypeSchemasResult()); |
| 133 | + |
| 134 | + return new ResultWithErrors<ResourceTypeSchema[]> |
| 135 | + { |
| 136 | + Value = resourceTypeSchemasResults.CollectValues().ToArray(), |
| 137 | + Errors = schemaNormalizationResults.Values.CollectErrors() |
| 138 | + .ConcatArray(resourceTypeSchemasResults.CollectErrors()), |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + /// <inheritdoc/> |
| 143 | + public ILookup<string, ResourceTypeSchema> GetSchemasForProvider(string providerNamespace) |
| 144 | + { |
| 145 | + if (this.schemaCache.TryGetValue(providerNamespace, out var schemaLookup)) |
| 146 | + { |
| 147 | + return schemaLookup; |
| 148 | + } |
| 149 | + |
| 150 | + return Enumerable.Empty<ResourceTypeSchema>().ToLookupOrdinalInsensitively(schema => schema.FullyQualifiedResourceType); |
| 151 | + } |
| 152 | + |
| 153 | + /// <inheritdoc/> |
| 154 | + public IEnumerable<ResourceTypeSchema> GetSchemasForResourceType(string fullyQualifiedResourceType) |
| 155 | + { |
| 156 | + var providerNamespace = IResourceIdentifiableExtensions.GetNamespaceFromFullyQualifiedType(fullyQualifiedResourceType); |
| 157 | + |
| 158 | + if (!this.schemaCache.TryGetValue(providerNamespace, out var schemaLookup)) |
| 159 | + { |
| 160 | + return Enumerable.Empty<ResourceTypeSchema>(); |
| 161 | + } |
| 162 | + |
| 163 | + return schemaLookup[fullyQualifiedResourceType]; |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments