From e88e54d1932fd9a7da1cc949e956e95cebd97268 Mon Sep 17 00:00:00 2001 From: heqingpan Date: Mon, 20 May 2024 09:01:10 +0800 Subject: [PATCH] update console naming_api --- src/common/constant.rs | 2 +- src/console/api.rs | 6 ++-- src/console/v2/naming_api.rs | 27 +++++++++++++++-- src/naming/api.rs | 2 +- src/openapi/naming/instance.rs | 53 ++-------------------------------- src/web_config.rs | 6 ++-- 6 files changed, 35 insertions(+), 61 deletions(-) diff --git a/src/common/constant.rs b/src/common/constant.rs index 45bad0d9..a0a86520 100644 --- a/src/common/constant.rs +++ b/src/common/constant.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -pub const APP_VERSION: &str = "0.5.8-beta.1"; +pub const APP_VERSION: &str = "0.5.8"; pub const EMPTY_STR: &str = ""; diff --git a/src/console/api.rs b/src/console/api.rs index dfb7d6c3..34db2997 100644 --- a/src/console/api.rs +++ b/src/console/api.rs @@ -7,7 +7,7 @@ use crate::common::appdata::AppShareData; use crate::config::core::ConfigActor; use crate::naming::ops::ops_api::query_opt_service_list; -use crate::openapi::naming::instance::{add_instance, del_instance, get_instance, update_instance}; +use crate::openapi::naming::instance::{del_instance, get_instance, update_instance}; use crate::openapi::naming::service::{query_service, remove_service, update_service}; //use crate::console::raft_api::{raft_add_learner, raft_change_membership, raft_init, raft_metrics, raft_read, raft_write}; @@ -145,7 +145,7 @@ pub fn console_api_config(config: &mut web::ServiceConfig) { ); } -pub fn console_api_config_new(config: &mut web::ServiceConfig) { +pub fn console_api_config_v1(config: &mut web::ServiceConfig) { config.service( web::scope("/rnacos/api/console") .service( @@ -165,7 +165,7 @@ pub fn console_api_config_new(config: &mut web::ServiceConfig) { .service( web::resource("/ns/instance") .route(web::get().to(get_instance)) - .route(web::post().to(add_instance)) + .route(web::post().to(update_instance)) .route(web::put().to(update_instance)) .route(web::delete().to(del_instance)), ) diff --git a/src/console/v2/naming_api.rs b/src/console/v2/naming_api.rs index 5076a5f9..0a939a28 100644 --- a/src/console/v2/naming_api.rs +++ b/src/console/v2/naming_api.rs @@ -6,7 +6,7 @@ use crate::console::model::naming_model::{ use crate::console::v2::ERROR_CODE_SYSTEM_ERROR; use crate::naming::api_model::InstanceVO; use crate::naming::core::{NamingActor, NamingCmd, NamingResult}; -use crate::naming::model::ServiceDetailDto; +use crate::naming::model::{InstanceUpdateTag, ServiceDetailDto}; use actix::Addr; use actix_web::web::Data; use actix_web::{web, HttpResponse, Responder}; @@ -170,6 +170,25 @@ pub async fn add_instance( appdata: Data>, web::Json(param): web::Json, ) -> impl Responder { + let update_tag = InstanceUpdateTag { + weight: match ¶m.weight { + Some(_v) => true, + None => false, + }, + metadata: match ¶m.metadata { + Some(_v) => true, + None => false, + }, + enabled: match ¶m.enabled { + Some(_v) => true, + None => false, + }, + ephemeral: match ¶m.ephemeral { + Some(_v) => true, + None => false, + }, + from_update: true, + }; match param.to_instance() { Ok(instance) => { if !instance.check_vaild() { @@ -178,7 +197,11 @@ pub async fn add_instance( Some("instance check is invalid".to_string()), )) } else { - match appdata.naming_route.update_instance(instance, None).await { + match appdata + .naming_route + .update_instance(instance, Some(update_tag)) + .await + { Ok(_) => HttpResponse::Ok().json(ApiResult::success(Some(true))), Err(err) => HttpResponse::Ok().json(ApiResult::<()>::error( ERROR_CODE_SYSTEM_ERROR.to_string(), diff --git a/src/naming/api.rs b/src/naming/api.rs index b0c2b2de..e61f96a2 100644 --- a/src/naming/api.rs +++ b/src/naming/api.rs @@ -476,7 +476,7 @@ pub async fn del_instance( match instance { Ok(instance) => { if !instance.check_vaild() { - HttpResponse::InternalServerError().body("instance check is invalid") + HttpResponse::InternalServerError().body("console: instance check is invalid") } else { match appdata.naming_route.delete_instance(instance).await { Ok(_) => HttpResponse::Ok().body("ok"), diff --git a/src/openapi/naming/instance.rs b/src/openapi/naming/instance.rs index f5195b6b..2a5fcd71 100644 --- a/src/openapi/naming/instance.rs +++ b/src/openapi/naming/instance.rs @@ -1,5 +1,4 @@ #![allow(unused_imports, unused_assignments, unused_variables)] - use std::collections::HashMap; use std::net::SocketAddr; use std::sync::Arc; @@ -23,7 +22,7 @@ pub(super) fn service() -> Scope { .service( web::resource(EMPTY) .route(web::get().to(get_instance)) - .route(web::post().to(add_instance)) + .route(web::post().to(update_instance)) .route(web::put().to(update_instance)) .route(web::delete().to(del_instance)), ) @@ -326,53 +325,6 @@ pub async fn get_instance( } } -pub async fn add_instance( - a: web::Query, - payload: web::Payload, - appdata: web::Data>, -) -> impl Responder { - let body = match get_req_body(payload).await { - Ok(v) => v, - Err(err) => { - return HttpResponse::InternalServerError().body(err.to_string()); - } - }; - let b = match serde_urlencoded::from_bytes(&body) { - Ok(v) => v, - Err(err) => { - return HttpResponse::InternalServerError().body(err.to_string()); - } - }; - let param = a.select_option(&b); - let update_tag = InstanceUpdateTag { - weight: match ¶m.weight { - Some(v) => *v != 1.0f32, - None => false, - }, - metadata: match ¶m.metadata { - Some(v) => !v.is_empty(), - None => false, - }, - enabled: false, - ephemeral: false, - from_update: false, - }; - let instance = param.convert_to_instance(); - match instance { - Ok(instance) => { - if !instance.check_vaild() { - HttpResponse::InternalServerError().body("instance check is invalid") - } else { - match appdata.naming_route.update_instance(instance, None).await { - Ok(_) => HttpResponse::Ok().body("ok"), - Err(e) => HttpResponse::InternalServerError().body(e.to_string()), - } - } - } - Err(e) => HttpResponse::InternalServerError().body(e), - } -} - pub async fn update_instance( a: web::Query, payload: web::Payload, @@ -397,8 +349,7 @@ pub async fn update_instance( None => false, }, metadata: match ¶m.metadata { - //Some(v) => !v.is_empty() && v!="{}", - Some(v) => !v.is_empty(), + Some(v) => !v.is_empty() && v != "{}", None => false, }, enabled: match ¶m.enabled { diff --git a/src/web_config.rs b/src/web_config.rs index 46de40a4..fa23db6c 100644 --- a/src/web_config.rs +++ b/src/web_config.rs @@ -4,7 +4,7 @@ use mime_guess::from_path; use rnacos_web_dist_wrap::get_embedded_file; use crate::common::AppSysConfig; -use crate::console::api::{console_api_config, console_api_config_new, console_api_config_v2}; +use crate::console::api::{console_api_config, console_api_config_v1, console_api_config_v2}; use crate::openapi::auth::{login_config, mock_token}; use crate::openapi::openapi_config; use crate::raft::network::raft_config; @@ -106,7 +106,7 @@ pub fn app_config(conf_data: AppSysConfig) -> impl FnOnce(&mut ServiceConfig) { raft_config(config); console_api_config(config); console_api_config_v2(config); - console_api_config_new(config); + console_api_config_v1(config); console_page_config(config); }; } @@ -129,7 +129,7 @@ pub fn app_without_no_auth_console_config(config: &mut web::ServiceConfig) { pub fn console_config(config: &mut web::ServiceConfig) { //console_api_config(config); console_api_config_v2(config); - console_api_config_new(config); + console_api_config_v1(config); console_page_config(config); }