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

Commit 8a25883

Browse files
committed
Before rewrite the new classes.
1 parent 904c82b commit 8a25883

File tree

8 files changed

+348
-125
lines changed

8 files changed

+348
-125
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,4 @@ ASALocalRun/
338338
.localhistory/
339339
/test01.json
340340
/MyAPI-01.json
341+
/GeneratedTemplates/GeneratedTemplates

src/APIM_ARMTemplate/apimtemplate/Commands/Extract.cs

Lines changed: 153 additions & 101 deletions
Large diffs are not rendered by default.

src/APIM_ARMTemplate/apimtemplate/Common/Api.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Net.Http;
43
using System.Net.Http.Headers;
5-
using System.Text;
64
using System.Threading.Tasks;
75

86
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
@@ -11,6 +9,7 @@ class Api
119
{
1210
static string baseUrl = "https://management.azure.com";
1311
internal Authentication auth = new Authentication();
12+
1413
public async Task<string> GetAPIOperations(string ApiManagementName, string ResourceGroupName, string ApiName)
1514
{
1615
(string azToken, string azSubId) = await auth.GetAccessToken();
@@ -20,6 +19,15 @@ public async Task<string> GetAPIOperations(string ApiManagementName, string Reso
2019

2120
return await CallApiManagement(azToken, requestUrl);
2221
}
22+
public async Task<string> GetAPIOperationDetail(string ApiManagementName, string ResourceGroupName, string ApiName, string OperationName)
23+
{
24+
(string azToken, string azSubId) = await auth.GetAccessToken();
25+
26+
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis/{4}/operations/{5}?api-version={6}",
27+
baseUrl, azSubId, ResourceGroupName, ApiManagementName, ApiName, OperationName, Constants.APIVersion);
28+
29+
return await CallApiManagement(azToken, requestUrl);
30+
}
2331
public async Task<string> GetOperationPolicy(string ApiManagementName, string ResourceGroupName, string ApiName, string OperationId)
2432
{
2533
(string azToken, string azSubId) = await auth.GetAccessToken();
@@ -38,6 +46,15 @@ public async Task<string> GetAPIDetails(string ApiManagementName, string Resourc
3846

3947
return await CallApiManagement(azToken, requestUrl);
4048
}
49+
public async Task<string> GetAPIVersionSet(string ApiManagementName, string ResourceGroupName, string VersionSetName)
50+
{
51+
(string azToken, string azSubId) = await auth.GetAccessToken();
52+
53+
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/{4}?api-version={5}",
54+
baseUrl, azSubId, ResourceGroupName, ApiManagementName, VersionSetName, Constants.APIVersion);
55+
56+
return await CallApiManagement(azToken, requestUrl);
57+
}
4158
public async Task<string> GetAPIs(string ApiManagementName, string ResourceGroupName)
4259
{
4360
(string azToken, string azSubId) = await auth.GetAccessToken();
@@ -47,15 +64,16 @@ public async Task<string> GetAPIs(string ApiManagementName, string ResourceGroup
4764

4865
return await CallApiManagement(azToken, requestUrl);
4966
}
50-
public async Task<string> GetAPIVersionSet(string ApiManagementName, string ResourceGroupName, string APIVersionSetId)
67+
public async Task<string> GetAPIPolicies(string ApiManagementName, string ResourceGroupName, string ApiName)
5168
{
5269
(string azToken, string azSubId) = await auth.GetAccessToken();
53-
54-
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/api-version-sets/{4}?api-version={5}",
55-
baseUrl, azSubId, ResourceGroupName, ApiManagementName, APIVersionSetId ,Constants.APIVersion);
70+
71+
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis/{4}/policies/policy?api-version={5}",
72+
baseUrl, azSubId, ResourceGroupName, ApiManagementName, ApiName, Constants.APIVersion);
5673

5774
return await CallApiManagement(azToken, requestUrl);
5875
}
76+
5977
private static async Task<string> CallApiManagement(string azToken, string requestUrl)
6078
{
6179
using (HttpClient httpClient = new HttpClient())
@@ -67,9 +85,7 @@ private static async Task<string> CallApiManagement(string azToken, string reque
6785
HttpResponseMessage response = await httpClient.SendAsync(request);
6886

6987
response.EnsureSuccessStatusCode();
70-
7188
string responseBody = await response.Content.ReadAsStringAsync();
72-
7389
return responseBody;
7490
}
7591
}

