Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 46bd262

Browse files
committed
refactor creation of empty template
1 parent 15303c9 commit 46bd262

File tree

1 file changed

+22
-57
lines changed
  • src/APIM_ARMTemplate/apimtemplate/Commands

1 file changed

+22
-57
lines changed

src/APIM_ARMTemplate/apimtemplate/Commands/Extract.cs

Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Newtonsoft.Json.Linq;
66
using System.IO;
77
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common;
8+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create;
89
using System.Linq;
910

1011
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extract
@@ -38,7 +39,7 @@ public ExtractCommand()
3839
{
3940
singleApiName = apiName.Values[0].ToString();
4041
}
41-
42+
4243
Console.WriteLine("API Management Template");
4344
Console.WriteLine();
4445
Console.WriteLine("Connecting to {0} API Management Service on {1} Resource Group ...", apimname, resourceGroup);
@@ -58,19 +59,9 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
5859
FileWriter fileWriter;
5960
APIExtractor apiExtractor = new APIExtractor();
6061
string apis = apiExtractor.GetAPIs(apimname, resourceGroup).Result;
61-
62-
Template armTemplate = new Template()
63-
{
64-
schema = "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
65-
contentVersion = "1.0.0.0",
66-
parameters = new Dictionary<string, TemplateParameterProperties>
67-
{
68-
{ "ApimServiceName", new TemplateParameterProperties(){ type = "string" } }
69-
},
70-
variables = { },
71-
resources = { },
72-
outputs = { }
73-
};
62+
TemplateCreator templateCreator = new TemplateCreator();
63+
Template armTemplate = templateCreator.CreateEmptyTemplate();
64+
armTemplate.parameters = new Dictionary<string, TemplateParameterProperties> { { "ApimServiceName", new TemplateParameterProperties() { type = "string" } } };
7465

7566
JObject oApi = JObject.Parse(apis);
7667
oApi = FormatoApi(singleApiName, oApi);
@@ -191,9 +182,9 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
191182
Console.WriteLine($" -- {apiProductName} Product found to {oApiName} API");
192183
ApiProductsTemplateResource apiProductsResource = JsonConvert.DeserializeObject<ApiProductsTemplateResource>(apiProducts);
193184
apiProductsResource.type = "Microsoft.ApiManagement/service/products/apis";
194-
apiProductsResource.name = $"[concat(parameters('ApimServiceName'), '/{apiProductName}/{oApiName}')]";
185+
apiProductsResource.name = $"[concat(parameters('ApimServiceName'), '/{apiProductName}/{oApiName}')]";
195186
apiProductsResource.apiVersion = "2018-06-01-preview";
196-
apiProductsResource.scale = null;
187+
apiProductsResource.scale = null;
197188
apiProductsResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{oApiName}')]" };
198189

199190
templateResources.Add(apiProductsResource);
@@ -205,7 +196,7 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
205196
Console.WriteLine("No API products!");
206197
}
207198
#endregion
208-
199+
209200
#region Diagnostics
210201

