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

Commit 1378d53

Browse files
authored
Merge pull request #45 from Azure/lucashuet93-openapispec
Lucashuet93 openapispec
2 parents 44d8d9e + 19160fb commit 1378d53

32 files changed

+1371
-170
lines changed

.gitattributes

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
###############################################################################
2+
# Set default behavior to automatically normalize line endings.
3+
###############################################################################
4+
* text=auto
5+
6+
###############################################################################
7+
# Set default behavior for command prompt diff.
8+
#
9+
# This is need for earlier builds of msysgit that does not have it on by
10+
# default for csharp files.
11+
# Note: This is only used by command line
12+
###############################################################################
13+
#*.cs diff=csharp
14+
15+
###############################################################################
16+
# Set the merge driver for project and solution files
17+
#
18+
# Merging from the command prompt will add diff markers to the files if there
19+
# are conflicts (Merging from VS is not affected by the settings below, in VS
20+
# the diff markers are never inserted). Diff markers may cause the following
21+
# file extensions to fail to load in VS. An alternative would be to treat
22+
# these files as binary and thus will always conflict and require user
23+
# intervention with every merge. To do so, just uncomment the entries below
24+
###############################################################################
25+
#*.sln merge=binary
26+
#*.csproj merge=binary
27+
#*.vbproj merge=binary
28+
#*.vcxproj merge=binary
29+
#*.vcproj merge=binary
30+
#*.dbproj merge=binary
31+
#*.fsproj merge=binary
32+
#*.lsproj merge=binary
33+
#*.wixproj merge=binary
34+
#*.modelproj merge=binary
35+
#*.sqlproj merge=binary
36+
#*.wwaproj merge=binary
37+
38+
###############################################################################
39+
# behavior for image files
40+
#
41+
# image files are treated as binary by default.
42+
###############################################################################
43+
#*.jpg binary
44+
#*.png binary
45+
#*.gif binary
46+
47+
###############################################################################
48+
# diff behavior for common document formats
49+
#
50+
# Convert binary document formats to text before diffing them. This feature
51+
# is only available from the command line. Turn it on by uncommenting the
52+
# entries below.
53+
###############################################################################
54+
#*.doc diff=astextplain
55+
#*.DOC diff=astextplain
56+
#*.docx diff=astextplain
57+
#*.DOCX diff=astextplain
58+
#*.dot diff=astextplain
59+
#*.DOT diff=astextplain
60+
#*.pdf diff=astextplain
61+
#*.PDF diff=astextplain
62+
#*.rtf diff=astextplain
63+
#*.RTF diff=astextplain
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27004.2010
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "apimtemplate", "apimtemplate\apimtemplate.csproj", "{882899E4-B3FE-49BA-9B4E-61B3496B37DE}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "apimtemplate.test", "apimtemplate.test\apimtemplate.test.csproj", "{9B0EF41E-7730-4FA6-BD9A-A1024CF80E58}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{882899E4-B3FE-49BA-9B4E-61B3496B37DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{882899E4-B3FE-49BA-9B4E-61B3496B37DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{882899E4-B3FE-49BA-9B4E-61B3496B37DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{882899E4-B3FE-49BA-9B4E-61B3496B37DE}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{9B0EF41E-7730-4FA6-BD9A-A1024CF80E58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{9B0EF41E-7730-4FA6-BD9A-A1024CF80E58}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{9B0EF41E-7730-4FA6-BD9A-A1024CF80E58}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{9B0EF41E-7730-4FA6-BD9A-A1024CF80E58}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {C6DDDC25-6EF1-4B74-941E-6C3C34D9178F}
30+
EndGlobalSection
31+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
using System;
2-
using Xunit;
3-
using McMaster.Extensions.CommandLineUtils;
4-
5-
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
6-
{
7-
public class CreateTests
8-
{
9-
10-
[Fact]
11-
public void ShouldFailWithUnknownCommand()
12-
{
13-
14-
var createCommand = new CreateCommand();
15-
16-
var ex = Assert.ThrowsAny<CommandParsingException>(() => createCommand.Execute("test"));
17-
//Console.WriteLine(ex.Message);
18-
Assert.Contains("Unrecognized command or argument 'test'", ex.Message);
19-
}
20-
}
21-
}
1+
using System;
2+
using Xunit;
3+
using McMaster.Extensions.CommandLineUtils;
4+
5+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
6+
{
7+
public class CreateTests
8+
{
9+
10+
[Fact]
11+
public void ShouldFailWithUnknownCommand()
12+
{
13+
14+
var createCommand = new CreateCommand();
15+
16+
var ex = Assert.ThrowsAny<CommandParsingException>(() => createCommand.Execute("test"));
17+
//Console.WriteLine(ex.Message);
18+
Assert.Contains("Unrecognized command or argument 'test'", ex.Message);
19+
}
20+
}
21+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.1</TargetFramework>
5-
6-
<IsPackable>false</IsPackable>
7-
</PropertyGroup>
8-
9-
<ItemGroup>
10-
<ProjectReference Include="..\..\src\apimtemplate\apimtemplate.csproj" />
11-
</ItemGroup>
12-
13-
<ItemGroup>
14-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
15-
<PackageReference Include="xunit" Version="2.4.0" />
16-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
17-
</ItemGroup>
18-
19-
</Project>
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\apimtemplate\apimtemplate.csproj" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
15+
<PackageReference Include="xunit" Version="2.4.0" />
16+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
using System.Collections.Generic;
2+
using McMaster.Extensions.CommandLineUtils;
3+
using Microsoft.OpenApi.Models;
4+
using Colors.Net;
5+
using System;
6+
using Newtonsoft.Json;
7+
8+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
9+
{
10+
public class CreateCommand : CommandLineApplication
11+
{
12+
public CreateCommand()
13+
{
14+
this.Name = Constants.CreateName;
15+
this.Description = Constants.CreateDescription;
16+
17+
// list command options
18+
CommandOption openAPISpecFile = this.Option("--openAPISpecFile <openAPISpecFile>", "Open API spec file location", CommandOptionType.SingleValue);
19+
CommandOption openAPISpecURL = this.Option("--openAPISpecURL <openAPISpecURL>", "Open API spec remote url", CommandOptionType.SingleValue);
20+
CommandOption outputLocation = this.Option("--outputLocation <outputLocation>", "Template output location", CommandOptionType.SingleValue).IsRequired();
21+
CommandOption xmlPolicyFile = this.Option("--xmlPolicyFile <xmlPolicyFile>", "XML policy file location", CommandOptionType.SingleValue);
22+
CommandOption xmlPolicyURL = this.Option("--xmlPolicyURL <xmlPolicyURL>", "XML policy remote url", CommandOptionType.SingleValue);
23+
CommandOption linked = this.Option("--linked <linked>", "Creates linked templates versus inlined into a single file", CommandOptionType.SingleValue);
24+
CommandOption path = this.Option("--path <path>", "API path", CommandOptionType.SingleValue);
25+
CommandOption apiRevision = this.Option("--apiRevision <apiRevision>", "API revision", CommandOptionType.SingleValue);
26+
CommandOption apiRevisionDescription = this.Option("--apiRevisionDescription <apiVersionSetId>", "Description of the API revision", CommandOptionType.SingleValue);
27+
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 apiVersionSetFile = this.Option("--apiVersionSetFile <apiVersionSetId>", "YAML file with 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);
30+
CommandOption authenticationSettingsFile = this.Option("--authenticationSettingsFile <apiVersionSetId>", "YAML file with object that follows the AuthenticationSettingsContract object schema - https://docs.microsoft.com/en-us/azure/templates/microsoft.apimanagement/2018-06-01-preview/service/apis#AuthenticationSettingsContract", CommandOptionType.SingleValue);
31+
CommandOption apiVersionSetId = this.Option("--apiVersionSetId <apiVersionSetId>", "API version set id", CommandOptionType.SingleValue);
32+
CommandOption productIds = this.Option("--productIds <productIds>", "Product ids to associate the API with", CommandOptionType.MultipleValue);
33+
34+
this.HelpOption();
35+
36+
this.OnExecute(async () =>
37+
{
38+
// ensure required parameters have been passed in
39+
if ((openAPISpecFile.HasValue() || openAPISpecURL.HasValue()) && outputLocation.HasValue())
40+
{
41+
// convert command options to CLIArguments class
42+
CLICreatorArguments cliArguments = new CLICreatorArguments()
43+
{
44+
openAPISpecFile = openAPISpecFile.Value(),
45+
openAPISpecURL = openAPISpecURL.Value(),
46+
outputLocation = outputLocation.Value(),
47+
xmlPolicyFile = xmlPolicyFile.Value(),
48+
xmlPolicyURL = xmlPolicyURL.Value(),
49+
linked = linked.HasValue(),
50+
path = path.Value(),
51+
apiRevision = apiRevision.Value(),
52+
apiRevisionDescription = apiRevisionDescription.Value(),
53+
apiVersion = apiVersion.Value(),
54+
apiVersionDescription = apiVersionDescription.Value(),
55+
apiVersionSetFile = apiVersionSetFile.Value(),
56+
apiVersionSetId = apiVersionSetId.Value(),
57+
authenticationSettingsFile = authenticationSettingsFile.Value(),
58+
productIds = productIds.Values
59+
};
60+
61+
if (apiVersionSetFile.HasValue() && AttemptAPIVersionSetConversion(cliArguments) != null)
62+
{
63+
// unable to convert version set argument into object, would cause failure down the line
64+
ColoredConsole.Error.WriteLine("Incorrect apiVersionSet object structure");
65+
return 0;
66+
}
67+
else if (authenticationSettingsFile.HasValue() && AttemptAuthenticationSettingsConversion(cliArguments) != null)
68+
{
69+
// unable to convert version set argument into object, would cause failure down the line
70+
ColoredConsole.Error.WriteLine("Incorrect authenticationSettings object structure");
71+
return 0;
72+
}
73+
else
74+
{
75+
// required parameters have been supplied and versionSet has correct object structure
76+
77+
// initialize helper classes
78+
OpenAPISpecReader openAPISpecReader = new OpenAPISpecReader();
79+
ARMTemplateWriter armTemplateWriter = new ARMTemplateWriter();
80+
APITemplateCreator apiTemplateCreator = new APITemplateCreator();
81+
TagTemplateCreator tagTemplateCreator = new TagTemplateCreator();
82+
83+
// create OpenApiDocument from Open API spec file
84+
OpenApiDocument doc = new OpenApiDocument();
85+
if (cliArguments.openAPISpecFile != null)
86+
{
87+
doc = openAPISpecReader.ConvertLocalFileToOpenAPISpec(cliArguments.openAPISpecFile);
88+
}
89+
else
90+
{
91+
doc = await openAPISpecReader.ConvertRemoteURLToOpenAPISpecAsync(cliArguments.openAPISpecURL);
92+
}
93+
94+
// create templates from OpenApiDocument
95+
APITemplate apiTemplate = await apiTemplateCreator.CreateAPITemplateAsync(doc, cliArguments);
96+
List<TagTemplate> tagTemplates = tagTemplateCreator.CreateTagTemplates(doc);
97+
List<TagDescriptionTemplate> tagDescriptionTemplates = tagTemplateCreator.CreateTagDescriptionTemplates(doc);
98+
99+
// write templates to outputLocation
100+
armTemplateWriter.WriteAPITemplateToFile(apiTemplate, cliArguments.outputLocation);
101+
armTemplateWriter.WriteTagTemplatesToFile(tagTemplates, cliArguments.outputLocation);
102+
armTemplateWriter.WriteTagDescriptionTemplatesToFile(tagDescriptionTemplates, cliArguments.outputLocation);
103+
ColoredConsole.WriteLine("Templates written to output location");
104+
}
105+
}
106+
else if (!outputLocation.HasValue())
107+
{
108+
ColoredConsole.Error.WriteLine("Output location is required");
109+
}
110+
else if (!(openAPISpecFile.HasValue() || openAPISpecURL.HasValue()))
111+
{
112+
ColoredConsole.Error.WriteLine("Open API spec file or remote url is required");
113+
};
114+
return 0;
115+
});
116+
}
117+
118+
public Exception AttemptAPIVersionSetConversion(CLICreatorArguments cliArguments)
119+
{
120+
try
121+
{
122+
YAMLReader yamlReader = new YAMLReader();
123+
APITemplateVersionSet versionSet = yamlReader.ConvertYAMLFileToAPIVersionSet(cliArguments.apiVersionSetFile);
124+
return null;
125+
}
126+
catch (Exception ex)
127+
{
128+
return ex;
129+
}
130+
}
131+
132+
public Exception AttemptAuthenticationSettingsConversion(CLICreatorArguments cliArguments)
133+
{
134+
try
135+
{
136+
YAMLReader yamlReader = new YAMLReader();
137+
APITemplateAuthenticationSettings authenticationSettings = yamlReader.ConvertYAMLFileToAuthenticationSettings(cliArguments.authenticationSettingsFile);
138+
return null;
139+
}
140+
catch (Exception ex)
141+
{
142+
return ex;
143+
}
144+
}
145+
}
146+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
6+
{
7+
public class CLICreatorArguments
8+
{
9+
public string openAPISpecFile { get; set; }
10+
public string openAPISpecURL { get; set; }
11+
public string xmlPolicyFile { get; set; }
12+
public string xmlPolicyURL { get; set; }
13+
public string outputLocation { get; set; }
14+
public bool linked { get; set; }
15+
public string path { get; set; }
16+
public string apiVersion { get; set; }
17+
public string apiVersionDescription { get; set; }
18+
public string apiVersionSetFile { get; set; }
19+
public string apiVersionSetId { get; set; }
20+
public string apiRevision { get; set; }
21+
public string apiRevisionDescription { get; set; }
22+
public string authenticationSettingsFile { get; set; }
23+
public List<string> productIds { get; set; }
24+
}
25+
}

0 commit comments

Comments
 (0)