Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit eb68e3f

Browse files
KitAmosKit AmosRupengLiu
authored
Addition of batch processing operations (#483)
* Added flag to configuration for breaking operations into chained groups * rebaselined with master * fixed nesting in new method * removed unused chunked method * tidied spacing Co-authored-by: Kit Amos <kamos@goodstart.org.au> Co-authored-by: RupengLiu <rliu1211@terpmail.umd.edu>
1 parent ac14568 commit eb68e3f

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

src/APIM_ARMTemplate/apimtemplate/Extractor/EntityExtractors/APIExtractor.cs

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ private async Task<IEnumerable<TemplateResource>> GetRelatedTemplateResourcesAsy
500500
List<TemplateResource> templateResources = new List<TemplateResource>();
501501
string apimname = exc.sourceApimName, resourceGroup = exc.resourceGroup, fileFolder = exc.fileFolder, policyXMLBaseUrl = exc.policyXMLBaseUrl, policyXMLSasToken = exc.policyXMLSasToken;
502502

503-
#region Schemas
503+
#region Schemas
504504
// add schema resources to api template
505505
List<TemplateResource> schemaResources = await GenerateSchemasARMTemplate(apimname, apiName, resourceGroup, fileFolder);
506506
templateResources.AddRange(schemaResources);
@@ -509,9 +509,30 @@ private async Task<IEnumerable<TemplateResource>> GetRelatedTemplateResourcesAsy
509509
#region Operations
510510
// pull api operations for service
511511
string[] operationNames = await GetAllOperationNames(apimname, resourceGroup, apiName);
512+
int numBatches = 0;
513+
514+
// create empty array for the batch operation owners
515+
List<string> batchOwners = new List<string>();
516+
517+
// if a batch size is specified
518+
if(exc.operationBatchSize > 0) {
519+
// store the number of batches required based on exc.operationBatchSize
520+
numBatches = (int)Math.Ceiling((double)operationNames.Length/ (double)exc.operationBatchSize);
521+
//Console.WriteLine ("Number of batches: {0}", numBatches);
522+
}
523+
512524

513525
foreach (string operationName in operationNames)
514526
{
527+
int opIndex = Array.IndexOf(operationNames, operationName);
528+
529+
//add batch owners into array
530+
// ensure each owner is linked to the one before
531+
if(exc.operationBatchSize > 0 && opIndex < numBatches) {
532+
batchOwners.Add(operationName);
533+
//Console.WriteLine("Adding operation {0} to owner list", operationName);
534+
}
535+
515536
string operationDetails = await GetAPIOperationDetailsAsync(apimname, resourceGroup, apiName, operationName);
516537

517538
Console.WriteLine("'{0}' Operation found", operationName);
@@ -540,6 +561,24 @@ private async Task<IEnumerable<TemplateResource>> GetRelatedTemplateResourcesAsy
540561
}
541562
}
542563

564+
// add to batch if flagged
565+
string batchdependsOn;
566+
567+
if(exc.operationBatchSize > 0 && opIndex > 0) {
568+
if(opIndex >= 1 && opIndex <= numBatches - 1) {
569+
// chain the owners to each other
570+
batchdependsOn = $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('{ParameterNames.ApimServiceName}'), '{apiName}', '{batchOwners[opIndex - 1]}')]";
571+
//Console.WriteLine("Owner chaining: this request {0} to previous {1}", operationName, batchOwners[opIndex-1]);
572+
} else {
573+
// chain the operation to respective owner
574+
int ownerIndex = (int)Math.Floor((opIndex - numBatches)/((double)exc.operationBatchSize-1));
575+
batchdependsOn = $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('{ParameterNames.ApimServiceName}'), '{apiName}', '{batchOwners[ownerIndex]}')]";
576+
//Console.WriteLine("Operation {0} chained to owner {1}", operationName, batchOwners[ownerIndex]);
577+
}
578+
579+
operationDependsOn.Add(batchdependsOn);
580+
}
581+
543582
operationResource.dependsOn = operationDependsOn.ToArray();
544583
templateResources.Add(operationResource);
545584

@@ -638,8 +677,8 @@ private async Task<IEnumerable<TemplateResource>> GetRelatedTemplateResourcesAsy
638677
catch (Exception) { }
639678
#endregion
640679

641-
#region API Tags
642-
// add tags associated with the api to template
680+
#region API Tags
681+
// add tags associated with the api to template
643682
try
644683
{
645684
// pull tags associated with the api
@@ -661,10 +700,11 @@ private async Task<IEnumerable<TemplateResource>> GetRelatedTemplateResourcesAsy
661700
}
662701
}
663702
catch (Exception) { }
664-
#endregion
703+
#endregion
665704

666-
#region API Products
705+
667706
// add product api associations to template
707+
#region API Products
668708
try
669709
{
670710
// pull product api associations
@@ -708,8 +748,8 @@ private async Task<IEnumerable<TemplateResource>> GetRelatedTemplateResourcesAsy
708748
diagnosticResource.scale = null;
709749
diagnosticResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('{ParameterNames.ApimServiceName}'), '{apiName}')]" };
710750

711-
if (exc.paramApiLoggerId)
712-
{
751+
if (exc.paramApiLoggerId) {
752+
713753
diagnosticResource.properties.loggerId = $"[parameters('{ParameterNames.ApiLoggerId}').{ExtractorUtils.GenValidParamName(apiName, ParameterPrefix.Api)}.{ExtractorUtils.GenValidParamName(diagnosticName, ParameterPrefix.Diagnostic)}]";
714754
}
715755

@@ -720,6 +760,7 @@ private async Task<IEnumerable<TemplateResource>> GetRelatedTemplateResourcesAsy
720760
}
721761

722762
templateResources.Add(diagnosticResource);
763+
723764
}
724765
#endregion
725766

src/APIM_ARMTemplate/apimtemplate/Extractor/Models/ExtractorConfiguration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public class ExtractorConfig
4949
[Description("Should not include named values template")]
5050
public string notIncludeNamedValue { get; set; }
5151

52+
[Description("Group the operations into batches of x?")]
53+
public int operationBatchSize {get;set;}
5254
public void Validate()
5355
{
5456
if (string.IsNullOrEmpty(sourceApimName)) throw new ArgumentException("Missing parameter <sourceApimName>.");
@@ -109,6 +111,8 @@ public class Extractor
109111
public bool paramLogResourceId { get; private set; }
110112
public bool notIncludeNamedValue { get; private set; }
111113

114+
public int operationBatchSize { get; private set;}
115+
112116
public Extractor(ExtractorConfig exc, string dirName)
113117
{
114118
this.sourceApimName = exc.sourceApimName;
@@ -128,6 +132,7 @@ public Extractor(ExtractorConfig exc, string dirName)
128132
this.paramApiLoggerId = exc.paramApiLoggerId != null && exc.paramApiLoggerId.Equals("true");
129133
this.paramLogResourceId = exc.paramLogResourceId != null && exc.paramLogResourceId.Equals("true");
130134
this.notIncludeNamedValue = exc.notIncludeNamedValue != null && exc.notIncludeNamedValue.Equals("true");
135+
this.operationBatchSize = exc.operationBatchSize;
131136
}
132137

133138
public Extractor(ExtractorConfig exc) : this(exc, exc.fileFolder)

0 commit comments

Comments
 (0)