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
+ }
0 commit comments