-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathUtils.cs
More file actions
976 lines (870 loc) · 41.7 KB
/
Utils.cs
File metadata and controls
976 lines (870 loc) · 41.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics.CodeAnalysis;
using System.IO.Abstractions;
using System.Text.Json;
using Azure.DataApiBuilder.Config;
using Azure.DataApiBuilder.Config.Converters;
using Azure.DataApiBuilder.Config.ObjectModel;
using Azure.DataApiBuilder.Core.Configurations;
using Cli.Commands;
using Microsoft.Extensions.Logging;
/// <summary>
/// Contains the methods for transforming objects, serialization options.
/// </summary>
namespace Cli
{
public class Utils
{
public const string PRODUCT_NAME = "Microsoft.DataApiBuilder";
public const string WILDCARD = "*";
public static readonly string SEPARATOR = ":";
/// <summary>
/// When true, CLI logging to stdout is suppressed to keep the MCP stdio channel clean.
/// </summary>
public static bool IsMcpStdioMode { get; set; }
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
private static ILogger<Utils> _logger;
#pragma warning restore CS8618
public static ILoggerFactory LoggerFactoryForCli = GetLoggerFactoryForCli();
public static void SetCliUtilsLogger(ILogger<Utils> cliUtilsLogger)
{
_logger = cliUtilsLogger;
}
/// Creates an array of Operation element which contains one of the CRUD operation and
/// fields to which this operation is allowed as permission setting based on the given input.
/// </summary>
public static EntityAction[] CreateOperations(string operations, EntityActionPolicy? policy, EntityActionFields? fields)
{
EntityAction[] operation_items;
if (policy is null && fields is null)
{
return operations.Split(",")
.Select(op => EnumExtensions.Deserialize<EntityActionOperation>(op))
.Select(op => new EntityAction(Action: op, Fields: null, Policy: null))
.ToArray();
}
if (operations is WILDCARD)
{
operation_items = new[] { new EntityAction(EntityActionOperation.All, fields, policy) };
}
else
{
string[]? operation_elements = operations.Split(",");
if (policy is not null || fields is not null)
{
List<EntityAction>? operation_list = new();
foreach (string? operation_element in operation_elements)
{
if (EnumExtensions.TryDeserialize(operation_element, out EntityActionOperation? op))
{
EntityAction operation_item = new((EntityActionOperation)op, fields, policy);
operation_list.Add(operation_item);
}
}
operation_items = operation_list.ToArray();
}
else
{
return operation_elements
.Select(op => EnumExtensions.Deserialize<EntityActionOperation>(op))
.Select(op => new EntityAction(Action: op, Fields: null, Policy: null))
.ToArray();
}
}
return operation_items;
}
/// <summary>
/// Given an array of operations, which is a type of JsonElement, convert it to a dictionary
/// key: Valid operation (wild card operation will be expanded)
/// value: Operation object
/// </summary>
/// <param name="operations">Array of operations which is of type JsonElement.</param>
/// <returns>Dictionary of operations</returns>
public static IDictionary<EntityActionOperation, EntityAction> ConvertOperationArrayToIEnumerable(EntityAction[] operations, EntitySourceType? sourceType)
{
Dictionary<EntityActionOperation, EntityAction> result = new();
foreach (EntityAction operation in operations)
{
EntityActionOperation op = operation.Action;
if (op is EntityActionOperation.All)
{
HashSet<EntityActionOperation> resolvedOperations = sourceType is EntitySourceType.StoredProcedure ?
EntityAction.ValidStoredProcedurePermissionOperations :
EntityAction.ValidPermissionOperations;
// Expand wildcard to all valid operations (except execute)
foreach (EntityActionOperation validOp in resolvedOperations)
{
result.Add(validOp, new EntityAction(Action: validOp, Fields: null, Policy: null));
}
}
else
{
result.Add(op, operation);
}
}
return result;
}
/// <summary>
/// Creates a single PermissionSetting Object based on role, operations, fieldsToInclude, and fieldsToExclude.
/// </summary>
public static EntityPermission CreatePermissions(string role, string operations, EntityActionPolicy? policy, EntityActionFields? fields)
{
return new(role, CreateOperations(operations, policy, fields));
}
/// <summary>
/// JsonNamingPolicy to convert all the keys in Json as lower case string.
/// </summary>
public class LowerCaseNamingPolicy : JsonNamingPolicy
{
public override string ConvertName(string name) => name.ToLower();
public static string ConvertName(Enum name) => name.ToString().ToLower();
}
/// <summary>
/// Returns true on successful parsing of mappings Dictionary from IEnumerable list.
/// Returns false in case the format of the input is not correct.
/// </summary>
/// <param name="mappingList">List of ':' separated values indicating exposed and backend names.</param>
/// <param name="mappings">Output a Dictionary containing mapping from backend name to exposed name.</param>
/// <returns> Returns true when successful else on failure, returns false. Else updated PermissionSettings array will be returned.</returns>
public static bool TryParseMappingDictionary(IEnumerable<string> mappingList, out Dictionary<string, string> mappings)
{
mappings = new();
foreach (string item in mappingList)
{
string[] map = item.Split(SEPARATOR);
if (map.Length != 2)
{
_logger.LogError("Invalid format for --map. Acceptable format --map \"backendName1:exposedName1,backendName2:exposedName2,...\".");
return false;
}
mappings.Add(map[0], map[1]);
}
return true;
}
/// <summary>
/// Checks whether the URI component conforms with the expected behavior and does not contain any reserved characters.
/// </summary>
/// <param name="uriComponent">Path prefix/base route for REST/GraphQL APIs.</param>
public static bool IsURIComponentValid(string? uriComponent)
{
// uriComponent is null only in case of cosmosDB and apiType=REST or when the runtime base-route is specified as null.
// For these cases, validation is not required.
if (uriComponent is null)
{
return true;
}
// removing leading '/' before checking for reserved characters.
if (uriComponent.StartsWith('/'))
{
uriComponent = uriComponent.Substring(1);
}
return !RuntimeConfigValidatorUtil.DoesUriComponentContainReservedChars(uriComponent);
}
/// <summary>
/// Returns an object of type Policy
/// If policyRequest or policyDatabase is provided. Otherwise, returns null.
/// </summary>
public static EntityActionPolicy? GetPolicyForOperation(string? policyRequest, string? policyDatabase)
{
if (policyDatabase is null && policyRequest is null)
{
return null;
}
return new EntityActionPolicy(policyRequest, policyDatabase);
}
/// <summary>
/// Returns an object of type Field
/// If fieldsToInclude or fieldsToExclude is provided. Otherwise, returns null.
/// </summary>
public static EntityActionFields? GetFieldsForOperation(IEnumerable<string>? fieldsToInclude, IEnumerable<string>? fieldsToExclude)
{
if (fieldsToInclude is not null && fieldsToInclude.Any() || fieldsToExclude is not null && fieldsToExclude.Any())
{
HashSet<string>? fieldsToIncludeSet = fieldsToInclude is not null && fieldsToInclude.Any() ? new HashSet<string>(fieldsToInclude) : null;
HashSet<string>? fieldsToExcludeSet = fieldsToExclude is not null && fieldsToExclude.Any() ? new HashSet<string>(fieldsToExclude) : new();
return new EntityActionFields(Include: fieldsToIncludeSet, Exclude: fieldsToExcludeSet);
}
return null;
}
/// <summary>
/// Verifies whether the operation provided by the user is valid or not
/// Example:
/// *, create -> Invalid
/// create, create, read -> Invalid
/// * -> Valid
/// fetch, read -> Invalid
/// read, delete -> Valid
/// Also verifies that stored-procedures are not allowed with more than 1 CRUD operations.
/// </summary>
/// <param name="operations">array of string containing operations for permissions</param>
/// <returns>True if no invalid operation is found.</returns>
public static bool VerifyOperations(string[] operations, EntitySourceType? sourceType)
{
// Check if there are any duplicate operations
// Ex: read,read,create
HashSet<string> uniqueOperations = operations.ToHashSet();
if (uniqueOperations.Count() != operations.Length)
{
_logger.LogError("Duplicate action found in --permissions");
return false;
}
// Currently, Stored Procedures can be configured with only Execute Operation.
bool isStoredProcedure = sourceType is EntitySourceType.StoredProcedure;
if (isStoredProcedure && !VerifyExecuteOperationForStoredProcedure(operations))
{
return false;
}
bool containsWildcardOperation = false;
foreach (string operation in uniqueOperations)
{
if (EnumExtensions.TryDeserialize(operation, out EntityActionOperation? op))
{
if (op is EntityActionOperation.All)
{
containsWildcardOperation = true;
}
else if (!isStoredProcedure && !EntityAction.ValidPermissionOperations.Contains((EntityActionOperation)op))
{
_logger.LogError("Invalid actions found in --permissions");
return false;
}
else if (isStoredProcedure && !EntityAction.ValidStoredProcedurePermissionOperations.Contains((EntityActionOperation)op))
{
_logger.LogError("Invalid stored procedure action(s) found in --permissions");
return false;
}
}
else
{
// Check for invalid operation.
_logger.LogError("Invalid actions found in --permissions");
return false;
}
}
// Check for WILDCARD operation with CRUD operations.
if (containsWildcardOperation && uniqueOperations.Count > 1)
{
_logger.LogError("WILDCARD(*) along with other CRUD operations in a single operation is not allowed.");
return false;
}
return true;
}
/// <summary>
/// This method will parse role and operation from permission string.
/// A valid permission string will be of the form "<<role>>:<<actions>>"
/// It will return true if parsing is successful and add the parsed value
/// to the out params role and operations.
/// </summary>
public static bool TryGetRoleAndOperationFromPermission(IEnumerable<string> permissions, [NotNullWhen(true)] out string? role, [NotNullWhen(true)] out string? operations)
{
// Split permission to role and operations.
role = null;
operations = null;
if (permissions.Count() != 2)
{
_logger.LogError("Invalid format for permission. Acceptable format: --permissions \"<<role>>:<<actions>>\"");
return false;
}
role = permissions.ElementAt(0);
operations = permissions.ElementAt(1);
return true;
}
/// <summary>
/// This method will try to find the config file based on the precedence.
/// If the config file is provided by user, it will return that.
/// Else it will check the DAB_ENVIRONMENT variable.
/// In case the environment variable is not set it will check for default config.
/// If none of the files exists it will return false. Else true with output in runtimeConfigFile.
/// In case of false, the runtimeConfigFile will be set to string.Empty.
/// </summary>
public static bool TryGetConfigFileBasedOnCliPrecedence(
FileSystemRuntimeConfigLoader loader,
string? userProvidedConfigFile,
out string runtimeConfigFile)
{
if (!string.IsNullOrEmpty(userProvidedConfigFile))
{
/// The existence of user provided config file is not checked here.
_logger.LogInformation("User provided config file: {userProvidedConfigFile}", userProvidedConfigFile);
runtimeConfigFile = userProvidedConfigFile;
return true;
}
else
{
_logger.LogInformation("Config not provided. Trying to get default config based on DAB_ENVIRONMENT...");
_logger.LogInformation("Environment variable DAB_ENVIRONMENT is {environment}", Environment.GetEnvironmentVariable("DAB_ENVIRONMENT"));
runtimeConfigFile = loader.GetFileNameForEnvironment(null, considerOverrides: false);
}
return !string.IsNullOrEmpty(runtimeConfigFile);
}
/// <summary>
/// Validates correct usage of parameters and key-fields based on the source type.
/// Ensures that parameters are only used with stored procedures, key-fields only with tables/views,
/// and that key-fields are always provided for views.
/// </summary>
/// <param name="sourceType">Type of the source object.</param>
/// <param name="parameters">IEnumerable of strings containing parameters for stored procedures (old format).</param>
/// <param name="parametersNameCollection">IEnumerable of strings containing parameter names for stored procedures (new format).</param>
/// <param name="keyFields">IEnumerable of strings containing key columns for tables/views.</param>
/// <returns>True if the pairing is valid; otherwise, false.</returns>
public static bool VerifyCorrectPairingOfParameterAndKeyFieldsWithType(
EntitySourceType? sourceType,
IEnumerable<string>? parameters, // old format
IEnumerable<string>? parametersNameCollection, // new format
IEnumerable<string>? keyFields)
{
bool hasOldParams = parameters is not null && parameters.Any();
bool hasNewParams = parametersNameCollection is not null && parametersNameCollection.Any();
if (sourceType is EntitySourceType.StoredProcedure)
{
if (keyFields is not null && keyFields.Any())
{
_logger.LogError("Stored Procedures don't support KeyFields.");
return false;
}
}
else
{
// For Views and Tables
if (hasOldParams || hasNewParams)
{
_logger.LogError("Tables/Views don't support parameters.");
return false;
}
if (sourceType is EntitySourceType.View && (keyFields is null || !keyFields.Any()))
{
_logger.LogError("Key-fields are mandatory for views, but not provided.");
return false;
}
}
return true;
}
/// <summary>
/// Creates source object by using valid type, params, and keyfields.
/// </summary>
/// <param name="name">Name of the source.</param>
/// <param name="type">Type of the source. i.e, table,view, and stored-procedure.</param>
/// <param name="parameters">Dictionary for parameters if source is stored-procedure</param>
/// <param name="keyFields">Array of string containing key columns for table/view type.</param>
/// <param name="sourceObject">Outputs the created source object.
/// It can be null, string, or DatabaseObjectSource</param>
/// <returns>True in case of successful creation of source object.</returns>
public static bool TryCreateSourceObject(
string name,
EntitySourceType? type,
List<ParameterMetadata>? parameters,
string[]? keyFields,
[NotNullWhen(true)] out EntitySource? sourceObject)
{
sourceObject = new EntitySource(
Type: type,
Object: name,
Parameters: parameters,
KeyFields: keyFields
);
return true;
}
/// <summary>
/// This method tries to parse the source parameters Dictionary from IEnumerable list
/// by splitting each item of the list on ':', where first item is param name and the
/// and the second item is the value. for any other item it should fail.
/// If Parameter List is null, no parsing happens and sourceParameter is returned as null.
/// </summary>
/// <param name="parametersList">List of ':' separated values indicating key and value.</param>
/// <param name="mappings">Output a Dictionary of parameters and their values.</param>
/// <returns> Returns true when successful else on failure, returns false.</returns>
public static bool TryParseSourceParameterDictionary(
IEnumerable<string>? parametersList,
out List<ParameterMetadata>? parameterMetadataList)
{
parameterMetadataList = null;
if (parametersList is null)
{
return true;
}
parameterMetadataList = new();
foreach (string param in parametersList)
{
string[] items = param.Split(SEPARATOR);
if (items.Length != 2)
{
parameterMetadataList = null;
_logger.LogError("Invalid format for --source.params");
_logger.LogError("Correct source parameter syntax: --source.params \"key1:value1,key2:value2,...\".");
return false;
}
string paramKey = items[0];
object paramValue = ParseStringValue(items[1]);
// Add to ParameterMetadata list with default values for rich metadata
parameterMetadataList.Add(new ParameterMetadata
{
Name = paramKey,
Default = paramValue.ToString(),
Required = false,
Description = null
});
}
if (!parameterMetadataList.Any())
{
parameterMetadataList = null;
}
return true;
}
/// <summary>
/// This method loops through every role specified for stored-procedure entity
/// and checks if it has only one CRUD operation.
/// </summary>
public static bool VerifyPermissionOperationsForStoredProcedures(
EntityPermission[] permissionSettings)
{
foreach (EntityPermission permissionSetting in permissionSettings)
{
if (!VerifyExecuteOperationForStoredProcedure(permissionSetting.Actions))
{
return false;
}
}
return true;
}
/// <summary>
/// This method checks that stored-procedure entity
/// is configured only with execute action
/// </summary>
private static bool VerifyExecuteOperationForStoredProcedure(EntityAction[] operations)
{
if (operations.Length > 1
|| (operations.First().Action is not EntityActionOperation.Execute && operations.First().Action is not EntityActionOperation.All))
{
_logger.LogError("Stored Procedure supports only execute operation.");
return false;
}
return true;
}
/// <summary>
/// This method checks that stored-procedure entity
/// is configured only with execute action
/// </summary>
private static bool VerifyExecuteOperationForStoredProcedure(string[] operations)
{
if (operations.Length > 1
|| !EnumExtensions.TryDeserialize(operations.First(), out EntityActionOperation? operation)
|| (operation is not EntityActionOperation.Execute && operation is not EntityActionOperation.All))
{
_logger.LogError("Stored Procedure supports only execute operation.");
return false;
}
return true;
}
/// <summary>
/// Check both Audience and Issuer are specified when the authentication provider is JWT.
/// Also providing Audience or Issuer with StaticWebApps or AppService wil result in failure.
/// </summary>
public static bool ValidateAudienceAndIssuerForJwtProvider(
string authenticationProvider,
string? audience,
string? issuer)
{
if (Enum.TryParse<EasyAuthType>(authenticationProvider, ignoreCase: true, out _)
|| AuthenticationOptions.SIMULATOR_AUTHENTICATION.Equals(authenticationProvider, StringComparison.OrdinalIgnoreCase)
|| AuthenticationOptions.UNAUTHENTICATED_AUTHENTICATION.Equals(authenticationProvider, StringComparison.OrdinalIgnoreCase))
{
if (!(string.IsNullOrWhiteSpace(audience)) || !(string.IsNullOrWhiteSpace(issuer)))
{
_logger.LogWarning("Audience and Issuer can't be set for EasyAuth, Simulator, or Unauthenticated authentication.");
return true;
}
}
else
{
if (string.IsNullOrWhiteSpace(audience) || string.IsNullOrWhiteSpace(issuer))
{
_logger.LogError($"Authentication providers other than EasyAuth, Simulator, and Unauthenticated require both Audience and Issuer.");
return false;
}
}
return true;
}
/// <summary>
/// Converts string into either integer, double, or boolean value.
/// If the given string is neither of the above, it returns as string.
/// </summary>
private static object ParseStringValue(string stringValue)
{
if (int.TryParse(stringValue, out int integerValue))
{
return integerValue;
}
else if (double.TryParse(stringValue, out double floatingValue))
{
return floatingValue;
}
else if (bool.TryParse(stringValue, out bool booleanValue))
{
return booleanValue;
}
return stringValue;
}
/// <summary>
/// This method will write all the json string in the given file.
/// </summary>
public static bool WriteRuntimeConfigToFile(string file, RuntimeConfig runtimeConfig, IFileSystem fileSystem)
{
try
{
string jsonContent = runtimeConfig.ToJson();
return WriteJsonToFile(file, jsonContent, fileSystem);
}
catch (Exception e)
{
_logger.LogError("Failed to generate the config file, operation failed with exception: {e}.", e);
return false;
}
}
public static bool WriteJsonToFile(string file, string jsonContent, IFileSystem fileSystem)
{
try
{
fileSystem.File.WriteAllText(file, jsonContent);
}
catch (Exception e)
{
_logger.LogError("Failed to generate the config file, operation failed with exception:{e}.", e);
return false;
}
return true;
}
/// <summary>
/// Utility method that converts REST HTTP verb string input to RestMethod Enum.
/// The method returns true/false corresponding to successful/unsuccessful conversion.
/// </summary>
/// <param name="method">String input entered by the user</param>
/// <param name="restMethod">RestMethod Enum type</param>
/// <returns></returns>
public static bool TryConvertRestMethodNameToRestMethod(string? method, out SupportedHttpVerb restMethod)
{
if (!Enum.TryParse(method, ignoreCase: true, out restMethod))
{
_logger.LogError("Invalid REST Method. Supported methods are {restMethods}.", string.Join(", ", Enum.GetNames<SupportedHttpVerb>()));
return false;
}
return true;
}
/// <summary>
/// Utility method that converts list of REST HTTP verbs configured for a
/// stored procedure into an array of RestMethod Enum type.
/// If any invalid REST methods are supplied, an empty array is returned.
/// </summary>
/// <param name="methods">Collection of REST HTTP verbs configured for the stored procedure</param>
/// <returns>REST methods as an array of RestMethod Enum type.</returns>
public static SupportedHttpVerb[] CreateRestMethods(IEnumerable<string> methods)
{
List<SupportedHttpVerb> restMethods = new();
foreach (string method in methods)
{
SupportedHttpVerb restMethod;
if (TryConvertRestMethodNameToRestMethod(method, out restMethod))
{
restMethods.Add(restMethod);
}
else
{
restMethods.Clear();
break;
}
}
return restMethods.ToArray();
}
/// <summary>
/// Utility method that converts the graphQL operation configured for the stored procedure to
/// GraphQLOperation Enum type.
/// The method returns true/false corresponding to successful/unsuccessful conversion.
/// </summary>
/// <param name="operation">GraphQL operation configured for the stored procedure</param>
/// <param name="graphQLOperation">GraphQL Operation as an Enum type</param>
/// <returns>true/false</returns>
public static bool TryConvertGraphQLOperationNameToGraphQLOperation(string? operation, [NotNullWhen(true)] out GraphQLOperation graphQLOperation)
{
if (!Enum.TryParse(operation, ignoreCase: true, out graphQLOperation))
{
_logger.LogError(
"Invalid GraphQL Operation. Supported operations are {queryName} and {mutationName}.",
GraphQLOperation.Query,
GraphQLOperation.Mutation);
return false;
}
return true;
}
/// <summary>
/// Method to check if the options for an entity represent a stored procedure
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
public static bool IsStoredProcedure(EntityOptions options)
{
if (options.SourceType is not null && EnumExtensions.TryDeserialize(options.SourceType, out EntitySourceType? sourceObjectType))
{
return sourceObjectType is EntitySourceType.StoredProcedure;
}
return false;
}
/// <summary>
/// Method to determine whether the type of an entity is being converted from stored-procedure to
/// table/view.
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public static bool IsStoredProcedure(Entity entity)
{
return entity.Source.Type is EntitySourceType.StoredProcedure;
}
/// <summary>
/// Method to determine if the type of the entity is being converted from
/// stored-procedure to table/view.
/// </summary>
/// <param name="entity">Entity for which the source type conversion is being determined</param>
/// <param name="options">Options from the CLI commands</param>
/// <returns>True when an entity of type stored-procedure is converted to a table/view</returns>
public static bool IsStoredProcedureConvertedToOtherTypes(Entity entity, EntityOptions options)
{
if (options.SourceType is null)
{
return false;
}
bool isCurrentEntityStoredProcedure = IsStoredProcedure(entity);
bool doOptionsRepresentStoredProcedure = options.SourceType is not null && IsStoredProcedure(options);
return isCurrentEntityStoredProcedure && !doOptionsRepresentStoredProcedure;
}
/// <summary>
/// Method to determine whether the type of an entity is being changed from
/// table/view to stored-procedure.
/// </summary>
/// <param name="entity">Entity for which the source type conversion is being determined</param>
/// <param name="options">Options from the CLI commands</param>
/// <returns>True when an entity of type table/view is converted to a stored-procedure</returns>
public static bool IsEntityBeingConvertedToStoredProcedure(Entity entity, EntityOptions options)
{
if (options.SourceType is null)
{
return false;
}
bool isCurrentEntityStoredProcedure = IsStoredProcedure(entity);
bool doOptionsRepresentStoredProcedure = options.SourceType is not null && IsStoredProcedure(options);
return !isCurrentEntityStoredProcedure && doOptionsRepresentStoredProcedure;
}
/// <summary>
/// For stored procedures, the rest HTTP verbs to be supported can be configured using
/// --rest.methods option.
/// Validation to ensure that configuring REST methods for a stored procedure that is
/// not enabled for REST results in an error. This validation is run along
/// with add command.
/// </summary>
/// <param name="options">Options entered using add command</param>
/// <returns>True for invalid conflicting REST options. False when the options are valid</returns>
public static bool CheckConflictingRestConfigurationForStoredProcedures(EntityOptions options)
{
return (options.RestRoute is not null && bool.TryParse(options.RestRoute, out bool restEnabled) && !restEnabled) &&
(options.RestMethodsForStoredProcedure is not null && options.RestMethodsForStoredProcedure.Any());
}
/// <summary>
/// For stored procedures, the graphql operation to be supported can be configured using
/// --graphql.operation.
/// Validation to ensure that configuring GraphQL operation for a stored procedure that is
/// not exposed for graphQL results in an error. This validation is run along with add
/// command
/// </summary>
/// <param name="options"></param>
/// <returns>True for invalid conflicting graphQL options. False when the options are not conflicting</returns>
public static bool CheckConflictingGraphQLConfigurationForStoredProcedures(EntityOptions options)
{
return (options.GraphQLType is not null && bool.TryParse(options.GraphQLType, out bool graphQLEnabled) && !graphQLEnabled)
&& (options.GraphQLOperationForStoredProcedure is not null);
}
/// <summary>
/// Constructs the REST Path using the add/update command --rest option
/// </summary>
/// <param name="restRoute">Input entered using --rest option</param>
/// <param name="supportedHttpVerbs">Supported HTTP verbs for the entity.</param>
/// <param name="isCosmosDbNoSql">True when the entity is a CosmosDB NoSQL entity, and if it is true, REST is disabled.</param>
/// <returns>Constructed REST options for the entity.</returns>
public static EntityRestOptions ConstructRestOptions(string? restRoute, SupportedHttpVerb[]? supportedHttpVerbs, bool isCosmosDbNoSql)
{
// REST is not supported for CosmosDB NoSQL, so we'll forcibly disable it.
if (isCosmosDbNoSql)
{
return new(Enabled: false);
}
EntityRestOptions restOptions = new(supportedHttpVerbs);
// Default state for REST is enabled, so if no value is provided, we enable it
if (restRoute is null)
{
return restOptions with { Enabled = true, Methods = supportedHttpVerbs };
}
else
{
if (bool.TryParse(restRoute, out bool restEnabled))
{
restOptions = restOptions with { Enabled = restEnabled };
}
else
{
restOptions = restOptions with { Enabled = true, Path = "/" + restRoute };
}
}
return restOptions;
}
/// <summary>
/// Constructs the graphQL Type from add/update command --graphql option
/// </summary>
/// <param name="graphQL">GraphQL type input from the CLI commands</param>
/// <param name="graphQLOperationsForStoredProcedures">GraphQL operation input from the CLI commands.</param>
/// <returns>Constructed GraphQL Type</returns>
public static EntityGraphQLOptions ConstructGraphQLTypeDetails(string? graphQL, GraphQLOperation? graphQLOperationsForStoredProcedures)
{
EntityGraphQLOptions graphQLType = new(
Singular: string.Empty,
Plural: string.Empty,
Operation: graphQLOperationsForStoredProcedures);
// Default state for GraphQL is enabled, so if no value is provided, we enable it
if (graphQL is null)
{
return graphQLType with { Enabled = true };
}
else
{
if (bool.TryParse(graphQL, out bool graphQLEnabled))
{
graphQLType = graphQLType with { Enabled = graphQLEnabled };
}
else
{
string singular, plural = string.Empty;
if (graphQL.Contains(SEPARATOR))
{
string[] arr = graphQL.Split(SEPARATOR);
if (arr.Length != 2)
{
_logger.LogError("Invalid format for --graphql. Accepted values are true/false, a string, or a pair of string in the format <singular>:<plural>");
return graphQLType;
}
singular = arr[0];
plural = arr[1];
}
else
{
singular = graphQL;
}
// If we have singular/plural text we infer that GraphQL is enabled
graphQLType = graphQLType with { Enabled = true, Singular = singular, Plural = plural };
}
}
return graphQLType;
}
/// <summary>
/// Constructs the EntityCacheOption for Add/Update.
/// </summary>
/// <param name="cacheEnabled">String value that defines if the cache is enabled.</param>
/// <param name="cacheTtl">Int that gives time to live in seconds for cache.</param>
/// <returns>EntityCacheOption if values are provided for cacheEnabled or cacheTtl, null otherwise.</returns>
public static EntityCacheOptions? ConstructCacheOptions(string? cacheEnabled, string? cacheTtl)
{
if (cacheEnabled is null && cacheTtl is null)
{
return null;
}
EntityCacheOptions cacheOptions = new();
bool isEnabled = false;
bool isCacheTtlUserProvided = false;
int ttl = EntityCacheOptions.DEFAULT_TTL_SECONDS;
if (cacheEnabled is not null && !bool.TryParse(cacheEnabled, out isEnabled))
{
_logger.LogError("Invalid format for --cache.enabled. Accepted values are true/false.");
}
if ((cacheTtl is not null && !int.TryParse(cacheTtl, out ttl)) || ttl < 0)
{
_logger.LogError("Invalid format for --cache.ttl. Accepted values are any non-negative integer.");
}
// This is needed so the cacheTtl is correctly written to config.
if (cacheTtl is not null)
{
isCacheTtlUserProvided = true;
}
// Both cacheEnabled and cacheTtl can not be null here, so if either one
// is, the other is not, and we return the cacheOptions with just that other
// value.
if (cacheEnabled is null)
{
return cacheOptions with { TtlSeconds = ttl, UserProvidedTtlOptions = isCacheTtlUserProvided };
}
if (cacheTtl is null)
{
return cacheOptions with { Enabled = isEnabled };
}
return cacheOptions with { Enabled = isEnabled, TtlSeconds = ttl, UserProvidedTtlOptions = isCacheTtlUserProvided };
}
/// <summary>
/// Constructs the EntityMcpOptions for Add/Update.
/// </summary>
/// <param name="mcpDmlTools">String value that defines if DML tools are enabled for MCP.</param>
/// <param name="mcpCustomTool">String value that defines if custom tool is enabled for MCP.</param>
/// <param name="isStoredProcedure">Whether the entity is a stored procedure.</param>
/// <returns>EntityMcpOptions if values are provided, null otherwise.</returns>
public static EntityMcpOptions? ConstructMcpOptions(string? mcpDmlTools, string? mcpCustomTool, bool isStoredProcedure)
{
if (mcpDmlTools is null && mcpCustomTool is null)
{
return null;
}
bool? dmlToolsEnabled = null;
bool? customToolEnabled = null;
// Parse dml-tools option
if (mcpDmlTools is not null)
{
if (!bool.TryParse(mcpDmlTools, out bool dmlValue))
{
_logger.LogError("Invalid format for --mcp.dml-tools. Accepted values are true/false.");
return null;
}
dmlToolsEnabled = dmlValue;
}
// Parse custom-tool option
if (mcpCustomTool is not null)
{
if (!bool.TryParse(mcpCustomTool, out bool customValue))
{
_logger.LogError("Invalid format for --mcp.custom-tool. Accepted values are true/false.");
return null;
}
// Validate that custom-tool can only be used with stored procedures
if (customValue && !isStoredProcedure)
{
_logger.LogError("--mcp.custom-tool can only be enabled for stored procedures.");
return null;
}
customToolEnabled = customValue;
}
return new EntityMcpOptions(customToolEnabled, dmlToolsEnabled);
}
/// <summary>
/// Check if add/update command has Entity provided. Return false otherwise.
/// </summary>
public static bool IsEntityProvided(string? entity, ILogger cliLogger, string command)
{
if (string.IsNullOrWhiteSpace(entity))
{
cliLogger.LogError("Entity name is missing. Usage: dab {command} [entity-name] [{command}-options]", command, command);
return false;
}
return true;
}
/// <summary>
/// Returns ILoggerFactory with CLI custom logger provider.
/// </summary>
public static ILoggerFactory GetLoggerFactoryForCli()
{
ILoggerFactory loggerFactory = new LoggerFactory();
loggerFactory.AddProvider(new CustomLoggerProvider());
return loggerFactory;
}
}
}