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

Commit 3e1d9b4

Browse files
committed
Commit before remove unnecessary files.
1 parent 4fbba3b commit 3e1d9b4

File tree

13 files changed

+206
-42
lines changed

13 files changed

+206
-42
lines changed

API.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"$schema":
3+
"https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
4+
"contentVersion": "1.0.0.0",
5+
"parameters": {
6+
"ApimServiceName": {
7+
"type": "string"
8+
},
9+
"repoBaseUrl":{
10+
"type": "string",
11+
"metadata": {
12+
"description": "Base URL of the repository"
13+
}
14+
}
15+
},
16+
"variables": {},
17+
"resources": [
18+
{
19+
"type": "#resources_ApiResourceType",
20+
"name": "[concat(parameters('#resources_ApimServiceName'), '#ApiName')]",
21+
"apiVersion": "#resources_apiVersion",
22+
"scale": null,
23+
"properties": {
24+
"displayName": "#resources_properties_displayName",
25+
"apiRevision": "#resources_properties_apiRevision",
26+
"description": "#resources_properties_description",
27+
"serviceUrl": "#resources_properties_serviceUrl",
28+
"path": "#resources_properties_path",
29+
"protocols": [
30+
"#resources_properties_protocols1"
31+
],
32+
"authenticationSettings": null,
33+
"subscriptionKeyParameterNames": null,
34+
"apiVersion": "#resources_properties_apiVersion",
35+
"apiVersionSetId": "[concat(resourceId('#resources_properties_ServiceResourceType', parameters('ApimServiceName')), '/api-version-sets/versionset-httpbin-api')]"
36+
}
37+
}
38+
]
39+
}

example/api-httpbin/v1/api-httpbin.v1.template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
],
3232
"authenticationSettings": null,
3333
"subscriptionKeyParameterNames": null,
34-
"apiVersion": "v1",
34+
"apiVersion": "v2",
3535
"apiVersionSetId": "[concat(resourceId('Microsoft.ApiManagement/service', parameters('ApimServiceName')), '/api-version-sets/versionset-httpbin-api')]"
3636
}
3737
},
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"$schema":
3+
"https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
4+
"contentVersion": "1.0.0.0",
5+
"parameters": {
6+
"ApimServiceName": {
7+
"type": "string"
8+
},
9+
"repoBaseUrl":{
10+
"type": "string",
11+
"metadata": {
12+
"description": "Base URL of the repository"
13+
}
14+
}
15+
},
16+
"variables": {},
17+
"resources": [
18+
{
19+
"type": "#resources_ApiResourceType",
20+
"name": "[concat(parameters('#resources_ApimServiceName'), '#ApiName')]",
21+
"apiVersion": "#resources_apiVersion",
22+
"scale": null,
23+
"properties": {
24+
"displayName": "#resources_properties_displayName",
25+
"apiRevision": "#resources_properties_apiRevision",
26+
"description": "#resources_properties_description",
27+
"serviceUrl": "#resources_properties_serviceUrl",
28+
"path": "#resources_properties_path",
29+
"protocols": [
30+
"#resources_properties_protocols1"
31+
],
32+
"authenticationSettings": null,
33+
"subscriptionKeyParameterNames": null,
34+
"apiVersion": "#resources_properties_apiVersion",
35+
"apiVersionSetId": "[concat(resourceId('#resources_properties_ServiceResourceType', parameters('ApimServiceName')), '/api-version-sets/versionset-httpbin-api')]"
36+
}
37+
}
38+
]
39+
}
40+

example/full-file.json

Whitespace-only changes.

example/service.parameters.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"value": "odaibert"
1111
},
1212
"ApimServiceName": {
13-
"value": "odaibert-deploy"
13+
"value": "odaibert-deploy2"
1414
}
1515
}
1616
}

file2.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"ApimServiceName": {
6+
"type": "string"
7+
},
8+
"repoBaseUrl": {
9+
"type": "string",
10+
"metadata": {
11+
"description": "Base URL of the repository"
12+
}
13+
}
14+
},
15+
"variables": {},
16+
"resources": [
17+
{
18+
"type": "#resources_ApiResourceType",
19+
"name": "[concat(parameters('#resources_ApimServiceName'), '#ApiName')]",
20+
"apiVersion": "teste",
21+
"scale": null,
22+
"properties": {
23+
"displayName": "#resources_properties_displayName",
24+
"apiRevision": "#resources_properties_apiRevision",
25+
"description": "#resources_properties_description",
26+
"serviceUrl": "#resources_properties_serviceUrl",
27+
"path": "#resources_properties_path",
28+
"protocols": [
29+
"#resources_properties_protocols1"
30+
],
31+
"authenticationSettings": null,
32+
"subscriptionKeyParameterNames": null,
33+
"apiVersion": "#resources_properties_apiVersion",
34+
"apiVersionSetId": "[concat(resourceId('#resources_properties_ServiceResourceType', parameters('ApimServiceName')), '/api-version-sets/versionset-httpbin-api')]"
35+
}
36+
}
37+
]
38+
}

