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

Commit 60e954b

Browse files
committed
refactor api version set extractor, move into own class. Update version to 2019-01-01
1 parent 679ead9 commit 60e954b

File tree

5 files changed

+81
-42
lines changed

5 files changed

+81
-42
lines changed

src/APIM_ARMTemplate/apimtemplate/Commands/Extract.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public ExtractCommand()
4545
// initialize entity extractor classes
4646
FileWriter fileWriter = new FileWriter();
4747
APIExtractor apiExtractor = new APIExtractor();
48+
APIVersionSetExtractor apiVersionSetExtractor = new APIVersionSetExtractor();
4849
AuthorizationServerExtractor authorizationServerExtractor = new AuthorizationServerExtractor();
4950
BackendExtractor backendExtractor = new BackendExtractor();
5051
LoggerExtractor loggerExtractor = new LoggerExtractor();
@@ -54,6 +55,7 @@ public ExtractCommand()
5455
// extract templates from apim service
5556
Template apiTemplate = await apiExtractor.GenerateAPIsARMTemplate(apimname, resourceGroup, fileFolder, singleApiName);
5657
List<TemplateResource> apiTemplateResources = apiTemplate.resources.ToList();
58+
Template apiVersionSetTemplate = await apiVersionSetExtractor.GenerateAPIVersionSetsARMTemplate(apimname, resourceGroup, singleApiName, apiTemplateResources);
5759
Template authorizationTemplate = await authorizationServerExtractor.GenerateAuthorizationServersARMTemplate(apimname, resourceGroup, singleApiName, apiTemplateResources);
5860
Template loggerTemplate = await loggerExtractor.GenerateLoggerTemplate(apimname, resourceGroup, singleApiName, apiTemplateResources);
5961
Template productTemplate = await productExtractor.GenerateProductsARMTemplate(apimname, resourceGroup, singleApiName, apiTemplateResources);
@@ -64,7 +66,8 @@ public ExtractCommand()
6466
// write templates to output file location
6567
string apiFileName = singleApiName == null ? @fileFolder + Path.DirectorySeparatorChar + apimname + "-apis-template.json" : @fileFolder + Path.DirectorySeparatorChar + apimname + "-" + singleApiName + "-api-template.json";
6668
fileWriter.WriteJSONToFile(apiTemplate, apiFileName);
67-
fileWriter.WriteJSONToFile(authorizationTemplate, @fileFolder + Path.DirectorySeparatorChar + apimname + "-authorizationServers.json");
69+
fileWriter.WriteJSONToFile(apiVersionSetTemplate, @fileFolder + Path.DirectorySeparatorChar + apimname + "-authorizationServers.json");
70+
fileWriter.WriteJSONToFile(authorizationTemplate, @fileFolder + Path.DirectorySeparatorChar + apimname + "-apiVersionSets.json");
6871
fileWriter.WriteJSONToFile(backendTemplate, @fileFolder + Path.DirectorySeparatorChar + apimname + "-backends.json");
6972
fileWriter.WriteJSONToFile(loggerTemplate, @fileFolder + Path.DirectorySeparatorChar + apimname + "-loggers.json");
7073
fileWriter.WriteJSONToFile(namedValueTemplate, @fileFolder + Path.DirectorySeparatorChar + apimname + "-namedValues.json");

src/APIM_ARMTemplate/apimtemplate/Common/Constants/GlobalConstants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal static class GlobalConstants
1111
public const string ExtractName = "extract";
1212
public const string ExtractDescription = "Extract an existing API Management instance";
1313

14-
public const string APIVersion = "2018-06-01-preview";
14+
public const string APIVersion = "2019-01-01";
1515
public const string LinkedAPIVersion = "2018-01-01";
1616

1717
public const string azAccessToken = "account get-access-token --query \"accessToken\" --output json";

