11
11
12
12
namespace Microsoft . Azure . Management . ApiManagement . ArmTemplates . Extract
13
13
{
14
- public class APIExtractor : EntityExtractor
15
- {
14
+ public class APIExtractor : EntityExtractor
15
+ {
16
16
public async Task < string > GetAPIOperations ( string ApiManagementName , string ResourceGroupName , string ApiName )
17
17
{
18
18
( string azToken , string azSubId ) = await auth . GetAccessToken ( ) ;
@@ -56,7 +56,7 @@ public async Task<string> GetAPIDetails(string ApiManagementName, string Resourc
56
56
public async Task < string > GetAPIVersionSet ( string ApiManagementName , string ResourceGroupName , string VersionSetName )
57
57
{
58
58
( string azToken , string azSubId ) = await auth . GetAccessToken ( ) ;
59
-
59
+
60
60
string requestUrl = string . Format ( "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/{4}?api-version={5}" ,
61
61
baseUrl , azSubId , ResourceGroupName , ApiManagementName , VersionSetName , GlobalConstants . APIVersion ) ;
62
62
@@ -69,7 +69,7 @@ public async Task<string> GetAPIs(string ApiManagementName, string ResourceGroup
69
69
70
70
string requestUrl = string . Format ( "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis?api-version={4}" ,
71
71
baseUrl , azSubId , ResourceGroupName , ApiManagementName , GlobalConstants . APIVersion ) ;
72
-
72
+
73
73
return await CallApiManagement ( azToken , requestUrl ) ;
74
74
}
75
75
@@ -93,17 +93,17 @@ public async Task<string> GetAPIDiagnostics(string ApiManagementName, string Res
93
93
return await CallApiManagement ( azToken , requestUrl ) ;
94
94
}
95
95
96
- public async Task < string > GetApiOperationPolicies ( string ApiManagementName , string ResourceGroupName , string ApiName , string OperationName )
96
+ public async Task < string > GetAPIOperationPolicies ( string ApiManagementName , string ResourceGroupName , string ApiName , string OperationName )
97
97
{
98
98
( string azToken , string azSubId ) = await auth . GetAccessToken ( ) ;
99
-
99
+
100
100
string requestUrl = string . Format ( "{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis/{4}/operations/get/policies/policy?api-version={5}" ,
101
101
baseUrl , azSubId , ResourceGroupName , ApiManagementName , ApiName , GlobalConstants . APIVersion ) ;
102
102
103
103
return await CallApiManagement ( azToken , requestUrl ) ;
104
104
}
105
105
106
- public async Task < string > GetApiProducts ( string ApiManagementName , string ResourceGroupName , string ApiName )
106
+ public async Task < string > GetAPIProducts ( string ApiManagementName , string ResourceGroupName , string ApiName )
107
107
{
108
108
( string azToken , string azSubId ) = await auth . GetAccessToken ( ) ;
109
109
@@ -113,7 +113,7 @@ public async Task<string> GetApiProducts(string ApiManagementName, string Resour
113
113
return await CallApiManagement ( azToken , requestUrl ) ;
114
114
}
115
115
116
- public async Task < string > GetApiSchemas ( string ApiManagementName , string ResourceGroupName , string ApiName )
116
+ public async Task < string > GetAPISchemas ( string ApiManagementName , string ResourceGroupName , string ApiName )
117
117
{
118
118
( string azToken , string azSubId ) = await auth . GetAccessToken ( ) ;
119
119
@@ -123,7 +123,7 @@ public async Task<string> GetApiSchemas(string ApiManagementName, string Resourc
123
123
return await CallApiManagement ( azToken , requestUrl ) ;
124
124
}
125
125
126
- public async Task < string > GetApiSchemaDetails ( string ApiManagementName , string ResourceGroupName , string ApiName , string schemaName )
126
+ public async Task < string > GetAPISchemaDetails ( string ApiManagementName , string ResourceGroupName , string ApiName , string schemaName )
127
127
{
128
128
( string azToken , string azSubId ) = await auth . GetAccessToken ( ) ;
129
129
@@ -135,7 +135,9 @@ public async Task<string> GetApiSchemaDetails(string ApiManagementName, string R
135
135
136
136
public async Task < Template > GenerateAPIsARMTemplate ( string apimname , string resourceGroup , string fileFolder , string singleApiName )
137
137
{
138
+ // pull all apis from service
138
139
string apis = await GetAPIs ( apimname , resourceGroup ) ;
140
+ // initialize arm template
139
141
Template armTemplate = GenerateEmptyTemplateWithParameters ( ) ;
140
142
141
143
JObject oApi = JObject . Parse ( apis ) ;
@@ -153,6 +155,7 @@ public async Task<Template> GenerateAPIsARMTemplate(string apimname, string reso
153
155
Console . WriteLine ( "------------------------------------------" ) ;
154
156
Console . WriteLine ( "Getting operations from {0} API:" , apiName ) ;
155
157
158
+ // convert returned api to template resource class
156
159
JObject oApiDetails = JObject . Parse ( apiDetails ) ;
157
160
APITemplateResource apiResource = JsonConvert . DeserializeObject < APITemplateResource > ( apiDetails ) ;
158
161
string oApiName = ( ( JValue ) oApiDetails [ "name" ] ) . Value . ToString ( ) ;
@@ -164,6 +167,7 @@ public async Task<Template> GenerateAPIsARMTemplate(string apimname, string reso
164
167
165
168
if ( apiResource . properties . apiVersionSetId != null )
166
169
{
170
+ // extract the version set if referenced by an
167
171
apiResource . dependsOn = new string [ ] { } ;
168
172
169
173
string versionSetName = apiResource . properties . apiVersionSetId ;
@@ -181,12 +185,14 @@ public async Task<Template> GenerateAPIsARMTemplate(string apimname, string reso
181
185
templateResources . Add ( apiResource ) ;
182
186
183
187
#region Schemas
188
+ // add schema resources to api template
184
189
List < TemplateResource > schemaResources = await GenerateSchemasARMTemplate ( apimname , apiName , resourceGroup , fileFolder ) ;
185
190
templateResources . AddRange ( schemaResources ) ;
186
191
#endregion
187
192
188
193
#region Operations
189
194
195
+ // pull api operations for service
190
196
string operations = await GetAPIOperations ( apimname , resourceGroup , apiName ) ;
191
197
JObject oOperations = JObject . Parse ( operations ) ;
192
198
@@ -197,13 +203,14 @@ public async Task<Template> GenerateAPIsARMTemplate(string apimname, string reso
197
203
198
204
Console . WriteLine ( "'{0}' Operation found" , operationName ) ;
199
205
206
+ // convert returned operation to template resource class
200
207
OperationTemplateResource operationResource = JsonConvert . DeserializeObject < OperationTemplateResource > ( operationDetails ) ;
201
208
string operationResourceName = operationResource . name ;
202
209
operationResource . name = $ "[concat(parameters('ApimServiceName'), '/{ oApiName } /{ operationResourceName } ')]";
203
210
operationResource . apiVersion = GlobalConstants . APIVersion ;
204
211
operationResource . scale = null ;
205
212
206
- // depend on api and schemas if necessary
213
+ // add api and schemas to operation dependsOn, if necessary
207
214
List < string > operationDependsOn = new List < string > ( ) { $ "[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{ oApiName } ')]" } ;
208
215
foreach ( OperationTemplateRepresentation operationTemplateRepresentation in operationResource . properties . request . representations )
209
216
{
@@ -233,8 +240,9 @@ public async Task<Template> GenerateAPIsARMTemplate(string apimname, string reso
233
240
}
234
241
}
235
242
operationResource . dependsOn = operationDependsOn . ToArray ( ) ;
236
-
237
243
templateResources . Add ( operationResource ) ;
244
+
245
+ // add operation policy resource to api template
238
246
try
239
247
{
240
248
string operationPolicy = await GetOperationPolicy ( apimname , resourceGroup , oApiName , operationName ) ;
@@ -255,6 +263,7 @@ public async Task<Template> GenerateAPIsARMTemplate(string apimname, string reso
255
263
#endregion
256
264
257
265
#region API Policies
266
+ // add api policy resource to api template
258
267
try
259
268
{
260
269
Console . WriteLine ( "Getting API Policy from {0} API: " , apiName ) ;
@@ -274,17 +283,22 @@ public async Task<Template> GenerateAPIsARMTemplate(string apimname, string reso
274
283
}
275
284
#endregion
276
285
286
+ // add product api associations to template
277
287
#region API Products
278
288
try
279
289
{
280
290
Console . WriteLine ( "Getting API Products from {0} API: " , apiName ) ;
281
- string apiProducts = await GetApiProducts ( apimname , resourceGroup , apiName ) ;
291
+
292
+ // pull product api associations
293
+ string apiProducts = await GetAPIProducts ( apimname , resourceGroup , apiName ) ;
282
294
JObject oApiProducts = JObject . Parse ( apiProducts ) ;
283
295
284
296
foreach ( var item in oApiProducts [ "value" ] )
285
297
{
286
298
string apiProductName = ( ( JValue ) item [ "name" ] ) . Value . ToString ( ) ;
287
299
Console . WriteLine ( $ " -- { apiProductName } Product found to { oApiName } API") ;
300
+
301
+ // convert returned api product associations to template resource class
288
302
ProductAPITemplateResource productAPIResource = JsonConvert . DeserializeObject < ProductAPITemplateResource > ( apiProducts ) ;
289
303
productAPIResource . type = ResourceTypeConstants . ProductAPI ;
290
304
productAPIResource . name = $ "[concat(parameters('ApimServiceName'), '/{ apiProductName } /{ oApiName } ')]";
@@ -302,16 +316,18 @@ public async Task<Template> GenerateAPIsARMTemplate(string apimname, string reso
302
316
#endregion
303
317
304
318
#region Diagnostics
305
-
319
+ // add diagnostics to template
306
320
Console . WriteLine ( "------------------------------------------" ) ;
307
321
Console . WriteLine ( "Getting diagnostics from {0} API:" , apiName ) ;
322
+ // pull diagnostics for api
308
323
string diagnostics = await GetAPIDiagnostics ( apimname , resourceGroup , apiName ) ;
309
324
JObject oDiagnostics = JObject . Parse ( diagnostics ) ;
310
325
foreach ( var diagnostic in oDiagnostics [ "value" ] )
311
326
{
312
327
string diagnosticName = ( ( JValue ) diagnostic [ "name" ] ) . Value . ToString ( ) ;
313
328
Console . WriteLine ( "'{0}' Diagnostic found" , diagnosticName ) ;
314
329
330
+ // convert returned diagnostic to template resource class
315
331
DiagnosticTemplateResource diagnosticResource = diagnostic . ToObject < DiagnosticTemplateResource > ( ) ;
316
332
diagnosticResource . name = $ "[concat(parameters('ApimServiceName'), '/{ oApiName } /{ diagnosticName } ')]";
317
333
diagnosticResource . type = ResourceTypeConstants . APIDiagnostic ;
@@ -328,7 +344,6 @@ public async Task<Template> GenerateAPIsARMTemplate(string apimname, string reso
328
344
templateResources . Add ( diagnosticResource ) ;
329
345
330
346
}
331
-
332
347
#endregion
333
348
}
334
349
@@ -364,7 +379,9 @@ public async void GenerateVersionSetARMTemplate(string apimname, string resource
364
379
365
380
List < TemplateResource > templateResources = new List < TemplateResource > ( ) ;
366
381
382
+ // pull version set resource
367
383
string versionSet = await GetAPIVersionSet ( apimname , resourceGroup , versionSetName ) ;
384
+ // convert returned version set to template resource
368
385
APIVersionSetTemplateResource versionSetResource = JsonConvert . DeserializeObject < APIVersionSetTemplateResource > ( versionSet ) ;
369
386
370
387
string filePath = fileFolder + Path . DirectorySeparatorChar + string . Format ( versionSetResource . name , "/" , "-" ) + ".json" ;
@@ -375,6 +392,7 @@ public async void GenerateVersionSetARMTemplate(string apimname, string resource
375
392
templateResources . Add ( versionSetResource ) ;
376
393
armTemplate . resources = templateResources . ToArray ( ) ;
377
394
395
+ // write version set template to output file location
378
396
FileWriter fileWriter = new FileWriter ( ) ;
379
397
fileWriter . WriteJSONToFile ( armTemplate , filePath ) ;
380
398
}
@@ -385,17 +403,18 @@ public async Task<List<TemplateResource>> GenerateSchemasARMTemplate(string apim
385
403
Console . WriteLine ( "Getting operation schemas from service" ) ;
386
404
List < TemplateResource > templateResources = new List < TemplateResource > ( ) ;
387
405
388
- string schemas = await GetApiSchemas ( apimServiceName , resourceGroup , apiName ) ;
406
+ // pull all schemas from service
407
+ string schemas = await GetAPISchemas ( apimServiceName , resourceGroup , apiName ) ;
389
408
JObject oSchemas = JObject . Parse ( schemas ) ;
390
409
391
410
foreach ( var item in oSchemas [ "value" ] )
392
411
{
393
412
string schemaName = ( ( JValue ) item [ "name" ] ) . Value . ToString ( ) ;
394
413
Console . WriteLine ( "'{0}' Schema found" , schemaName ) ;
395
414
396
- string schemaDetails = await GetApiSchemaDetails ( apimServiceName , resourceGroup , apiName , schemaName ) ;
415
+ string schemaDetails = await GetAPISchemaDetails ( apimServiceName , resourceGroup , apiName , schemaName ) ;
397
416
398
- // pull returned document and convert to correct format
417
+ // pull returned schema and convert to template resource
399
418
RESTReturnedSchemaTemplate restReturnedSchemaTemplate = JsonConvert . DeserializeObject < RESTReturnedSchemaTemplate > ( schemaDetails ) ;
400
419
SchemaTemplateResource schemaDetailsResource = JsonConvert . DeserializeObject < SchemaTemplateResource > ( schemaDetails ) ;
401
420
schemaDetailsResource . properties . document . value = JsonConvert . SerializeObject ( restReturnedSchemaTemplate . properties . document ) ;
0 commit comments