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

Commit b3399a6

Browse files
committed
add remaining api properties as cli arguments, error handling
1 parent cd8b72b commit b3399a6

File tree

5 files changed

+81
-29
lines changed

5 files changed

+81
-29
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using Xunit;
3+
using McMaster.Extensions.CommandLineUtils;
4+
5+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
6+
{
7+
public class APITemplateCreatorTests
8+
{
9+
10+
}
11+
}

src/APIM_ARMTemplate/apimtemplate/Commands/Create.cs

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using McMaster.Extensions.CommandLineUtils;
33
using Microsoft.OpenApi.Models;
44
using Colors.Net;
5+
using System;
6+
using Newtonsoft.Json;
57

68
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
79
{
@@ -21,22 +23,20 @@ public CreateCommand()
2123
CommandOption linked = this.Option("--linked <linked>", "Creates linked templates versus inlined into a single file", CommandOptionType.SingleValue);
2224
CommandOption path = this.Option("--path <path>", "API path", CommandOptionType.SingleValue);
2325
CommandOption apiRevision = this.Option("--apiRevision <apiRevision>", "API revision", CommandOptionType.SingleValue);
26+
CommandOption apiRevisionDescription = this.Option("--apiRevisionDescription <apiVersionSetId>", "Description of the API revision", CommandOptionType.SingleValue);
2427
CommandOption apiVersion = this.Option("--apiVersion <apiVersion>", "API version", CommandOptionType.SingleValue);
28+
CommandOption apiVersionDescription = this.Option("--apiVersionDescription <apiVersionSetId>", "Description of the API version", CommandOptionType.SingleValue);
29+
CommandOption apiVersionSet = this.Option("--apiVersionSet <apiVersionSetId>", "Serialized JSON object that follows the ApiVersionSetContractDetails object schema - https://docs.microsoft.com/en-us/azure/templates/microsoft.apimanagement/2018-06-01-preview/service/apis#ApiVersionSetContractDetails", CommandOptionType.SingleValue);
2530
CommandOption apiVersionSetId = this.Option("--apiVersionSetId <apiVersionSetId>", "API version set id", CommandOptionType.SingleValue);
2631
CommandOption productIds = this.Option("--productIds <productIds>", "Product ids to associate the API with", CommandOptionType.MultipleValue);
27-
32+
2833
this.HelpOption();
2934

3035
this.OnExecute(async () =>
3136
{
3237
if ((openAPISpecFile.HasValue() || openAPISpecURL.HasValue()) && outputLocation.HasValue())
3338
{
34-
// initialize helper classes
35-
OpenAPISpecReader openAPISpecReader = new OpenAPISpecReader();
36-
ARMTemplateWriter armTemplateWriter = new ARMTemplateWriter();
37-
APITemplateCreator apiTemplateCreator = new APITemplateCreator();
38-
TagTemplateCreator tagTemplateCreator = new TagTemplateCreator();
39-
39+
// required parameters have been passed in
4040
// convert command options to CLIArguments class
4141
CLICreatorArguments cliArguments = new CLICreatorArguments()
4242
{
@@ -48,32 +48,50 @@ public CreateCommand()
4848
linked = linked.HasValue(),
4949
path = path.Value(),
5050
apiRevision = apiRevision.Value(),
51+
apiRevisionDescription = apiRevisionDescription.Value(),
5152
apiVersion = apiVersion.Value(),
53+
apiVersionDescription = apiVersionDescription.Value(),
54+
apiVersionSet = apiVersionSet.Value(),
5255
apiVersionSetId = apiVersionSetId.Value(),
5356
productIds = productIds.Values
5457
};
5558

56-
// create OpenApiDocument
57-
OpenApiDocument doc = new OpenApiDocument();
58-
if (cliArguments.openAPISpecFile != null)
59+
if (apiVersionSet.HasValue() && AttemptAPIVersionSetConversion(cliArguments) != null)
5960
{
60-
doc = openAPISpecReader.ConvertLocalFileToOpenAPISpec(cliArguments.openAPISpecFile);
61+
// unable to convert version set argument into object
62+
ColoredConsole.Error.WriteLine("Incorrect API Version Set object structure");
63+
return 0;
6164
}
6265
else
6366
{
64-
doc = await openAPISpecReader.ConvertRemoteURLToOpenAPISpecAsync(cliArguments.openAPISpecURL);
65-
}
67+
// initialize helper classes
68+
OpenAPISpecReader openAPISpecReader = new OpenAPISpecReader();
69+
ARMTemplateWriter armTemplateWriter = new ARMTemplateWriter();
70+
APITemplateCreator apiTemplateCreator = new APITemplateCreator();
71+
TagTemplateCreator tagTemplateCreator = new TagTemplateCreator();
6672

67-
// create templates from OpenApiDocument
68-
APITemplate apiTemplate = await apiTemplateCreator.CreateAPITemplateAsync(doc, cliArguments);
69-
List<TagTemplate> tagTemplates = tagTemplateCreator.CreateTagTemplates(doc);
70-
List<TagDescriptionTemplate> tagDescriptionTemplates = tagTemplateCreator.CreateTagDescriptionTemplates(doc);
73+
// create OpenApiDocument
74+
OpenApiDocument doc = new OpenApiDocument();
75+
if (cliArguments.openAPISpecFile != null)
76+
{
77+
doc = openAPISpecReader.ConvertLocalFileToOpenAPISpec(cliArguments.openAPISpecFile);
78+
}
79+
else
80+
{
81+
doc = await openAPISpecReader.ConvertRemoteURLToOpenAPISpecAsync(cliArguments.openAPISpecURL);
82+
}
7183

72-
// write templates to outputLocation
73-
armTemplateWriter.WriteAPITemplateToFile(apiTemplate, cliArguments.outputLocation);
74-
armTemplateWriter.WriteTagTemplatesToFile(tagTemplates, cliArguments.outputLocation);
75-
armTemplateWriter.WriteTagDescriptionTemplatesToFile(tagDescriptionTemplates, cliArguments.outputLocation);
76-
ColoredConsole.WriteLine("Templates written to output location");
84+
// create templates from OpenApiDocument
85+
APITemplate apiTemplate = await apiTemplateCreator.CreateAPITemplateAsync(doc, cliArguments);
86+
List<TagTemplate> tagTemplates = tagTemplateCreator.CreateTagTemplates(doc);
87+
List<TagDescriptionTemplate> tagDescriptionTemplates = tagTemplateCreator.CreateTagDescriptionTemplates(doc);
88+
89+
// write templates to outputLocation
90+
armTemplateWriter.WriteAPITemplateToFile(apiTemplate, cliArguments.outputLocation);
91+
armTemplateWriter.WriteTagTemplatesToFile(tagTemplates, cliArguments.outputLocation);
92+
armTemplateWriter.WriteTagDescriptionTemplatesToFile(tagDescriptionTemplates, cliArguments.outputLocation);
93+
ColoredConsole.WriteLine("Templates written to output location");
94+
}
7795
}
7896
else if (!outputLocation.HasValue())
7997
{
@@ -86,5 +104,18 @@ public CreateCommand()
86104
return 0;
87105
});
88106
}
107+
108+
public Exception AttemptAPIVersionSetConversion(CLICreatorArguments cliArguments)
109+
{
110+
try
111+
{
112+
JsonConvert.DeserializeObject<APITemplateVersionSet>(cliArguments.apiVersionSet);
113+
return null;
114+
}
115+
catch (Exception ex)
116+
{
117+
return ex;
118+
}
119+
}
89120
}
90121
}

