1
- using System . Collections . Generic ;
2
1
using McMaster . Extensions . CommandLineUtils ;
3
- using Microsoft . OpenApi . Models ;
4
2
using Colors . Net ;
5
- using System ;
6
- using Newtonsoft . Json ;
7
3
8
4
namespace Microsoft . Azure . Management . ApiManagement . ArmTemplates
9
5
{
@@ -15,132 +11,54 @@ public CreateCommand()
15
11
this . Description = Constants . CreateDescription ;
16
12
17
13
// 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 ) ;
14
+ CommandOption configFile = this . Option ( "--configFile <configFile>" , "Config YAML file location" , CommandOptionType . SingleValue ) . IsRequired ( ) ;
33
15
34
16
this . HelpOption ( ) ;
35
17
36
18
this . OnExecute ( async ( ) =>
37
19
{
20
+ // convert config file to CreatorConfig class
21
+ YAMLReader yamlReader = new YAMLReader ( ) ;
22
+ CreatorConfig creatorConfig = yamlReader . ConvertConfigYAMLToCreatorConfig ( configFile . Value ( ) ) ;
23
+
38
24
// ensure required parameters have been passed in
39
- if ( ( openAPISpecFile . HasValue ( ) || openAPISpecURL . HasValue ( ) ) && outputLocation . HasValue ( ) )
25
+ if ( creatorConfig . outputLocation == null )
40
26
{
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
- }
27
+ throw new CommandParsingException ( this , "Output location is required" ) ;
105
28
}
106
- else if ( ! outputLocation . HasValue ( ) )
29
+ else if ( creatorConfig . version == null )
107
30
{
108
- ColoredConsole . Error . WriteLine ( "Output location is required") ;
31
+ throw new CommandParsingException ( this , "Version is required") ;
109
32
}
110
- else if ( ! ( openAPISpecFile . HasValue ( ) || openAPISpecURL . HasValue ( ) ) )
33
+ else if ( creatorConfig . api == null )
111
34
{
112
- ColoredConsole . Error . WriteLine ( "Open API spec file or remote url is required" ) ;
113
- } ;
114
- return 0 ;
115
- } ) ;
116
- }
35
+ throw new CommandParsingException ( this , "API configuration is required" ) ;
36
+ }
37
+ else if ( creatorConfig . api . openApiSpec == null )
38
+ {
39
+ throw new CommandParsingException ( this , "Open API Spec is required" ) ;
40
+ }
41
+ else if ( creatorConfig . api . suffix == null )
42
+ {
43
+ throw new CommandParsingException ( this , "API suffix is required" ) ;
44
+ }
45
+ else
46
+ {
47
+ // required parameters have been supplied
117
48
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
- }
49
+ // initialize helper classes
50
+ APITemplateCreator apiTemplateCreator = new APITemplateCreator ( ) ;
51
+ ARMTemplateWriter armTemplateWriter = new ARMTemplateWriter ( ) ;
131
52
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
- }
53
+ // create templates from provided configuration
54
+ APITemplate apiTemplate = await apiTemplateCreator . CreateAPITemplateAsync ( creatorConfig ) ;
55
+
56
+ // write templates to outputLocation
57
+ armTemplateWriter . WriteAPITemplateToFile ( apiTemplate , creatorConfig . outputLocation ) ;
58
+ ColoredConsole . WriteLine ( "Templates written to output location" ) ;
59
+ }
60
+ return 0 ;
61
+ } ) ;
144
62
}
145
63
}
146
64
}
0 commit comments