-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #223 from nacos-group/dev
prepare v1.3.4
- Loading branch information
Showing
32 changed files
with
956 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
namespace App3.Controllers | ||
{ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Nacos.V2.Utils; | ||
using System.Threading.Tasks; | ||
|
||
[ApiController] | ||
[Route("o")] | ||
public class OpenApiController : ControllerBase | ||
{ | ||
private readonly Nacos.OpenApi.INacosOpenApi _api; | ||
|
||
public OpenApiController(Nacos.OpenApi.INacosOpenApi api) | ||
{ | ||
_api = api; | ||
} | ||
|
||
// GET o/n-g | ||
[HttpGet("n-g")] | ||
public async Task<string> NamespaceGetAll() | ||
{ | ||
var list = await _api.GetNamespacesAsync().ConfigureAwait(false); | ||
|
||
var res = list.ToJsonString(); | ||
|
||
return res ?? "GetAllInstances"; | ||
} | ||
|
||
// GET o/n-g | ||
[HttpGet("n-c")] | ||
public async Task<string> NamespaceCreate(string i, string n) | ||
{ | ||
var flag = await _api.CreateNamespaceAsync(i, n, "").ConfigureAwait(false); | ||
|
||
return flag.ToString(); | ||
} | ||
|
||
// GET o/n-u | ||
[HttpGet("n-u")] | ||
public async Task<string> NamespaceUpdate(string i, string n) | ||
{ | ||
var flag = await _api.UpdateNamespaceAsync(i, n, "").ConfigureAwait(false); | ||
|
||
return flag.ToString(); | ||
} | ||
|
||
// GET o/n-u | ||
[HttpGet("n-d")] | ||
public async Task<string> NamespaceDelete(string i) | ||
{ | ||
var flag = await _api.DeleteNamespaceAsync(i).ConfigureAwait(false); | ||
|
||
return flag.ToString(); | ||
} | ||
|
||
// GET o/metrics | ||
[HttpGet("metrics")] | ||
public async Task<string> GetMetrics() | ||
{ | ||
var flag = await _api.GetMetricsAsync(false).ConfigureAwait(false); | ||
|
||
return flag.ToJsonString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Nacos.OpenApi | ||
{ | ||
internal class Constants | ||
{ | ||
public static readonly string HttpClientName = "OpenApi"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
namespace Nacos.OpenApi | ||
{ | ||
using Microsoft.Extensions.Options; | ||
using Nacos.V2.Utils; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
public class DefaultNacosOpenApi : INacosOpenApi | ||
{ | ||
private const string _namespacePath = "nacos/v1/console/namespaces"; | ||
private const string _metricsPath = "nacos/v1/ns/operator/metrics"; | ||
|
||
private readonly IHttpClientFactory _httpClientFactory; | ||
private readonly Nacos.V2.NacosSdkOptions _options; | ||
|
||
public DefaultNacosOpenApi(IHttpClientFactory httpClientFactory, IOptions<Nacos.V2.NacosSdkOptions> optionsAccs) | ||
{ | ||
this._httpClientFactory = httpClientFactory; | ||
this._options = optionsAccs.Value; | ||
} | ||
|
||
public async Task<bool> CreateNamespaceAsync(string customNamespaceId, string namespaceName, string namespaceDesc) | ||
{ | ||
var client = _httpClientFactory.CreateClient(Constants.HttpClientName); | ||
|
||
var content = new StringContent($"customNamespaceId={customNamespaceId}&namespaceName={namespaceName}&namespaceDesc={namespaceDesc}"); | ||
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded"); | ||
var req = new HttpRequestMessage(HttpMethod.Post, $"{_options.ServerAddresses.First().TrimEnd('/')}/{_namespacePath}"); | ||
req.Content = content; | ||
|
||
var resp = await client.SendAsync(req).ConfigureAwait(false); | ||
|
||
if (resp.IsSuccessStatusCode) | ||
{ | ||
var res = await resp.Content.ReadAsStringAsync().ConfigureAwait(false); | ||
bool.TryParse(res, out bool result); | ||
return result; | ||
} | ||
else | ||
{ | ||
throw new Nacos.V2.Exceptions.NacosException((int)resp.StatusCode, "CreateNamespaceAsync exception"); | ||
} | ||
} | ||
|
||
public async Task<bool> DeleteNamespaceAsync(string namespaceId) | ||
{ | ||
var client = _httpClientFactory.CreateClient(Constants.HttpClientName); | ||
|
||
var content = new StringContent($"namespaceId={namespaceId}"); | ||
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded"); | ||
var req = new HttpRequestMessage(HttpMethod.Delete, $"{_options.ServerAddresses.First().TrimEnd('/')}/{_namespacePath}"); | ||
req.Content = content; | ||
|
||
var resp = await client.SendAsync(req).ConfigureAwait(false); | ||
|
||
if (resp.IsSuccessStatusCode) | ||
{ | ||
var res = await resp.Content.ReadAsStringAsync().ConfigureAwait(false); | ||
bool.TryParse(res, out bool result); | ||
return result; | ||
} | ||
else | ||
{ | ||
throw new Nacos.V2.Exceptions.NacosException((int)resp.StatusCode, "DeleteNamespaceAsync exception"); | ||
} | ||
} | ||
|
||
public async Task<NacosMetrics> GetMetricsAsync(bool onlyStatus) | ||
{ | ||
var client = _httpClientFactory.CreateClient(Constants.HttpClientName); | ||
|
||
var req = new HttpRequestMessage(HttpMethod.Get, $"{_options.ServerAddresses.First().TrimEnd('/')}/{_metricsPath}?onlyStatus={onlyStatus.ToString().ToLower()}"); | ||
|
||
var resp = await client.SendAsync(req).ConfigureAwait(false); | ||
|
||
if (resp.IsSuccessStatusCode) | ||
{ | ||
var res = await resp.Content.ReadAsStringAsync().ConfigureAwait(false); | ||
return res.ToObj<NacosMetrics>(); | ||
} | ||
else | ||
{ | ||
throw new Nacos.V2.Exceptions.NacosException((int)resp.StatusCode, "GetMetricsAsync exception"); | ||
} | ||
} | ||
|
||
public async Task<List<NacosNamespace>> GetNamespacesAsync() | ||
{ | ||
var client = _httpClientFactory.CreateClient(Constants.HttpClientName); | ||
|
||
var req = new HttpRequestMessage(HttpMethod.Get, $"{_options.ServerAddresses.First().TrimEnd('/')}/{_namespacePath}"); | ||
|
||
var resp = await client.SendAsync(req).ConfigureAwait(false); | ||
|
||
if (resp.IsSuccessStatusCode) | ||
{ | ||
var res = await resp.Content.ReadAsStringAsync().ConfigureAwait(false); | ||
var jobj = Newtonsoft.Json.Linq.JObject.Parse(res); | ||
|
||
if (jobj.TryGetValue("data", System.StringComparison.OrdinalIgnoreCase, out var val)) | ||
{ | ||
return val.ToString().ToObj<List<NacosNamespace>>(); | ||
} | ||
else | ||
{ | ||
return new List<NacosNamespace>(); | ||
} | ||
} | ||
else | ||
{ | ||
throw new Nacos.V2.Exceptions.NacosException((int)resp.StatusCode, "GetNamespacesAsync exception"); | ||
} | ||
} | ||
|
||
public async Task<bool> UpdateNamespaceAsync(string namespaceId, string namespaceName, string namespaceDesc) | ||
{ | ||
var client = _httpClientFactory.CreateClient(Constants.HttpClientName); | ||
|
||
var content = new StringContent($"customNamespaceId={namespaceId}&namespaceName={namespaceName}&namespaceDesc={namespaceDesc}"); | ||
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded"); | ||
var req = new HttpRequestMessage(HttpMethod.Post, $"{_options.ServerAddresses.First().TrimEnd('/')}/{_namespacePath}"); | ||
req.Content = content; | ||
|
||
var resp = await client.SendAsync(req).ConfigureAwait(false); | ||
|
||
if (resp.IsSuccessStatusCode) | ||
{ | ||
var res = await resp.Content.ReadAsStringAsync().ConfigureAwait(false); | ||
bool.TryParse(res, out bool result); | ||
return result; | ||
} | ||
else | ||
{ | ||
throw new Nacos.V2.Exceptions.NacosException((int)resp.StatusCode, "UpdateNamespaceAsync exception"); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.