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

Commit 6f3dcc5

Browse files
committed
write all apis to files, master points to all files in linked case
1 parent 525f848 commit 6f3dcc5

File tree

7 files changed

+78
-65
lines changed

7 files changed

+78
-65
lines changed

src/APIM_ARMTemplate/apimtemplate/Commands/Create.cs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common;
55
using System.Collections.Generic;
6+
using System.Linq;
67

78
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create
89
{
@@ -31,6 +32,7 @@ public CreateCommand()
3132

3233
// initialize helper classes
3334
FileWriter fileWriter = new FileWriter();
35+
FileNameGenerator fileNameGenerator = new FileNameGenerator();
3436
TemplateCreator templateCreator = new TemplateCreator();
3537
APIVersionSetTemplateCreator apiVersionSetTemplateCreator = new APIVersionSetTemplateCreator(templateCreator);
3638
ProductAPITemplateCreator productAPITemplateCreator = new ProductAPITemplateCreator();
@@ -41,48 +43,51 @@ public CreateCommand()
4143

4244
// create templates from provided configuration
4345
Template apiVersionSetTemplate = creatorConfig.apiVersionSet != null ? apiVersionSetTemplateCreator.CreateAPIVersionSetTemplate(creatorConfig) : null;
44-
List<Template> initialAPITemplates = new List<Template>();
45-
List<Template> subsequentAPITemplates = new List<Template>();
46-
List<LinkedMasterTemplateAPIInformation> apiInformation = new List<LinkedMasterTemplateAPIInformation>();
46+
// store name and full template on each api necessary to build unlinked templates
47+
Dictionary<string, Template> initialAPITemplates = new Dictionary<string, Template>();
48+
Dictionary<string, Template> subsequentAPITemplates = new Dictionary<string, Template>();
49+
// store name and whether the api will depend on the version set template each api necessary to build linked templates
50+
Dictionary<string, bool> apiInformation = new Dictionary<string, bool>();
4751

4852
foreach (APIConfig api in creatorConfig.apis)
4953
{
5054
Template initialAPITemplate = await apiTemplateCreator.CreateInitialAPITemplateAsync(creatorConfig, api);
5155
Template subsequentAPITemplate = apiTemplateCreator.CreateSubsequentAPITemplate(api);
52-
initialAPITemplates.Add(initialAPITemplate);
53-
subsequentAPITemplates.Add(subsequentAPITemplate);
54-
apiInformation.Add(new LinkedMasterTemplateAPIInformation() { name = api.name, hasAPIVersionSetId = api.apiVersionSetId != null });
56+
initialAPITemplates.Add(api.name, initialAPITemplate);
57+
subsequentAPITemplates.Add(api.name, subsequentAPITemplate);
58+
apiInformation.Add(api.name, api.apiVersionSetId != null);
5559
}
5660

57-
CreatorFileNames creatorFileNames = fileWriter.GenerateCreatorLinkedFileNames(creatorConfig);
61+
CreatorFileNames creatorFileNames = fileNameGenerator.GenerateCreatorLinkedFileNames(creatorConfig);
62+
Template masterTemplateParameters = masterTemplateCreator.CreateMasterTemplateParameterValues(creatorConfig);
5863
if (creatorConfig.linked == true)
5964
{
6065
// create linked master template
61-
Template masterTemplate = masterTemplateCreator.CreateLinkedMasterTemplate(apiVersionSetTemplate, apiInformation, creatorFileNames);
66+
Template masterTemplate = masterTemplateCreator.CreateLinkedMasterTemplate(apiVersionSetTemplate, apiInformation, creatorFileNames, fileNameGenerator);
67+
// write parameters to outputLocation
68+
fileWriter.WriteJSONToFile(masterTemplateParameters, String.Concat(creatorConfig.outputLocation, creatorFileNames.linkedParameters));
6269
// write linked specific template to outputLocationc
6370
fileWriter.WriteJSONToFile(masterTemplate, String.Concat(creatorConfig.outputLocation, creatorFileNames.linkedMaster));
6471
}
6572
else
6673
{
67-
// write unlinked specific templates to outputLocationc
68-
foreach (Template initialAPITemplate in initialAPITemplates)
69-
{
70-
fileWriter.WriteJSONToFile(initialAPITemplate, String.Concat(creatorConfig.outputLocation, creatorFileNames.initialAPI));
71-
}
72-
foreach (Template subsequentAPITemplate in subsequentAPITemplates)
73-
{
74-
fileWriter.WriteJSONToFile(subsequentAPITemplate, String.Concat(creatorConfig.outputLocation, creatorFileNames.subsequentAPI));
75-
}
76-
74+
// write parameters to outputLocation
75+
fileWriter.WriteJSONToFile(masterTemplateParameters, String.Concat(creatorConfig.outputLocation, creatorFileNames.unlinkedParameters));
76+
}
77+
// write templates to outputLocation
78+
foreach (KeyValuePair<string, Template> initialAPITemplatePair in initialAPITemplates)
79+
{
80+
string initialAPIFileName = fileNameGenerator.GenerateAPIFileName(initialAPITemplatePair.Key, true);
81+
fileWriter.WriteJSONToFile(initialAPITemplatePair.Value, String.Concat(creatorConfig.outputLocation, initialAPIFileName));
82+
}
83+
foreach (KeyValuePair<string, Template> subsequentAPITemplatePair in subsequentAPITemplates)
84+
{
85+
string subsequentAPIFileName = fileNameGenerator.GenerateAPIFileName(subsequentAPITemplatePair.Key, false);
86+
fileWriter.WriteJSONToFile(subsequentAPITemplatePair.Value, String.Concat(creatorConfig.outputLocation, subsequentAPIFileName));
7787
}
78-
// write parameters to outputLocation
79-
Template masterTemplateParameters = masterTemplateCreator.CreateMasterTemplateParameterValues(creatorConfig);
80-
fileWriter.WriteJSONToFile(masterTemplateParameters, String.Concat(creatorConfig.outputLocation, creatorFileNames.masterParameters));
81-
82-
// write common templates to outputLocationc
8388
if (apiVersionSetTemplate != null)
8489
{
85-
fileWriter.WriteJSONToFile(apiVersionSetTemplate, String.Concat(creatorConfig.outputLocation, creatorFileNames.apiVersionSet));
90+
fileWriter.WriteJSONToFile(apiVersionSetTemplate, String.Concat(creatorConfig.outputLocation, creatorFileNames.apiVersionSets));
8691
}
8792
ColoredConsole.WriteLine("Templates written to output location");
8893
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Newtonsoft.Json;
2+
using System.IO;
3+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create;
4+
5+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common
6+
{
7+
public class FileNameGenerator
8+
{
9+
public CreatorFileNames GenerateCreatorLinkedFileNames(CreatorConfig creatorConfig)
10+
{
11+
// generate useable object with file names for consistency throughout project
12+
return new CreatorFileNames()
13+
{
14+
apiVersionSets = @"/version-sets.template.json",
15+
products = @"/products.template.json",
16+
loggers = @"/loggers.template.json",
17+
linkedMaster = @"/master.template.json",
18+
linkedParameters = @"/master.parameters.json",
19+
unlinkedParameters = @"/parameters.json"
20+
};
21+
}
22+
23+
public string GenerateAPIFileName(string apiName, bool isInitialAPI)
24+
{
25+
return isInitialAPI == true ? $@"/{apiName}-initial.api.template.json" : $@"/{apiName}-subsequent.api.template.json";
26+
}
27+
}
28+
}

src/APIM_ARMTemplate/apimtemplate/Common/FileHandlers/FileWriter.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,5 @@ public void WriteJSONToFile(object template, string location)
1717
});
1818
File.WriteAllText(location, jsonString);
1919
}
20-
21-
public CreatorFileNames GenerateCreatorLinkedFileNames(CreatorConfig creatorConfig)
22-
{
23-
// generate useable object with file names for consistency throughout project
24-
return new CreatorFileNames()
25-
{
26-
apiVersionSet = @"/version-sets.template.json",
27-
linkedMaster = @"/master.template.json",
28-
masterParameters = @"/master.parameters.json",
29-
};
30-
}
3120
}
3221
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create
33
{
44
public class CreatorFileNames
55
{
6-
public string apiVersionSet { get; set; }
7-
public string initialAPI { get; set; }
8-
public string subsequentAPI { get; set; }
6+
public string apiVersionSets { get; set; }
7+
public string products { get; set; }
8+
public string loggers { get; set; }
99
// linked property outputs 1 master template
1010
public string linkedMaster { get; set; }
11-
// unlined property outputs 2 master templates
12-
public string unlinkedMasterOne { get; set; }
13-
public string unlinkedMasterTwo { get; set; }
14-
public string masterParameters { get; set; }
11+
public string linkedParameters { get; set; }
12+
public string unlinkedParameters { get; set; }
1513
}
1614
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ public async Task<APITemplateResource> CreateInitialAPITemplateResourceAsync(Cre
104104
// if the template is not linked the depends on for the apiVersionSet needs to be inlined here
105105
dependsOn = new string[] { }
106106
};
107-
// if the template is linked and a version set was created, the initial api depends on it
108-
if (creatorConfig.linked == false && api.apiVersionSetId != null)
109-
{
110-
apiTemplateResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/api-version-sets', parameters('ApimServiceName'), '{api.apiVersionSetId}')]" };
111-
}
112107
// set the version set id
113108
if (api.apiVersionSetId != null)
114109
{

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ public MasterTemplateCreator(TemplateCreator templateCreator)
1313
}
1414

1515
public Template CreateLinkedMasterTemplate(Template apiVersionSetTemplate,
16-
List<LinkedMasterTemplateAPIInformation> apiInformation,
17-
CreatorFileNames creatorFileNames)
16+
Dictionary<string, bool> apiInformation,
17+
CreatorFileNames creatorFileNames,
18+
FileNameGenerator fileNameGenerator)
1819
{
1920
// create empty template
2021
Template masterTemplate = this.templateCreator.CreateEmptyTemplate();
@@ -28,22 +29,25 @@ public Template CreateLinkedMasterTemplate(Template apiVersionSetTemplate,
2829
// apiVersionSet
2930
if (apiVersionSetTemplate != null)
3031
{
31-
string apiVersionSetUri = $"[concat(parameters('LinkedTemplatesBaseUrl'), '{creatorFileNames.apiVersionSet}')]";
32+
string apiVersionSetUri = $"[concat(parameters('LinkedTemplatesBaseUrl'), '{creatorFileNames.apiVersionSets}')]";
3233
resources.Add(this.CreateLinkedMasterTemplateResource("versionSetTemplate", apiVersionSetUri, new string[] { }));
3334
}
3435

35-
//api
36-
foreach(LinkedMasterTemplateAPIInformation apiInfo in apiInformation)
36+
// api info stores api name as the key and whether the api depends on the version set template as the value
37+
foreach(KeyValuePair<string, bool> apiInfo in apiInformation)
3738
{
38-
string initialAPIFileName = $@"/{apiInfo.name}-initial.api.template.json";
39+
string initialAPIDeploymentResourceName = $"{apiInfo.Key}-InitialAPITemplate";
40+
string subsequentAPIDeploymentResourceName = $"{apiInfo.Key}-SubsequentAPITemplate";
41+
42+
string initialAPIFileName = fileNameGenerator.GenerateAPIFileName(apiInfo.Key, true);
3943
string initialAPIUri = $"[concat(parameters('LinkedTemplatesBaseUrl'), '{initialAPIFileName}')]";
40-
string[] initialAPIDependsOn = apiVersionSetTemplate != null && apiInfo.hasAPIVersionSetId == true ? new string[] { "[resourceId('Microsoft.Resources/deployments', 'versionSetTemplate')]" } : new string[] { };
41-
resources.Add(this.CreateLinkedMasterTemplateResource("initialAPITemplate", initialAPIUri, initialAPIDependsOn));
44+
string[] initialAPIDependsOn = apiVersionSetTemplate != null && apiInfo.Value == true ? new string[] { "[resourceId('Microsoft.Resources/deployments', 'versionSetTemplate')]" } : new string[] { };
45+
resources.Add(this.CreateLinkedMasterTemplateResource(initialAPIDeploymentResourceName, initialAPIUri, initialAPIDependsOn));
4246

43-
string subsequentAPIFileName = $@"/{apiInfo.name}-subsequent.api.template.json";
47+
string subsequentAPIFileName = fileNameGenerator.GenerateAPIFileName(apiInfo.Key, false);
4448
string subsequentAPIUri = $"[concat(parameters('LinkedTemplatesBaseUrl'), '{subsequentAPIFileName}')]";
45-
string[] subsequentAPIDependsOn = new string[] { "[resourceId('Microsoft.Resources/deployments', 'initialAPITemplate')]" };
46-
resources.Add(this.CreateLinkedMasterTemplateResource("subsequentAPITemplate", subsequentAPIUri, subsequentAPIDependsOn));
49+
string[] subsequentAPIDependsOn = new string[] { $"[resourceId('Microsoft.Resources/deployments', '{initialAPIDeploymentResourceName}')]" };
50+
resources.Add(this.CreateLinkedMasterTemplateResource(subsequentAPIDeploymentResourceName, subsequentAPIUri, subsequentAPIDependsOn));
4751
}
4852

4953
masterTemplate.resources = resources.ToArray();
@@ -129,11 +133,4 @@ public Template CreateMasterTemplateParameterValues(CreatorConfig creatorConfig)
129133
return masterTemplate;
130134
}
131135
}
132-
133-
public class LinkedMasterTemplateAPIInformation
134-
{
135-
136-
public string name { get; set; }
137-
public bool hasAPIVersionSetId { get; set; }
138-
}
139136
}

src/APIM_ARMTemplate/apimtemplate/apimtemplate.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
<ItemGroup>
3939
<Folder Include="Creator\ExampleFiles\OpenAPISpecs\" />
40+
<Folder Include="Creator\Utilities\" />
4041
</ItemGroup>
4142

4243
</Project>

0 commit comments

Comments
 (0)