@@ -24,6 +24,7 @@ public ExtractCommand()
24
24
var linkedTemplatesUrlQueryString = this . Option ( "--linkedTemplatesUrlQueryString <linkedTemplatesUrlQueryString>" , "Query string appended to linked templates uris that enables retrieval from private storage" , CommandOptionType . SingleValue ) ;
25
25
var policyXMLBaseUrlName = this . Option ( "--policyXMLBaseUrl <policyXMLBaseUrl>" , "Writes policies to local XML files that require deployment to remote folder" , CommandOptionType . SingleValue ) ;
26
26
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 ) ;
27
28
28
29
this . HelpOption ( ) ;
29
30
@@ -36,11 +37,23 @@ public ExtractCommand()
36
37
if ( ! resourceGroupName . HasValue ( ) ) throw new Exception ( "Missing parameter <resourceGroup>." ) ;
37
38
if ( ! fileFolderName . HasValue ( ) ) throw new Exception ( "Missing parameter <filefolder>." ) ;
38
39
39
- // check if specify "split apis" and use "single api" at the same time
40
40
string splitAPIs = splitAPITemplates . HasValue ( ) ? splitAPITemplates . Value ( ) . ToString ( ) : null ;
41
+ string apiVersionSetName = apiVersionSetNameId . HasValue ( ) ? apiVersionSetNameId . Value ( ) . ToString ( ) : null ;
42
+
43
+ // validaion check
41
44
if ( splitAPIs != null && splitAPIs . Equals ( "true" ) && apiName . Values . Count > 0 )
42
45
{
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" ) ;
44
57
}
45
58
46
59
// isolate cli parameters
@@ -78,64 +91,11 @@ public ExtractCommand()
78
91
// create template folder with all apis and split api templates
79
92
if ( splitAPIs != null && splitAPIs . Equals ( "true" ) )
80
93
{
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 ) ;
139
99
}
140
100
else
141
101
{
@@ -154,8 +114,18 @@ public ExtractCommand()
154
114
}
155
115
} ) ;
156
116
}
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 )
158
124
{
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
+ }
159
129
// initialize entity extractor classes
160
130
APIExtractor apiExtractor = new APIExtractor ( fileWriter ) ;
161
131
APIVersionSetExtractor apiVersionSetExtractor = new APIVersionSetExtractor ( ) ;
@@ -206,7 +176,108 @@ private async Task<Boolean> GenerateTemplates(string sourceApim, string destinat
206
176
207
177
// write parameters to outputLocation
208
178
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 ;
210
281
}
211
282
}
212
283
}
0 commit comments