211202
Console.WriteLine("------------------------------------------");
@@ -251,7 +242,7 @@ private void GenerateARMTemplate(string apimname, string resourceGroup, string f
251242
fileWriter = new FileWriter();
252243
fileWriter.WriteJSONToFile(armTemplate, @fileFolder + Path.DirectorySeparatorChar + apimname + "-apis-template.json");
253244
}
254-
#endregion
245+
#endregion
255246
}
256247
private static JObject FormatoApi(string singleApiName, JObject oApi)
257248
{
@@ -265,7 +256,7 @@ private static JObject FormatoApi(string singleApiName, JObject oApi)
265256
var selectedApi = (JObject)oApi.SelectTokens(objectSelector).FirstOrDefault();
266257
if (selectedApi == null)
267258
{
268-
throw new Exception($"{singleApiName} API not found!" );
259+
throw new Exception($"{singleApiName} API not found!");
269260
}
270261
item2.Add(selectedApi);
271262
value["value"] = item2;
@@ -277,20 +268,11 @@ private static JObject FormatoApi(string singleApiName, JObject oApi)
277268
private void GenerateVersionSetARMTemplate(string apimname, string resourceGroup, string versionSetName, string fileFolder)
278269
{
279270
APIExtractor apiExtractor = new APIExtractor();
280-
Template armTemplate = new Template()
281-
{
282-
schema = "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
283-
contentVersion = "1.0.0.0",
284-
parameters = new Dictionary<string, TemplateParameterProperties>
285-
{
286-
{ "ApimServiceName", new TemplateParameterProperties(){ type = "string" } }
287-
},
288-
variables = { },
289-
resources = { },
290-
outputs = { }
291-
};
271+
TemplateCreator templateCreator = new TemplateCreator();
272+
Template armTemplate = templateCreator.CreateEmptyTemplate();
273+
armTemplate.parameters = new Dictionary<string, TemplateParameterProperties> { { "ApimServiceName", new TemplateParameterProperties() { type = "string" } } };
292274

293-
List<TemplateResource> templateResources = new List<TemplateResource>();
275+
List<TemplateResource> templateResources = new List<TemplateResource>();
294276

295277
string versionSet = apiExtractor.GetAPIVersionSet(apimname, resourceGroup, versionSetName).Result;
296278
APIVersionSetTemplateResource versionSetResource = JsonConvert.DeserializeObject<APIVersionSetTemplateResource>(versionSet);
@@ -309,18 +291,9 @@ private void GenerateVersionSetARMTemplate(string apimname, string resourceGroup
309291
private void GenerateProductsARMTemplate(string apimname, string resourceGroup, string fileFolder)
310292
{
311293
APIExtractor apiExtractor = new APIExtractor();
312-
Template armTemplate = new Template()
313-
{
314-
schema = "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
315-
contentVersion = "1.0.0.0",
316-
parameters = new Dictionary<string, TemplateParameterProperties>
317-
{
318-
{ "ApimServiceName", new TemplateParameterProperties(){ type = "string" } }
319-
},
320-
variables = { },
321-
resources = { },
322-
outputs = { }
323-
};
294+
TemplateCreator templateCreator = new TemplateCreator();
295+
Template armTemplate = templateCreator.CreateEmptyTemplate();
296+
armTemplate.parameters = new Dictionary<string, TemplateParameterProperties> { { "ApimServiceName", new TemplateParameterProperties() { type = "string" } } };
324297

325298
List<TemplateResource> templateResources = new List<TemplateResource>();
326299

@@ -354,18 +327,9 @@ private async void GenerateLoggerTemplate(string resourceGroup, string apimname,
354327
Console.WriteLine("Geting loggers from service");
355328
LoggerExtractor loggerExtractor = new LoggerExtractor();
356329
PropertyExtractor propertyExtractor = new PropertyExtractor();
357-
Template armTemplate = new Template()
358-
{
359-
schema = "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
360-
contentVersion = "1.0.0.0",
361-
parameters = new Dictionary<string, TemplateParameterProperties>
362-
{
363-
{ "ApimServiceName", new TemplateParameterProperties(){ type = "string" } }
364-
},
365-
variables = { },
366-
resources = { },
367-
outputs = { }
368-
};
330+
TemplateCreator templateCreator = new TemplateCreator();
331+
Template armTemplate = templateCreator.CreateEmptyTemplate();
332+
armTemplate.parameters = new Dictionary<string, TemplateParameterProperties> { { "ApimServiceName", new TemplateParameterProperties() { type = "string" } } };
369333

370334
List<TemplateResource> templateResources = new List<TemplateResource>();
371335

@@ -395,7 +359,8 @@ private async void GenerateLoggerTemplate(string resourceGroup, string apimname,
395359
{
396360
string hiddenKey = loggerResource.properties.credentials.instrumentationKey.Substring(2, loggerResource.properties.credentials.instrumentationKey.Length - 4);
397361
loggerResource.properties.credentials.instrumentationKey = propertyResources.Find(p => p.properties.displayName == hiddenKey).properties.value;
398-
} else if (loggerResource.properties.credentials.connectionString != null)
362+
}
363+
else if (loggerResource.properties.credentials.connectionString != null)
399364
{
400365
string hiddenKey = loggerResource.properties.credentials.connectionString.Substring(2, loggerResource.properties.credentials.connectionString.Length - 4);
401366
loggerResource.properties.credentials.connectionString = propertyResources.Find(p => p.properties.displayName == hiddenKey).properties.value;

0 commit comments

Comments
 (0)