From a890d0d520d483dfe4463f46f2db53fd2bef4f13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=85=9C=E5=9D=A4?= Date: Wed, 4 Sep 2024 14:43:53 +0800 Subject: [PATCH] [ISSUE #12017] Refactor the old version of the console's controller (#12591) * Refactor the old version of the console's controller --- .../v3/ConsoleHealthController.java | 69 +++++++ .../v3/ConsoleServerStateController.java | 81 ++++++++ .../v3/core/ConsoleNamespaceController.java | 185 ++++++++++++++++++ .../nacos/console/handler/HealthHandler.java | 36 ++++ .../console/handler/ServerStateHandler.java | 51 +++++ .../handler/core/NamespaceHandler.java | 85 ++++++++ .../handler/inner/HealthInnerHandler.java | 43 ++++ .../inner/ServerStateInnerHandler.java | 79 ++++++++ .../inner/core/NamespaceInnerHandler.java | 80 ++++++++ .../nacos/console/proxy/HealthProxy.java | 60 ++++++ .../nacos/console/proxy/ServerStateProxy.java | 68 +++++++ .../console/proxy/core/NamespaceProxy.java | 115 +++++++++++ 12 files changed, 952 insertions(+) create mode 100644 console/src/main/java/com/alibaba/nacos/console/controller/v3/ConsoleHealthController.java create mode 100644 console/src/main/java/com/alibaba/nacos/console/controller/v3/ConsoleServerStateController.java create mode 100644 console/src/main/java/com/alibaba/nacos/console/controller/v3/core/ConsoleNamespaceController.java create mode 100644 console/src/main/java/com/alibaba/nacos/console/handler/HealthHandler.java create mode 100644 console/src/main/java/com/alibaba/nacos/console/handler/ServerStateHandler.java create mode 100644 console/src/main/java/com/alibaba/nacos/console/handler/core/NamespaceHandler.java create mode 100644 console/src/main/java/com/alibaba/nacos/console/handler/inner/HealthInnerHandler.java create mode 100644 console/src/main/java/com/alibaba/nacos/console/handler/inner/ServerStateInnerHandler.java create mode 100644 console/src/main/java/com/alibaba/nacos/console/handler/inner/core/NamespaceInnerHandler.java create mode 100644 console/src/main/java/com/alibaba/nacos/console/proxy/HealthProxy.java create mode 100644 console/src/main/java/com/alibaba/nacos/console/proxy/ServerStateProxy.java create mode 100644 console/src/main/java/com/alibaba/nacos/console/proxy/core/NamespaceProxy.java diff --git a/console/src/main/java/com/alibaba/nacos/console/controller/v3/ConsoleHealthController.java b/console/src/main/java/com/alibaba/nacos/console/controller/v3/ConsoleHealthController.java new file mode 100644 index 00000000000..17a461b0f68 --- /dev/null +++ b/console/src/main/java/com/alibaba/nacos/console/controller/v3/ConsoleHealthController.java @@ -0,0 +1,69 @@ +/* + * Copyright 1999-2024 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.alibaba.nacos.console.controller.v3; + +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.model.v2.Result; +import com.alibaba.nacos.console.paramcheck.ConsoleDefaultHttpParamExtractor; +import com.alibaba.nacos.console.proxy.HealthProxy; +import com.alibaba.nacos.core.paramcheck.ExtractorManager; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; + +/** + * Controller class for handling health check operations. + * + * @author zhangyukun on:2024/8/27 + */ +@RestController() +@RequestMapping("/v3/console/health") +@ExtractorManager.Extractor(httpExtractor = ConsoleDefaultHttpParamExtractor.class) +public class ConsoleHealthController { + + private final HealthProxy healthProxy; + + public ConsoleHealthController(HealthProxy healthProxy) { + this.healthProxy = healthProxy; + } + + /** + * Whether the Nacos is in broken states or not, and cannot recover except by being restarted. + * + * @return HTTP code equal to 200 indicates that Nacos is in right states. HTTP code equal to 500 indicates that + * Nacos is in broken states. + */ + @GetMapping("/liveness") + public Result liveness() { + return Result.success("ok"); + } + + /** + * Ready to receive the request or not. + * + * @return HTTP code equal to 200 indicates that Nacos is ready. HTTP code equal to 500 indicates that Nacos is not + * ready. + */ + @GetMapping("/readiness") + public Result readiness(HttpServletRequest request) throws NacosException { + return healthProxy.checkReadiness(); + } + +} diff --git a/console/src/main/java/com/alibaba/nacos/console/controller/v3/ConsoleServerStateController.java b/console/src/main/java/com/alibaba/nacos/console/controller/v3/ConsoleServerStateController.java new file mode 100644 index 00000000000..50f9746e42d --- /dev/null +++ b/console/src/main/java/com/alibaba/nacos/console/controller/v3/ConsoleServerStateController.java @@ -0,0 +1,81 @@ +/* + * Copyright 1999-2024 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.alibaba.nacos.console.controller.v3; + +import com.alibaba.nacos.api.model.v2.Result; +import com.alibaba.nacos.console.paramcheck.ConsoleDefaultHttpParamExtractor; +import com.alibaba.nacos.console.proxy.ServerStateProxy; +import com.alibaba.nacos.core.paramcheck.ExtractorManager; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; + +/** + * Controller for managing server state-related operations. + * + * @author zhangyukun on:2024/8/27 + */ +@RestController +@RequestMapping("/v3/console/server") +@ExtractorManager.Extractor(httpExtractor = ConsoleDefaultHttpParamExtractor.class) +public class ConsoleServerStateController { + + private final ServerStateProxy serverStateProxy; + + public ConsoleServerStateController(ServerStateProxy serverStateProxy) { + this.serverStateProxy = serverStateProxy; + } + + /** + * Get server state of current server. + * + * @return state json. + */ + @GetMapping("/state") + public Result> serverState() { + Map serverState = serverStateProxy.getServerState(); + return Result.success(serverState); + } + + /** + * Get the announcement content based on the specified language. + * + * @param language Language for the announcement (default: "zh-CN") + * @return Announcement content as a string wrapped in a Result object + */ + @GetMapping("/announcement") + public Result getAnnouncement( + @RequestParam(required = false, name = "language", defaultValue = "zh-CN") String language) { + String announcement = serverStateProxy.getAnnouncement(language); + return Result.success(announcement); + } + + /** + * Get the console UI guide information. + * + * @return Console UI guide information as a string wrapped in a Result object + */ + @GetMapping("/guide") + public Result getConsoleUiGuide() { + String guideInformation = serverStateProxy.getConsoleUiGuide(); + return Result.success(guideInformation); + } +} diff --git a/console/src/main/java/com/alibaba/nacos/console/controller/v3/core/ConsoleNamespaceController.java b/console/src/main/java/com/alibaba/nacos/console/controller/v3/core/ConsoleNamespaceController.java new file mode 100644 index 00000000000..86da70d8678 --- /dev/null +++ b/console/src/main/java/com/alibaba/nacos/console/controller/v3/core/ConsoleNamespaceController.java @@ -0,0 +1,185 @@ +/* + * Copyright 1999-2024 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.alibaba.nacos.console.controller.v3.core; + +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.exception.api.NacosApiException; +import com.alibaba.nacos.api.model.v2.ErrorCode; +import com.alibaba.nacos.api.model.v2.Result; +import com.alibaba.nacos.auth.annotation.Secured; +import com.alibaba.nacos.common.utils.StringUtils; +import com.alibaba.nacos.console.paramcheck.ConsoleDefaultHttpParamExtractor; +import com.alibaba.nacos.console.proxy.core.NamespaceProxy; +import com.alibaba.nacos.core.namespace.model.Namespace; +import com.alibaba.nacos.core.namespace.model.form.NamespaceForm; +import com.alibaba.nacos.core.namespace.repository.NamespacePersistService; +import com.alibaba.nacos.core.paramcheck.ExtractorManager; +import com.alibaba.nacos.plugin.auth.constant.ActionTypes; +import com.alibaba.nacos.plugin.auth.constant.SignType; +import com.alibaba.nacos.plugin.auth.impl.constant.AuthConstants; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; +import java.util.UUID; +import java.util.regex.Pattern; + +/** + * Controller for handling HTTP requests related to namespace operations. + * + * @author zhangyukun on:2024/8/27 + */ +@RestController +@RequestMapping("/v3/console/namespace") +@ExtractorManager.Extractor(httpExtractor = ConsoleDefaultHttpParamExtractor.class) +public class ConsoleNamespaceController { + + private final NamespaceProxy namespaceProxy; + + private final NamespacePersistService namespacePersistService; + + public ConsoleNamespaceController(NamespaceProxy namespaceProxy, NamespacePersistService namespacePersistService) { + this.namespaceProxy = namespaceProxy; + this.namespacePersistService = namespacePersistService; + } + + private final Pattern namespaceIdCheckPattern = Pattern.compile("^[\\w-]+"); + + private final Pattern namespaceNameCheckPattern = Pattern.compile("^[^@#$%^&*]+$"); + + private static final int NAMESPACE_ID_MAX_LENGTH = 128; + + /** + * Get namespace list. + * + * @return namespace list + */ + @GetMapping("/list") + public Result> getNamespaceList() throws NacosException { + return Result.success(namespaceProxy.getNamespaceList()); + } + + /** + * get namespace all info by namespace id. + * + * @param namespaceId namespaceId + * @return namespace all info + */ + @GetMapping() + @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + + "namespaces", action = ActionTypes.READ, signType = SignType.CONSOLE) + public Result getNamespaceDetail(@RequestParam("namespaceId") String namespaceId) throws NacosException { + return Result.success(namespaceProxy.getNamespaceDetail(namespaceId)); + } + + /** + * create namespace. + * + * @param namespaceForm namespaceForm. + * @return whether create ok + */ + @PostMapping + @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + + "namespaces", action = ActionTypes.WRITE, signType = SignType.CONSOLE) + public Result createNamespace(NamespaceForm namespaceForm) throws NacosException { + + namespaceForm.validate(); + + String namespaceId = namespaceForm.getNamespaceId(); + String namespaceName = namespaceForm.getNamespaceName(); + String namespaceDesc = namespaceForm.getNamespaceDesc(); + + if (StringUtils.isBlank(namespaceId)) { + namespaceId = UUID.randomUUID().toString(); + } else { + namespaceId = namespaceId.trim(); + if (!namespaceIdCheckPattern.matcher(namespaceId).matches()) { + throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE, + "namespaceId [" + namespaceId + "] mismatch the pattern"); + } + if (namespaceId.length() > NAMESPACE_ID_MAX_LENGTH) { + throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE, + "too long namespaceId, over " + NAMESPACE_ID_MAX_LENGTH); + } + // check unique + if (namespacePersistService.tenantInfoCountByTenantId(namespaceId) > 0) { + throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE, + "the namespaceId is existed, namespaceId: " + namespaceForm.getNamespaceId()); + } + } + // contains illegal chars + if (!namespaceNameCheckPattern.matcher(namespaceName).matches()) { + throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE, + "namespaceName [" + namespaceName + "] contains illegal char"); + } + return Result.success(namespaceProxy.createNamespace(namespaceId, namespaceName, namespaceDesc)); + } + + /** + * edit namespace. + * + * @param namespaceForm namespace params + * @return whether edit ok + */ + @PutMapping + @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + + "namespaces", action = ActionTypes.WRITE, signType = SignType.CONSOLE) + public Result updateNamespace(NamespaceForm namespaceForm) throws NacosException { + namespaceForm.validate(); + // contains illegal chars + if (!namespaceNameCheckPattern.matcher(namespaceForm.getNamespaceName()).matches()) { + throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE, + "namespaceName [" + namespaceForm.getNamespaceName() + "] contains illegal char"); + } + return Result.success(namespaceProxy.updateNamespace(namespaceForm)); + } + + /** + * delete namespace by id. + * + * @param namespaceId namespace ID + * @return whether delete ok + */ + @DeleteMapping + @Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + + "namespaces", action = ActionTypes.WRITE, signType = SignType.CONSOLE) + public Result deleteNamespace(@RequestParam("namespaceId") String namespaceId) throws NacosException { + return Result.success(namespaceProxy.deleteNamespace(namespaceId)); + } + + /** + * check namespaceId exist. + * + * @param namespaceId namespace id + * @return true if exist, otherwise false + */ + @GetMapping("/exist") + public Result checkNamespaceIdExist(@RequestParam("customNamespaceId") String namespaceId) + throws NacosException { + if (StringUtils.isBlank(namespaceId)) { + return Result.success(false); + } + return Result.success(namespaceProxy.checkNamespaceIdExist(namespaceId)); + } +} diff --git a/console/src/main/java/com/alibaba/nacos/console/handler/HealthHandler.java b/console/src/main/java/com/alibaba/nacos/console/handler/HealthHandler.java new file mode 100644 index 00000000000..9866cf330ee --- /dev/null +++ b/console/src/main/java/com/alibaba/nacos/console/handler/HealthHandler.java @@ -0,0 +1,36 @@ +/* + * Copyright 1999-2024 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.alibaba.nacos.console.handler; + +import com.alibaba.nacos.api.model.v2.Result; + +/** + * Interface for handling health check operations. + * + * @author zhangyukun + */ +public interface HealthHandler { + + /** + * Perform readiness check to determine if Nacos is ready to handle requests. + * + * @return readiness result + */ + Result checkReadiness(); +} + diff --git a/console/src/main/java/com/alibaba/nacos/console/handler/ServerStateHandler.java b/console/src/main/java/com/alibaba/nacos/console/handler/ServerStateHandler.java new file mode 100644 index 00000000000..4382db996e1 --- /dev/null +++ b/console/src/main/java/com/alibaba/nacos/console/handler/ServerStateHandler.java @@ -0,0 +1,51 @@ +/* + * Copyright 1999-2024 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.alibaba.nacos.console.handler; + +import java.util.Map; + +/** + * Interface for handling server state operations. + * + * @author zhangyukun + */ +public interface ServerStateHandler { + + /** + * Get the current state of the server. + * + * @return a map containing the server state + */ + Map getServerState(); + + /** + * Get the announcement content based on the language. + * + * @param language the language for the announcement + * @return the announcement content + */ + String getAnnouncement(String language); + + /** + * Get the console UI guide information. + * + * @return the console UI guide information + */ + String getConsoleUiGuide(); +} + diff --git a/console/src/main/java/com/alibaba/nacos/console/handler/core/NamespaceHandler.java b/console/src/main/java/com/alibaba/nacos/console/handler/core/NamespaceHandler.java new file mode 100644 index 00000000000..63ac5f75663 --- /dev/null +++ b/console/src/main/java/com/alibaba/nacos/console/handler/core/NamespaceHandler.java @@ -0,0 +1,85 @@ +/* + * Copyright 1999-2024 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.alibaba.nacos.console.handler.core; + +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.core.namespace.model.Namespace; +import com.alibaba.nacos.core.namespace.model.form.NamespaceForm; + +import java.util.List; + +/** + * Interface for handling namespace-related operations. + * + * @author zhangyukun + */ +public interface NamespaceHandler { + + /** + * Get a list of namespaces. + * + * @return list of namespaces + */ + List getNamespaceList(); + + /** + * Get details of a specific namespace. + * + * @param namespaceId the ID of the namespace + * @return namespace details + * @throws NacosException if there is an issue fetching the namespace + */ + Namespace getNamespaceDetail(String namespaceId) throws NacosException; + + /** + * Create a new namespace. + * + * @param namespaceId the ID of the namespace + * @param namespaceName the name of the namespace + * @param namespaceDesc the description of the namespace + * @return true if the namespace was successfully created, otherwise false + * @throws NacosException if there is an issue creating the namespace + */ + Boolean createNamespace(String namespaceId, String namespaceName, String namespaceDesc) throws NacosException; + + /** + * Update an existing namespace. + * + * @param namespaceForm the form containing the updated namespace details + * @return true if the namespace was successfully updated, otherwise false + * @throws NacosException if there is an issue updating the namespace + */ + Boolean updateNamespace(NamespaceForm namespaceForm) throws NacosException; + + /** + * Delete a namespace by its ID. + * + * @param namespaceId the ID of the namespace + * @return true if the namespace was successfully deleted, otherwise false + */ + Boolean deleteNamespace(String namespaceId); + + /** + * Check if a namespace ID exists. + * + * @param namespaceId the ID of the namespace to check + * @return true if the namespace exists, otherwise false + */ + Boolean checkNamespaceIdExist(String namespaceId); +} + diff --git a/console/src/main/java/com/alibaba/nacos/console/handler/inner/HealthInnerHandler.java b/console/src/main/java/com/alibaba/nacos/console/handler/inner/HealthInnerHandler.java new file mode 100644 index 00000000000..82c2512c4bf --- /dev/null +++ b/console/src/main/java/com/alibaba/nacos/console/handler/inner/HealthInnerHandler.java @@ -0,0 +1,43 @@ +/* + * Copyright 1999-2024 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.alibaba.nacos.console.handler.inner; + +import com.alibaba.nacos.api.model.v2.Result; +import com.alibaba.nacos.console.handler.HealthHandler; +import com.alibaba.nacos.core.cluster.health.ModuleHealthCheckerHolder; +import com.alibaba.nacos.core.cluster.health.ReadinessResult; +import org.springframework.stereotype.Service; + +/** + * Implementation of HealthHandler that performs health check operations. + * + * @author zhangyukun + */ +@Service +public class HealthInnerHandler implements HealthHandler { + + @Override + public Result checkReadiness() { + ReadinessResult result = ModuleHealthCheckerHolder.getInstance().checkReadiness(); + if (result.isSuccess()) { + return Result.success("ok"); + } + return Result.failure(result.getResultMessage()); + } +} + diff --git a/console/src/main/java/com/alibaba/nacos/console/handler/inner/ServerStateInnerHandler.java b/console/src/main/java/com/alibaba/nacos/console/handler/inner/ServerStateInnerHandler.java new file mode 100644 index 00000000000..e6beb1cf4b6 --- /dev/null +++ b/console/src/main/java/com/alibaba/nacos/console/handler/inner/ServerStateInnerHandler.java @@ -0,0 +1,79 @@ +/* + * Copyright 1999-2024 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.alibaba.nacos.console.handler.inner; + +import com.alibaba.nacos.console.handler.ServerStateHandler; +import com.alibaba.nacos.sys.env.EnvUtil; +import com.alibaba.nacos.sys.module.ModuleState; +import com.alibaba.nacos.sys.module.ModuleStateHolder; +import com.alibaba.nacos.sys.utils.DiskUtils; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +import static com.alibaba.nacos.common.utils.StringUtils.FOLDER_SEPARATOR; +import static com.alibaba.nacos.common.utils.StringUtils.TOP_PATH; +import static com.alibaba.nacos.common.utils.StringUtils.WINDOWS_FOLDER_SEPARATOR; + +/** + * Implementation of ServerStateHandler that performs server state operations. + * + * @author zhangyukun + */ +@Service +public class ServerStateInnerHandler implements ServerStateHandler { + + private static final String ANNOUNCEMENT_FILE = "announcement.conf"; + + private static final String GUIDE_FILE = "console-guide.conf"; + + public Map getServerState() { + Map serverState = new HashMap<>(4); + for (ModuleState each : ModuleStateHolder.getInstance().getAllModuleStates()) { + each.getStates().forEach((s, o) -> serverState.put(s, null == o ? null : o.toString())); + } + return serverState; + } + + @Override + public String getAnnouncement(String language) { + String file = ANNOUNCEMENT_FILE.substring(0, ANNOUNCEMENT_FILE.length() - 5) + "_" + language + ".conf"; + if (file.contains(TOP_PATH) || file.contains(FOLDER_SEPARATOR) || file.contains(WINDOWS_FOLDER_SEPARATOR)) { + throw new IllegalArgumentException("Invalid filename"); + } + File announcementFile = new File(EnvUtil.getConfPath(), file); + String announcement = null; + if (announcementFile.exists() && announcementFile.isFile()) { + announcement = DiskUtils.readFile(announcementFile); + } + return announcement; + } + + @Override + public String getConsoleUiGuide() { + File guideFile = new File(EnvUtil.getConfPath(), GUIDE_FILE); + String guideInformation = null; + if (guideFile.exists() && guideFile.isFile()) { + guideInformation = DiskUtils.readFile(guideFile); + } + return guideInformation; + } +} + diff --git a/console/src/main/java/com/alibaba/nacos/console/handler/inner/core/NamespaceInnerHandler.java b/console/src/main/java/com/alibaba/nacos/console/handler/inner/core/NamespaceInnerHandler.java new file mode 100644 index 00000000000..6d121329794 --- /dev/null +++ b/console/src/main/java/com/alibaba/nacos/console/handler/inner/core/NamespaceInnerHandler.java @@ -0,0 +1,80 @@ +/* + * Copyright 1999-2024 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.alibaba.nacos.console.handler.inner.core; + +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.console.handler.core.NamespaceHandler; +import com.alibaba.nacos.core.namespace.model.Namespace; +import com.alibaba.nacos.core.namespace.model.form.NamespaceForm; +import com.alibaba.nacos.core.namespace.repository.NamespacePersistService; +import com.alibaba.nacos.core.service.NamespaceOperationService; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * Implementation of NamespaceHandler that handles namespace-related operations. + * + * @author zhangyukun + */ +@Service +public class NamespaceInnerHandler implements NamespaceHandler { + + private final NamespaceOperationService namespaceOperationService; + + private final NamespacePersistService namespacePersistService; + + public NamespaceInnerHandler(NamespaceOperationService namespaceOperationService, + NamespacePersistService namespacePersistService) { + this.namespaceOperationService = namespaceOperationService; + this.namespacePersistService = namespacePersistService; + } + + @Override + public List getNamespaceList() { + return namespaceOperationService.getNamespaceList(); + } + + @Override + public Namespace getNamespaceDetail(String namespaceId) throws NacosException { + return namespaceOperationService.getNamespace(namespaceId); + } + + @Override + public Boolean createNamespace(String namespaceId, String namespaceName, String namespaceDesc) + throws NacosException { + return namespaceOperationService.createNamespace(namespaceId, namespaceName, namespaceDesc); + } + + @Override + public Boolean updateNamespace(NamespaceForm namespaceForm) throws NacosException { + return namespaceOperationService.editNamespace(namespaceForm.getNamespaceId(), namespaceForm.getNamespaceName(), + namespaceForm.getNamespaceDesc()); + } + + @Override + public Boolean deleteNamespace(String namespaceId) { + return namespaceOperationService.removeNamespace(namespaceId); + } + + @Override + public Boolean checkNamespaceIdExist(String namespaceId) { + return (namespacePersistService.tenantInfoCountByTenantId(namespaceId) > 0); + } +} + diff --git a/console/src/main/java/com/alibaba/nacos/console/proxy/HealthProxy.java b/console/src/main/java/com/alibaba/nacos/console/proxy/HealthProxy.java new file mode 100644 index 00000000000..21a1a13d3ad --- /dev/null +++ b/console/src/main/java/com/alibaba/nacos/console/proxy/HealthProxy.java @@ -0,0 +1,60 @@ +/* + * Copyright 1999-2024 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.alibaba.nacos.console.proxy; + +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.model.v2.Result; +import com.alibaba.nacos.console.config.ConsoleConfig; +import com.alibaba.nacos.console.handler.HealthHandler; +import com.alibaba.nacos.console.handler.inner.HealthInnerHandler; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; + +/** + * Proxy class for handling health check operations. + * + * @author zhangyukun + */ +@Service +public class HealthProxy { + + private final Map healthHandlerMap = new HashMap<>(); + + private final ConsoleConfig consoleConfig; + + public HealthProxy(HealthInnerHandler healthInnerHandler, ConsoleConfig consoleConfig) { + this.healthHandlerMap.put("merged", healthInnerHandler); + this.consoleConfig = consoleConfig; + } + + /** + * Perform readiness check to determine if Nacos is ready to handle requests. + * + * @return readiness result + */ + public Result checkReadiness() throws NacosException { + HealthHandler healthHandler = healthHandlerMap.get(consoleConfig.getType()); + if (healthHandler == null) { + throw new NacosException(NacosException.INVALID_PARAM, "Invalid deployment type"); + } + return healthHandler.checkReadiness(); + } +} + diff --git a/console/src/main/java/com/alibaba/nacos/console/proxy/ServerStateProxy.java b/console/src/main/java/com/alibaba/nacos/console/proxy/ServerStateProxy.java new file mode 100644 index 00000000000..29cbc751b86 --- /dev/null +++ b/console/src/main/java/com/alibaba/nacos/console/proxy/ServerStateProxy.java @@ -0,0 +1,68 @@ +/* + * Copyright 1999-2024 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.alibaba.nacos.console.proxy; + +import com.alibaba.nacos.console.handler.ServerStateHandler; +import com.alibaba.nacos.console.handler.inner.ServerStateInnerHandler; +import org.springframework.stereotype.Service; + +import java.util.Map; + +/** + * Proxy class for handling server state operations. + * + * @author zhangyukun + */ +@Service +public class ServerStateProxy { + + private final ServerStateHandler serverStateHandler; + + public ServerStateProxy(ServerStateInnerHandler serverStateHandler) { + this.serverStateHandler = serverStateHandler; + } + + /** + * Get the current state of the server. + * + * @return the server state as a Map + */ + public Map getServerState() { + return serverStateHandler.getServerState(); + } + + /** + * Get the announcement content based on the language. + * + * @param language the language for the announcement + * @return the announcement content as a String + */ + public String getAnnouncement(String language) { + return serverStateHandler.getAnnouncement(language); + } + + /** + * Get the console UI guide information. + * + * @return the console UI guide information as a String + */ + public String getConsoleUiGuide() { + return serverStateHandler.getConsoleUiGuide(); + } +} + diff --git a/console/src/main/java/com/alibaba/nacos/console/proxy/core/NamespaceProxy.java b/console/src/main/java/com/alibaba/nacos/console/proxy/core/NamespaceProxy.java new file mode 100644 index 00000000000..b3d47a7e0d8 --- /dev/null +++ b/console/src/main/java/com/alibaba/nacos/console/proxy/core/NamespaceProxy.java @@ -0,0 +1,115 @@ +/* + * Copyright 1999-2024 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.alibaba.nacos.console.proxy.core; + +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.console.config.ConsoleConfig; +import com.alibaba.nacos.console.handler.core.NamespaceHandler; +import com.alibaba.nacos.console.handler.inner.core.NamespaceInnerHandler; +import com.alibaba.nacos.core.namespace.model.Namespace; +import com.alibaba.nacos.core.namespace.model.form.NamespaceForm; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Proxy class for handling namespace operations. + * + * @author zhangyukun + */ +@Service +public class NamespaceProxy { + + private final Map namespaceHandlerMap = new HashMap<>(); + + private final ConsoleConfig consoleConfig; + + public NamespaceProxy(NamespaceInnerHandler namespaceInnerHandler, ConsoleConfig consoleConfig) { + this.namespaceHandlerMap.put("merged", namespaceInnerHandler); + this.consoleConfig = consoleConfig; + } + + /** + * Get namespace list. + */ + public List getNamespaceList() throws NacosException { + NamespaceHandler namespaceHandler = namespaceHandlerMap.get(consoleConfig.getType()); + if (namespaceHandler == null) { + throw new NacosException(NacosException.INVALID_PARAM, "Invalid deployment type"); + } + return namespaceHandler.getNamespaceList(); + } + + /** + * Get the specific namespace information. + */ + public Namespace getNamespaceDetail(String namespaceId) throws NacosException { + NamespaceHandler namespaceHandler = namespaceHandlerMap.get(consoleConfig.getType()); + if (namespaceHandler == null) { + throw new NacosException(NacosException.INVALID_PARAM, "Invalid deployment type"); + } + return namespaceHandler.getNamespaceDetail(namespaceId); + } + + /** + * Create or update namespace. + */ + public Boolean createNamespace(String namespaceId, String namespaceName, String namespaceDesc) + throws NacosException { + NamespaceHandler namespaceHandler = namespaceHandlerMap.get(consoleConfig.getType()); + if (namespaceHandler == null) { + throw new NacosException(NacosException.INVALID_PARAM, "Invalid deployment type"); + } + return namespaceHandler.createNamespace(namespaceId, namespaceName, namespaceDesc); + } + + /** + * Edit namespace. + */ + public Boolean updateNamespace(NamespaceForm namespaceForm) throws NacosException { + NamespaceHandler namespaceHandler = namespaceHandlerMap.get(consoleConfig.getType()); + if (namespaceHandler == null) { + throw new NacosException(NacosException.INVALID_PARAM, "Invalid deployment type"); + } + return namespaceHandler.updateNamespace(namespaceForm); + } + + /** + * Delete namespace. + */ + public Boolean deleteNamespace(String namespaceId) throws NacosException { + NamespaceHandler namespaceHandler = namespaceHandlerMap.get(consoleConfig.getType()); + if (namespaceHandler == null) { + throw new NacosException(NacosException.INVALID_PARAM, "Invalid deployment type"); + } + return namespaceHandler.deleteNamespace(namespaceId); + } + + /** + * Check if namespace exists. + */ + public Boolean checkNamespaceIdExist(String namespaceId) throws NacosException { + NamespaceHandler namespaceHandler = namespaceHandlerMap.get(consoleConfig.getType()); + if (namespaceHandler == null) { + throw new NacosException(NacosException.INVALID_PARAM, "Invalid deployment type"); + } + return namespaceHandler.checkNamespaceIdExist(namespaceId); + } +}