src/APIM_ARMTemplate/apimtemplate/Common/Authentication.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,28 @@ namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
88
{
99
public class Authentication
1010
{
11+
internal static bool isTokenValid = false;
12+
public static DateTime start = DateTime.Now;
13+
internal static string internalAzToken;
14+
internal static string internalAzSubscriptionId;
1115
public async Task<(string azToken, string azSubscriptionId)> GetAccessToken()
1216
{
17+
var tokenTimeout = DateTime.Now;
18+
19+
if ((tokenTimeout - start).TotalSeconds <= 55 && isTokenValid)
20+
{
21+
return (internalAzToken, internalAzSubscriptionId);
22+
}
23+
1324
(bool cliTokenSucceeded, string cliToken) = await TryGetAzCliToken();
1425
(bool cliSubscriptionIdSucceeded, string cliSubscriptionId) = await TryGetAzSubscriptionId();
1526

1627
if (cliTokenSucceeded && cliSubscriptionIdSucceeded)
1728
{
29+
start = DateTime.Now;
30+
internalAzToken = cliToken;
31+
internalAzSubscriptionId = cliSubscriptionId;
32+
isTokenValid = true;
1833
return (cliToken, cliSubscriptionId);
1934
}
2035

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using Newtonsoft.Json;
2+
using System.Collections.Generic;
3+
4+
namespace ARMTemplateNS
5+
{
6+
public class ApimServiceName
7+
{
8+
public string type { get; set; }
9+
}
10+
11+
public class Metadata
12+
{
13+
public string description { get; set; }
14+
}
15+
16+
public class RepoBaseUrl
17+
{
18+
public string type { get; set; }
19+
public Metadata metadata { get; set; }
20+
}
21+
22+
public class Parameters
23+
{
24+
public ApimServiceName ApimServiceName { get; set; }
25+
public RepoBaseUrl repoBaseUrl { get; set; }
26+
}
27+
28+
public class Variables
29+
{
30+
}
31+
32+
public class Respons
33+
{
34+
public int statusCode { get; set; }
35+
public string description { get; set; }
36+
public List<object> headers { get; set; }
37+
}
38+
39+
public class Properties
40+
{
41+
42+
}
43+
44+
public class OperationProperties : Properties
45+
{ // "type": "Microsoft.ApiManagement/service/apis/operations"
46+
public string displayName { get; set; }
47+
public string method { get; set; }
48+
public string urlTemplate { get; set; }
49+
public List<object> templateParameters { get; set; }
50+
public List<Respons> responses { get; set; }
51+
public object policies { get; set; }
52+
}
53+
54+
public class PoliciesProperties : Properties
55+
{ // "type": "Microsoft.ApiManagement/service/apis/policies"
56+
public string policyContent { get; set; }
57+
}
58+
59+
public class APIProperties : Properties
60+
{ //"type": "Microsoft.ApiManagement/service/apis"
61+
public string displayName { get; set; }
62+
public string apiRevision { get; set; }
63+
public string description { get; set; }
64+
public string serviceUrl { get; set; }
65+
public string path { get; set; }
66+
public List<string> protocols { get; set; }
67+
public object authenticationSettings { get; set; }
68+
public object subscriptionKeyParameterNames { get; set; }
69+
public string apiVersion { get; set; }
70+
public string apiVersionSetId { get; set; }
71+
}
72+
73+
public class Resource
74+
{
75+
76+
}
77+
78+
public class APIResource : Resource
79+
{
80+
public string type { get; set; }
81+
public string name { get; set; }
82+
public string apiVersion { get; set; }
83+
public object scale { get; set; }
84+
public APIProperties properties { get; set; }
85+
public string[] dependsOn { get; set; }
86+
}
87+
88+
public class OperationResource : Resource
89+
{
90+
public string type { get; set; }
91+
public string name { get; set; }
92+
public string apiVersion { get; set; }
93+
public object scale { get; set; }
94+
public OperationProperties properties { get; set; }
95+
public string[] dependsOn { get; set; }
96+
}
97+
public class VersionSetProperties : Properties
98+
{
99+
public string description { get; set; }
100+
public string versionQueryName { get; set; }
101+
public string displayName { get; set; }
102+
public string versioningScheme { get; set; }
103+
}
104+
public class VersionSetResource : Resource
105+
{
106+
public string name { get; set; }
107+
public string type { get; set; }
108+
public string apiVersion { get; set; }
109+
public VersionSetProperties properties { get; set; }
110+
}
111+
112+
public class ApiPoliciesResource : Resource
113+
{
114+
public string type { get; set; }
115+
public string name { get; set; }
116+
public string apiVersion { get; set; }
117+
public PoliciesProperties properties { get; set; }
118+
public string[] dependsOn { get; set; }
119+
}
120+
121+
public class ExtractorTemplateParameterProperties
122+
{
123+
public string type { get; set; }
124+
public TemplateParameterMetadata metadata { get; set; }
125+
public string[] allowedValues { get; set; }
126+
public string defaultValue { get; set; }
127+
public string value { get; set; }
128+
}
129+
130+
public class TemplateParameterMetadata
131+
{
132+
public string description { get; set; }
133+
}
134+
135+
public class ARMTemplate
136+
{
137+
[JsonProperty(PropertyName = "$schema")]
138+
public string schema { get; set; }
139+
public string contentVersion { get; set; }
140+
public Dictionary<string, ExtractorTemplateParameterProperties> parameters { get; set; }
141+
public Variables variables { get; set; }
142+
public List<Resource> resources { get; set; }
143+
public object outputs { get; internal set; }
144+
}
145+
}

src/APIM_ARMTemplate/apimtemplate/Extractor/Models/CreatorConfiguration.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ public class CreatorConfig
2222
public class APIConfig
2323
{
2424
public string name { get; set; }
25-
// openApiSpec file location (local or url)
26-
public string openApiSpec { get; set; }
27-
// policy file location (local or url)
25+
// policy file location (local or url)
2826
public string policy { get; set; }
2927
public string suffix { get; set; }
3028
public string apiVersion { get; set; }

src/APIM_ARMTemplate/apimtemplate/Extractor/TemplateCreators/APITemplateCreator.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Newtonsoft.Json;
44
using Microsoft.OpenApi.Models;
55
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create;
6+
using Newtonsoft.Json.Linq;
67

78
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extract
89
{
@@ -15,7 +16,6 @@ public class APITemplateCreatorEx
1516

1617
public APITemplateCreatorEx(TemplateCreator templateCreator, PolicyTemplateCreator policyTemplateCreator, ProductAPITemplateCreator productAPITemplateCreator)
1718
{
18-
1919
this.templateCreator = templateCreator;
2020
this.policyTemplateCreator = policyTemplateCreator;
2121
this.productAPITemplateCreator = productAPITemplateCreator;
@@ -64,18 +64,17 @@ public async Task<Template> CreateSubsequentAPITemplateAsync(CreatorConfig creat
6464
resources.Add(subsequentAPITemplateResource);
6565
resources.Add(apiPolicyResource);
6666
resources.AddRange(operationPolicyResources);
67+
6768
//resources.AddRange(productAPIResources);
6869

6970
apiTemplate.resources = resources.ToArray();
7071
return apiTemplate;
7172
}
7273

74+
75+
7376
public async Task<APITemplateResource> CreateInitialAPITemplateResource(CreatorConfig creatorConfig)
7477
{
75-
// protocols can be pulled by converting the OpenApiSpec into the OpenApiDocument class
76-
OpenAPISpecReader openAPISpecReader = new OpenAPISpecReader();
77-
OpenApiDocument doc = await openAPISpecReader.ConvertOpenAPISpecToDoc(creatorConfig.api.openApiSpec);
78-
7978
// create api resource with properties
8079
APITemplateResource apiTemplateResource = new APITemplateResource()
8180
{
@@ -92,7 +91,7 @@ public async Task<APITemplateResource> CreateInitialAPITemplateResource(CreatorC
9291
authenticationSettings = creatorConfig.api.authenticationSettings,
9392
path = creatorConfig.api.suffix,
9493
displayName = creatorConfig.api.name,
95-
protocols = this.CreateProtocols(doc)
94+
protocols = null
9695
},
9796
// if the template is not linked the depends on for the apiVersionSet needs to be inlined here
9897
dependsOn = new string[] { }
@@ -129,7 +128,7 @@ public async Task<APITemplateResource> CreateSubsequentAPITemplateResourceAsync(
129128
properties = new APITemplateProperties()
130129
{
131130
contentFormat = "swagger-json",
132-
contentValue = "{\"swagger\":\"2.0\"}", //JsonConvert.SerializeObject(deserializedFileContents),
131+
contentValue = null, //JsonConvert.SerializeObject(deserializedFileContents),
133132
// supplied via optional arguments
134133
path = creatorConfig.api.suffix
135134
},
@@ -140,12 +139,9 @@ public async Task<APITemplateResource> CreateSubsequentAPITemplateResourceAsync(
140139

141140
public string[] CreateProtocols(OpenApiDocument doc)
142141
{
143-
// pull protocols from swagger OpenApiDocument
142+
144143
List<string> protocols = new List<string>();
145-
//foreach (OpenApiServer server in doc.Servers)
146-
//{
147-
// protocols.Add(server.Url.Split(":")[0]);
148-
//}
144+
// just to debug
149145
protocols.Add("http");
150146
protocols.Add("https");
151147
return protocols.ToArray();

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": "extract --name apim-extractor --resourceGroup APIM-Extractor"
5+
"commandLineArgs": "extract --name apim-extractor --resourceGroup APIM-Extractor --fileFolder c:\\projs"
66
}
77
}
88
}

0 commit comments

Comments
 (0)