src/APIM_ARMTemplate/apimtemplate/Extractor/EntityExtractors/APIExtractor.cs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
using System;
2-
using System.Net.Http;
3-
using System.Net.Http.Headers;
42
using System.Threading.Tasks;
53
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common;
64
using System.Collections.Generic;
75
using Newtonsoft.Json.Linq;
86
using Newtonsoft.Json;
9-
using System.IO;
107
using System.Linq;
118

129
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extract
@@ -53,16 +50,6 @@ public async Task<string> GetAPIDetails(string ApiManagementName, string Resourc
5350
return await CallApiManagement(azToken, requestUrl);
5451
}
5552

56-
public async Task<string> GetAPIVersionSet(string ApiManagementName, string ResourceGroupName, string VersionSetName)
57-
{
58-
(string azToken, string azSubId) = await auth.GetAccessToken();
59-
60-
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/{4}?api-version={5}",
61-
baseUrl, azSubId, ResourceGroupName, ApiManagementName, VersionSetName, GlobalConstants.APIVersion);
62-
63-
return await CallApiManagement(azToken, requestUrl);
64-
}
65-
6653
public async Task<string> GetAPIs(string ApiManagementName, string ResourceGroupName)
6754
{
6855
(string azToken, string azSubId) = await auth.GetAccessToken();
@@ -167,15 +154,13 @@ public async Task<Template> GenerateAPIsARMTemplate(string apimname, string reso
167154

168155
if (apiResource.properties.apiVersionSetId != null)
169156
{
170-
// extract the version set if referenced by an
171157
apiResource.dependsOn = new string[] { };
172158

173159
string versionSetName = apiResource.properties.apiVersionSetId;
174160
int versionSetPosition = versionSetName.IndexOf("api-version-sets/");
175161

176162
versionSetName = versionSetName.Substring(versionSetPosition, (versionSetName.Length - versionSetPosition));
177163
apiResource.properties.apiVersionSetId = $"[concat(resourceId('Microsoft.ApiManagement/service', parameters('ApimServiceName')), '/{versionSetName}')]";
178-
GenerateVersionSetARMTemplate(apimname, resourceGroup, versionSetName, fileFolder);
179164
}
180165
else
181166
{
@@ -373,30 +358,6 @@ public static JObject FormatoApi(string singleApiName, JObject oApi)
373358
return oApi;
374359
}
375360

376-
public async void GenerateVersionSetARMTemplate(string apimname, string resourceGroup, string versionSetName, string fileFolder)
377-
{
378-
Template armTemplate = GenerateEmptyTemplateWithParameters();
379-
380-
List<TemplateResource> templateResources = new List<TemplateResource>();
381-
382-
// pull version set resource
383-
string versionSet = await GetAPIVersionSet(apimname, resourceGroup, versionSetName);
384-
// convert returned version set to template resource
385-
APIVersionSetTemplateResource versionSetResource = JsonConvert.DeserializeObject<APIVersionSetTemplateResource>(versionSet);
386-
387-
string filePath = fileFolder + Path.DirectorySeparatorChar + string.Format(versionSetResource.name, "/", "-") + ".json";
388-
389-
versionSetResource.name = $"[concat(parameters('ApimServiceName'), '/{versionSetResource.name}')]";
390-
versionSetResource.apiVersion = GlobalConstants.APIVersion;
391-
392-
templateResources.Add(versionSetResource);
393-
armTemplate.resources = templateResources.ToArray();
394-
395-
// write version set template to output file location
396-
FileWriter fileWriter = new FileWriter();
397-
fileWriter.WriteJSONToFile(armTemplate, filePath);
398-
}
399-
400361
public async Task<List<TemplateResource>> GenerateSchemasARMTemplate(string apimServiceName, string apiName, string resourceGroup, string fileFolder)
401362
{
402363
Console.WriteLine("------------------------------------------");
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common;
3+
using System.Collections.Generic;
4+
using Newtonsoft.Json.Linq;
5+
using System.Linq;
6+
using Newtonsoft.Json;
7+
using System;
8+
9+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extract
10+
{
11+
public class APIVersionSetExtractor : EntityExtractor
12+
{
13+
public async Task<string> GetAPIVersionSets(string ApiManagementName, string ResourceGroupName)
14+
{
15+
(string azToken, string azSubId) = await auth.GetAccessToken();
16+
17+
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apiVersionSets?api-version={4}",
18+
baseUrl, azSubId, ResourceGroupName, ApiManagementName, GlobalConstants.APIVersion);
19+
20+
return await CallApiManagement(azToken, requestUrl);
21+
}
22+
23+
public async Task<string> GetAPIVersionSetDetails(string ApiManagementName, string ResourceGroupName, string APIVersionSetName)
24+
{
25+
(string azToken, string azSubId) = await auth.GetAccessToken();
26+
27+
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apiVersionSets/{4}?api-version={5}",
28+
baseUrl, azSubId, ResourceGroupName, ApiManagementName, APIVersionSetName, GlobalConstants.APIVersion);
29+
30+
return await CallApiManagement(azToken, requestUrl);
31+
}
32+
33+
public async Task<Template> GenerateAPIVersionSetsARMTemplate(string apimname, string resourceGroup, string singleApiName, List<TemplateResource> apiTemplateResources)
34+
{
35+
Console.WriteLine("------------------------------------------");
36+
Console.WriteLine("Getting API Version Sets from service");
37+
Template armTemplate = GenerateEmptyTemplateWithParameters();
38+
39+
// isolate apis in the case of a single api extraction
40+
var apiResources = apiTemplateResources.Where(resource => resource.type == ResourceTypeConstants.API);
41+
42+
List<TemplateResource> templateResources = new List<TemplateResource>();
43+
44+
// pull all version sets for service
45+
string versionSets = await GetAPIVersionSets(apimname, resourceGroup);
46+
JObject oVersionSets = JObject.Parse(versionSets);
47+
48+
foreach (var item in oVersionSets["value"])
49+
{
50+
string versionSetName = ((JValue)item["name"]).Value.ToString();
51+
string versionSetDetails = await GetAPIVersionSetDetails(apimname, resourceGroup, versionSetName);
52+
53+
// convert returned product to template resource class
54+
JsonSerializerSettings settings = new JsonSerializerSettings
55+
{
56+
NullValueHandling = NullValueHandling.Ignore,
57+
MissingMemberHandling = MissingMemberHandling.Ignore
58+
};
59+
APIVersionSetTemplateResource versionSetTemplateResource = JsonConvert.DeserializeObject<APIVersionSetTemplateResource>(versionSetDetails, settings);
60+
versionSetTemplateResource.name = $"[concat(parameters('ApimServiceName'), '/{versionSetName}')]";
61+
versionSetTemplateResource.apiVersion = GlobalConstants.APIVersion;
62+
63+
// 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
64+
if (singleApiName == null || apiResources.SingleOrDefault(api => (api as APITemplateResource).properties.apiVersionSetId.Contains(versionSetName)) != null)
65+
{
66+
Console.WriteLine("'{0}' API Version Set found", versionSetName);
67+
templateResources.Add(versionSetTemplateResource);
68+
}
69+
}
70+
71+
armTemplate.resources = templateResources.ToArray();
72+
return armTemplate;
73+
}
74+
}
75+
}

src/APIM_ARMTemplate/apimtemplate/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"apimtemplate": {
44
"commandName": "Project",
5-
"commandLineArgs": "create --configFile ./Creator/ExampleFiles/YAMLConfigs/validTesting.yml"
5+
"commandLineArgs": "extract --name LucasBlankUnlinked --resourceGroup LucasHuetHudsonInternal --fileFolder C:\\\\\\\\\\\\\\\\Users\\\\\\\\\\\\\\\\lucashh\\\\\\\\\\\\\\\\Desktop\\\\\\\\\\\\\\\\Projects\\\\\\\\\\\\\\\\APIM-ARM\\\\\\\\\\\\\\\\ExtractedTemplates\\\\\\\\Full"
66
}
77
}
88
}

0 commit comments

Comments
 (0)