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

Commit 707f54c

Browse files
committed
add api name to config yaml file
1 parent 8979bb7 commit 707f54c

File tree

8 files changed

+99
-19
lines changed

8 files changed

+99
-19
lines changed

src/APIM_ARMTemplate/apimtemplate.test/apimtemplate.test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
<ItemGroup>
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
15+
<PackageReference Include="Microsoft.OpenApi" Version="1.1.2" />
16+
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.1.2" />
1517
<PackageReference Include="xunit" Version="2.4.0" />
1618
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
1719
</ItemGroup>

src/APIM_ARMTemplate/apimtemplate/Creator/ExampleFiles/YAMLConfigs/valid.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
version: 0.0.1
2-
apimServiceName: testing-grounds
2+
apimServiceName: testapimlucas
33
apiVersionSet:
44
displayName: myAPIVersionSet
55
description: a description
66
versioningScheme: Query
77
versionQueryName: versionQuery
88
versionHeaderName: versionHeader
99
api:
10+
name: ijustdidthis
1011
openApiSpec: C:\Users\lucashh\Desktop\Projects\APIM-ARM\azure-api-management-devops-example\src\APIM_ARMTemplate\apimtemplate\Creator\ExampleFiles\OpenApiSpecs\swaggerPetstore.json
1112
policy: C:\Users\lucashh\Desktop\Projects\APIM-ARM\azure-api-management-devops-example\src\APIM_ARMTemplate\apimtemplate\Creator\ExampleFiles\XMLPolicies\apiPolicyHeaders.xml
1213
suffix: conf
@@ -19,6 +20,6 @@ api:
1920
policy: C:\Users\lucashh\Desktop\Projects\APIM-ARM\azure-api-management-devops-example\src\APIM_ARMTemplate\apimtemplate\Creator\ExampleFiles\XMLPolicies\operationRateLimit.xml
2021
deletePet:
2122
policy: C:\Users\lucashh\Desktop\Projects\APIM-ARM\azure-api-management-devops-example\src\APIM_ARMTemplate\apimtemplate\Creator\ExampleFiles\XMLPolicies\operationRateLimit.xml
22-
products: starter, plantinum
23+
products: starter, platinum
2324
outputLocation: C:\Users\lucashh\Desktop\Projects\APIM-ARM\GeneratedTemplates
2425
linked: false
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using Microsoft.OpenApi.Models;
2+
using Microsoft.OpenApi.Readers;
3+
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Linq;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Net.Http;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
13+
{
14+
public class OpenAPISpecReader
15+
{
16+
public OpenApiDocument ConvertToOpenAPISpec(string json)
17+
{
18+
OpenApiStringReader reader = new OpenApiStringReader();
19+
OpenApiDocument doc = reader.Read(json, out var diagnostic);
20+
return doc;
21+
}
22+
23+
public OpenApiDocument ConvertLocalFileToOpenAPISpec(string jsonFile)
24+
{
25+
JObject jObject = JObject.Parse(File.ReadAllText(jsonFile));
26+
string json = JsonConvert.SerializeObject(jObject);
27+
OpenApiDocument document = ConvertToOpenAPISpec(json);
28+
return document;
29+
}
30+
31+
public async Task<OpenApiDocument> ConvertRemoteURLToOpenAPISpecAsync(Uri uriResult)
32+
{
33+
HttpClient client = new HttpClient();
34+
HttpResponseMessage response = await client.GetAsync(uriResult);
35+
if (response.IsSuccessStatusCode)
36+
{
37+
string json = await response.Content.ReadAsStringAsync();
38+
OpenApiDocument document = ConvertToOpenAPISpec(json);
39+
return document;
40+
}
41+
else
42+
{
43+
return new OpenApiDocument();
44+
}
45+
}
46+
47+
public async Task<OpenApiDocument> ConvertOpenAPISpecToDoc(string openApiSpecFileLocation)
48+
{
49+
Uri uriResult;
50+
bool isUrl = Uri.TryCreate(openApiSpecFileLocation, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
51+
if (isUrl)
52+
{
53+
return await this.ConvertRemoteURLToOpenAPISpecAsync(uriResult);
54+
} else
55+
{
56+
return this.ConvertLocalFileToOpenAPISpec(openApiSpecFileLocation);
57+
}
58+
}
59+
}
60+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class CreatorConfig
2121

2222
public class APIConfig
2323
{
24+
public string name { get; set; }
2425
// openApiSpec file location (local or url)
2526
public string openApiSpec { get; set; }
2627
// policy file location (local or url)

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading.Tasks;
55
using System.Linq;
66
using Newtonsoft.Json;
7+
using Microsoft.OpenApi.Models;
78

89
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
910
{
@@ -25,18 +26,21 @@ public APITemplateCreator(FileReader fileReader, TemplateCreator templateCreator
2526
public async Task<Template> CreateAPITemplateAsync(CreatorConfig creatorConfig)
2627
{
2728
Template apiTemplate = this.templateCreator.CreateEmptyTemplate();
29+
OpenAPISpecReader openAPISpecReader = new OpenAPISpecReader();
30+
OpenApiDocument doc = await openAPISpecReader.ConvertOpenAPISpecToDoc(creatorConfig.api.openApiSpec);
2831

2932
// add parameters
3033
apiTemplate.parameters = new Dictionary<string, TemplateParameterProperties>
3134
{
3235
{ "ApimServiceName", new TemplateParameterProperties(){ type = "string" } }
3336
};
3437

35-
string[] dependsOnSubsequentAPI = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), 'subsequent-api')]" };
38+
string apiName = creatorConfig.api.name;
39+
string[] dependsOnSubsequentAPI = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{apiName}')]" };
3640

3741
List<TemplateResource> resources = new List<TemplateResource>();
3842
// create api resource with properties
39-
APITemplateResource initialAPITemplateResource = this.CreateInitialAPITemplateResource(creatorConfig);
43+
APITemplateResource initialAPITemplateResource = this.CreateInitialAPITemplateResource(creatorConfig, doc);
4044
APITemplateResource subsequentAPITemplateResource = await this.CreateSubsequentAPITemplateResourceAsync(creatorConfig);
4145
PolicyTemplateResource apiPolicyResource = await this.policyTemplateCreator.CreateAPIPolicyTemplateResourceAsync(creatorConfig, dependsOnSubsequentAPI);
4246
List<PolicyTemplateResource> operationPolicyResources = await this.policyTemplateCreator.CreateOperationPolicyTemplateResourcesAsync(creatorConfig, dependsOnSubsequentAPI);
@@ -51,12 +55,12 @@ public async Task<Template> CreateAPITemplateAsync(CreatorConfig creatorConfig)
5155
return apiTemplate;
5256
}
5357

54-
public APITemplateResource CreateInitialAPITemplateResource(CreatorConfig creatorConfig)
58+
public APITemplateResource CreateInitialAPITemplateResource(CreatorConfig creatorConfig, OpenApiDocument doc)
5559
{
5660
// create api resource with properties
5761
APITemplateResource apiTemplateResource = new APITemplateResource()
5862
{
59-
name = "[concat(parameters('ApimServiceName'), '/initial-api')]",
63+
name = $"[concat(parameters('ApimServiceName'), '/{creatorConfig.api.name}-initial')]",
6064
type = "Microsoft.ApiManagement/service/apis",
6165
apiVersion = "2018-06-01-preview",
6266
properties = new APITemplateProperties()
@@ -68,13 +72,13 @@ public APITemplateResource CreateInitialAPITemplateResource(CreatorConfig creato
6872
apiVersionDescription = creatorConfig.api.apiVersionDescription,
6973
authenticationSettings = creatorConfig.api.authenticationSettings,
7074
path = creatorConfig.api.suffix,
71-
displayName = "api",
72-
protocols = new string[] {"http" }
75+
displayName = creatorConfig.api.name,
76+
protocols = this.CreateProtocols(doc)
7377
},
7478
// if the template is not linked the depends on for the apiVersionSet needs to be inlined here
7579
dependsOn = creatorConfig.linked == true ? new string[] { } : new string[] { $"[resourceId('Microsoft.ApiManagement/service/api-version-sets', parameters('ApimServiceName'), 'versionset')]" }
7680
};
77-
if(creatorConfig.apiVersionSet != null)
81+
if (creatorConfig.apiVersionSet != null)
7882
{
7983
apiTemplateResource.properties.apiVersionSetId = "[resourceId('Microsoft.ApiManagement/service/api-version-sets', parameters('ApimServiceName'), 'versionset')]";
8084
}
@@ -86,7 +90,7 @@ public async Task<APITemplateResource> CreateSubsequentAPITemplateResourceAsync(
8690
// create api resource with properties
8791
// used to escape characters in json file
8892
object deserializedFileContents = JsonConvert.DeserializeObject<object>(await this.fileReader.RetrieveLocationContentsAsync(creatorConfig.api.openApiSpec));
89-
string subsequentAPIName = "[concat(parameters('ApimServiceName'), '/subsequent-api')]";
93+
string subsequentAPIName = $"[concat(parameters('ApimServiceName'), '/{creatorConfig.api.name}')]";
9094
string subsequentAPIType = "Microsoft.ApiManagement/service/apis";
9195
APITemplateResource apiTemplateResource = new APITemplateResource()
9296
{
@@ -100,9 +104,19 @@ public async Task<APITemplateResource> CreateSubsequentAPITemplateResourceAsync(
100104
// supplied via optional arguments
101105
path = creatorConfig.api.suffix
102106
},
103-
dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), 'initial-api')]" }
104-
};
107+
dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('ApimServiceName'), '{creatorConfig.api.name}-initial')]" }
108+
};
105109
return apiTemplateResource;
106110
}
111+
112+
public string[] CreateProtocols(OpenApiDocument doc)
113+
{
114+
List<string> protocols = new List<string>();
115+
foreach (OpenApiServer server in doc.Servers)
116+
{
117+
protocols.Add(server.Url.Split(":")[0]);
118+
}
119+
return protocols.ToArray();
120+
}
107121
}
108122
}

