@@ -14,8 +14,8 @@ public class ExtractCommand : CommandLineApplication
14
14
{
15
15
public ExtractCommand ( )
16
16
{
17
- this . Name = Constants . ExtractName ;
18
- this . Description = Constants . ExtractDescription ;
17
+ this . Name = GlobalConstants . ExtractName ;
18
+ this . Description = GlobalConstants . ExtractDescription ;
19
19
20
20
var apiManagementName = this . Option ( "--name <apimname>" , "API Management name" , CommandOptionType . SingleValue ) ;
21
21
var resourceGroupName = this . Option ( "--resourceGroup <resourceGroup>" , "Resource Group name" , CommandOptionType . SingleValue ) ;
@@ -67,12 +67,6 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
67
67
68
68
Console . WriteLine ( "{0} API's found ..." , ( ( JContainer ) oApi [ "value" ] ) . Count . ToString ( ) ) ;
69
69
70
- // store api and operation policies as well as diagnostics for the time being in order to extract loggers correctly in case of a single api
71
- List < PolicyTemplateResource > policyResources = new List < PolicyTemplateResource > ( ) ;
72
- List < DiagnosticTemplateResource > diagnosticResources = new List < DiagnosticTemplateResource > ( ) ;
73
- // store product names for the time being in order to extract products correctly in case of a single api
74
- List < string > productNames = new List < string > ( ) ;
75
-
76
70
List < TemplateResource > templateResources = new List < TemplateResource > ( ) ;
77
71
78
72
for ( int i = 0 ; i < ( ( JContainer ) oApi [ "value" ] ) . Count ; i ++ )
@@ -176,8 +170,6 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
176
170
operationPolicyResource . dependsOn = new string [ ] { $ "[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('ApimServiceName'), '{ oApiName } ', '{ operationResourceName } ')]" } ;
177
171
178
172
templateResources . Add ( operationPolicyResource ) ;
179
- // add the operation policy resource to the list used to extract loggers later on
180
- policyResources . Add ( operationPolicyResource ) ;
181
173
}
182
174
catch ( Exception )
183
175
{
@@ -199,8 +191,6 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
199
191
apiPoliciesResource . dependsOn = new string [ ] { $ "[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{ apiName } ')]" } ;
200
192
201
193
templateResources . Add ( apiPoliciesResource ) ;
202
- // add the api policy resource to the list used to extract loggers later on
203
- policyResources . Add ( apiPoliciesResource ) ;
204
194
}
205
195
catch ( Exception )
206
196
{
@@ -219,16 +209,14 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
219
209
{
220
210
string apiProductName = ( ( JValue ) item [ "name" ] ) . Value . ToString ( ) ;
221
211
Console . WriteLine ( $ " -- { apiProductName } Product found to { oApiName } API") ;
222
- ApiProductsTemplateResource apiProductsResource = JsonConvert . DeserializeObject < ApiProductsTemplateResource > ( apiProducts ) ;
223
- apiProductsResource . type = "Microsoft.ApiManagement/service/products/apis" ;
224
- apiProductsResource . name = $ "[concat(parameters('ApimServiceName'), '/{ apiProductName } /{ oApiName } ')]";
225
- apiProductsResource . apiVersion = "2018-06-01-preview" ;
226
- apiProductsResource . scale = null ;
227
- apiProductsResource . dependsOn = new string [ ] { $ "[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{ oApiName } ')]" } ;
212
+ ProductAPITemplateResource productAPIResource = JsonConvert . DeserializeObject < ProductAPITemplateResource > ( apiProducts ) ;
213
+ productAPIResource . type = ResourceTypeConstants . ProductAPI ;
214
+ productAPIResource . name = $ "[concat(parameters('ApimServiceName'), '/{ apiProductName } /{ oApiName } ')]";
215
+ productAPIResource . apiVersion = "2018-06-01-preview" ;
216
+ productAPIResource . scale = null ;
217
+ productAPIResource . dependsOn = new string [ ] { $ "[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{ oApiName } ')]" } ;
228
218
229
- templateResources . Add ( apiProductsResource ) ;
230
- // add the product name to the list used to extract products later on
231
- productNames . Add ( apiProductName ) ;
219
+ templateResources . Add ( productAPIResource ) ;
232
220
233
221
}
234
222
}
@@ -251,7 +239,7 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
251
239
252
240
DiagnosticTemplateResource diagnosticResource = diagnostic . ToObject < DiagnosticTemplateResource > ( ) ;
253
241
diagnosticResource . name = $ "[concat(parameters('ApimServiceName'), '/{ oApiName } /{ diagnosticName } ')]";
254
- diagnosticResource . type = "Microsoft.ApiManagement/service/apis/diagnostics" ;
242
+ diagnosticResource . type = ResourceTypeConstants . APIDiagnostic ;
255
243
diagnosticResource . apiVersion = "2018-06-01-preview" ;
256
244
diagnosticResource . scale = null ;
257
245
diagnosticResource . dependsOn = new string [ ] { $ "[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{ oApiName } ')]" } ;
@@ -263,8 +251,6 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
263
251
}
264
252
265
253
templateResources . Add ( diagnosticResource ) ;
266
- // add the diagnostic resource to the list used to extract loggers later on
267
- diagnosticResources . Add ( diagnosticResource ) ;
268
254
269
255
}
270
256
@@ -280,9 +266,9 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
280
266
}
281
267
282
268
// extract resources that do not fall under api. Pass in the single api name and associated resources for the single api case
283
- GenerateProductsARMTemplate ( apimname , resourceGroup , fileFolder , singleApiName , productNames ) ;
284
- GenerateNamedValuesTemplate ( resourceGroup , apimname , fileFolder , singleApiName , policyResources , diagnosticResources ) ;
285
- GenerateLoggerTemplate ( resourceGroup , apimname , fileFolder , singleApiName , policyResources , diagnosticResources ) ;
269
+ GenerateProductsARMTemplate ( apimname , resourceGroup , fileFolder , singleApiName , templateResources ) ;
270
+ GenerateNamedValuesTemplate ( resourceGroup , apimname , fileFolder , singleApiName , templateResources ) ;
271
+ GenerateLoggerTemplate ( resourceGroup , apimname , fileFolder , singleApiName , templateResources ) ;
286
272
287
273
if ( singleApiName == null )
288
274
{
@@ -334,11 +320,14 @@ private void GenerateVersionSetARMTemplate(string apimname, string resourceGroup
334
320
fileWriter . WriteJSONToFile ( armTemplate , filePath ) ;
335
321
}
336
322
337
- private void GenerateProductsARMTemplate ( string apimname , string resourceGroup , string fileFolder , string singleApiName , List < string > productNames )
323
+ private void GenerateProductsARMTemplate ( string apimname , string resourceGroup , string fileFolder , string singleApiName , List < TemplateResource > armTemplateResources )
338
324
{
339
325
APIExtractor apiExtractor = new APIExtractor ( ) ;
340
326
Template armTemplate = GenerateEmptyTemplateWithParameters ( ) ;
341
327
328
+ // isolate product api associations in the case of a single api extraction
329
+ var productAPIResources = armTemplateResources . Where ( resource => resource . type == ResourceTypeConstants . ProductAPI ) ;
330
+
342
331
List < TemplateResource > templateResources = new List < TemplateResource > ( ) ;
343
332
344
333
string products = apiExtractor . GetProducts ( apimname , resourceGroup ) . Result ;
@@ -362,8 +351,8 @@ private void GenerateProductsARMTemplate(string apimname, string resourceGroup,
362
351
productsDetailsResource . name = $ "[concat(parameters('ApimServiceName'), '/{ productName } ')]";
363
352
productsDetailsResource . apiVersion = "2018-06-01-preview" ;
364
353
365
- // only extract the product if this is a full extraction, or in the case of a single api, if it is found in productNames(all prodct names associated with the specific api)
366
- if ( singleApiName == null || productNames . SingleOrDefault ( p => p == productName ) != null )
354
+ // only extract the product if this is a full extraction, or in the case of a single api, if it is found in products associated with the api
355
+ if ( singleApiName == null || productAPIResources . SingleOrDefault ( p => p . name . Contains ( productName ) ) != null )
367
356
{
368
357
templateResources . Add ( productsDetailsResource ) ;
369
358
}
@@ -407,13 +396,17 @@ private List<TemplateResource> GenerateSchemasARMTemplate(string apimServiceName
407
396
return templateResources ;
408
397
}
409
398
410
- private async void GenerateLoggerTemplate ( string resourceGroup , string apimname , string fileFolder , string singleApiName , List < PolicyTemplateResource > policyResources , List < DiagnosticTemplateResource > diagnosticResources )
399
+ private async void GenerateLoggerTemplate ( string resourceGroup , string apimname , string fileFolder , string singleApiName , List < TemplateResource > armTemplateResources )
411
400
{
412
401
Console . WriteLine ( "------------------------------------------" ) ;
413
402
Console . WriteLine ( "Getting loggers from service" ) ;
414
403
LoggerExtractor loggerExtractor = new LoggerExtractor ( ) ;
415
404
Template armTemplate = GenerateEmptyTemplateWithParameters ( ) ;
416
405
406
+ // isolate product api associations in the case of a single api extraction
407
+ var diagnosticResources = armTemplateResources . Where ( resource => resource . type == ResourceTypeConstants . APIDiagnostic ) ;
408
+ var policyResources = armTemplateResources . Where ( resource => ( resource . type == ResourceTypeConstants . APIPolicy || resource . type == ResourceTypeConstants . APIOperationPolicy ) ) ;
409
+
417
410
List < TemplateResource > templateResources = new List < TemplateResource > ( ) ;
418
411
419
412
string loggers = loggerExtractor . GetLoggers ( apimname , resourceGroup ) . Result ;
@@ -425,16 +418,17 @@ private async void GenerateLoggerTemplate(string resourceGroup, string apimname,
425
418
string fullLoggerResource = await loggerExtractor . GetLogger ( apimname , resourceGroup , loggerName ) ;
426
419
LoggerTemplateResource loggerResource = JsonConvert . DeserializeObject < LoggerTemplateResource > ( fullLoggerResource ) ;
427
420
loggerResource . name = $ "[concat(parameters('ApimServiceName'), '/{ loggerName } ')]";
428
- loggerResource . type = "Microsoft.ApiManagement/service/loggers" ;
421
+ loggerResource . type = ResourceTypeConstants . Logger ;
429
422
loggerResource . apiVersion = "2018-06-01-preview" ;
430
423
loggerResource . scale = null ;
431
424
432
- if ( singleApiName == null )
425
+ if ( singleApiName == null )
433
426
{
434
427
// if the user is extracting all apis, extract all the loggers
435
428
Console . WriteLine ( "'{0}' Logger found" , loggerName ) ;
436
429
templateResources . Add ( loggerResource ) ;
437
- } else
430
+ }
431
+ else
438
432
{
439
433
// if the user is extracting a single api, extract the loggers referenced by its diagnostics and api policies
440
434
bool isReferencedInPolicy = false ;
@@ -453,7 +447,7 @@ private async void GenerateLoggerTemplate(string resourceGroup, string apimname,
453
447
isReferencedInPolicy = true ;
454
448
}
455
449
}
456
- if ( isReferencedInPolicy == true || isReferencedInDiagnostic == true )
450
+ if ( isReferencedInPolicy == true || isReferencedInDiagnostic == true )
457
451
{
458
452
// logger was used in policy or diagnostic, extract it
459
453
Console . WriteLine ( "'{0}' Logger found" , loggerName ) ;
@@ -467,7 +461,7 @@ private async void GenerateLoggerTemplate(string resourceGroup, string apimname,
467
461
fileWriter . WriteJSONToFile ( armTemplate , @fileFolder + Path . DirectorySeparatorChar + apimname + "-loggers.json" ) ;
468
462
}
469
463
470
- private async void GenerateNamedValuesTemplate ( string resourceGroup , string apimname , string fileFolder , string singleApiName , List < PolicyTemplateResource > policyResources , List < DiagnosticTemplateResource > diagnosticResources )
464
+ private async void GenerateNamedValuesTemplate ( string resourceGroup , string apimname , string fileFolder , string singleApiName , List < TemplateResource > armTemplateResources )
471
465
{
472
466
Console . WriteLine ( "------------------------------------------" ) ;
473
467
Console . WriteLine ( "Getting named values from service" ) ;
@@ -482,16 +476,26 @@ private async void GenerateNamedValuesTemplate(string resourceGroup, string apim
482
476
foreach ( var extractedProperty in oProperties [ "value" ] )
483
477
{
484
478
string propertyName = ( ( JValue ) extractedProperty [ "name" ] ) . Value . ToString ( ) ;
485
- Console . WriteLine ( "'{0}' Named value found" , propertyName ) ;
486
479
487
480
string fullLoggerResource = await propertyExtractor . GetProperty ( apimname , resourceGroup , propertyName ) ;
488
481
PropertyTemplateResource propertyTemplateResource = JsonConvert . DeserializeObject < PropertyTemplateResource > ( fullLoggerResource ) ;
489
482
propertyTemplateResource . name = $ "[concat(parameters('ApimServiceName'), '/{ propertyName } ')]";
490
- propertyTemplateResource . type = "Microsoft.ApiManagement/service/properties" ;
483
+ propertyTemplateResource . type = ResourceTypeConstants . Property ;
491
484
propertyTemplateResource . apiVersion = "2018-06-01-preview" ;
492
485
propertyTemplateResource . scale = null ;
493
486
494
- templateResources . Add ( propertyTemplateResource ) ;
487
+ if ( singleApiName == null )
488
+ {
489
+ // if the user is executing a full extraction, extract all the loggers
490
+ Console . WriteLine ( "'{0}' Named value found" , propertyName ) ;
491
+ templateResources . Add ( propertyTemplateResource ) ;
492
+ }
493
+ else
494
+ {
495
+ // if the user is executing a single api, extract all the named values used in the template resources
496
+ Console . WriteLine ( "'{0}' Named value found" , propertyName ) ;
497
+ templateResources . Add ( propertyTemplateResource ) ;
498
+ } ;
495
499
}
496
500
497
501
armTemplate . resources = templateResources . ToArray ( ) ;
0 commit comments