@@ -30,17 +30,17 @@ public ExtractCommand()
30
30
if ( extractorConfig . resourceGroup == null ) throw new Exception ( "Missing parameter <resourceGroup>." ) ;
31
31
if ( extractorConfig . fileFolder == null ) throw new Exception ( "Missing parameter <filefolder>." ) ;
32
32
33
- string splitAPIs = extractorConfig . splitAPIs ;
33
+ bool splitAPIs = extractorConfig . splitAPIs != null && extractorConfig . splitAPIs . Equals ( "true" ) ;
34
34
string apiVersionSetName = extractorConfig . apiVersionSetName ;
35
35
string singleApiName = extractorConfig . apiName ;
36
36
37
37
// validaion check
38
- if ( splitAPIs != null && splitAPIs . Equals ( "true" ) && singleApiName != null )
38
+ if ( splitAPIs && singleApiName != null )
39
39
{
40
40
throw new Exception ( "Can't use --splitAPIs and --apiName at same time" ) ;
41
41
}
42
42
43
- if ( splitAPIs != null && splitAPIs . Equals ( "true" ) && apiVersionSetName != null )
43
+ if ( splitAPIs && apiVersionSetName != null )
44
44
{
45
45
throw new Exception ( "Can't use --splitAPIs and --apiVersionSetName at same time" ) ;
46
46
}
@@ -50,44 +50,38 @@ public ExtractCommand()
50
50
throw new Exception ( "Can't use --apiName and --apiVersionSetName at same time" ) ;
51
51
}
52
52
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
-
62
53
Console . WriteLine ( "API Management Template" ) ;
63
54
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 ) ;
65
56
if ( singleApiName != null )
66
57
{
67
58
Console . WriteLine ( "Executing extraction for {0} API ..." , singleApiName ) ;
68
59
}
69
60
else
70
61
{
71
- Console . WriteLine ( "Executing full extraction ..." , singleApiName ) ;
62
+ Console . WriteLine ( "Executing full extraction ..." ) ;
72
63
}
73
64
74
65
// initialize file helper classes
75
66
FileWriter fileWriter = new FileWriter ( ) ;
76
67
FileNameGenerator fileNameGenerator = new FileNameGenerator ( ) ;
77
- FileNames fileNames = fileNameGenerator . GenerateFileNames ( sourceApim ) ;
68
+ FileNames fileNames = fileNameGenerator . GenerateFileNames ( extractorConfig . sourceApimName ) ;
78
69
79
70
// create template folder with all apis and split api templates
80
- if ( splitAPIs != null && splitAPIs . Equals ( "true" ) )
71
+ if ( splitAPIs )
81
72
{
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 ) ;
83
75
}
84
76
else if ( apiVersionSetName != null )
85
77
{
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 ) ;
87
80
}
88
81
else
89
82
{
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 ) ;
91
85
}
92
86
Console . WriteLine ( "Templates written to output location" ) ;
93
87
Console . WriteLine ( "Press any key to exit process:" ) ;
@@ -108,9 +102,9 @@ public ExtractCommand()
108
102
2. multipleApiNams is null, then generate separate folder and master template for each API
109
103
3. when both singleApiName and multipleApiNams is null, then generate one master template to link all apis in the sourceapim
110
104
*/
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 )
112
106
{
113
- if ( singleApiName != null && multipleApiNams != null )
107
+ if ( exc . apiName != null && exc . multipleAPINames != null )
114
108
{
115
109
throw new Exception ( "can't specify single API and multiple APIs to extract at the same time" ) ;
116
110
}
@@ -126,9 +120,20 @@ private async Task GenerateTemplates(string sourceApim, string destinationApim,
126
120
ProductExtractor productExtractor = new ProductExtractor ( fileWriter ) ;
127
121
MasterTemplateExtractor masterTemplateExtractor = new MasterTemplateExtractor ( ) ;
128
122
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
+
129
134
// extract templates from apim service
130
135
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 ) ;
132
137
List < TemplateResource > apiTemplateResources = apiTemplate . resources . ToList ( ) ;
133
138
Template apiVersionSetTemplate = await apiVersionSetExtractor . GenerateAPIVersionSetsARMTemplateAsync ( sourceApim , resourceGroup , singleApiName , apiTemplateResources , policyXMLBaseUrl ) ;
134
139
Template authorizationServerTemplate = await authorizationServerExtractor . GenerateAuthorizationServersARMTemplateAsync ( sourceApim , resourceGroup , singleApiName , apiTemplateResources , policyXMLBaseUrl ) ;
@@ -187,48 +192,49 @@ private async Task GenerateTemplates(string sourceApim, string destinationApim,
187
192
}
188
193
189
194
// 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 )
191
196
{
192
197
// 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 ) )
195
200
{
196
201
throw new Exception ( "API Version Set with this name doesn't exist" ) ;
197
202
}
198
203
else
199
204
{
200
- foreach ( string apiName in apiDictionary [ apiVersionSetName ] )
205
+ foreach ( string apiName in apiDictionary [ exc . apiVersionSetName ] )
201
206
{
202
207
// generate seperate folder for each API
203
- string apiFileFolder = String . Concat ( @dirName , $@ "/{ apiName } ") ;
208
+ string apiFileFolder = String . Concat ( @exc . fileFolder , $@ "/{ apiName } ") ;
204
209
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 ) ;
207
213
}
208
214
209
215
// create master templates for this apiVersionSet
210
- string versionSetFolder = String . Concat ( @dirName , fileNames . versionSetMasterFolder ) ;
216
+ string versionSetFolder = String . Concat ( @exc . fileFolder , fileNames . versionSetMasterFolder ) ;
211
217
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 ) ;
214
220
215
- Console . WriteLine ( $@ "Finish extracting APIVersionSet { apiVersionSetName } ") ;
221
+ Console . WriteLine ( $@ "Finish extracting APIVersionSet { exc . apiVersionSetName } ") ;
216
222
217
223
}
218
224
}
219
225
220
226
// 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 )
222
228
{
223
229
// 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 ) ;
225
231
226
232
// Generate templates based on each API/APIversionSet
227
233
foreach ( KeyValuePair < string , List < string > > versionSetEntry in apiDictionary )
228
234
{
229
- string apiFileFolder = dirName ;
235
+ string apiFileFolder = exc . fileFolder ;
230
236
231
- // Check if it's APIVersionSet
237
+ // if it's APIVersionSet, generate the versionsetfolder for templates
232
238
if ( versionSetEntry . Value . Count > 1 )
233
239
{
234
240
// this API has VersionSet
@@ -241,20 +247,21 @@ public async Task GenerateSplitAPITemplates(string sourceApim, string destinatio
241
247
// create master templates for each apiVersionSet
242
248
string versionSetFolder = String . Concat ( @apiFileFolder , fileNames . versionSetMasterFolder ) ;
243
249
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 ) ;
245
252
246
253
Console . WriteLine ( $@ "Finish extracting APIVersionSet { versionSetEntry . Key } ") ;
247
254
}
248
255
249
- // Generate templates
256
+ // Generate templates for each api
250
257
foreach ( string apiName in versionSetEntry . Value )
251
258
{
252
259
// create folder for each API
253
260
string tempFileFolder = String . Concat ( @apiFileFolder , $@ "/{ apiName } ") ;
254
261
System . IO . Directory . CreateDirectory ( tempFileFolder ) ;
255
-
262
+ Extractor excConfig = new Extractor ( exc , apiName , tempFileFolder ) ;
256
263
// 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 ) ;
258
265
259
266
Console . WriteLine ( $@ "Finish extracting API { apiName } ") ;
260
267
}
0 commit comments