1
1
using System ;
2
- using System . IO ;
3
- using System . Net ;
4
2
using McMaster . Extensions . CommandLineUtils ;
5
- using Colors . Net ;
6
- using Newtonsoft . Json . Linq ;
7
- using System . Linq ;
8
3
using Newtonsoft . Json ;
4
+ using System . Collections . Generic ;
9
5
10
- namespace Microsoft . Azure . Management . ApiManagement . ArmTemplates
6
+ namespace Microsoft . Azure . Management . ApiManagement . ArmTemplates . Extract
11
7
{
12
8
public class ExtractCommand : CommandLineApplication
13
9
{
@@ -20,89 +16,71 @@ public ExtractCommand()
20
16
var resourceGroupName = this . Option ( "--resourceGroup <resourceGroup>" , "Resource Group name" , CommandOptionType . SingleValue ) ;
21
17
22
18
this . HelpOption ( ) ;
23
-
24
- this . OnExecute ( ( ) =>
19
+
20
+ this . OnExecute ( async ( ) =>
25
21
{
26
22
if ( ! apiManagementName . HasValue ( ) ) throw new Exception ( "Missing parameter <apimname>." ) ;
27
23
if ( ! resourceGroupName . HasValue ( ) ) throw new Exception ( "Missing parameter <resourceGroup>." ) ;
28
24
29
- // TemplateCreator templateCreator = new TemplateCreator();
30
-
31
25
string resourceGroup = resourceGroupName . Values [ 0 ] . ToString ( ) ;
32
26
string apimname = apiManagementName . Values [ 0 ] . ToString ( ) ;
33
27
Api api = new Api ( ) ;
34
28
string apis = api . GetAPIs ( apimname , resourceGroup ) . Result ;
35
- JObject oApis = JObject . Parse ( apis ) ;
36
- int count = oApis [ "value" ] . Count < object > ( ) ;
37
-
38
- ConsoleColor lastColor = Console . ForegroundColor ;
39
- Console . ForegroundColor = ConsoleColor . Green ;
40
- Console . WriteLine ( "{0} API's found!" , count ) ;
41
- Console . ForegroundColor = lastColor ;
42
29
43
- DownloadFile ( "https://github.com/odaibert/apim_templates/blob/master/API.json" , "API.json" ) ;
30
+ ExtractedAPI extractedAPI = JsonConvert . DeserializeObject < ExtractedAPI > ( apis ) ;
31
+ Console . WriteLine ( "{0} API's found!" , extractedAPI . value . Count . ToString ( ) ) ;
44
32
45
- for ( int i = 0 ; i < count ; i ++ )
33
+ for ( int i = 0 ; i < extractedAPI . value . Count ; i ++ )
46
34
{
47
- Console . WriteLine ( oApis ) ;
48
- string apiname = ( string ) oApis [ "value" ] [ i ] [ "name" ] ;
49
- ColoredConsole . WriteLine ( apiname ) ;
50
-
51
- ColoredConsole . WriteLine ( api . GetAPIOperations ( apimname , resourceGroup , apiname ) . Result ) ;
35
+ APIConfig apiConfig = new APIConfig ( ) ;
36
+
37
+ CreatorConfig creatorConfig = new CreatorConfig
38
+ {
39
+ version = "1.0.0" ,
40
+ outputLocation = @"c:\\projs\\" ,
41
+ apimServiceName = apimname ,
42
+ api = apiConfig
43
+ } ;
44
+ creatorConfig . api . openApiSpec = null ;
45
+ creatorConfig . api . name = extractedAPI . value [ i ] . name ;
46
+ creatorConfig . api . apiVersion = extractedAPI . value [ i ] . properties . apiVersion ;
47
+ creatorConfig . api . apiVersionDescription = extractedAPI . value [ i ] . properties . apiVersionDescription ;
48
+ creatorConfig . api . suffix = extractedAPI . value [ i ] . properties . path ;
49
+ creatorConfig . linked = false ;
50
+
51
+ if ( extractedAPI . value [ i ] . properties . apiVersionSetId != null )
52
+ {
53
+ string APIVersionSetFull = extractedAPI . value [ i ] . properties . apiVersionSetId ;
54
+ string APIVersionSetId = APIVersionSetFull . Substring ( APIVersionSetFull . LastIndexOf ( '/' ) + 1 ) ;
55
+ APIVersionSetId = api . GetAPIVersionSet ( apimname , resourceGroup , APIVersionSetId ) . Result ;
56
+ APIVersionSetTemplateResource apiv = JsonConvert . DeserializeObject < APIVersionSetTemplateResource > ( APIVersionSetId ) ;
57
+
58
+ creatorConfig . apiVersionSet = apiv . properties ;
59
+ }
60
+
61
+ TemplateCreator templateCreator = new TemplateCreator ( ) ;
62
+ APIVersionSetTemplateCreator apiVersionSetTemplateCreator = new APIVersionSetTemplateCreator ( templateCreator ) ;
63
+ ProductAPITemplateCreator productAPITemplateCreator = new ProductAPITemplateCreator ( ) ;
64
+ //PolicyTemplateCreator policyTemplateCreator = new PolicyTemplateCreator(fileReader);
65
+ APITemplateCreator apiTemplateCreator = new APITemplateCreator ( templateCreator ) ;
66
+ //MasterTemplateCreator masterTemplateCreator = new MasterTemplateCreator(templateCreator);
67
+
68
+ // create templates from provided configuration
69
+ Template apiVersionSetTemplate = creatorConfig . apiVersionSet != null ? apiVersionSetTemplateCreator . CreateAPIVersionSetTemplate ( creatorConfig ) : null ;
70
+ Template apiTemplate = await apiTemplateCreator . CreateAPITemplateAsync ( creatorConfig ) ;
71
+
72
+ FileWriter fileWriter = new FileWriter ( ) ;
73
+ CreatorFileNames creatorFileNames = fileWriter . GenerateCreatorFileNames ( ) ;
74
+
75
+ Console . WriteLine ( "Writing API Version Set File for {0} API ..." , extractedAPI . value [ i ] . name ) ;
76
+ fileWriter . WriteJSONToFile ( apiVersionSetTemplate , String . Concat ( creatorConfig . outputLocation , extractedAPI . value [ i ] . name , "-" , creatorFileNames . apiVersionSet ) ) ;
77
+ Console . WriteLine ( "Writing API File for {0} API ..." , extractedAPI . value [ i ] . name ) ;
78
+ fileWriter . WriteJSONToFile ( apiTemplate , String . Concat ( creatorConfig . outputLocation , extractedAPI . value [ i ] . name , "-" , creatorFileNames . api ) ) ;
79
+
52
80
}
53
81
Console . ReadKey ( ) ;
54
-
55
82
return 0 ;
56
83
} ) ;
57
84
}
58
- private static string FormatJSON ( string json )
59
- {
60
- dynamic parsedJson = JsonConvert . DeserializeObject ( json ) ;
61
- return JsonConvert . SerializeObject ( parsedJson , Formatting . Indented ) ;
62
- }
63
- public static void DownloadFile ( string sourceURL , string destinationPath )
64
- {
65
- long fileSize = 0 ;
66
- int bufferSize = 1024 ;
67
- bufferSize *= 1000 ;
68
- long existLen = 0 ;
69
- if ( File . Exists ( destinationPath ) )
70
- {
71
- FileInfo destinationFileInfo = new FileInfo ( destinationPath ) ;
72
- existLen = destinationFileInfo . Length ;
73
- }
74
-
75
-
76
- FileStream saveFileStream ;
77
- if ( existLen > 0 )
78
- saveFileStream = new FileStream ( destinationPath ,
79
- FileMode . Append ,
80
- FileAccess . Write ,
81
- FileShare . ReadWrite ) ;
82
- else
83
- saveFileStream = new FileStream ( destinationPath ,
84
- FileMode . Create ,
85
- FileAccess . Write ,
86
- FileShare . ReadWrite ) ;
87
-
88
- HttpWebRequest httpReq ;
89
- HttpWebResponse httpRes ;
90
- httpReq = ( HttpWebRequest ) WebRequest . Create ( sourceURL ) ;
91
- httpReq . AddRange ( ( int ) existLen ) ;
92
- Stream resStream ;
93
- httpRes = ( HttpWebResponse ) httpReq . GetResponse ( ) ;
94
- resStream = httpRes . GetResponseStream ( ) ;
95
-
96
- fileSize = httpRes . ContentLength ;
97
-
98
- int byteSize ;
99
- byte [ ] downBuffer = new byte [ bufferSize ] ;
100
-
101
- while ( ( byteSize = resStream . Read ( downBuffer , 0 , downBuffer . Length ) ) > 0 )
102
- {
103
- saveFileStream . Write ( downBuffer , 0 , byteSize ) ;
104
- }
105
- }
106
-
107
85
}
108
86
}
0 commit comments