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

Commit 5662e2f

Browse files
committed
port open api spec code into cli project
1 parent 803df72 commit 5662e2f

18 files changed

+939
-8
lines changed

src/apimtemplate/Commands/Create.cs

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System;
1+
using System.Collections.Generic;
22
using McMaster.Extensions.CommandLineUtils;
3-
using McMaster.Extensions.CommandLineUtils.HelpText;
3+
using Microsoft.OpenApi.Models;
44
using Colors.Net;
55

66
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
@@ -12,15 +12,77 @@ public CreateCommand()
1212
this.Name = Constants.CreateName;
1313
this.Description = Constants.CreateDescription;
1414

15-
var filename = this.Option("--filename <filename>", "File name", CommandOptionType.SingleValue).IsRequired();
15+
// list command options
16+
CommandOption openAPISpecFile = this.Option("--openAPISpecFile <openAPISpecFile>", "Open API spec file location", CommandOptionType.SingleValue);
17+
CommandOption openAPISpecURL = this.Option("--openAPISpecURL <openAPISpecURL>", "Open API spec remote url", CommandOptionType.SingleValue);
18+
CommandOption outputLocation = this.Option("--outputLocation <outputLocation>", "Template output location", CommandOptionType.SingleValue);
19+
CommandOption xmlPolicyFile = this.Option("--xmlPolicyFile <xmlPolicyFile>", "XML policy file location", CommandOptionType.SingleValue);
20+
CommandOption xmlPolicyURL = this.Option("--xmlPolicyURL <xmlPolicyURL>", "XML policy remote url", CommandOptionType.SingleValue);
21+
CommandOption linked = this.Option("--linked <linked>", "Creates linked templates versus inlined into a single file", CommandOptionType.SingleValue);
22+
CommandOption path = this.Option("--path <path>", "API path", CommandOptionType.SingleValue);
23+
CommandOption apiRevision = this.Option("--apiRevision <apiRevision>", "API revision", CommandOptionType.SingleValue);
24+
CommandOption apiVersion = this.Option("--apiVersion <apiVersion>", "API version", CommandOptionType.SingleValue);
25+
CommandOption apiVersionSetId = this.Option("--apiVersionSetId <apiVersionSetId>", "API version set id", CommandOptionType.SingleValue);
26+
CommandOption productIds = this.Option("--productIds <productIds>", "Product ids to associate the API with", CommandOptionType.MultipleValue);
27+
28+
// convert command options to CLIArguments class
29+
CLICreatorArguments cliArguments = new CLICreatorArguments()
30+
{
31+
openAPISpecFile = openAPISpecFile.Value(),
32+
openAPISpecURL = openAPISpecURL.Value(),
33+
outputLocation = outputLocation.Value(),
34+
xmlPolicyFile = xmlPolicyFile.Value(),
35+
xmlPolicyURL = xmlPolicyURL.Value(),
36+
linked = linked.HasValue(),
37+
path = path.Value(),
38+
apiRevision = apiRevision.Value(),
39+
apiVersion = apiVersion.Value(),
40+
apiVersionSetId = apiVersionSetId.Value(),
41+
productIds = productIds.Values
42+
};
43+
1644
this.HelpOption();
1745

