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

Commit 11d0495

Browse files
authored
[Extractor] Extract all apis within an APIVersionSet (#274)
* temp * [Extractor] Extract apis within the APIVersionSet * Remove console code
1 parent 791c8bf commit 11d0495

File tree

2 files changed

+134
-63
lines changed

2 files changed

+134
-63
lines changed

src/APIM_ARMTemplate/apimtemplate/Commands/Extract.cs

Lines changed: 133 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public ExtractCommand()
2424
var linkedTemplatesUrlQueryString = this.Option("--linkedTemplatesUrlQueryString <linkedTemplatesUrlQueryString>", "Query string appended to linked templates uris that enables retrieval from private storage", CommandOptionType.SingleValue);
2525
var policyXMLBaseUrlName = this.Option("--policyXMLBaseUrl <policyXMLBaseUrl>", "Writes policies to local XML files that require deployment to remote folder", CommandOptionType.SingleValue);
2626
var splitAPITemplates = this.Option("--splitAPIs <splitAPIs>", "Split APIs into multiple templates", CommandOptionType.SingleValue);
27+
var apiVersionSetNameId = this.Option("--apiVersionSetName <apiVersionSetName>", "Name of the apiVersionSet you want to extract", CommandOptionType.SingleValue);
2728

2829
this.HelpOption();
2930

@@ -36,11 +37,23 @@ public ExtractCommand()
3637
if (!resourceGroupName.HasValue()) throw new Exception("Missing parameter <resourceGroup>.");
3738
if (!fileFolderName.HasValue()) throw new Exception("Missing parameter <filefolder>.");
3839

39-
// check if specify "split apis" and use "single api" at the same time
4040
string splitAPIs = splitAPITemplates.HasValue() ? splitAPITemplates.Value().ToString() : null;
41+
string apiVersionSetName = apiVersionSetNameId.HasValue() ? apiVersionSetNameId.Value().ToString() : null;
42+
43+
// validaion check
4144
if (splitAPIs != null && splitAPIs.Equals("true") && apiName.Values.Count > 0)
4245
{
43-
throw new Exception("Can't use splitAPIs when you extract single API");
46+
throw new Exception("Can't use --splitAPIs and --apiName at same time");
47+
}
48+
49+
if (splitAPIs != null && splitAPIs.Equals("true") && apiVersionSetName != null)
50+
{
51+
throw new Exception("Can't use --splitAPIs and --apiVersionSetName at same time");
52+
}
53+
54+
if (apiName.Values.Count > 0 && apiVersionSetName != null)
55+
{
56+
throw new Exception("Can't use --apiName and --apiVersionSetName at same time");
4457
}
4558

4659
// isolate cli parameters
@@ -78,64 +91,11 @@ public ExtractCommand()
7891
// create template folder with all apis and split api templates
7992
if (splitAPIs != null && splitAPIs.Equals("true"))
8093
{
81-
APIExtractor apiExtractor = new APIExtractor(fileWriter);
82-
// pull all apis from service
83-
string apis = await apiExtractor.GetAPIsAsync(sourceApim, resourceGroup);
84-
JObject oApi = JObject.Parse(apis);
85-
86-
// Generate folders based on all apiversionset
87-
var apiDictionary = new Dictionary<string, List<string>>();
88-
for (int i = 0; i < ((JContainer)oApi["value"]).Count; i++)
89-
{
90-
string apiDisplayName = ((JValue)oApi["value"][i]["properties"]["displayName"]).Value.ToString();
91-
if (!apiDictionary.ContainsKey(apiDisplayName))
92-
{
93-
List<string> apiVersionSet = new List<string>();
94-
apiVersionSet.Add(((JValue)oApi["value"][i]["name"]).Value.ToString());
95-
apiDictionary[apiDisplayName] = apiVersionSet;
96-
}
97-
else
98-
{
99-
apiDictionary[apiDisplayName].Add(((JValue)oApi["value"][i]["name"]).Value.ToString());
100-
}
101-
}
102-
103-
// Generate templates based on each API/APIversionSet
104-
foreach (KeyValuePair<string, List<string>> versionSetEntry in apiDictionary)
105-
{
106-
string apiFileFolder = dirName;
107-
108-
// Check if it's APIVersionSet
109-
if (versionSetEntry.Value.Count > 1)
110-
{
111-
// this API has VersionSet
112-
string apiDisplayName = versionSetEntry.Key;
113-
114-
// create apiVersionSet folder
115-
apiFileFolder = String.Concat(@apiFileFolder, $@"/{apiDisplayName}");
116-
System.IO.Directory.CreateDirectory(apiFileFolder);
117-
118-
// create master templates for each apiVersionSet
119-
string versionSetFolder = String.Concat(@apiFileFolder, fileNames.versionSetMasterFolder);
120-
System.IO.Directory.CreateDirectory(versionSetFolder);
121-
await this.GenerateTemplates(sourceApim, destinationApim, null, versionSetEntry.Value, resourceGroup, policyXMLBaseUrl, versionSetFolder, linkedBaseUrl, linkedUrlQueryString, fileNameGenerator, fileNames, fileWriter);
122-
123-
Console.WriteLine($@"Finish extracting APIVersionSet {versionSetEntry.Key}");
124-
}
125-
126-
// Generate templates
127-
foreach (string apiName in versionSetEntry.Value)
128-
{
129-
// create folder for each API
130-
string tempFileFolder = String.Concat(@apiFileFolder, $@"/{apiName}");
131-
System.IO.Directory.CreateDirectory(tempFileFolder);
132-
133-
// generate templates for each API
134-
await this.GenerateTemplates(sourceApim, destinationApim, apiName, null, resourceGroup, policyXMLBaseUrl, tempFileFolder, linkedBaseUrl, linkedUrlQueryString, fileNameGenerator, fileNames, fileWriter);
135-
136-
Console.WriteLine($@"Finish extracting API {apiName}");
137-
}
138-
}
94+
await this.GenerateSplitAPITemplates(sourceApim, destinationApim, resourceGroup, policyXMLBaseUrl, dirName, linkedBaseUrl, linkedUrlQueryString, fileNameGenerator, fileNames, fileWriter);
95+
}
96+
else if (apiVersionSetName != null)
97+
{
98+
await this.GenerateAPIVersionSetTemplates(apiVersionSetName, sourceApim, destinationApim, resourceGroup, policyXMLBaseUrl, dirName, linkedBaseUrl, linkedUrlQueryString, fileNameGenerator, fileNames, fileWriter);
13999
}
140100
else
141101
{
@@ -154,8 +114,18 @@ public ExtractCommand()
154114
}
155115
});
156116
}
157-
private async Task<Boolean> 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)
117+
118+
/* three condistions to use this function:
119+
1. singleApiName is null, then generate one master template for the multipleAPIs in multipleApiNams
120+
2. multipleApiNams is null, then generate separate folder and master template for each API
121+
3. when both singleApiName and multipleApiNams is null, then generate one master template to link all apis in the sourceapim
122+
*/
123+
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)
158124
{
125+
if (singleApiName != null && multipleApiNams != null)
126+
{
127+
throw new Exception("can't specify single API and multiple APIs to extract at the same time");
128+
}
159129
// initialize entity extractor classes
160130
APIExtractor apiExtractor = new APIExtractor(fileWriter);
161131
APIVersionSetExtractor apiVersionSetExtractor = new APIVersionSetExtractor();
@@ -206,7 +176,108 @@ private async Task<Boolean> GenerateTemplates(string sourceApim, string destinat
206176

207177
// write parameters to outputLocation
208178
fileWriter.WriteJSONToFile(templateParameters, String.Concat(dirName, fileNames.parameters));
209-
return true;
179+
}
180+
181+
// this function will generate master template for each API within this version set and an extra master template to link these apis
182+
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)
183+
{
184+
// get api dictionary and check api version set
185+
var apiDictionary = await this.GetAllAPIsDictionary(sourceApim, resourceGroup, fileWriter);
186+
if (!apiDictionary.ContainsKey(apiVersionSetName))
187+
{
188+
throw new Exception("API Version Set with this name doesn't exist");
189+
}
190+
else
191+
{
192+
foreach (string apiName in apiDictionary[apiVersionSetName])
193+
{
194+
// generate seperate folder for each API
195+
string apiFileFolder = String.Concat(@dirName, $@"/{apiName}");
196+
System.IO.Directory.CreateDirectory(apiFileFolder);
197+
Console.WriteLine(apiFileFolder + " " + apiName);
198+
await this.GenerateTemplates(sourceApim, destinationApim, null, apiDictionary[apiVersionSetName], resourceGroup, policyXMLBaseUrl, apiFileFolder, linkedBaseUrl, linkedUrlQueryString, fileNameGenerator, fileNames, fileWriter);
199+
}
200+
201+
// create master templates for this apiVersionSet
202+
string versionSetFolder = String.Concat(@dirName, fileNames.versionSetMasterFolder);
203+
System.IO.Directory.CreateDirectory(versionSetFolder);
204+
Console.WriteLine(versionSetFolder);
205+
await this.GenerateTemplates(sourceApim, destinationApim, null, apiDictionary[apiVersionSetName], resourceGroup, policyXMLBaseUrl, versionSetFolder, linkedBaseUrl, linkedUrlQueryString, fileNameGenerator, fileNames, fileWriter);
206+
207+
Console.WriteLine($@"Finish extracting APIVersionSet {apiVersionSetName}");
208+
209+
}
210+
}
211+
212+
// this function will generate split api templates / folders for each api in this sourceApim
213+
public async Task GenerateSplitAPITemplates(string sourceApim, string destinationApim, string resourceGroup, string policyXMLBaseUrl, string dirName, string linkedBaseUrl, string linkedUrlQueryString, FileNameGenerator fileNameGenerator, FileNames fileNames, FileWriter fileWriter)
214+
{
215+
// Generate folders based on all apiversionset
216+
var apiDictionary = await this.GetAllAPIsDictionary(sourceApim, resourceGroup, fileWriter);
217+
218+
// Generate templates based on each API/APIversionSet
219+
foreach (KeyValuePair<string, List<string>> versionSetEntry in apiDictionary)
220+
{
221+
string apiFileFolder = dirName;
222+
223+
// Check if it's APIVersionSet
224+
if (versionSetEntry.Value.Count > 1)
225+
{
226+
// this API has VersionSet
227+
string apiDisplayName = versionSetEntry.Key;
228+
229+
// create apiVersionSet folder
230+
apiFileFolder = String.Concat(@apiFileFolder, $@"/{apiDisplayName}");
231+
System.IO.Directory.CreateDirectory(apiFileFolder);
232+
233+
// create master templates for each apiVersionSet
234+
string versionSetFolder = String.Concat(@apiFileFolder, fileNames.versionSetMasterFolder);
235+
System.IO.Directory.CreateDirectory(versionSetFolder);
236+
await this.GenerateTemplates(sourceApim, destinationApim, null, versionSetEntry.Value, resourceGroup, policyXMLBaseUrl, versionSetFolder, linkedBaseUrl, linkedUrlQueryString, fileNameGenerator, fileNames, fileWriter);
237+
238+
Console.WriteLine($@"Finish extracting APIVersionSet {versionSetEntry.Key}");
239+
}
240+
241+
// Generate templates
242+
foreach (string apiName in versionSetEntry.Value)
243+
{
244+
// create folder for each API
245+
string tempFileFolder = String.Concat(@apiFileFolder, $@"/{apiName}");
246+
System.IO.Directory.CreateDirectory(tempFileFolder);
247+
248+
// generate templates for each API
249+
await this.GenerateTemplates(sourceApim, destinationApim, apiName, null, resourceGroup, policyXMLBaseUrl, tempFileFolder, linkedBaseUrl, linkedUrlQueryString, fileNameGenerator, fileNames, fileWriter);
250+
251+
Console.WriteLine($@"Finish extracting API {apiName}");
252+
}
253+
}
254+
}
255+
256+
// this function will generate an api dictionary with apiName/versionsetName (if exist one) as key, list of apiNames as value
257+
public async Task<Dictionary<string, List<string>>> GetAllAPIsDictionary(string sourceApim, string resourceGroup, FileWriter fileWriter)
258+
{
259+
APIExtractor apiExtractor = new APIExtractor(fileWriter);
260+
// pull all apis from service
261+
string apis = await apiExtractor.GetAPIsAsync(sourceApim, resourceGroup);
262+
JObject oApi = JObject.Parse(apis);
263+
264+
// Generate folders based on all apiversionset
265+
var apiDictionary = new Dictionary<string, List<string>>();
266+
for (int i = 0; i < ((JContainer)oApi["value"]).Count; i++)
267+
{
268+
string apiDisplayName = ((JValue)oApi["value"][i]["properties"]["displayName"]).Value.ToString();
269+
if (!apiDictionary.ContainsKey(apiDisplayName))
270+
{
271+
List<string> apiVersionSet = new List<string>();
272+
apiVersionSet.Add(((JValue)oApi["value"][i]["name"]).Value.ToString());
273+
apiDictionary[apiDisplayName] = apiVersionSet;
274+
}
275+
else
276+
{
277+
apiDictionary[apiDisplayName].Add(((JValue)oApi["value"][i]["name"]).Value.ToString());
278+
}
279+
}
280+
return apiDictionary;
210281
}
211282
}
212283
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async Task<string> GetAPIVersionSetDetailsAsync(string ApiManagementName,
2626

2727
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apiVersionSets/{4}?api-version={5}",
2828
baseUrl, azSubId, ResourceGroupName, ApiManagementName, APIVersionSetName, GlobalConstants.APIVersion);
29-
29+
3030
return await CallApiManagementAsync(azToken, requestUrl);
3131
}
3232

0 commit comments

Comments
 (0)