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

Commit 83bd714

Browse files
authored
[Extractor] Refactor code for better maintenance (#284)
* [Extractor] Refactor Code for better maintance * [Extractor] Refactor code V2
1 parent f262bad commit 83bd714

File tree

4 files changed

+165
-51
lines changed

4 files changed

+165
-51
lines changed

src/APIM_ARMTemplate/apimtemplate/Commands/Extract.cs

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ public ExtractCommand()
3030
if (extractorConfig.resourceGroup == null) throw new Exception("Missing parameter <resourceGroup>.");
3131
if (extractorConfig.fileFolder == null) throw new Exception("Missing parameter <filefolder>.");
3232

33-
string splitAPIs = extractorConfig.splitAPIs;
33+
bool splitAPIs = extractorConfig.splitAPIs != null && extractorConfig.splitAPIs.Equals("true");
3434
string apiVersionSetName = extractorConfig.apiVersionSetName;
3535
string singleApiName = extractorConfig.apiName;
3636

3737
// validaion check
38-
if (splitAPIs != null && splitAPIs.Equals("true") && singleApiName != null)
38+
if (splitAPIs && singleApiName != null)
3939
{
4040
throw new Exception("Can't use --splitAPIs and --apiName at same time");
4141
}
4242

43-
if (splitAPIs != null && splitAPIs.Equals("true") && apiVersionSetName != null)
43+
if (splitAPIs && apiVersionSetName != null)
4444
{
4545
throw new Exception("Can't use --splitAPIs and --apiVersionSetName at same time");
4646
}
@@ -50,44 +50,38 @@ public ExtractCommand()
5050
throw new Exception("Can't use --apiName and --apiVersionSetName at same time");
5151
}
5252

53-
// isolate cli parameters
54-
string resourceGroup = extractorConfig.resourceGroup;
55-
string sourceApim = extractorConfig.sourceApimName;
56-
string destinationApim = extractorConfig.destinationApimName;
57-
string dirName = extractorConfig.fileFolder;
58-
string linkedBaseUrl = extractorConfig.linkedTemplatesBaseUrl;
59-
string linkedUrlQueryString = extractorConfig.linkedTemplatesUrlQueryString;
60-
string policyXMLBaseUrl = extractorConfig.policyXMLBaseUrl;
61-
6253
Console.WriteLine("API Management Template");
6354
Console.WriteLine();
64-
Console.WriteLine("Connecting to {0} API Management Service on {1} Resource Group ...", sourceApim, resourceGroup);
55+
Console.WriteLine("Connecting to {0} API Management Service on {1} Resource Group ...", extractorConfig.sourceApimName, extractorConfig.resourceGroup);
6556
if (singleApiName != null)
6657
{
6758
Console.WriteLine("Executing extraction for {0} API ...", singleApiName);
6859
}
6960
else
7061
{
71-
Console.WriteLine("Executing full extraction ...", singleApiName);
62+
Console.WriteLine("Executing full extraction ...");
7263
}
7364

7465
// initialize file helper classes
7566
FileWriter fileWriter = new FileWriter();
7667
FileNameGenerator fileNameGenerator = new FileNameGenerator();
77-
FileNames fileNames = fileNameGenerator.GenerateFileNames(sourceApim);
68+
FileNames fileNames = fileNameGenerator.GenerateFileNames(extractorConfig.sourceApimName);
7869

