You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@linkis.apache.org by pe...@apache.org on 2022/04/20 08:11:16 UTC

[incubator-linkis] 02/04: Add configuration add/get/delete interfaces to configuration #2013

This is an automated email from the ASF dual-hosted git repository.

peacewong pushed a commit to branch dev-1.1.2
in repository https://gitbox.apache.org/repos/asf/incubator-linkis.git

commit 8e25ad3ff92cc00069b0dceb55a274688f04559f
Author: peacewong <wp...@gmail.com>
AuthorDate: Wed Apr 20 11:56:22 2022 +0800

    Add configuration add/get/delete interfaces to configuration #2013
---
 .../linkis/configuration/dao/ConfigMapper.java     |   6 +-
 .../linkis/configuration/dao/impl/ConfigMapper.xml |  34 +++--
 .../exception/ConfigurationException.java          |  14 +-
 .../restful/api/ConfigurationRestfulApi.java       |  86 ++++++++++++
 .../ConfigKeyService.java}                         |  23 ++-
 .../service/impl/ConfigKeyServiceImpl.java         | 154 +++++++++++++++++++++
 6 files changed, 300 insertions(+), 17 deletions(-)

diff --git a/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/dao/ConfigMapper.java b/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/dao/ConfigMapper.java
index f38bfd6b7..0c8545764 100644
--- a/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/dao/ConfigMapper.java
+++ b/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/dao/ConfigMapper.java
@@ -43,6 +43,10 @@ public interface ConfigMapper {
 
     ConfigValue getConfigValueById(@Param("id") Long id);
 
+    ConfigValue getConfigValueByKeyAndLabel(ConfigValue configValue);
+
+    void deleteConfigKeyValue(ConfigValue configValue);
+
     void insertValueList(@Param("configValues") List<ConfigValue> configValues);
 
     void updateUserValue(@Param("value") String value, @Param("id") Long id);
@@ -51,7 +55,7 @@ public interface ConfigMapper {
 
     ConfigKey selectKeyByKeyID(@Param("id") Long keyID);
 
-    ConfigKey seleteKeyByKeyName(@Param("keyName") String keyName);
+    List<ConfigKey> seleteKeyByKeyName(@Param("keyName") String keyName);
 
     List<ConfigKey> listKeyByStringValue(@Param("stringValue") String stringValue);
 
diff --git a/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/dao/impl/ConfigMapper.xml b/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/dao/impl/ConfigMapper.xml
index 7e83f01ad..b72380833 100644
--- a/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/dao/impl/ConfigMapper.xml
+++ b/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/dao/impl/ConfigMapper.xml
@@ -128,16 +128,18 @@
         WHERE v.config_label_id = #{labelId}
     </select>
 
+
+
     <select id="getConfigById" resultMap="ConfigKeyValueMap">
         SELECT <include refid="Config_Key_Column"/>
         FROM linkis_ps_configuration_config_key
         WHERE id = #{id}
     </select>
 
-    <select id="seleteKeyByKeyName" resultMap="ConfigKeyValueMap">
+    <select id="seleteKeyByKeyName" resultMap="ConfigKeyMap">
         SELECT <include refid="Config_Key_Column"/>
         FROM linkis_ps_configuration_config_key
-        WHERE key = #{keyName}
+        WHERE `key` = #{keyName}
     </select>
 
     <select id="listKeyByStringValue" resultMap="ConfigKeyMap">
@@ -148,13 +150,7 @@
     </select>
 
 
-    <!--ConfigValue-->
-    <insert id="insertValue" useGeneratedKeys="true" keyProperty="id" parameterType="org.apache.linkis.configuration.entity.ConfigValue">
-        INSERT INTO linkis_ps_configuration_config_value(
-        id, config_key_id, config_value, config_label_id, create_time, update_time)
-        VALUES (
-        #{id},#{configKeyId},#{configValue}, #{configLabelId}, now(), now())
-    </insert>
+
 
     <insert id="insertValueList" useGeneratedKeys="true" keyProperty="id" parameterType="org.apache.linkis.configuration.entity.ConfigValue">
         INSERT INTO linkis_ps_configuration_config_value(
@@ -182,11 +178,31 @@
         </foreach>
     </update>
 
+    <!--ConfigValue-->
+    <insert id="insertValue" useGeneratedKeys="true" keyProperty="id" parameterType="org.apache.linkis.configuration.entity.ConfigValue">
+        REPLACE INTO linkis_ps_configuration_config_value(
+            id, config_key_id, config_value, config_label_id, create_time, update_time)
+        VALUES (
+                   #{id},#{configKeyId},#{configValue}, #{configLabelId}, now(), now())
+    </insert>
+
     <select id="getConfigValueById" resultMap="ConfigValueMap">
         SELECT * FROM linkis_ps_configuration_config_value
         WHERE id = #{id}
     </select>
 
+    <select id="getConfigValueByKeyAndLabel" resultMap="ConfigValueMap">
+        SELECT * FROM linkis_ps_configuration_config_value
+        WHERE  config_key_id=#{configKeyId}
+        AND config_label_id = #{configLabelId}
+    </select>
+
+    <delete id="deleteConfigKeyValue" >
+        DELETE FROM linkis_ps_configuration_config_value
+        WHERE  config_key_id=#{configKeyId}
+        AND config_label_id = #{configLabelId}
+    </delete>
+
     <!--Category-->
     <select id="getCategory" resultMap="CategoryMap">
         SELECT c.id AS category_id, c.level, c.description, c.tag, c.update_time, c.create_time,
diff --git a/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/exception/ConfigurationException.java b/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/exception/ConfigurationException.java
index 5b27162b2..c728ec437 100644
--- a/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/exception/ConfigurationException.java
+++ b/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/exception/ConfigurationException.java
@@ -17,8 +17,18 @@
 
 package org.apache.linkis.configuration.exception;
 
-public class ConfigurationException extends Exception {
+import org.apache.linkis.common.exception.ErrorException;
+
+public class ConfigurationException extends ErrorException {
+
+    public static final int CONFIGURATION_ERROR_CODE = 14100;
+
     public ConfigurationException(String message) {
-        super(message);
+        super(14100, message);
+    }
+
+    public ConfigurationException(String message, Throwable throwable) {
+        super(14100, message);
+        initCause(throwable);
     }
 }
diff --git a/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/restful/api/ConfigurationRestfulApi.java b/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/restful/api/ConfigurationRestfulApi.java
index 6d21707a9..df20a4c36 100644
--- a/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/restful/api/ConfigurationRestfulApi.java
+++ b/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/restful/api/ConfigurationRestfulApi.java
@@ -21,6 +21,7 @@ import org.apache.linkis.common.conf.Configuration;
 import org.apache.linkis.configuration.entity.*;
 import org.apache.linkis.configuration.exception.ConfigurationException;
 import org.apache.linkis.configuration.service.CategoryService;
+import org.apache.linkis.configuration.service.ConfigKeyService;
 import org.apache.linkis.configuration.service.ConfigurationService;
 import org.apache.linkis.configuration.util.ConfigurationConfiguration;
 import org.apache.linkis.configuration.util.JsonNodeUtil;
@@ -41,19 +42,26 @@ import javax.servlet.http.HttpServletRequest;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 @RestController
 @RequestMapping(path = "/configuration")
 public class ConfigurationRestfulApi {
 
+    private static final Logger logger = LoggerFactory.getLogger(ConfigurationRestfulApi.class);
+
     @Autowired private ConfigurationService configurationService;
 
     @Autowired private CategoryService categoryService;
 
+    @Autowired private ConfigKeyService configKeyService;
+
     ObjectMapper mapper = new ObjectMapper();
 
     private static final String NULL = "null";
@@ -243,4 +251,82 @@ public class ConfigurationRestfulApi {
             throw new ConfigurationException("only admin can modify category(只有管理员才能修改目录)");
         }
     }
+
+    @RequestMapping(path = "/keyvalue", method = RequestMethod.GET)
+    public Message getKeyValue(
+            HttpServletRequest req,
+            @RequestParam(value = "engineType", required = false, defaultValue = "*")
+                    String engineType,
+            @RequestParam(value = "version", required = false, defaultValue = "*") String version,
+            @RequestParam(value = "creator", required = false, defaultValue = "*") String creator,
+            @RequestParam(value = "configKey") String configKey)
+            throws ConfigurationException {
+        String username = ModuleUserUtils.getOperationUser(req, "saveKey");
+        if (engineType.equals("*") && !version.equals("*")) {
+            return Message.error(
+                    "When engineType is any engine, the version must also be any version");
+        }
+        List labelList =
+                LabelEntityParser.generateUserCreatorEngineTypeLabelList(
+                        username, creator, engineType, version);
+
+        List<ConfigValue> configValues = configKeyService.getConfigValue(configKey, labelList);
+        Message message = Message.ok().data("configValues", configValues);
+        if (configValues.size() > 1) {
+            message.data(
+                    "warnMessage",
+                    "There are multiple values for the corresponding Key: " + configKey);
+        }
+        return message;
+    }
+
+    @RequestMapping(path = "/keyvalue", method = RequestMethod.POST)
+    public Message saveKeyValue(HttpServletRequest req, @RequestBody Map<String, Object> json)
+            throws ConfigurationException {
+        String username = ModuleUserUtils.getOperationUser(req, "saveKey");
+        String engineType = (String) json.getOrDefault("engineType", "*");
+        String version = (String) json.getOrDefault("version", "*");
+        String creator = (String) json.getOrDefault("creator", "*");
+        String configKey = (String) json.get("configKey");
+        String value = (String) json.get("configValue");
+        if (engineType.equals("*") && !version.equals("*")) {
+            return Message.error(
+                    "When engineType is any engine, the version must also be any version");
+        }
+        if (StringUtils.isBlank(configKey) || StringUtils.isBlank(value)) {
+            return Message.error("key or value cannot be empty");
+        }
+        List labelList =
+                LabelEntityParser.generateUserCreatorEngineTypeLabelList(
+                        username, creator, engineType, version);
+
+        ConfigKeyValue configKeyValue = new ConfigKeyValue();
+        configKeyValue.setKey(configKey);
+        configKeyValue.setConfigValue(value);
+
+        ConfigValue configValue = configKeyService.saveConfigValue(configKeyValue, labelList);
+        return Message.ok().data("configValue", configValue);
+    }
+
+    @RequestMapping(path = "/keyvalue", method = RequestMethod.DELETE)
+    public Message deleteKeyValue(HttpServletRequest req, @RequestBody Map<String, Object> json)
+            throws ConfigurationException {
+        String username = ModuleUserUtils.getOperationUser(req, "saveKey");
+        String engineType = (String) json.getOrDefault("engineType", "*");
+        String version = (String) json.getOrDefault("version", "*");
+        String creator = (String) json.getOrDefault("creator", "*");
+        String configKey = (String) json.get("configKey");
+        if (engineType.equals("*") && !version.equals("*")) {
+            return Message.error(
+                    "When engineType is any engine, the version must also be any version");
+        }
+        if (StringUtils.isBlank(configKey)) {
+            return Message.error("key cannot be empty");
+        }
+        List labelList =
+                LabelEntityParser.generateUserCreatorEngineTypeLabelList(
+                        username, creator, engineType, version);
+        List<ConfigValue> configValues = configKeyService.deleteConfigValue(configKey, labelList);
+        return Message.ok().data("configValues", configValues);
+    }
 }
diff --git a/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/exception/ConfigurationException.java b/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/service/ConfigKeyService.java
similarity index 51%
copy from linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/exception/ConfigurationException.java
copy to linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/service/ConfigKeyService.java
index 5b27162b2..238b4e38a 100644
--- a/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/exception/ConfigurationException.java
+++ b/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/service/ConfigKeyService.java
@@ -15,10 +15,23 @@
  * limitations under the License.
  */
 
-package org.apache.linkis.configuration.exception;
+package org.apache.linkis.configuration.service;
 
-public class ConfigurationException extends Exception {
-    public ConfigurationException(String message) {
-        super(message);
-    }
+import org.apache.linkis.configuration.entity.ConfigKeyValue;
+import org.apache.linkis.configuration.entity.ConfigValue;
+import org.apache.linkis.configuration.exception.ConfigurationException;
+import org.apache.linkis.manager.label.entity.Label;
+
+import java.util.List;
+
+public interface ConfigKeyService {
+
+    ConfigValue saveConfigValue(ConfigKeyValue configKeyValue, List<Label<?>> labelList)
+            throws ConfigurationException;
+
+    List<ConfigValue> getConfigValue(String configKey, List<Label<?>> labelList)
+            throws ConfigurationException;
+
+    List<ConfigValue> deleteConfigValue(String configKey, List<Label<?>> labelList)
+            throws ConfigurationException;
 }
diff --git a/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/service/impl/ConfigKeyServiceImpl.java b/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/service/impl/ConfigKeyServiceImpl.java
new file mode 100644
index 000000000..381aa41be
--- /dev/null
+++ b/linkis-public-enhancements/linkis-publicservice/linkis-configuration/src/main/java/org/apache/linkis/configuration/service/impl/ConfigKeyServiceImpl.java
@@ -0,0 +1,154 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.linkis.configuration.service.impl;
+
+import org.apache.linkis.configuration.dao.ConfigMapper;
+import org.apache.linkis.configuration.dao.LabelMapper;
+import org.apache.linkis.configuration.entity.ConfigKey;
+import org.apache.linkis.configuration.entity.ConfigKeyValue;
+import org.apache.linkis.configuration.entity.ConfigLabel;
+import org.apache.linkis.configuration.entity.ConfigValue;
+import org.apache.linkis.configuration.exception.ConfigurationException;
+import org.apache.linkis.configuration.service.ConfigKeyService;
+import org.apache.linkis.configuration.util.LabelEntityParser;
+import org.apache.linkis.configuration.util.LabelParameterParser;
+import org.apache.linkis.manager.label.builder.CombinedLabelBuilder;
+import org.apache.linkis.manager.label.entity.CombinedLabel;
+import org.apache.linkis.manager.label.entity.Label;
+import org.apache.linkis.manager.label.exception.LabelErrorException;
+
+import org.apache.commons.lang3.StringUtils;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Service
+public class ConfigKeyServiceImpl implements ConfigKeyService {
+
+    private static final Logger logger = LoggerFactory.getLogger(ConfigKeyServiceImpl.class);
+
+    @Autowired private ConfigMapper configMapper;
+
+    @Autowired private LabelMapper labelMapper;
+
+    private CombinedLabelBuilder combinedLabelBuilder = new CombinedLabelBuilder();
+
+    @Override
+    public ConfigValue saveConfigValue(ConfigKeyValue configKeyValue, List<Label<?>> labelList)
+            throws ConfigurationException {
+
+        if (StringUtils.isBlank(configKeyValue.getConfigValue())
+                || StringUtils.isBlank(configKeyValue.getKey())) {
+            throw new ConfigurationException("key or value cannot be null");
+        }
+
+        LabelParameterParser.labelCheck(labelList);
+        List<ConfigKey> configKeys = configMapper.seleteKeyByKeyName(configKeyValue.getKey());
+        if (null == configKeys || configKeys.isEmpty()) {
+            throw new ConfigurationException("config key not exists: " + configKeyValue.getKey());
+        }
+        ConfigKey configKey = configKeys.get(0);
+
+        CombinedLabel combinedLabel = getCombinedLabel(labelList);
+
+        ConfigLabel configLabel =
+                labelMapper.getLabelByKeyValue(
+                        combinedLabel.getLabelKey(), combinedLabel.getStringValue());
+        if (null == configLabel || configLabel.getId() < 0) {
+            configLabel = LabelEntityParser.parseToConfigLabel(combinedLabel);
+            labelMapper.insertLabel(configLabel);
+            logger.info("succeed to create label: {}", configLabel.getStringValue());
+        }
+        ConfigValue configValue = new ConfigValue();
+        configValue.setConfigKeyId(configKey.getId());
+        configValue.setConfigValue(configKeyValue.getConfigValue());
+        configValue.setConfigLabelId(configLabel.getId());
+        configMapper.insertValue(configValue);
+        logger.info(
+                "succeed to save key: {} by label: {} value: {} ",
+                configKeyValue.getKey(),
+                combinedLabel.getStringValue(),
+                configKeyValue.getConfigValue());
+        return configValue;
+    }
+
+    private CombinedLabel getCombinedLabel(List<Label<?>> labelList) throws ConfigurationException {
+        CombinedLabel combinedLabel = null;
+        try {
+            combinedLabel = (CombinedLabel) combinedLabelBuilder.build("", labelList);
+        } catch (LabelErrorException e) {
+            throw new ConfigurationException("Failed to build label", e);
+        }
+        if (null == combinedLabel) {
+            throw new ConfigurationException("Failed to build label ,label is null");
+        }
+        return combinedLabel;
+    }
+
+    @Override
+    public List<ConfigValue> getConfigValue(String key, List<Label<?>> labelList)
+            throws ConfigurationException {
+        if (StringUtils.isBlank(key)) {
+            throw new ConfigurationException("configKey cannot be null");
+        }
+        LabelParameterParser.labelCheck(labelList);
+        List<ConfigKey> configKeys = configMapper.seleteKeyByKeyName(key);
+
+        if (null == configKeys || configKeys.isEmpty()) {
+            throw new ConfigurationException("config key not exists: " + key);
+        }
+        CombinedLabel combinedLabel = getCombinedLabel(labelList);
+
+        ConfigLabel configLabel =
+                labelMapper.getLabelByKeyValue(
+                        combinedLabel.getLabelKey(), combinedLabel.getStringValue());
+        if (null == configLabel || configLabel.getId() < 0) {
+            throw new ConfigurationException("label not exists: " + combinedLabel.getStringValue());
+        }
+        List<ConfigValue> configValues = new ArrayList<>();
+        for (ConfigKey configKey : configKeys) {
+            ConfigValue configValue = new ConfigValue();
+            configValue.setConfigKeyId(configKey.getId());
+            configValue.setConfigLabelId(configLabel.getId());
+            ConfigValue configValueByKeyAndLabel =
+                    configMapper.getConfigValueByKeyAndLabel(configValue);
+            if (null != configValueByKeyAndLabel) {
+                configValues.add(configValueByKeyAndLabel);
+            }
+        }
+        return configValues;
+    }
+
+    @Override
+    public List<ConfigValue> deleteConfigValue(String key, List<Label<?>> labelList)
+            throws ConfigurationException {
+        CombinedLabel combinedLabel = getCombinedLabel(labelList);
+        List<ConfigValue> configValues = getConfigValue(key, labelList);
+        for (ConfigValue configValue : configValues) {
+            configMapper.deleteConfigKeyValue(configValue);
+        }
+        logger.info("succeed to remove key: {} by label:{} ", key, combinedLabel.getStringValue());
+        return configValues;
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@linkis.apache.org
For additional commands, e-mail: commits-help@linkis.apache.org