@@ -47,6 +47,7 @@ public ExtractCommand()
47
47
GenerateARMTemplate ( apimname , resourceGroup , fileFolder , singleApiName ) ;
48
48
49
49
Console . WriteLine ( "Templates written to output location" ) ;
50
+ Console . WriteLine ( "Press any key to exit process:" ) ;
50
51
#if DEBUG
51
52
Console . ReadKey ( ) ;
52
53
#endif
@@ -79,7 +80,7 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
79
80
string apiDetails = apiExtractor . GetAPIDetails ( apimname , resourceGroup , apiName ) . Result ;
80
81
81
82
Console . WriteLine ( "------------------------------------------" ) ;
82
- Console . WriteLine ( "Geting operations from {0} API:" , apiName ) ;
83
+ Console . WriteLine ( "Getting operations from {0} API:" , apiName ) ;
83
84
84
85
JObject oApiDetails = JObject . Parse ( apiDetails ) ;
85
86
APITemplateResource apiResource = JsonConvert . DeserializeObject < APITemplateResource > ( apiDetails ) ;
@@ -108,6 +109,11 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
108
109
109
110
templateResources . Add ( apiResource ) ;
110
111
112
+ #region Schemas
113
+ List < TemplateResource > schemaResources = GenerateSchemasARMTemplate ( apimname , apiName , resourceGroup , fileFolder ) ;
114
+ templateResources . AddRange ( schemaResources ) ;
115
+ #endregion
116
+
111
117
#region Operations
112
118
113
119
string operations = apiExtractor . GetAPIOperations ( apimname , resourceGroup , apiName ) . Result ;
@@ -125,7 +131,37 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
125
131
operationResource . name = $ "[concat(parameters('ApimServiceName'), '/{ oApiName } /{ operationResourceName } ')]";
126
132
operationResource . apiVersion = "2018-06-01-preview" ;
127
133
operationResource . scale = null ;
128
- operationResource . dependsOn = new string [ ] { $ "[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{ oApiName } ')]" } ;
134
+
135
+ // depend on api and schemas if necessary
136
+ List < string > operationDependsOn = new List < string > ( ) { $ "[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{ oApiName } ')]" } ;
137
+ foreach ( OperationTemplateRepresentation operationTemplateRepresentation in operationResource . properties . request . representations )
138
+ {
139
+ if ( operationTemplateRepresentation . schemaId != null )
140
+ {
141
+ string dependsOn = $ "[resourceId('Microsoft.ApiManagement/service/apis/schemas', parameters('ApimServiceName'), '{ oApiName } ', '{ operationTemplateRepresentation . schemaId } ')]";
142
+ // add value to list if schema has not already been added
143
+ if ( ! operationDependsOn . Exists ( o => o == dependsOn ) )
144
+ {
145
+ operationDependsOn . Add ( dependsOn ) ;
146
+ }
147
+ }
148
+ }
149
+ foreach ( OperationsTemplateResponse operationTemplateResponse in operationResource . properties . responses )
150
+ {
151
+ foreach ( OperationTemplateRepresentation operationTemplateRepresentation in operationTemplateResponse . representations )
152
+ {
153
+ if ( operationTemplateRepresentation . schemaId != null )
154
+ {
155
+ string dependsOn = $ "[resourceId('Microsoft.ApiManagement/service/apis/schemas', parameters('ApimServiceName'), '{ oApiName } ', '{ operationTemplateRepresentation . schemaId } ')]";
156
+ // add value to list if schema has not already been added
157
+ if ( ! operationDependsOn . Exists ( o => o == dependsOn ) )
158
+ {
159
+ operationDependsOn . Add ( dependsOn ) ;
160
+ }
161
+ }
162
+ }
163
+ }
164
+ operationResource . dependsOn = operationDependsOn . ToArray ( ) ;
129
165
130
166
templateResources . Add ( operationResource ) ;
131
167
try
@@ -150,7 +186,7 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
150
186
#region API Policies
151
187
try
152
188
{
153
- Console . WriteLine ( "Geting API Policy from {0} API: " , apiName ) ;
189
+ Console . WriteLine ( "Getting API Policy from {0} API: " , apiName ) ;
154
190
string apiPolicies = apiExtractor . GetAPIPolicies ( apimname , resourceGroup , apiName ) . Result ;
155
191
Console . WriteLine ( "API Policy found!" ) ;
156
192
PolicyTemplateResource apiPoliciesResource = JsonConvert . DeserializeObject < PolicyTemplateResource > ( apiPolicies ) ;
@@ -170,7 +206,7 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
170
206
#region API Products
171
207
try
172
208
{
173
- Console . WriteLine ( "Geting API Products from {0} API: " , apiName ) ;
209
+ Console . WriteLine ( "Getting API Products from {0} API: " , apiName ) ;
174
210
string apiProducts = apiExtractor . GetApiProducts ( apimname , resourceGroup , apiName ) . Result ;
175
211
JObject oApiProducts = JObject . Parse ( apiProducts ) ;
176
212
@@ -198,7 +234,7 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
198
234
#region Diagnostics
199
235
200
236
Console . WriteLine ( "------------------------------------------" ) ;
201
- Console . WriteLine ( "Geting diagnostics from {0} API:" , apiName ) ;
237
+ Console . WriteLine ( "Getting diagnostics from {0} API:" , apiName ) ;
202
238
string diagnostics = apiExtractor . GetAPIDiagnostics ( apimname , resourceGroup , apiName ) . Result ;
203
239
JObject oDiagnostics = JObject . Parse ( diagnostics ) ;
204
240
foreach ( var diagnostic in oDiagnostics [ "value" ] )
@@ -316,10 +352,43 @@ private void GenerateProductsARMTemplate(string apimname, string resourceGroup,
316
352
fileWriter . WriteJSONToFile ( armTemplate , @fileFolder + Path . DirectorySeparatorChar + apimname + "-products.json" ) ;
317
353
318
354
}
355
+
356
+ private List < TemplateResource > GenerateSchemasARMTemplate ( string apimServiceName , string apiName , string resourceGroup , string fileFolder )
357
+ {
358
+ Console . WriteLine ( "------------------------------------------" ) ;
359
+ Console . WriteLine ( "Getting operation schemas from service" ) ;
360
+
361
+ APIExtractor apiExtractor = new APIExtractor ( ) ;
362
+ List < TemplateResource > templateResources = new List < TemplateResource > ( ) ;
363
+
364
+ string schemas = apiExtractor . GetApiSchemas ( apimServiceName , resourceGroup , apiName ) . Result ;
365
+ JObject oSchemas = JObject . Parse ( schemas ) ;
366
+
367
+ foreach ( var item in oSchemas [ "value" ] )
368
+ {
369
+ string schemaName = ( ( JValue ) item [ "name" ] ) . Value . ToString ( ) ;
370
+ Console . WriteLine ( "'{0}' Schema found" , schemaName ) ;
371
+
372
+ string schemaDetails = apiExtractor . GetApiSchemaDetails ( apimServiceName , resourceGroup , apiName , schemaName ) . Result ;
373
+
374
+ // pull returned document and convert to correct format
375
+ RESTReturnedSchemaTemplate restReturnedSchemaTemplate = JsonConvert . DeserializeObject < RESTReturnedSchemaTemplate > ( schemaDetails ) ;
376
+ SchemaTemplateResource schemaDetailsResource = JsonConvert . DeserializeObject < SchemaTemplateResource > ( schemaDetails ) ;
377
+ schemaDetailsResource . properties . document . value = JsonConvert . SerializeObject ( restReturnedSchemaTemplate . properties . document ) ;
378
+ schemaDetailsResource . name = $ "[concat(parameters('ApimServiceName'), '/{ apiName } /{ schemaName } ')]";
379
+ schemaDetailsResource . apiVersion = "2018-06-01-preview" ;
380
+ schemaDetailsResource . dependsOn = new string [ ] { $ "[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{ apiName } ')]" } ;
381
+
382
+ templateResources . Add ( schemaDetailsResource ) ;
383
+
384
+ }
385
+ return templateResources ;
386
+ }
387
+
319
388
private async void GenerateLoggerTemplate ( string resourceGroup , string apimname , string fileFolder )
320
389
{
321
390
Console . WriteLine ( "------------------------------------------" ) ;
322
- Console . WriteLine ( "Geting loggers from service" ) ;
391
+ Console . WriteLine ( "Getting loggers from service" ) ;
323
392
LoggerExtractor loggerExtractor = new LoggerExtractor ( ) ;
324
393
PropertyExtractor propertyExtractor = new PropertyExtractor ( ) ;
325
394
Template armTemplate = GenerateEmptyTemplateWithParameters ( ) ;
@@ -365,8 +434,7 @@ private async void GenerateLoggerTemplate(string resourceGroup, string apimname,
365
434
366
435
armTemplate . resources = templateResources . ToArray ( ) ;
367
436
FileWriter fileWriter = new FileWriter ( ) ;
368
- string filePath = fileFolder + Path . DirectorySeparatorChar + string . Format ( "loggers" , "/" , "-" ) + ".json" ;
369
- fileWriter . WriteJSONToFile ( armTemplate , filePath ) ;
437
+ fileWriter . WriteJSONToFile ( armTemplate , @fileFolder + Path . DirectorySeparatorChar + apimname + "-loggers.json" ) ;
370
438
}
371
439
372
440
public Template GenerateEmptyTemplateWithParameters ( )
0 commit comments