src/APIM_ARMTemplate/apimtemplate/Common/CLIArguments.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ public class CLICreatorArguments
1414
public bool linked { get; set; }
1515
public string path { get; set; }
1616
public string apiVersion { get; set; }
17-
public string apiRevision { get; set; }
17+
public string apiVersionDescription { get; set; }
18+
public string apiVersionSet { get; set; }
1819
public string apiVersionSetId { get; set; }
20+
public string apiRevision { get; set; }
21+
public string apiRevisionDescription { get; set; }
1922
public List<string> productIds { get; set; }
2023
}
2124
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"apimtemplate": {
4+
"commandName": "Project",
5+
"commandLineArgs": "create --outputLocation=\"C:\\Users\\lucashh\\Desktop\\Projects\\APIM-ARM\\GeneratedTemplates\" --openAPISpecFile=\"C:\\Users\\lucashh\\Desktop\\Projects\\APIM-ARM\\ExampleSpecs\\tags.json\" --apiVersionSet=\"\"{\\\"id\\\":\\\"1\\\",\\\"description\\\":\\\"d\\\",\\\"versioningScheme\\\":\\\"s\\\",\\\"versionQueryName\\\":\\\"q\\\",\\\"versionHeaderName\\\":\\\"v\\\"}\"\""
6+
}
7+
}
8+
}

src/APIM_ARMTemplate/apimtemplate/TemplateCreators/APITemplateCreator.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Net.Http;
55
using System.Threading.Tasks;
66
using System.Linq;
7+
using Newtonsoft.Json;
78

89
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
910
{
@@ -31,16 +32,14 @@ public async Task<APITemplate> CreateAPITemplateAsync(OpenApiDocument doc, CLICr
3132
apiRevision = cliArguments.apiRevision ?? "",
3233
apiVersionSetId = cliArguments.apiVersionSetId ?? "",
3334
path = cliArguments.path ?? "",
35+
apiRevisionDescription = cliArguments.apiRevisionDescription ?? "",
36+
apiVersionDescription = cliArguments.apiVersionDescription ?? "",
37+
apiVersionSet = cliArguments.apiVersionSet != null ? JsonConvert.DeserializeObject<APITemplateVersionSet>(cliArguments.apiVersionSet) : null,
3438
authenticationSettings = CreateAuthenticationSettings(doc),
3539
// assumptions
3640
type = "http",
3741
apiType = "http",
38-
wsdlSelector = null,
39-
40-
// unfinished
41-
apiRevisionDescription = null,
42-
apiVersionDescription = null,
43-
apiVersionSet = null
42+
wsdlSelector = null
4443
}
4544
};
4645

0 commit comments

Comments
 (0)