feat(nacos): improve service and instance management
This commit is contained in:
38 files changed
+4787
-695
No files matched your search
@@ -618,11 +618,31 @@ mod tests {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
|
||||
async fn get_service(&self, _: NacosServiceQuery) -> Result<NacosServiceDetail, String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
async fn create_service(&self, _: NacosServiceUpsert) -> Result<(), String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
async fn update_service(&self, _: NacosServiceUpsert) -> Result<(), String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
async fn delete_service(&self, _: NacosServiceQuery) -> Result<(), String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
|
||||
async fn list_instances(&self, _: NacosInstanceQuery) -> Result<Vec<NacosInstanceInfo>, String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
|
||||
async fn update_instance(&self, _: NacosInstanceUpdate) -> Result<(), String> {
|
||||
async fn update_instance(&self, _: NacosInstanceUpdateRequest) -> Result<(), String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
|
||||
async fn register_instance(&self, _: NacosInstanceRegistration) -> Result<(), String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
async fn deregister_instance(&self, _: NacosInstanceRef) -> Result<(), String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
|
||||
|
||||
@@ -147,10 +147,9 @@ impl NacosAdminConfig {
|
||||
}
|
||||
let context_path_is_explicit_root = self.context_path.trim() == "/";
|
||||
self.context_path = normalize_context_path(&self.context_path);
|
||||
// Nacos 3 separates the console (normally :8080) from the server-side
|
||||
// Admin API (normally :8848/nacos). Older DBX connection records did
|
||||
// not persist the default server context, so repair only explicit
|
||||
// Nacos 3 profiles here while preserving custom contexts.
|
||||
// Nacos 3 management uses the server-side Admin API, normally
|
||||
// `:8848/nacos`. Keep the documented default context for explicit V3
|
||||
// profiles while preserving custom reverse-proxy prefixes.
|
||||
if self.context_path.is_empty()
|
||||
&& !context_path_is_explicit_root
|
||||
&& matches!(self.implementation, Some(NacosImplementation::Nacos))
|
||||
|
||||
+1665
-277
File diff suppressed because it is too large.
Load diff
@@ -4,6 +4,10 @@ use crate::nacos::types::*;
|
||||
|
||||
#[async_trait]
|
||||
pub trait NacosAdmin: Send + Sync {
|
||||
fn service_capabilities(&self) -> NacosServiceCapabilities {
|
||||
NacosServiceCapabilities::default()
|
||||
}
|
||||
|
||||
async fn test_connection(&self) -> Result<NacosConnectionInfo, String>;
|
||||
async fn list_namespaces(&self) -> Result<Vec<NacosNamespaceInfo>, String>;
|
||||
async fn create_namespace(&self, req: NacosNamespaceCreate) -> Result<(), String>;
|
||||
@@ -29,8 +33,23 @@ pub trait NacosAdmin: Send + Sync {
|
||||
async fn get_rnacos_console_captcha(&self) -> Result<NacosRNacosConsoleCaptcha, String>;
|
||||
async fn login_rnacos_console(&self, captcha: Option<String>) -> Result<(), String>;
|
||||
async fn list_services(&self, query: NacosServiceQuery) -> Result<NacosServiceList, String>;
|
||||
async fn get_service(&self, query: NacosServiceQuery) -> Result<NacosServiceDetail, String>;
|
||||
async fn create_service(&self, req: NacosServiceUpsert) -> Result<(), String>;
|
||||
async fn update_service(&self, req: NacosServiceUpsert) -> Result<(), String>;
|
||||
async fn delete_service(&self, query: NacosServiceQuery) -> Result<(), String>;
|
||||
async fn list_instances(&self, query: NacosInstanceQuery) -> Result<Vec<NacosInstanceInfo>, String>;
|
||||
async fn update_instance(&self, req: NacosInstanceUpdate) -> Result<(), String>;
|
||||
/// Returns the authoritative management view used before deleting a
|
||||
/// service. Implementations whose discovery API hides disabled instances
|
||||
/// must override this instead of falling back to that lossy view.
|
||||
async fn list_instances_for_service_delete(
|
||||
&self,
|
||||
query: NacosInstanceQuery,
|
||||
) -> Result<Vec<NacosInstanceInfo>, String> {
|
||||
self.list_instances(query).await
|
||||
}
|
||||
async fn update_instance(&self, req: NacosInstanceUpdateRequest) -> Result<(), String>;
|
||||
async fn register_instance(&self, req: NacosInstanceRegistration) -> Result<(), String>;
|
||||
async fn deregister_instance(&self, req: NacosInstanceRef) -> Result<(), String>;
|
||||
async fn get_dashboard(&self, query: NacosDashboardQuery) -> Result<NacosDashboardSnapshot, String>;
|
||||
async fn raw_request(&self, req: NacosRawRequest) -> Result<NacosRawResponse, String>;
|
||||
}
|
||||
@@ -693,10 +693,28 @@ mod tests {
|
||||
async fn list_services(&self, _: NacosServiceQuery) -> Result<NacosServiceList, String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
async fn get_service(&self, _: NacosServiceQuery) -> Result<NacosServiceDetail, String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
async fn create_service(&self, _: NacosServiceUpsert) -> Result<(), String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
async fn update_service(&self, _: NacosServiceUpsert) -> Result<(), String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
async fn delete_service(&self, _: NacosServiceQuery) -> Result<(), String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
async fn list_instances(&self, _: NacosInstanceQuery) -> Result<Vec<NacosInstanceInfo>, String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
async fn update_instance(&self, _: NacosInstanceUpdate) -> Result<(), String> {
|
||||
async fn update_instance(&self, _: NacosInstanceUpdateRequest) -> Result<(), String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
async fn register_instance(&self, _: NacosInstanceRegistration) -> Result<(), String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
async fn deregister_instance(&self, _: NacosInstanceRef) -> Result<(), String> {
|
||||
Err("unused".to_string())
|
||||
}
|
||||
async fn get_dashboard(&self, _: NacosDashboardQuery) -> Result<NacosDashboardSnapshot, String> {
|
||||
|
||||
@@ -140,6 +140,7 @@ pub async fn nacos_list_services_core(
|
||||
query: NacosServiceQuery,
|
||||
) -> Result<NacosServiceList, String> {
|
||||
let admin = get_admin(state, conn_id).await?;
|
||||
ensure_service_operation(admin.as_ref(), NacosServiceOperation::ListServices)?;
|
||||
admin.list_services(query).await
|
||||
}
|
||||
|
||||
@@ -149,19 +150,100 @@ pub async fn nacos_list_instances_core(
|
||||
query: NacosInstanceQuery,
|
||||
) -> Result<Vec<NacosInstanceInfo>, String> {
|
||||
let admin = get_admin(state, conn_id).await?;
|
||||
ensure_service_operation(admin.as_ref(), NacosServiceOperation::ListInstances)?;
|
||||
admin.list_instances(query).await
|
||||
}
|
||||
|
||||
pub async fn nacos_get_service_core(
|
||||
state: &AppState,
|
||||
conn_id: &str,
|
||||
query: NacosServiceQuery,
|
||||
) -> Result<NacosServiceDetail, String> {
|
||||
let admin = get_admin(state, conn_id).await?;
|
||||
ensure_service_operation(admin.as_ref(), NacosServiceOperation::GetService)?;
|
||||
admin.get_service(query).await
|
||||
}
|
||||
|
||||
pub async fn nacos_create_service_core(state: &AppState, conn_id: &str, req: NacosServiceUpsert) -> Result<(), String> {
|
||||
ensure_connection_writable(state, conn_id, "Create Nacos service").await?;
|
||||
let admin = get_admin(state, conn_id).await?;
|
||||
ensure_service_operation(admin.as_ref(), NacosServiceOperation::CreateService)?;
|
||||
admin.create_service(req).await
|
||||
}
|
||||
|
||||
pub async fn nacos_update_service_core(state: &AppState, conn_id: &str, req: NacosServiceUpsert) -> Result<(), String> {
|
||||
ensure_connection_writable(state, conn_id, "Update Nacos service").await?;
|
||||
let admin = get_admin(state, conn_id).await?;
|
||||
ensure_service_operation(admin.as_ref(), NacosServiceOperation::UpdateService)?;
|
||||
admin.update_service(req).await
|
||||
}
|
||||
|
||||
pub async fn nacos_delete_service_core(
|
||||
state: &AppState,
|
||||
conn_id: &str,
|
||||
query: NacosServiceQuery,
|
||||
) -> Result<(), String> {
|
||||
ensure_connection_writable(state, conn_id, "Delete Nacos service").await?;
|
||||
let admin = get_admin(state, conn_id).await?;
|
||||
ensure_service_operation(admin.as_ref(), NacosServiceOperation::DeleteService)?;
|
||||
ensure_service_operation(admin.as_ref(), NacosServiceOperation::ListInstances)?;
|
||||
let service_name = query
|
||||
.service_name
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|name| !name.is_empty())
|
||||
.ok_or_else(|| "Nacos service name is required".to_string())?
|
||||
.to_string();
|
||||
let instances = admin
|
||||
.list_instances_for_service_delete(NacosInstanceQuery {
|
||||
namespace: query.namespace.clone(),
|
||||
service_name,
|
||||
group_name: query.group_name.clone(),
|
||||
clusters: None,
|
||||
})
|
||||
.await?;
|
||||
if !instances.is_empty() {
|
||||
return Err(format!(
|
||||
"NACOS_ERROR[serviceNotEmpty]: Nacos service still contains {} instance(s); deregister them before deletion",
|
||||
instances.len()
|
||||
));
|
||||
}
|
||||
admin.delete_service(query).await
|
||||
}
|
||||
|
||||
pub async fn nacos_update_instance_core(
|
||||
state: &AppState,
|
||||
conn_id: &str,
|
||||
req: NacosInstanceUpdate,
|
||||
req: NacosInstanceUpdateRequest,
|
||||
) -> Result<(), String> {
|
||||
ensure_connection_writable(state, conn_id, "Update Nacos instance").await?;
|
||||
let admin = get_admin(state, conn_id).await?;
|
||||
ensure_service_operation(admin.as_ref(), NacosServiceOperation::UpdateInstance)?;
|
||||
admin.update_instance(req).await
|
||||
}
|
||||
|
||||
pub async fn nacos_register_instance_core(
|
||||
state: &AppState,
|
||||
conn_id: &str,
|
||||
req: NacosInstanceRegistration,
|
||||
) -> Result<(), String> {
|
||||
ensure_connection_writable(state, conn_id, "Register Nacos instance").await?;
|
||||
let admin = get_admin(state, conn_id).await?;
|
||||
ensure_service_operation(admin.as_ref(), NacosServiceOperation::RegisterInstance)?;
|
||||
admin.register_instance(req).await
|
||||
}
|
||||
|
||||
pub async fn nacos_deregister_instance_core(
|
||||
state: &AppState,
|
||||
conn_id: &str,
|
||||
req: NacosInstanceRef,
|
||||
) -> Result<(), String> {
|
||||
ensure_connection_writable(state, conn_id, "Deregister Nacos instance").await?;
|
||||
let admin = get_admin(state, conn_id).await?;
|
||||
ensure_service_operation(admin.as_ref(), NacosServiceOperation::DeregisterInstance)?;
|
||||
admin.deregister_instance(req).await
|
||||
}
|
||||
|
||||
pub async fn nacos_get_dashboard_core(
|
||||
state: &AppState,
|
||||
conn_id: &str,
|
||||
@@ -205,10 +287,65 @@ pub(crate) async fn ensure_connection_writable(state: &AppState, conn_id: &str,
|
||||
}
|
||||
}
|
||||
|
||||
fn ensure_service_operation(
|
||||
admin: &dyn crate::nacos::port::NacosAdmin,
|
||||
operation: NacosServiceOperation,
|
||||
) -> Result<(), String> {
|
||||
let capabilities = admin.service_capabilities();
|
||||
let capability = capabilities.operation(operation);
|
||||
if capability.supported {
|
||||
return Ok(());
|
||||
}
|
||||
let reason = match capability.reason {
|
||||
Some(NacosCapabilityReason::ImplementationReadOnly) => "implementationReadOnly",
|
||||
Some(NacosCapabilityReason::VersionUnsupported) => "versionUnsupported",
|
||||
Some(NacosCapabilityReason::EndpointUnavailable) => "endpointUnavailable",
|
||||
Some(NacosCapabilityReason::NotVerified) => "notVerified",
|
||||
Some(NacosCapabilityReason::ConnectionReadOnly) => "connectionReadOnly",
|
||||
None => "notVerified",
|
||||
};
|
||||
Err(format!("NACOS_ERROR[unsupportedOperation]: Nacos service operation {operation:?} is unavailable ({reason})"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn core_service_operation_guard_allows_documented_rnacos_v1_writes() {
|
||||
use crate::nacos::config::{
|
||||
NacosAdminConfig, NacosAuthConfig, NacosImplementation, NacosMetricsMode, NacosRNacosConsoleAuth,
|
||||
NacosVersionMode,
|
||||
};
|
||||
use crate::nacos::http::NacosOpenApiAdmin;
|
||||
use crate::nacos::port::NacosAdmin;
|
||||
|
||||
let admin = NacosOpenApiAdmin::new(NacosAdminConfig {
|
||||
implementation: Some(NacosImplementation::RNacos),
|
||||
server_addr: "http://127.0.0.1:3848".to_string(),
|
||||
display_server_addr: "http://127.0.0.1:3848".to_string(),
|
||||
namespace: "public".to_string(),
|
||||
version_mode: Some(NacosVersionMode::Auto),
|
||||
context_path: "/nacos".to_string(),
|
||||
rnacos_console_addr: String::new(),
|
||||
rnacos_history_enabled: Some(false),
|
||||
rnacos_console_auth: NacosRNacosConsoleAuth::Inherit,
|
||||
auth: NacosAuthConfig::None,
|
||||
tls_skip_verify: false,
|
||||
metrics_mode: NacosMetricsMode::Disabled,
|
||||
metrics_url: String::new(),
|
||||
page_size: 20,
|
||||
connect_override: None,
|
||||
})
|
||||
.unwrap();
|
||||
assert!(ensure_service_operation(&admin, NacosServiceOperation::ListServices).is_ok());
|
||||
assert!(ensure_service_operation(&admin, NacosServiceOperation::CreateService).is_ok());
|
||||
assert!(ensure_service_operation(&admin, NacosServiceOperation::UpdateInstance).is_ok());
|
||||
assert!(admin.service_capabilities().create_service.supported);
|
||||
let error = ensure_service_operation(&admin, NacosServiceOperation::DeleteService).unwrap_err();
|
||||
assert!(error.contains("endpointUnavailable"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn raw_mutation_requires_writable_connection_before_adapter_build() {
|
||||
let dir = std::env::temp_dir().join(format!("dbx-nacos-service-test-{}", uuid::Uuid::new_v4()));
|
||||
|
||||
@@ -10,6 +10,99 @@ pub struct NacosCapabilities {
|
||||
pub supports_service_management: bool,
|
||||
pub supports_instance_update: bool,
|
||||
pub supports_raw_api: bool,
|
||||
#[serde(default)]
|
||||
pub service_management: NacosServiceCapabilities,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct NacosOperationCapability {
|
||||
pub supported: bool,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub reason: Option<NacosCapabilityReason>,
|
||||
}
|
||||
|
||||
impl NacosOperationCapability {
|
||||
pub fn supported() -> Self {
|
||||
Self { supported: true, reason: None }
|
||||
}
|
||||
|
||||
pub fn unsupported(reason: NacosCapabilityReason) -> Self {
|
||||
Self { supported: false, reason: Some(reason) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for NacosOperationCapability {
|
||||
fn default() -> Self {
|
||||
Self::supported()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum NacosCapabilityReason {
|
||||
ImplementationReadOnly,
|
||||
VersionUnsupported,
|
||||
EndpointUnavailable,
|
||||
NotVerified,
|
||||
ConnectionReadOnly,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum NacosServiceOperation {
|
||||
ListServices,
|
||||
GetService,
|
||||
CreateService,
|
||||
UpdateService,
|
||||
DeleteService,
|
||||
ListInstances,
|
||||
UpdateInstance,
|
||||
RegisterInstance,
|
||||
DeregisterInstance,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct NacosServiceCapabilities {
|
||||
pub list_services: NacosOperationCapability,
|
||||
pub get_service: NacosOperationCapability,
|
||||
pub create_service: NacosOperationCapability,
|
||||
pub update_service: NacosOperationCapability,
|
||||
pub delete_service: NacosOperationCapability,
|
||||
pub list_instances: NacosOperationCapability,
|
||||
pub update_instance: NacosOperationCapability,
|
||||
pub register_instance: NacosOperationCapability,
|
||||
pub deregister_instance: NacosOperationCapability,
|
||||
}
|
||||
|
||||
impl NacosServiceCapabilities {
|
||||
pub fn read_only(reason: NacosCapabilityReason) -> Self {
|
||||
Self {
|
||||
list_services: NacosOperationCapability::supported(),
|
||||
get_service: NacosOperationCapability::supported(),
|
||||
create_service: NacosOperationCapability::unsupported(reason),
|
||||
update_service: NacosOperationCapability::unsupported(reason),
|
||||
delete_service: NacosOperationCapability::unsupported(reason),
|
||||
list_instances: NacosOperationCapability::supported(),
|
||||
update_instance: NacosOperationCapability::unsupported(reason),
|
||||
register_instance: NacosOperationCapability::unsupported(reason),
|
||||
deregister_instance: NacosOperationCapability::unsupported(reason),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn operation(&self, operation: NacosServiceOperation) -> &NacosOperationCapability {
|
||||
match operation {
|
||||
NacosServiceOperation::ListServices => &self.list_services,
|
||||
NacosServiceOperation::GetService => &self.get_service,
|
||||
NacosServiceOperation::CreateService => &self.create_service,
|
||||
NacosServiceOperation::UpdateService => &self.update_service,
|
||||
NacosServiceOperation::DeleteService => &self.delete_service,
|
||||
NacosServiceOperation::ListInstances => &self.list_instances,
|
||||
NacosServiceOperation::UpdateInstance => &self.update_instance,
|
||||
NacosServiceOperation::RegisterInstance => &self.register_instance,
|
||||
NacosServiceOperation::DeregisterInstance => &self.deregister_instance,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for NacosCapabilities {
|
||||
@@ -21,6 +114,7 @@ impl Default for NacosCapabilities {
|
||||
supports_service_management: true,
|
||||
supports_instance_update: true,
|
||||
supports_raw_api: true,
|
||||
service_management: NacosServiceCapabilities::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -437,6 +531,40 @@ pub struct NacosServiceList {
|
||||
pub items: Vec<NacosServiceInfo>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct NacosServiceDetail {
|
||||
pub service_name: String,
|
||||
#[serde(default)]
|
||||
pub group_name: Option<String>,
|
||||
#[serde(default)]
|
||||
pub metadata: serde_json::Value,
|
||||
#[serde(default)]
|
||||
pub protect_threshold: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub selector: Option<serde_json::Value>,
|
||||
#[serde(default)]
|
||||
pub ephemeral: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct NacosServiceUpsert {
|
||||
#[serde(default)]
|
||||
pub namespace: Option<String>,
|
||||
pub service_name: String,
|
||||
#[serde(default)]
|
||||
pub group_name: Option<String>,
|
||||
#[serde(default)]
|
||||
pub metadata: Option<serde_json::Value>,
|
||||
#[serde(default)]
|
||||
pub protect_threshold: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub selector: Option<serde_json::Value>,
|
||||
#[serde(default)]
|
||||
pub ephemeral: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct NacosInstanceInfo {
|
||||
@@ -474,7 +602,7 @@ pub struct NacosInstanceQuery {
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct NacosInstanceUpdate {
|
||||
pub struct NacosInstanceRef {
|
||||
#[serde(default)]
|
||||
pub namespace: Option<String>,
|
||||
pub service_name: String,
|
||||
@@ -484,12 +612,42 @@ pub struct NacosInstanceUpdate {
|
||||
pub group_name: Option<String>,
|
||||
#[serde(default)]
|
||||
pub cluster_name: Option<String>,
|
||||
#[serde(default)]
|
||||
pub ephemeral: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct NacosInstancePatch {
|
||||
#[serde(default)]
|
||||
pub healthy: Option<bool>,
|
||||
#[serde(default)]
|
||||
pub enabled: Option<bool>,
|
||||
#[serde(default)]
|
||||
pub ephemeral: Option<bool>,
|
||||
pub weight: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub metadata: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct NacosInstanceUpdateRequest {
|
||||
pub target: NacosInstanceRef,
|
||||
pub patch: NacosInstancePatch,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct NacosInstanceRegistration {
|
||||
#[serde(default)]
|
||||
pub namespace: Option<String>,
|
||||
pub service_name: String,
|
||||
pub ip: String,
|
||||
pub port: u16,
|
||||
#[serde(default)]
|
||||
pub group_name: Option<String>,
|
||||
#[serde(default)]
|
||||
pub cluster_name: Option<String>,
|
||||
#[serde(default)]
|
||||
pub weight: Option<f64>,
|
||||
#[serde(default)]
|
||||
@@ -503,6 +661,86 @@ pub struct NacosDashboardQuery {
|
||||
pub namespace: Option<String>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod service_capability_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn official_nacos_service_capabilities_enable_every_supported_operation() {
|
||||
let capabilities = NacosServiceCapabilities::default();
|
||||
for operation in [
|
||||
NacosServiceOperation::ListServices,
|
||||
NacosServiceOperation::GetService,
|
||||
NacosServiceOperation::CreateService,
|
||||
NacosServiceOperation::UpdateService,
|
||||
NacosServiceOperation::DeleteService,
|
||||
NacosServiceOperation::ListInstances,
|
||||
NacosServiceOperation::UpdateInstance,
|
||||
NacosServiceOperation::RegisterInstance,
|
||||
NacosServiceOperation::DeregisterInstance,
|
||||
] {
|
||||
assert!(capabilities.operation(operation).supported);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_only_implementation_preserves_reads_and_explains_writes() {
|
||||
let capabilities = NacosServiceCapabilities::read_only(NacosCapabilityReason::ImplementationReadOnly);
|
||||
assert!(capabilities.list_services.supported);
|
||||
assert!(capabilities.get_service.supported);
|
||||
assert!(capabilities.list_instances.supported);
|
||||
assert_eq!(capabilities.update_instance.reason, Some(NacosCapabilityReason::ImplementationReadOnly));
|
||||
assert_eq!(capabilities.create_service.reason, Some(NacosCapabilityReason::ImplementationReadOnly));
|
||||
assert_eq!(capabilities.deregister_instance.reason, Some(NacosCapabilityReason::ImplementationReadOnly));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn service_capabilities_use_the_tauri_and_web_camel_case_contract() {
|
||||
let value =
|
||||
serde_json::to_value(NacosServiceCapabilities::read_only(NacosCapabilityReason::NotVerified)).unwrap();
|
||||
assert_eq!(value["listServices"]["supported"], true);
|
||||
assert_eq!(value["createService"]["supported"], false);
|
||||
assert_eq!(value["createService"]["reason"], "notVerified");
|
||||
assert!(value.get("manageServices").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn instance_update_uses_nested_target_and_patch_transport_contract() {
|
||||
let value = serde_json::to_value(NacosInstanceUpdateRequest {
|
||||
target: NacosInstanceRef {
|
||||
namespace: Some("public".to_string()),
|
||||
service_name: "api".to_string(),
|
||||
ip: "127.0.0.1".to_string(),
|
||||
port: 8080,
|
||||
group_name: Some("DBX_TEST".to_string()),
|
||||
cluster_name: Some("blue".to_string()),
|
||||
ephemeral: Some(false),
|
||||
},
|
||||
patch: NacosInstancePatch { enabled: Some(false), ..Default::default() },
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(value["target"]["serviceName"], "api");
|
||||
assert_eq!(value["target"]["ephemeral"], false);
|
||||
assert_eq!(value["patch"]["enabled"], false);
|
||||
assert!(value.get("serviceName").is_none());
|
||||
assert!(value["patch"].get("weight").is_some_and(serde_json::Value::is_null));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_capabilities_without_the_operation_matrix_remain_readable() {
|
||||
let capabilities: NacosCapabilities = serde_json::from_value(serde_json::json!({
|
||||
"supportsConfigManagement": true,
|
||||
"supportsConfigHistory": true,
|
||||
"supportsServiceManagement": true,
|
||||
"supportsInstanceUpdate": true,
|
||||
"supportsRawApi": true
|
||||
}))
|
||||
.unwrap();
|
||||
assert!(capabilities.service_management.list_services.supported);
|
||||
assert!(capabilities.service_management.update_instance.supported);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct NacosDashboardMetrics {
|
||||
|
||||
Reference in new issue
Block a user