src/apimtemplate.test/CmdLine/ExtractTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public void ShouldFailWithUnknownCommand()
1515
//act - execute
1616

1717
//assert
18-
var createCommand = new ExtractCommand();
18+
var extractCommand = new ExtractCommand();
1919

20-
var ex = Assert.ThrowsAny<CommandParsingException>(() => createCommand.Execute("test"));
20+
var ex = Assert.ThrowsAny<CommandParsingException>(() => extractCommand.Execute("test"));
2121
//Console.WriteLine(ex.Message);
2222
Assert.Contains("Unrecognized command or argument 'test'", ex.Message);
2323
}

src/apimtemplate/Commands/Extract.cs

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.IO;
3+
using System.Net;
24
using McMaster.Extensions.CommandLineUtils;
35
using Colors.Net;
46
using Newtonsoft.Json.Linq;
@@ -14,28 +16,30 @@ public ExtractCommand()
1416
this.Name = Constants.ExtractName;
1517
this.Description = Constants.ExtractDescription;
1618

17-
var apiManagementName = this.Option("--name <apimname>", "API Management name", CommandOptionType.SingleValue).IsRequired();
19+
var apiManagementName = this.Option("--name <apimname>", "API Management name", CommandOptionType.SingleValue);
20+
var resourceGroupName = this.Option("--resourceGroup <resourceGroup>", "Resource Group name", CommandOptionType.SingleValue);
1821

1922
this.HelpOption();
2023

