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

Commit 91511f1

Browse files
committed
write products to template files
1 parent 6f3dcc5 commit 91511f1

File tree

3 files changed

+100
-23
lines changed

3 files changed

+100
-23
lines changed

src/APIM_ARMTemplate/apimtemplate/Commands/Create.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,46 +35,47 @@ public CreateCommand()
3535
FileNameGenerator fileNameGenerator = new FileNameGenerator();
3636
TemplateCreator templateCreator = new TemplateCreator();
3737
APIVersionSetTemplateCreator apiVersionSetTemplateCreator = new APIVersionSetTemplateCreator(templateCreator);
38+
ProductTemplateCreator productTemplateCreator = new ProductTemplateCreator(templateCreator);
3839
ProductAPITemplateCreator productAPITemplateCreator = new ProductAPITemplateCreator();
3940
PolicyTemplateCreator policyTemplateCreator = new PolicyTemplateCreator(fileReader);
4041
DiagnosticTemplateCreator diagnosticTemplateCreator = new DiagnosticTemplateCreator();
4142
APITemplateCreator apiTemplateCreator = new APITemplateCreator(fileReader, templateCreator, policyTemplateCreator, productAPITemplateCreator, diagnosticTemplateCreator);
4243
MasterTemplateCreator masterTemplateCreator = new MasterTemplateCreator(templateCreator);
44+
CreatorFileNames creatorFileNames = fileNameGenerator.GenerateCreatorLinkedFileNames(creatorConfig);
4345

4446
// create templates from provided configuration
4547
Template apiVersionSetTemplate = creatorConfig.apiVersionSet != null ? apiVersionSetTemplateCreator.CreateAPIVersionSetTemplate(creatorConfig) : null;
48+
Template productsTemplate = creatorConfig.products != null ? productTemplateCreator.CreateProductTemplate(creatorConfig) : null;
4649
// store name and full template on each api necessary to build unlinked templates
4750
Dictionary<string, Template> initialAPITemplates = new Dictionary<string, Template>();
4851
Dictionary<string, Template> subsequentAPITemplates = new Dictionary<string, Template>();
4952
// 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>();
53+
List<LinkedMasterTemplateAPIInformation> apiInformation = new List<LinkedMasterTemplateAPIInformation>();
54+
// create parameters file
55+
Template masterTemplateParameters = masterTemplateCreator.CreateMasterTemplateParameterValues(creatorConfig);
5156

5257
foreach (APIConfig api in creatorConfig.apis)
5358
{
5459
Template initialAPITemplate = await apiTemplateCreator.CreateInitialAPITemplateAsync(creatorConfig, api);
5560
Template subsequentAPITemplate = apiTemplateCreator.CreateSubsequentAPITemplate(api);
5661
initialAPITemplates.Add(api.name, initialAPITemplate);
5762
subsequentAPITemplates.Add(api.name, subsequentAPITemplate);
58-
apiInformation.Add(api.name, api.apiVersionSetId != null);
63+
apiInformation.Add(new LinkedMasterTemplateAPIInformation()
64+
{
65+
name = api.name,
66+
dependsOnVersionSets = api.apiVersionSetId != null,
67+
dependsOnProducts = api.products != null,
68+
dependsOnLoggers = true
69+
});
5970
}
6071

61-
CreatorFileNames creatorFileNames = fileNameGenerator.GenerateCreatorLinkedFileNames(creatorConfig);
62-
Template masterTemplateParameters = masterTemplateCreator.CreateMasterTemplateParameterValues(creatorConfig);
72+
// write templates to outputLocation
6373
if (creatorConfig.linked == true)
6474
{
6575
// create linked master template
66-
Template masterTemplate = masterTemplateCreator.CreateLinkedMasterTemplate(apiVersionSetTemplate, apiInformation, creatorFileNames, fileNameGenerator);
67-
// write parameters to outputLocation
68-
fileWriter.WriteJSONToFile(masterTemplateParameters, String.Concat(creatorConfig.outputLocation, creatorFileNames.linkedParameters));
69-
// write linked specific template to outputLocationc
76+
Template masterTemplate = masterTemplateCreator.CreateLinkedMasterTemplate(apiVersionSetTemplate, productsTemplate, apiInformation, creatorFileNames, fileNameGenerator);
7077
fileWriter.WriteJSONToFile(masterTemplate, String.Concat(creatorConfig.outputLocation, creatorFileNames.linkedMaster));
7178
}
72-
else
73-
{
74-
// write parameters to outputLocation
75-
fileWriter.WriteJSONToFile(masterTemplateParameters, String.Concat(creatorConfig.outputLocation, creatorFileNames.unlinkedParameters));
76-
}
77-
// write templates to outputLocation
7879
foreach (KeyValuePair<string, Template> initialAPITemplatePair in initialAPITemplates)
7980
{
8081
string initialAPIFileName = fileNameGenerator.GenerateAPIFileName(initialAPITemplatePair.Key, true);
@@ -89,6 +90,12 @@ public CreateCommand()
8990
{
9091
fileWriter.WriteJSONToFile(apiVersionSetTemplate, String.Concat(creatorConfig.outputLocation, creatorFileNames.apiVersionSets));
9192
}
93+
if (productsTemplate != null)
94+
{
95+
fileWriter.WriteJSONToFile(productsTemplate, String.Concat(creatorConfig.outputLocation, creatorFileNames.products));
96+
}
97+
// write parameters to outputLocation
98+
fileWriter.WriteJSONToFile(masterTemplateParameters, String.Concat(creatorConfig.outputLocation, creatorConfig.linked == true ? creatorFileNames.linkedParameters : creatorFileNames.unlinkedParameters));
9299
ColoredConsole.WriteLine("Templates written to output location");
93100
}
94101
return 0;

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33