18-
this.OnExecute(() =>
46+
this.OnExecute(async () =>
1947
{
20-
if (filename.HasValue())
21-
Console.WriteLine($"Create command executed with filename {filename.Value()}");
22-
else
23-
ColoredConsole.Error.WriteLine("No file name passed in");
48+
if ((openAPISpecFile.HasValue() || openAPISpecURL.HasValue()) && outputLocation.HasValue())
49+
{
50+
// initialize helper classes
51+
OpenAPISpecReader openAPISpecReader = new OpenAPISpecReader();
52+
ARMTemplateWriter armTemplateWriter = new ARMTemplateWriter();
53+
APITemplateCreator apiTemplateCreator = new APITemplateCreator();
54+
TagTemplateCreator tagTemplateCreator = new TagTemplateCreator();
55+
56+
// create OpenApiDocument
57+
OpenApiDocument doc = new OpenApiDocument();
58+
if (cliArguments.openAPISpecFile != null)
59+
{
60+
doc = openAPISpecReader.ConvertLocalFileToOpenAPISpec(cliArguments.openAPISpecFile);
61+
}
62+
else
63+
{
64+
doc = await openAPISpecReader.ConvertRemoteURLToOpenAPISpecAsync(cliArguments.openAPISpecURL);
65+
}
66+
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);
71+
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");
77+
}
78+
else if (!outputLocation.HasValue())
79+
{
80+
ColoredConsole.Error.WriteLine("Output location is required");
81+
}
82+
else if (!(openAPISpecFile.HasValue() || openAPISpecURL.HasValue()))
83+
{
84+
ColoredConsole.Error.WriteLine("Open API spec file or remote url is required");
85+
};
2486
return 0;
2587
});
2688
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 apiRevision { get; set; }
18+
public string apiVersionSetId { get; set; }
19+
public List<string> productIds { get; set; }
20+
}
21+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
6+
{
7+
public class APITemplate
8+
{
9+
public string name { get; set; }
10+
public string type { get; set; }
11+
public string apiVersion { get; set; }
12+
public APITemplateProperties properties { get; set; }
13+
public APITemplateResource[] resources { get; set; }
14+
}
15+
16+
public class APITemplateProperties
17+
{
18+
public string description { get; set; }
19+
public APITemplateAuthenticationSettings authenticationSettings { get; set; }
20+
public APITemplateSubscriptionKeyParameterNames subscriptionKeyParameterNames { get; set; }
21+
public string type { get; set; }
22+
public string apiRevision { get; set; }
23+
public string apiVersion { get; set; }
24+
public string apiRevisionDescription { get; set; }
25+
public string apiVersionDescription { get; set; }
26+
public string apiVersionSetId { get; set; }
27+
public bool subscriptionRequired { get; set; }
28+
public string displayName { get; set; }
29+
public string serviceUrl { get; set; }
30+
public string path { get; set; }
31+
public string[] protocols { get; set; }
32+
public APITemplateVersionSet apiVersionSet { get; set; }
33+
public string contentValue { get; set; }
34+
public string contentFormat { get; set; }
35+
public APITemplateWSDLSelector wsdlSelector { get; set; }
36+
public string apiType { get; set; }
37+
}
38+
39+
public class APITemplateAuthenticationSettings
40+
{
41+
public APITemplateOAuth2 oAuth2 { get; set; }
42+
public APITemplateOpenID openid { get; set; }
43+
public bool subscriptionKeyRequired { get; set; }
44+
}
45+
46+
public class APITemplateSubscriptionKeyParameterNames
47+
{
48+
public string header { get; set; }
49+
public string query { get; set; }
50+
}
51+
52+
public class APITemplateVersionSet
53+
{
54+
public string id { get; set; }
55+
public string description { get; set; }
56+
public string versioningScheme { get; set; }
57+
public string versionQueryName { get; set; }
58+
public string versionHeaderName { get; set; }
59+
}
60+
61+
public class APITemplateWSDLSelector
62+
{
63+
public string wsdlServiceName { get; set; }
64+
public string wsdlEndpointName { get; set; }
65+
}
66+
67+
public class APITemplateOAuth2
68+
{
69+
public string authorizationServerId { get; set; }
70+
public string scope { get; set; }
71+
}
72+
73+
public class APITemplateOpenID
74+
{
75+
public string openidProviderId { get; set; }
76+
public string[] bearerTokenSendingMethods { get; set; }
77+
}
78+
79+
public abstract class APITemplateResource { }
80+
81+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
6+
{
7+
public class DiagnosticTemplate : APITemplateResource
8+
{
9+
public string name { get; set; }
10+
public string type { get; set; }
11+
public string apiVersion { get; set; }
12+
public DiagnosticTemplateProperties properties { get; set; }
13+
}
14+
15+
public class DiagnosticTemplateProperties
16+
{
17+
public string alwaysLog { get; set; }
18+
public string loggerId { get; set; }
19+
public DiagnosticTemplateSampling sampling { get; set; }
20+
public DiagnosticTemplateFrontendBackend frontend { get; set; }
21+
public DiagnosticTemplateFrontendBackend backend { get; set; }
22+
public bool enableHttpCorrelationHeaders { get; set; }
23+
}
24+
25+
public class DiagnosticTemplateSampling
26+
{
27+
public string samplingType { get; set; }
28+
public double percentage { get; set; }
29+
}
30+
31+
public class DiagnosticTemplateFrontendBackend
32+
{
33+
public DiagnosticTemplateRequestResponse request { get; set; }
34+
public DiagnosticTemplateRequestResponse response { get; set; }
35+
}
36+
37+
public class DiagnosticTemplateRequestResponse
38+
{
39+
public string[] headers { get; set; }
40+
public DiagnosticTemplateRequestResponseBody body { get; set; }
41+
}
42+
43+
public class DiagnosticTemplateRequestResponseBody
44+
{
45+
public int bytes { get; set; }
46+
}
47+
48+
49+
50+
51+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
6+
{
7+
public class IssueTemplate : APITemplateResource
8+
{
9+
public string name { get; set; }
10+
public string type { get; set; }
11+
public string apiVersion { get; set; }
12+
public IssueTemplateProperties properties { get; set; }
13+
public IssueTemplateResource[] resources { get; set; }
14+
}
15+
16+
public class IssueTemplateProperties
17+
{
18+
public string title { get; set; }
19+
public string description { get; set; }
20+
public string createdDate { get; set; }
21+
public string state { get; set; }
22+
public string userId { get; set; }
23+
public string apiId { get; set; }
24+
}
25+
26+
public class IssueTemplateAttachment : IssueTemplateResource
27+
{
28+
public string name { get; set; }
29+
public string type { get; set; }
30+
public string apiVersion { get; set; }
31+
public IssueTemplateAttachmentProperties properties { get; set; }
32+
}
33+
34+
public class IssueTemplateAttachmentProperties
35+
{
36+
public string title { get; set; }
37+
public string contentFormat { get; set; }
38+
public string content { get; set; }
39+
}
40+
41+
public class IssueTemplateComment : IssueTemplateResource
42+
{
43+
public string name { get; set; }
44+
public string type { get; set; }
45+
public string apiVersion { get; set; }
46+
public IssueTemplateCommentProperties properties { get; set; }
47+
}
48+
49+
public class IssueTemplateCommentProperties
50+
{
51+
public string text { get; set; }
52+
public string createdDate { get; set; }
53+
public string userId { get; set; }
54+
}
55+
56+
public abstract class IssueTemplateResource { }
57+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
6+
{
7+
public class OperationTemplate : APITemplateResource
8+
{
9+
public string name { get; set; }
10+
public string type { get; set; }
11+
public string apiVersion { get; set; }
12+
public OperationTemplateProperties properties { get; set; }
13+
public PolicyTemplate[] resources { get; set; }
14+
}
15+
16+
public class OperationTemplateProperties
17+
{
18+
public OperationTemplateParameter[] templateParameters { get; set; }
19+
public string description { get; set; }
20+
public OperationTemplateRequest request { get; set; }
21+
public OperationsTemplateResponse[] responses { get; set; }
22+
public string policies { get; set; }
23+
public string displayName { get; set; }
24+
public string method { get; set; }
25+
public string urlTemplate { get; set; }
26+
}
27+
28+
public class OperationTemplateParameter
29+
{
30+
public string name { get; set; }
31+
public string description { get; set; }
32+
public string type { get; set; }
33+
public string defaultValue { get; set; }
34+
public bool required { get; set; }
35+
public string[] values { get; set; }
36+
}
37+
38+
public class OperationTemplateRequest
39+
{
40+
public string description { get; set; }
41+
public OperationTemplateParameter[] queryParameters { get; set; }
42+
public OperationTemplateParameter[] headers { get; set; }
43+
public OperationTemplateRepresentation[] representations { get; set; }
44+
}
45+
46+
public class OperationTemplateRepresentation
47+
{
48+
public string contentType { get; set; }
49+
public string sample { get; set; }
50+
public string schemaId { get; set; }
51+
public string typeName { get; set; }
52+
public OperationTemplateParameter[] formParameters { get; set; }
53+
}
54+
55+
public class OperationsTemplateResponse
56+
{
57+
public string statusCode { get; set; }
58+
public string description { get; set; }
59+
public OperationTemplateParameter[] headers { get; set; }
60+
public OperationTemplateRepresentation[] representations { get; set; }
61+
}
62+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
6+
{
7+
8+
public class PolicyTemplate : APITemplateResource
9+
{
10+
public string name { get; set; }
11+
public string type { get; set; }
12+
public string apiVersion { get; set; }
13+
public PolicyTemplateProperties properties { get; set; }
14+
}
15+
16+
public class PolicyTemplateProperties
17+
{
18+
public string policyContent { get; set; }
19+
public string contentFormat { get; set; }
20+
}
21+
}

0 commit comments

Comments
 (0)