2
2
using McMaster . Extensions . CommandLineUtils ;
3
3
using Microsoft . OpenApi . Models ;
4
4
using Colors . Net ;
5
+ using System ;
6
+ using Newtonsoft . Json ;
5
7
6
8
namespace Microsoft . Azure . Management . ApiManagement . ArmTemplates
7
9
{
@@ -21,22 +23,20 @@ public CreateCommand()
21
23
CommandOption linked = this . Option ( "--linked <linked>" , "Creates linked templates versus inlined into a single file" , CommandOptionType . SingleValue ) ;
22
24
CommandOption path = this . Option ( "--path <path>" , "API path" , CommandOptionType . SingleValue ) ;
23
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 ) ;
24
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 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 ) ;
25
30
CommandOption apiVersionSetId = this . Option ( "--apiVersionSetId <apiVersionSetId>" , "API version set id" , CommandOptionType . SingleValue ) ;
26
31
CommandOption productIds = this . Option ( "--productIds <productIds>" , "Product ids to associate the API with" , CommandOptionType . MultipleValue ) ;
27
-
32
+
28
33
this . HelpOption ( ) ;
29
34
30
35
this . OnExecute ( async ( ) =>
31
36
{
32
37
if ( ( openAPISpecFile . HasValue ( ) || openAPISpecURL . HasValue ( ) ) && outputLocation . HasValue ( ) )
33
38
{
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
40
40
// convert command options to CLIArguments class
41
41
CLICreatorArguments cliArguments = new CLICreatorArguments ( )
42
42
{
@@ -48,32 +48,50 @@ public CreateCommand()
48
48
linked = linked . HasValue ( ) ,
49
49
path = path . Value ( ) ,
50
50
apiRevision = apiRevision . Value ( ) ,
51
+ apiRevisionDescription = apiRevisionDescription . Value ( ) ,
51
52
apiVersion = apiVersion . Value ( ) ,
53
+ apiVersionDescription = apiVersionDescription . Value ( ) ,
54
+ apiVersionSet = apiVersionSet . Value ( ) ,
52
55
apiVersionSetId = apiVersionSetId . Value ( ) ,
53
56
productIds = productIds . Values
54
57
} ;
55
58
56
- // create OpenApiDocument
57
- OpenApiDocument doc = new OpenApiDocument ( ) ;
58
- if ( cliArguments . openAPISpecFile != null )
59
+ if ( apiVersionSet . HasValue ( ) && AttemptAPIVersionSetConversion ( cliArguments ) != null )
59
60
{
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 ;
61
64
}
62
65
else
63
66
{
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 ( ) ;
66
72
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
+ }
71
83
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
+ }
77
95
}
78
96
else if ( ! outputLocation . HasValue ( ) )
79
97
{
@@ -86,5 +104,18 @@ public CreateCommand()
86
104
return 0 ;
87
105
} ) ;
88
106
}
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
+ }
89
120
}
90
121
}
0 commit comments