4
4
using System . Threading . Tasks ;
5
5
using System . Linq ;
6
6
using Newtonsoft . Json ;
7
+ using Microsoft . OpenApi . Models ;
7
8
8
9
namespace Microsoft . Azure . Management . ApiManagement . ArmTemplates
9
10
{
@@ -25,18 +26,21 @@ public APITemplateCreator(FileReader fileReader, TemplateCreator templateCreator
25
26
public async Task < Template > CreateAPITemplateAsync ( CreatorConfig creatorConfig )
26
27
{
27
28
Template apiTemplate = this . templateCreator . CreateEmptyTemplate ( ) ;
29
+ OpenAPISpecReader openAPISpecReader = new OpenAPISpecReader ( ) ;
30
+ OpenApiDocument doc = await openAPISpecReader . ConvertOpenAPISpecToDoc ( creatorConfig . api . openApiSpec ) ;
28
31
29
32
// add parameters
30
33
apiTemplate . parameters = new Dictionary < string , TemplateParameterProperties >
31
34
{
32
35
{ "ApimServiceName" , new TemplateParameterProperties ( ) { type = "string" } }
33
36
} ;
34
37
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 } ')]" } ;
36
40
37
41
List < TemplateResource > resources = new List < TemplateResource > ( ) ;
38
42
// create api resource with properties
39
- APITemplateResource initialAPITemplateResource = this . CreateInitialAPITemplateResource ( creatorConfig ) ;
43
+ APITemplateResource initialAPITemplateResource = this . CreateInitialAPITemplateResource ( creatorConfig , doc ) ;
40
44
APITemplateResource subsequentAPITemplateResource = await this . CreateSubsequentAPITemplateResourceAsync ( creatorConfig ) ;
41
45
PolicyTemplateResource apiPolicyResource = await this . policyTemplateCreator . CreateAPIPolicyTemplateResourceAsync ( creatorConfig , dependsOnSubsequentAPI ) ;
42
46
List < PolicyTemplateResource > operationPolicyResources = await this . policyTemplateCreator . CreateOperationPolicyTemplateResourcesAsync ( creatorConfig , dependsOnSubsequentAPI ) ;
@@ -51,12 +55,12 @@ public async Task<Template> CreateAPITemplateAsync(CreatorConfig creatorConfig)
51
55
return apiTemplate ;
52
56
}
53
57
54
- public APITemplateResource CreateInitialAPITemplateResource ( CreatorConfig creatorConfig )
58
+ public APITemplateResource CreateInitialAPITemplateResource ( CreatorConfig creatorConfig , OpenApiDocument doc )
55
59
{
56
60
// create api resource with properties
57
61
APITemplateResource apiTemplateResource = new APITemplateResource ( )
58
62
{
59
- name = "[concat(parameters('ApimServiceName'), '/initial- api')]" ,
63
+ name = $ "[concat(parameters('ApimServiceName'), '/{ creatorConfig . api . name } -initial ')]",
60
64
type = "Microsoft.ApiManagement/service/apis" ,
61
65
apiVersion = "2018-06-01-preview" ,
62
66
properties = new APITemplateProperties ( )
@@ -68,13 +72,13 @@ public APITemplateResource CreateInitialAPITemplateResource(CreatorConfig creato
68
72
apiVersionDescription = creatorConfig . api . apiVersionDescription ,
69
73
authenticationSettings = creatorConfig . api . authenticationSettings ,
70
74
path = creatorConfig . api . suffix ,
71
- displayName = " api" ,
72
- protocols = new string [ ] { "http" }
75
+ displayName = creatorConfig . api . name ,
76
+ protocols = this . CreateProtocols ( doc )
73
77
} ,
74
78
// if the template is not linked the depends on for the apiVersionSet needs to be inlined here
75
79
dependsOn = creatorConfig . linked == true ? new string [ ] { } : new string [ ] { $ "[resourceId('Microsoft.ApiManagement/service/api-version-sets', parameters('ApimServiceName'), 'versionset')]" }
76
80
} ;
77
- if ( creatorConfig . apiVersionSet != null )
81
+ if ( creatorConfig . apiVersionSet != null )
78
82
{
79
83
apiTemplateResource . properties . apiVersionSetId = "[resourceId('Microsoft.ApiManagement/service/api-version-sets', parameters('ApimServiceName'), 'versionset')]" ;
80
84
}
@@ -86,7 +90,7 @@ public async Task<APITemplateResource> CreateSubsequentAPITemplateResourceAsync(
86
90
// create api resource with properties
87
91
// used to escape characters in json file
88
92
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 } ')]";
90
94
string subsequentAPIType = "Microsoft.ApiManagement/service/apis" ;
91
95
APITemplateResource apiTemplateResource = new APITemplateResource ( )
92
96
{
@@ -100,9 +104,19 @@ public async Task<APITemplateResource> CreateSubsequentAPITemplateResourceAsync(
100
104
// supplied via optional arguments
101
105
path = creatorConfig . api . suffix
102
106
} ,
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
+ } ;
105
109
return apiTemplateResource ;
106
110
}
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
+ }
107
121
}
108
122
}
0 commit comments