src/APIM_ARMTemplate/apimtemplate/Creator/TemplateCreators/PolicyTemplateCreator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async Task<PolicyTemplateResource> CreateAPIPolicyTemplateResourceAsync(C
2121
// create policy resource with properties
2222
PolicyTemplateResource policyTemplateResource = new PolicyTemplateResource()
2323
{
24-
name = "[concat(parameters('ApimServiceName'), '/subsequent-api/policy')]",
24+
name = $"[concat(parameters('ApimServiceName'), '/{creatorConfig.api.name}/policy')]",
2525
type = "Microsoft.ApiManagement/service/apis/policies",
2626
apiVersion = "2018-06-01-preview",
2727
properties = new PolicyTemplateProperties()
@@ -34,12 +34,12 @@ public async Task<PolicyTemplateResource> CreateAPIPolicyTemplateResourceAsync(C
3434
return policyTemplateResource;
3535
}
3636

37-
public async Task<PolicyTemplateResource> CreateOperationPolicyTemplateResourceAsync(KeyValuePair<string, OperationsConfig> policyPair, string[] dependsOn)
37+
public async Task<PolicyTemplateResource> CreateOperationPolicyTemplateResourceAsync(KeyValuePair<string, OperationsConfig> policyPair, string apiName, string[] dependsOn)
3838
{
3939
// create policy resource with properties
4040
PolicyTemplateResource policyTemplateResource = new PolicyTemplateResource()
4141
{
42-
name = $"[concat(parameters('ApimServiceName'), '/subsequent-api/{policyPair.Key}/policy')]",
42+
name = $"[concat(parameters('ApimServiceName'), '/{apiName}/{policyPair.Key}/policy')]",
4343
type = "Microsoft.ApiManagement/service/apis/operations/policies",
4444
apiVersion = "2018-06-01-preview",
4545
properties = new PolicyTemplateProperties()
@@ -57,7 +57,7 @@ public async Task<List<PolicyTemplateResource>> CreateOperationPolicyTemplateRes
5757
List<PolicyTemplateResource> policyTemplateResources = new List<PolicyTemplateResource>();
5858
foreach (KeyValuePair<string, OperationsConfig> pair in creatorConfig.api.operations)
5959
{
60-
policyTemplateResources.Add(await this.CreateOperationPolicyTemplateResourceAsync(pair, dependsOn));
60+
policyTemplateResources.Add(await this.CreateOperationPolicyTemplateResourceAsync(pair, creatorConfig.api.name, dependsOn));
6161
}
6262
return policyTemplateResources;
6363
}

src/APIM_ARMTemplate/apimtemplate/Creator/TemplateCreators/ProductAPITemplateCreator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
1010
{
1111
public class ProductAPITemplateCreator
1212
{
13-
public ProductAPITemplateResource CreateProductAPITemplateResource(string productID, string[] dependsOn)
13+
public ProductAPITemplateResource CreateProductAPITemplateResource(string productID, string apiName, string[] dependsOn)
1414
{
1515
// create products/apis resource with properties
1616
ProductAPITemplateResource productAPITemplateResource = new ProductAPITemplateResource()
1717
{
18-
name = $"[concat(parameters('ApimServiceName'), '/{productID}/subsequent-api')]",
18+
name = $"[concat(parameters('ApimServiceName'), '/{productID}/{apiName}')]",
1919
type = "Microsoft.ApiManagement/service/products/apis",
2020
apiVersion = "2018-06-01-preview",
2121
properties = new ProductAPITemplateProperties(),
@@ -30,7 +30,7 @@ public List<ProductAPITemplateResource> CreateProductAPITemplateResources(Creato
3030
string[] productIDs = creatorConfig.api.products.Split(", ");
3131
foreach (string productID in productIDs)
3232
{
33-
ProductAPITemplateResource productAPITemplate = this.CreateProductAPITemplateResource(productID, dependsOn);
33+
ProductAPITemplateResource productAPITemplate = this.CreateProductAPITemplateResource(productID, creatorConfig.api.name, dependsOn);
3434
productAPITemplates.Add(productAPITemplate);
3535
}
3636
return productAPITemplates;

src/APIM_ARMTemplate/apimtemplate/apimtemplate.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
<ItemGroup>
99
<PackageReference Include="Colors.Net" Version="1.1.0" />
1010
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.2.5" />
11+
<PackageReference Include="Microsoft.OpenApi" Version="1.1.2" />
12+
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.1.2" />
1113
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
1214
<PackageReference Include="YamlDotNet" Version="5.3.0" />
1315
</ItemGroup>

0 commit comments

Comments
 (0)