|
1 | 1 | // Copyright (c) Microsoft Corporation. |
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
| 4 | +using System.Net; |
4 | 5 | using System.Text.Json; |
5 | 6 | using System.Text.Json.Serialization; |
| 7 | +using Azure.DataApiBuilder.Service.Exceptions; |
6 | 8 |
|
7 | 9 | namespace Azure.DataApiBuilder.Config.ObjectModel; |
8 | 10 |
|
9 | | -public record RuntimeConfig( |
10 | | - [property: JsonPropertyName("$schema")] string Schema, |
11 | | - DataSource DataSource, |
12 | | - RuntimeOptions Runtime, |
13 | | - RuntimeEntities Entities) |
| 11 | +public record RuntimeConfig |
14 | 12 | { |
| 13 | + [JsonPropertyName("$schema")] |
| 14 | + public string Schema { get; init; } |
| 15 | + |
| 16 | + public DataSource DataSource { get; init; } |
| 17 | + |
| 18 | + public RuntimeOptions Runtime { get; init; } |
| 19 | + |
| 20 | + public RuntimeEntities Entities { get; init; } |
| 21 | + |
| 22 | + private string _defaultDataSourceName; |
| 23 | + |
| 24 | + private Dictionary<string, DataSource> _dataSourceNameToDataSource; |
| 25 | + |
| 26 | + private Dictionary<string, string> _entityNameToDataSourceName; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// List of all datasources. |
| 30 | + /// </summary> |
| 31 | + /// <returns>List of datasources</returns> |
| 32 | + public IEnumerable<DataSource> ListAllDataSources() |
| 33 | + { |
| 34 | + return _dataSourceNameToDataSource.Values; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Get Iterator to iterate over dictionary. |
| 39 | + /// </summary> |
| 40 | + public IEnumerable<KeyValuePair<string, DataSource>> GetDataSourceNamesToDataSourcesIterator() |
| 41 | + { |
| 42 | + return _dataSourceNameToDataSource.AsEnumerable(); |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Constructor for runtimeConfig. |
| 47 | + /// </summary> |
| 48 | + /// <param name="Schema">schema.</param> |
| 49 | + /// <param name="DataSource">Default datasource.</param> |
| 50 | + /// <param name="Runtime">Runtime settings.</param> |
| 51 | + /// <param name="Entities">Entities</param> |
| 52 | + [JsonConstructor] |
| 53 | + public RuntimeConfig(string Schema, DataSource DataSource, RuntimeOptions Runtime, RuntimeEntities Entities) |
| 54 | + { |
| 55 | + this.Schema = Schema; |
| 56 | + this.DataSource = DataSource; |
| 57 | + this.Runtime = Runtime; |
| 58 | + this.Entities = Entities; |
| 59 | + this._dataSourceNameToDataSource = new Dictionary<string, DataSource>(); |
| 60 | + this._defaultDataSourceName = Guid.NewGuid().ToString(); |
| 61 | + this._dataSourceNameToDataSource.Add(this._defaultDataSourceName, this.DataSource); |
| 62 | + |
| 63 | + this._entityNameToDataSourceName = new Dictionary<string, string>(); |
| 64 | + foreach (KeyValuePair<string, Entity> entity in Entities) |
| 65 | + { |
| 66 | + _entityNameToDataSourceName.TryAdd(entity.Key, this._defaultDataSourceName); |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Gets the DataSource corresponding to the datasourceName. |
| 73 | + /// </summary> |
| 74 | + /// <param name="dataSourceName">Name of datasource.</param> |
| 75 | + /// <returns>DataSource object.</returns> |
| 76 | + /// <exception cref="DataApiBuilderException">Not found exception if key is not found.</exception> |
| 77 | + public DataSource GetDataSourceFromDataSourceName(string dataSourceName) |
| 78 | + { |
| 79 | + CheckDataSourceNamePresent(dataSourceName); |
| 80 | + return _dataSourceNameToDataSource[dataSourceName]; |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Tries to add the datasource to the DataSourceNameToDataSource dictionary. |
| 85 | + /// </summary> |
| 86 | + /// <param name="dataSourceName">dataSourceName.</param> |
| 87 | + /// <param name="dataSource">dataSource.</param> |
| 88 | + /// <returns>True indicating success, False indicating failure.</returns> |
| 89 | + public bool AddDataSource(string dataSourceName, DataSource dataSource) |
| 90 | + { |
| 91 | + return _dataSourceNameToDataSource.TryAdd(dataSourceName, dataSource); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Updates the DataSourceNameToDataSource dictionary with the new datasource. |
| 96 | + /// </summary> |
| 97 | + /// <param name="dataSourceName">Name of datasource</param> |
| 98 | + /// <param name="dataSource">Updated datasource value.</param> |
| 99 | + /// <exception cref="DataApiBuilderException">Not found exception if key is not found.</exception> |
| 100 | + public void UpdateDataSourceNameToDataSource(string dataSourceName, DataSource dataSource) |
| 101 | + { |
| 102 | + CheckDataSourceNamePresent(dataSourceName); |
| 103 | + _dataSourceNameToDataSource[dataSourceName] = dataSource; |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Removes the datasource from the DataSourceNameToDataSource dictionary. |
| 108 | + /// </summary> |
| 109 | + /// <param name="dataSourceName">DataSourceName.</param> |
| 110 | + /// <returns>True indicating success, False indicating failure.</returns> |
| 111 | + /// <exception cref="DataApiBuilderException">Not found exception if key is not found.</exception> |
| 112 | + public bool RemoveDataSource(string dataSourceName) |
| 113 | + { |
| 114 | + CheckDataSourceNamePresent(dataSourceName); |
| 115 | + return _dataSourceNameToDataSource.Remove(dataSourceName); |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Gets datasourceName from EntityNameToDatasourceName dictionary. |
| 120 | + /// </summary> |
| 121 | + /// <param name="entityName">entityName</param> |
| 122 | + /// <returns>DataSourceName</returns> |
| 123 | + public string GetDataSourceNameFromEntityName(string entityName) |
| 124 | + { |
| 125 | + CheckEntityNamePresent(entityName); |
| 126 | + return _entityNameToDataSourceName[entityName]; |
| 127 | + } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Gets datasource using entityName. |
| 131 | + /// </summary> |
| 132 | + /// <param name="entityName">entityName.</param> |
| 133 | + /// <returns>DataSource using EntityName.</returns> |
| 134 | + public DataSource GetDataSourceFromEntityName(string entityName) |
| 135 | + { |
| 136 | + CheckEntityNamePresent(entityName); |
| 137 | + return _dataSourceNameToDataSource[_entityNameToDataSourceName[entityName]]; |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Add entity to the EntityNameToDataSourceName dictionary. |
| 142 | + /// </summary> |
| 143 | + /// <param name="entityName">EntityName</param> |
| 144 | + /// <param name="dataSourceName">DatasourceName.</param> |
| 145 | + /// <returns>True indicating success, False indicating failure.</returns> |
| 146 | + /// <exception cref="DataApiBuilderException">Not found exception if key is not found.</exception> |
| 147 | + public bool AddEntity(string entityName, string dataSourceName) |
| 148 | + { |
| 149 | + CheckDataSourceNamePresent(dataSourceName); |
| 150 | + return _entityNameToDataSourceName.TryAdd(entityName, dataSourceName); |
| 151 | + } |
| 152 | + |
| 153 | + /// <summary> |
| 154 | + /// Updates the EntityNameToDataSourceName dictionary with the new Entity to datasource mapping. |
| 155 | + /// </summary> |
| 156 | + /// <param name="entityName">EntityName.</param> |
| 157 | + /// <param name="dataSourceName">dataSourceName.</param> |
| 158 | + /// <exception cref="DataApiBuilderException"></exception> |
| 159 | + public void UpdateEntityNameToDataSourceName(string entityName, string dataSourceName) |
| 160 | + { |
| 161 | + CheckDataSourceNamePresent(dataSourceName); |
| 162 | + CheckEntityNamePresent(entityName); |
| 163 | + _entityNameToDataSourceName[entityName] = dataSourceName; |
| 164 | + } |
| 165 | + |
| 166 | + /// <summary> |
| 167 | + /// Removes the entity from the EntityNameToDataSourceName dictionary. |
| 168 | + /// </summary> |
| 169 | + /// <param name="entityName">Name of Entity</param> |
| 170 | + /// <exception cref="DataApiBuilderException">Not found exception if key is not found.</exception> |
| 171 | + public bool RemoveEntity(string entityName) |
| 172 | + { |
| 173 | + CheckEntityNamePresent(entityName); |
| 174 | + return _entityNameToDataSourceName.Remove(entityName); |
| 175 | + } |
| 176 | + |
| 177 | + /// <summary> |
| 178 | + /// Get the default datasource name. |
| 179 | + /// </summary> |
| 180 | + /// <returns>default datasourceName.</returns> |
| 181 | +#pragma warning disable CA1024 // Use properties where appropriate. Reason: Do not want datasource serialized and want to keep it private to restrict set; |
| 182 | + public string GetDefaultDataSourceName() |
| 183 | +#pragma warning restore CA1024 // Use properties where appropriate |
| 184 | + { |
| 185 | + return _defaultDataSourceName; |
| 186 | + } |
| 187 | + |
15 | 188 | /// <summary> |
16 | 189 | /// Serializes the RuntimeConfig object to JSON for writing to file. |
17 | 190 | /// </summary> |
18 | 191 | /// <returns></returns> |
19 | | - public string ToJson() |
| 192 | + public string ToJson(JsonSerializerOptions? jsonSerializerOptions = null) |
| 193 | + { |
| 194 | + // get default serializer options if none provided. |
| 195 | + jsonSerializerOptions = jsonSerializerOptions ?? RuntimeConfigLoader.GetSerializationOptions(); |
| 196 | + return JsonSerializer.Serialize(this, jsonSerializerOptions); |
| 197 | + } |
| 198 | + |
| 199 | + private void CheckDataSourceNamePresent(string dataSourceName) |
| 200 | + { |
| 201 | + if (!_dataSourceNameToDataSource.ContainsKey(dataSourceName)) |
| 202 | + { |
| 203 | + throw new DataApiBuilderException($"{nameof(dataSourceName)}:{dataSourceName} could not be found within the config", HttpStatusCode.BadRequest, DataApiBuilderException.SubStatusCodes.DataSourceNotFound); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + private void CheckEntityNamePresent(string entityName) |
20 | 208 | { |
21 | | - return JsonSerializer.Serialize(this, RuntimeConfigLoader.GetSerializationOptions()); |
| 209 | + if (!_entityNameToDataSourceName.ContainsKey(entityName)) |
| 210 | + { |
| 211 | + throw new DataApiBuilderException($"{nameof(entityName)}:{entityName} could not be found within the config", HttpStatusCode.BadRequest, DataApiBuilderException.SubStatusCodes.EntityNotFound); |
| 212 | + } |
22 | 213 | } |
23 | 214 | } |
0 commit comments