44
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create
55
{
6+
7+
public class LinkedMasterTemplateAPIInformation
8+
{
9+
public string name { get; set; }
10+
public bool dependsOnVersionSets { get; set; }
11+
public bool dependsOnProducts { get; set; }
12+
public bool dependsOnLoggers { get; set; }
13+
}
14+
615
public class MasterTemplateCreator
716
{
817
private TemplateCreator templateCreator;
@@ -13,7 +22,8 @@ public MasterTemplateCreator(TemplateCreator templateCreator)
1322
}
1423

1524
public Template CreateLinkedMasterTemplate(Template apiVersionSetTemplate,
16-
Dictionary<string, bool> apiInformation,
25+
Template productsTemplate,
26+
List<LinkedMasterTemplateAPIInformation> apiInformation,
1727
CreatorFileNames creatorFileNames,
1828
FileNameGenerator fileNameGenerator)
1929
{
@@ -33,18 +43,33 @@ public Template CreateLinkedMasterTemplate(Template apiVersionSetTemplate,
3343
resources.Add(this.CreateLinkedMasterTemplateResource("versionSetTemplate", apiVersionSetUri, new string[] { }));
3444
}
3545

46+
// product
47+
if (productsTemplate != null)
48+
{
49+
string productsUri = $"[concat(parameters('LinkedTemplatesBaseUrl'), '{creatorFileNames.products}')]";
50+
resources.Add(this.CreateLinkedMasterTemplateResource("productsTemplate", productsUri, new string[] { }));
51+
}
52+
3653
// 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)
54+
foreach (LinkedMasterTemplateAPIInformation apiInfo in apiInformation)
3855
{
39-
string initialAPIDeploymentResourceName = $"{apiInfo.Key}-InitialAPITemplate";
40-
string subsequentAPIDeploymentResourceName = $"{apiInfo.Key}-SubsequentAPITemplate";
56+
string initialAPIDeploymentResourceName = $"{apiInfo.name}-InitialAPITemplate";
57+
string subsequentAPIDeploymentResourceName = $"{apiInfo.name}-SubsequentAPITemplate";
4158

42-
string initialAPIFileName = fileNameGenerator.GenerateAPIFileName(apiInfo.Key, true);
59+
string initialAPIFileName = fileNameGenerator.GenerateAPIFileName(apiInfo.name, true);
4360
string initialAPIUri = $"[concat(parameters('LinkedTemplatesBaseUrl'), '{initialAPIFileName}')]";
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));
61+
List<string> initialAPIDependsOn = new List<string>();
62+
if (apiVersionSetTemplate != null && apiInfo.dependsOnVersionSets == true)
63+
{
64+
initialAPIDependsOn.Add("[resourceId('Microsoft.Resources/deployments', 'versionSetTemplate')]");
65+
}
66+
if (productsTemplate != null && apiInfo.dependsOnProducts == true)
67+
{
68+
initialAPIDependsOn.Add("[resourceId('Microsoft.Resources/deployments', 'productsTemplate')]");
69+
}
70+
resources.Add(this.CreateLinkedMasterTemplateResource(initialAPIDeploymentResourceName, initialAPIUri, initialAPIDependsOn.ToArray()));
4671

47-
string subsequentAPIFileName = fileNameGenerator.GenerateAPIFileName(apiInfo.Key, false);
72+
string subsequentAPIFileName = fileNameGenerator.GenerateAPIFileName(apiInfo.name, false);
4873
string subsequentAPIUri = $"[concat(parameters('LinkedTemplatesBaseUrl'), '{subsequentAPIFileName}')]";
4974
string[] subsequentAPIDependsOn = new string[] { $"[resourceId('Microsoft.Resources/deployments', '{initialAPIDeploymentResourceName}')]" };
5075
resources.Add(this.CreateLinkedMasterTemplateResource(subsequentAPIDeploymentResourceName, subsequentAPIUri, subsequentAPIDependsOn));
@@ -93,7 +118,7 @@ public Dictionary<string, TemplateParameterProperties> CreateMasterTemplateParam
93118
type = "string"
94119
};
95120
parameters.Add("ApimServiceName", apimServiceNameProperties);
96-
if(linked == true)
121+
if (linked == true)
97122
{
98123
TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
99124
{
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Collections.Generic;
2+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common;
3+
4+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Create
5+
{
6+
public class ProductTemplateCreator
7+
{
8+
private TemplateCreator templateCreator;
9+
10+
public ProductTemplateCreator(TemplateCreator templateCreator)
11+
{
12+
this.templateCreator = templateCreator;
13+
}
14+
15+
public Template CreateProductTemplate(CreatorConfig creatorConfig)
16+
{
17+
// create empty template
18+
Template productTemplate = this.templateCreator.CreateEmptyTemplate();
19+
20+
// add parameters
21+
productTemplate.parameters = new Dictionary<string, TemplateParameterProperties>
22+
{
23+
{ "ApimServiceName", new TemplateParameterProperties(){ type = "string" } }
24+
};
25+
26+
List<TemplateResource> resources = new List<TemplateResource>();
27+
foreach (ProductsTemplateProperties productTemplateProperties in creatorConfig.products)
28+
{
29+
// create product resource with properties
30+
ProductsTemplateResource productsTemplateResource = new ProductsTemplateResource()
31+
{
32+
name = $"[concat(parameters('ApimServiceName'), '/{productTemplateProperties.displayName}')]",
33+
type = ResourceTypeConstants.APIVersionSet,
34+
apiVersion = "2018-06-01-preview",
35+
properties = productTemplateProperties,
36+
dependsOn = new string[] { }
37+
};
38+
resources.Add(productsTemplateResource);
39+
}
40+
41+
productTemplate.resources = resources.ToArray();
42+
return productTemplate;
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)