2124
this.OnExecute(() =>
2225
{
23-
if (apiManagementName.HasValue()) throw new Exception("Missing parameter(s)."); //Validade if is better exception or not
24-
25-
string resourceGroup = "APIM-Extractor"; //change to get from commandline parameter
26-
string apimname;
27-
string apis;
28-
int count;
26+
if (!apiManagementName.HasValue()) throw new Exception("Missing parameter <apimname>.");
27+
if (!resourceGroupName.HasValue()) throw new Exception("Missing parameter <resourceGroup>.");
2928

29+
string resourceGroup = resourceGroupName.Values[0].ToString();
30+
string apimname = apiManagementName.Values[0].ToString();
3031
Api api = new Api();
31-
JObject oApis;
32+
string apis = api.GetAPIs(apimname, resourceGroup).Result;
33+
JObject oApis = JObject.Parse(apis);
34+
int count = oApis["value"].Count<object>();
3235

33-
apimname = apiManagementName.Values[0].ToString();
34-
apis = api.GetAPIs(apimname, resourceGroup).Result;
35-
oApis = JObject.Parse(apis);
36-
count = oApis["value"].Count<object>();
36+
ConsoleColor lastColor = Console.ForegroundColor;
37+
Console.ForegroundColor = ConsoleColor.Green;
3738

3839
Console.WriteLine("{0} API's found!", count);
40+
Console.ForegroundColor = lastColor;
41+
42+
DownloadFile("https://github.com/odaibert/apim_templates/blob/master/API.json", "API.json");
3943

4044
for (int i = 0; i < count; i++)
4145
{
@@ -54,5 +58,50 @@ private static string FormatJSON(string json)
5458
dynamic parsedJson = JsonConvert.DeserializeObject(json);
5559
return JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
5660
}
61+
62+
public static void DownloadFile(string sourceURL, string destinationPath)
63+
{
64+
long fileSize = 0;
65+
int bufferSize = 1024;
66+
bufferSize *= 1000;
67+
long existLen = 0;
68+
if (File.Exists(destinationPath))
69+
{
70+
FileInfo destinationFileInfo = new FileInfo(destinationPath);
71+
existLen = destinationFileInfo.Length;
72+
}
73+
74+
75+
FileStream saveFileStream;
76+
if (existLen > 0)
77+
saveFileStream = new FileStream(destinationPath,
78+
FileMode.Append,
79+
FileAccess.Write,
80+
FileShare.ReadWrite);
81+
else
82+
saveFileStream = new FileStream(destinationPath,
83+
FileMode.Create,
84+
FileAccess.Write,
85+
FileShare.ReadWrite);
86+
87+
HttpWebRequest httpReq;
88+
HttpWebResponse httpRes;
89+
httpReq = (HttpWebRequest)WebRequest.Create(sourceURL);
90+
httpReq.AddRange((int)existLen);
91+
Stream resStream;
92+
httpRes = (HttpWebResponse)httpReq.GetResponse();
93+
resStream = httpRes.GetResponseStream();
94+
95+
fileSize = httpRes.ContentLength;
96+
97+
int byteSize;
98+
byte[] downBuffer = new byte[bufferSize];
99+
100+
while ((byteSize = resStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
101+
{
102+
saveFileStream.Write(downBuffer, 0, byteSize);
103+
}
104+
}
105+
57106
}
58107
}

src/apimtemplate/Common/Api.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,24 @@ public async Task<string> GetAPIOperations(string ApiManagementName, string Reso
1919
baseUrl, azSubId, ResourceGroupName, ApiManagementName, ApiName, Constants.APIVersion);
2020

2121
return await CallApiManagement(azToken, requestUrl);
22+
}
23+
public async Task<string> GetAPIDetails(string ApiManagementName, string ResourceGroupName, string ApiName)
24+
{
25+
(string azToken, string azSubId) = await auth.GetAccessToken();
26+
27+
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis/{4}?api-version={5}",
28+
baseUrl, azSubId, ResourceGroupName, ApiManagementName, ApiName, Constants.APIVersion);
2229

30+
return await CallApiManagement(azToken, requestUrl);
2331
}
2432
public async Task<string> GetAPIs(string ApiManagementName, string ResourceGroupName)
2533
{
2634
(string azToken, string azSubId) = await auth.GetAccessToken();
2735

2836
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis?api-version={4}",
2937
baseUrl, azSubId, ResourceGroupName, ApiManagementName, Constants.APIVersion);
30-
38+
3139
return await CallApiManagement(azToken, requestUrl);
32-
3340
}
3441
private static async Task<string> CallApiManagement(string azToken, string requestUrl)
3542
{

src/apimtemplate/Common/Authentication.cs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Colors.Net;
2-
using Microsoft.Azure.Management.ApiManagement.ArmTemplates;
32
using System;
43
using System.Runtime.InteropServices;
54
using System.Text;
@@ -14,7 +13,7 @@ public class Authentication
1413
(bool cliTokenSucceeded, string cliToken) = await TryGetAzCliToken();
1514
(bool cliSubscriptionIdSucceeded, string cliSubscriptionId) = await TryGetAzSubscriptionId();
1615

17-
if (cliTokenSucceeded || cliSubscriptionIdSucceeded)
16+
if (cliTokenSucceeded && cliSubscriptionIdSucceeded)
1817
{
1918
return (cliToken, cliSubscriptionId);
2019
}
@@ -24,27 +23,19 @@ public class Authentication
2423

2524
private async Task<(bool succeeded, string token)> TryGetAzCliToken()
2625
{
27-
var az = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
28-
? new Executable("cmd", "/c az " + Constants.azAccessToken)
29-
: new Executable("az", Constants.azAccessToken);
30-
31-
var stdout = new StringBuilder();
32-
var stderr = new StringBuilder();
33-
var completed = az.RunAsync(o => stdout.AppendLine(o), e => stderr.AppendLine(e));
34-
35-
if (await completed == 0)
36-
return (true, stdout.ToString().Trim(' ', '\n', '\r', '"'));
37-
else
38-
{
39-
ColoredConsole.WriteLine(($"Unable to fetch access token from az cli. Error: {stderr.ToString().Trim(' ', '\n', '\r')}"));
40-
return (false, stdout.ToString().Trim(' ', '\n', '\r', '"'));
41-
}
26+
return await ExecuteCommand(Constants.azAccessToken);
4227
}
28+
4329
private async Task<(bool succeeded, string token)> TryGetAzSubscriptionId()
30+
{
31+
return await ExecuteCommand(Constants.azSubscriptionId);
32+
}
33+
34+
private static async Task<(bool succeeded, string token)> ExecuteCommand(string commandParameters)
4435
{
4536
var az = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
46-
? new Executable("cmd", "/c az " + Constants.azSubscriptionId)
47-
: new Executable("az", Constants.azSubscriptionId);
37+
? new Executable("cmd", "/c az " + commandParameters)
38+
: new Executable("az", commandParameters);
4839

4940
var stdout = new StringBuilder();
5041
var stderr = new StringBuilder();
@@ -54,7 +45,7 @@ public class Authentication
5445
return (true, stdout.ToString().Trim(' ', '\n', '\r', '"'));
5546
else
5647
{
57-
ColoredConsole.WriteLine(($"Unable to fetch subscription id from az cli. Error: {stderr.ToString().Trim(' ', '\n', '\r')}"));
48+
ColoredConsole.WriteLine(($"Unable to fetch access token from az cli. Error: {stderr.ToString().Trim(' ', '\n', '\r')}"));
5849
return (false, stdout.ToString().Trim(' ', '\n', '\r', '"'));
5950
}
6051
}

0 commit comments

Comments
 (0)