7970
// create template folder with all apis and split api templates
80-
if (splitAPIs != null && splitAPIs.Equals("true"))
71+
if (splitAPIs)
8172
{
82-
await this.GenerateSplitAPITemplates(sourceApim, destinationApim, resourceGroup, policyXMLBaseUrl, dirName, linkedBaseUrl, linkedUrlQueryString, fileNameGenerator, fileNames, fileWriter);
73+
// create split api templates for all apis in the sourceApim
74+
await this.GenerateSplitAPITemplates(extractorConfig, fileNameGenerator, fileWriter, fileNames);
8375
}
8476
else if (apiVersionSetName != null)
8577
{
86-
await this.GenerateAPIVersionSetTemplates(apiVersionSetName, sourceApim, destinationApim, resourceGroup, policyXMLBaseUrl, dirName, linkedBaseUrl, linkedUrlQueryString, fileNameGenerator, fileNames, fileWriter);
78+
// create split api templates and aggregated api templates for this apiversionset
79+
await this.GenerateAPIVersionSetTemplates(extractorConfig, fileNameGenerator, fileNames, fileWriter);
8780
}
8881
else
8982
{
90-
await this.GenerateTemplates(sourceApim, destinationApim, singleApiName, null, resourceGroup, policyXMLBaseUrl, dirName, linkedBaseUrl, linkedUrlQueryString, fileNameGenerator, fileNames, fileWriter);
83+
// create single api template or create aggregated api templates for all apis within the sourceApim
84+
await this.GenerateTemplates(new Extractor(extractorConfig), fileNameGenerator, fileNames, fileWriter);
9185
}
9286
Console.WriteLine("Templates written to output location");
9387
Console.WriteLine("Press any key to exit process:");
@@ -108,9 +102,9 @@ public ExtractCommand()
108102
2. multipleApiNams is null, then generate separate folder and master template for each API
109103
3. when both singleApiName and multipleApiNams is null, then generate one master template to link all apis in the sourceapim
110104
*/
111-
private async Task GenerateTemplates(string sourceApim, string destinationApim, string singleApiName, List<string> multipleApiNams, string resourceGroup, string policyXMLBaseUrl, string dirName, string linkedBaseUrl, string linkedUrlQueryString, FileNameGenerator fileNameGenerator, FileNames fileNames, FileWriter fileWriter)
105+
private async Task GenerateTemplates(Extractor exc, FileNameGenerator fileNameGenerator, FileNames fileNames, FileWriter fileWriter)
112106
{
113-
if (singleApiName != null && multipleApiNams != null)
107+
if (exc.apiName != null && exc.multipleAPINames != null)
114108
{
115109
throw new Exception("can't specify single API and multiple APIs to extract at the same time");
116110
}
@@ -126,9 +120,20 @@ private async Task GenerateTemplates(string sourceApim, string destinationApim,
126120
ProductExtractor productExtractor = new ProductExtractor(fileWriter);
127121
MasterTemplateExtractor masterTemplateExtractor = new MasterTemplateExtractor();
128122

123+
// read parameters
124+
string sourceApim = exc.sourceApimName;
125+
string resourceGroup = exc.resourceGroup;
126+
string singleApiName = exc.apiName;
127+
string destinationApim = exc.destinationApimName;
128+
string linkedBaseUrl = exc.linkedTemplatesBaseUrl;
129+
string policyXMLBaseUrl = exc.policyXMLBaseUrl;
130+
string dirName = exc.fileFolder;
131+
List<string> multipleApiNames = exc.multipleAPINames;
132+
string linkedUrlQueryString = exc.linkedTemplatesUrlQueryString;
133+
129134
// extract templates from apim service
130135
Template globalServicePolicyTemplate = await policyExtractor.GenerateGlobalServicePolicyTemplateAsync(sourceApim, resourceGroup, policyXMLBaseUrl, dirName);
131-
Template apiTemplate = await apiExtractor.GenerateAPIsARMTemplateAsync(sourceApim, resourceGroup, singleApiName, multipleApiNams, policyXMLBaseUrl, dirName);
136+
Template apiTemplate = await apiExtractor.GenerateAPIsARMTemplateAsync(sourceApim, resourceGroup, singleApiName, multipleApiNames, policyXMLBaseUrl, dirName);
132137
List<TemplateResource> apiTemplateResources = apiTemplate.resources.ToList();
133138
Template apiVersionSetTemplate = await apiVersionSetExtractor.GenerateAPIVersionSetsARMTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, policyXMLBaseUrl);
134139
Template authorizationServerTemplate = await authorizationServerExtractor.GenerateAuthorizationServersARMTemplateAsync(sourceApim, resourceGroup, singleApiName, apiTemplateResources, policyXMLBaseUrl);
@@ -187,48 +192,49 @@ private async Task GenerateTemplates(string sourceApim, string destinationApim,
187192
}
188193

189194
// this function will generate master template for each API within this version set and an extra master template to link these apis
190-
public async Task GenerateAPIVersionSetTemplates(string apiVersionSetName, string sourceApim, string destinationApim, string resourceGroup, string policyXMLBaseUrl, string dirName, string linkedBaseUrl, string linkedUrlQueryString, FileNameGenerator fileNameGenerator, FileNames fileNames, FileWriter fileWriter)
195+
public async Task GenerateAPIVersionSetTemplates(ExtractorConfig exc, FileNameGenerator fileNameGenerator, FileNames fileNames, FileWriter fileWriter)
191196
{
192197
// get api dictionary and check api version set
193-
var apiDictionary = await this.GetAllAPIsDictionary(sourceApim, resourceGroup, fileWriter);
194-
if (!apiDictionary.ContainsKey(apiVersionSetName))
198+
var apiDictionary = await this.GetAllAPIsDictionary(exc.sourceApimName, exc.resourceGroup, fileWriter);
199+
if (!apiDictionary.ContainsKey(exc.apiVersionSetName))
195200
{
196201
throw new Exception("API Version Set with this name doesn't exist");
197202
}
198203
else
199204
{
200-
foreach (string apiName in apiDictionary[apiVersionSetName])
205+
foreach (string apiName in apiDictionary[exc.apiVersionSetName])
201206
{
202207
// generate seperate folder for each API
203-
string apiFileFolder = String.Concat(@dirName, $@"/{apiName}");
208+
string apiFileFolder = String.Concat(@exc.fileFolder, $@"/{apiName}");
204209
System.IO.Directory.CreateDirectory(apiFileFolder);
205-
Console.WriteLine(apiFileFolder + " " + apiName);
206-
await this.GenerateTemplates(sourceApim, destinationApim, null, apiDictionary[apiVersionSetName], resourceGroup, policyXMLBaseUrl, apiFileFolder, linkedBaseUrl, linkedUrlQueryString, fileNameGenerator, fileNames, fileWriter);
210+
// config instance with singleApiName
211+
Extractor excConfig = new Extractor(exc, apiName, apiFileFolder);
212+
await this.GenerateTemplates(excConfig, fileNameGenerator, fileNames, fileWriter);
207213
}
208214

209215
// create master templates for this apiVersionSet
210-
string versionSetFolder = String.Concat(@dirName, fileNames.versionSetMasterFolder);
216+
string versionSetFolder = String.Concat(@exc.fileFolder, fileNames.versionSetMasterFolder);
211217
System.IO.Directory.CreateDirectory(versionSetFolder);
212-
Console.WriteLine(versionSetFolder);
213-
await this.GenerateTemplates(sourceApim, destinationApim, null, apiDictionary[apiVersionSetName], resourceGroup, policyXMLBaseUrl, versionSetFolder, linkedBaseUrl, linkedUrlQueryString, fileNameGenerator, fileNames, fileWriter);
218+
Extractor versionConfig = new Extractor(exc, apiDictionary[exc.apiVersionSetName], versionSetFolder);
219+
await this.GenerateTemplates(versionConfig, fileNameGenerator, fileNames, fileWriter);
214220

215-
Console.WriteLine($@"Finish extracting APIVersionSet {apiVersionSetName}");
221+
Console.WriteLine($@"Finish extracting APIVersionSet {exc.apiVersionSetName}");
216222

217223
}
218224
}
219225

220226
// this function will generate split api templates / folders for each api in this sourceApim
221-
public async Task GenerateSplitAPITemplates(string sourceApim, string destinationApim, string resourceGroup, string policyXMLBaseUrl, string dirName, string linkedBaseUrl, string linkedUrlQueryString, FileNameGenerator fileNameGenerator, FileNames fileNames, FileWriter fileWriter)
227+
public async Task GenerateSplitAPITemplates(ExtractorConfig exc, FileNameGenerator fileNameGenerator, FileWriter fileWriter, FileNames fileNames)
222228
{
223229
// Generate folders based on all apiversionset
224-
var apiDictionary = await this.GetAllAPIsDictionary(sourceApim, resourceGroup, fileWriter);
230+
var apiDictionary = await this.GetAllAPIsDictionary(exc.sourceApimName, exc.resourceGroup, fileWriter);
225231

226232
// Generate templates based on each API/APIversionSet
227233
foreach (KeyValuePair<string, List<string>> versionSetEntry in apiDictionary)
228234
{
229-
string apiFileFolder = dirName;
235+
string apiFileFolder = exc.fileFolder;
230236

231-
// Check if it's APIVersionSet
237+
// if it's APIVersionSet, generate the versionsetfolder for templates
232238
if (versionSetEntry.Value.Count > 1)
233239
{
234240
// this API has VersionSet
@@ -241,20 +247,21 @@ public async Task GenerateSplitAPITemplates(string sourceApim, string destinatio
241247
// create master templates for each apiVersionSet
242248
string versionSetFolder = String.Concat(@apiFileFolder, fileNames.versionSetMasterFolder);
243249
System.IO.Directory.CreateDirectory(versionSetFolder);
244-
await this.GenerateTemplates(sourceApim, destinationApim, null, versionSetEntry.Value, resourceGroup, policyXMLBaseUrl, versionSetFolder, linkedBaseUrl, linkedUrlQueryString, fileNameGenerator, fileNames, fileWriter);
250+
Extractor masterConfig = new Extractor(exc, versionSetEntry.Value, versionSetFolder);
251+
await this.GenerateTemplates(masterConfig, fileNameGenerator, fileNames, fileWriter);
245252

246253
Console.WriteLine($@"Finish extracting APIVersionSet {versionSetEntry.Key}");
247254
}
248255

249-
// Generate templates
256+
// Generate templates for each api
250257
foreach (string apiName in versionSetEntry.Value)
251258
{
252259
// create folder for each API
253260
string tempFileFolder = String.Concat(@apiFileFolder, $@"/{apiName}");
254261
System.IO.Directory.CreateDirectory(tempFileFolder);
255-
262+
Extractor excConfig = new Extractor(exc, apiName, tempFileFolder);
256263
// generate templates for each API
257-
await this.GenerateTemplates(sourceApim, destinationApim, apiName, null, resourceGroup, policyXMLBaseUrl, tempFileFolder, linkedBaseUrl, linkedUrlQueryString, fileNameGenerator, fileNames, fileWriter);
264+
await this.GenerateTemplates(excConfig, fileNameGenerator, fileNames, fileWriter);
258265

259266
Console.WriteLine($@"Finish extracting API {apiName}");
260267
}

src/APIM_ARMTemplate/apimtemplate/Common/FileHandlers/FileReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public bool isJSON(string fileContents)
109109
object deserializedFileContents = JsonConvert.DeserializeObject<object>(fileContents);
110110
return true;
111111
}
112-
catch (Exception ex)
112+
catch (Exception)
113113
{
114114
return false;
115115
}

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Collections.Generic;
2+
13
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extract
24
{
35
public class ExtractorConfig
@@ -12,5 +14,88 @@ public class ExtractorConfig
1214
public string policyXMLBaseUrl { get; set; }
1315
public string splitAPIs { get; set; }
1416
public string apiVersionSetName { get; set; }
17+
public string includeAllRevisions { get; set; }
18+
public List<string> multipleAPINames { get; set; }
19+
}
20+
21+
public class Extractor
22+
{
23+
public string sourceApimName { get; private set; }
24+
public string destinationApimName { get; private set; }
25+
public string resourceGroup { get; private set; }
26+
public string fileFolder { get; private set; }
27+
public string apiName { get; private set; }
28+
public string linkedTemplatesBaseUrl { get; private set; }
29+
public string linkedTemplatesUrlQueryString { get; private set; }
30+
public string policyXMLBaseUrl { get; private set; }
31+
public string apiVersionSetName { get; private set; }
32+
public string includeAllRevisions { get; private set; }
33+
public List<string> multipleAPINames { get; private set; }
34+
35+
public Extractor(ExtractorConfig exc, List<string> multipleAPINames, string dirName)
36+
{
37+
this.sourceApimName = exc.sourceApimName;
38+
this.destinationApimName = exc.destinationApimName;
39+
this.resourceGroup = exc.resourceGroup;
40+
this.fileFolder = dirName;
41+
this.apiName = null;
42+
this.linkedTemplatesBaseUrl = exc.linkedTemplatesBaseUrl;
43+
this.linkedTemplatesUrlQueryString = exc.linkedTemplatesUrlQueryString;
44+
this.policyXMLBaseUrl = exc.policyXMLBaseUrl;
45+
this.apiVersionSetName = exc.apiVersionSetName;
46+
this.includeAllRevisions = exc.includeAllRevisions;
47+
if (exc.multipleAPINames != null) {
48+
this.multipleAPINames = new List<string>(exc.multipleAPINames);
49+
}
50+
}
51+
52+
public Extractor(ExtractorConfig exc, string singleApiName, string dirName)
53+
{
54+
this.sourceApimName = exc.sourceApimName;
55+
this.destinationApimName = exc.destinationApimName;
56+
this.resourceGroup = exc.resourceGroup;
57+
this.fileFolder = dirName;
58+
this.apiName = singleApiName;
59+
this.linkedTemplatesBaseUrl = exc.linkedTemplatesBaseUrl;
60+
this.linkedTemplatesUrlQueryString = exc.linkedTemplatesUrlQueryString;
61+
this.policyXMLBaseUrl = exc.policyXMLBaseUrl;
62+
this.apiVersionSetName = exc.apiVersionSetName;
63+
this.includeAllRevisions = exc.includeAllRevisions;
64+
this.multipleAPINames = null;
65+
}
66+
67+
public Extractor(ExtractorConfig exc, string dirName)
68+
{
69+
this.sourceApimName = exc.sourceApimName;
70+
this.destinationApimName = exc.destinationApimName;
71+
this.resourceGroup = exc.resourceGroup;
72+
this.fileFolder = dirName;
73+
this.apiName = exc.apiName;
74+
this.linkedTemplatesBaseUrl = exc.linkedTemplatesBaseUrl;
75+
this.linkedTemplatesUrlQueryString = exc.linkedTemplatesUrlQueryString;
76+
this.policyXMLBaseUrl = exc.policyXMLBaseUrl;
77+
this.apiVersionSetName = exc.apiVersionSetName;
78+
this.includeAllRevisions = exc.includeAllRevisions;
79+
if (exc.multipleAPINames != null) {
80+
this.multipleAPINames = new List<string>(exc.multipleAPINames);
81+
}
82+
}
83+
84+
public Extractor(ExtractorConfig exc)
85+
{
86+
this.sourceApimName = exc.sourceApimName;
87+
this.destinationApimName = exc.destinationApimName;
88+
this.resourceGroup = exc.resourceGroup;
89+
this.fileFolder = exc.fileFolder;
90+
this.apiName = exc.apiName;
91+
this.linkedTemplatesBaseUrl = exc.linkedTemplatesBaseUrl;
92+
this.linkedTemplatesUrlQueryString = exc.linkedTemplatesUrlQueryString;
93+
this.policyXMLBaseUrl = exc.policyXMLBaseUrl;
94+
this.apiVersionSetName = exc.apiVersionSetName;
95+
this.includeAllRevisions = exc.includeAllRevisions;
96+
if (exc.multipleAPINames != null) {
97+
this.multipleAPINames = new List<string>(exc.multipleAPINames);
98+
}
99+
}
15100
}
16101
}

0 commit comments

Comments
 (0)