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 2023/04/14 15:11:04 UTC

[linkis] branch dev-1.4.0 updated: push tenant config code (#4453)

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

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


The following commit(s) were added to refs/heads/dev-1.4.0 by this push:
     new 899bb15e7 push tenant config code (#4453)
899bb15e7 is described below

commit 899bb15e7c40101527ffdb0883760039a474b5b5
Author: huangKai-2323 <62...@users.noreply.github.com>
AuthorDate: Fri Apr 14 23:10:57 2023 +0800

    push tenant config code (#4453)
---
 .../interceptor/impl/TenantLabelSetUtils.scala     |  5 ++
 .../restful/api/TenantConfigrationRestfulApi.java  | 13 ++--
 .../configuration/service/TenantConfigService.java |  2 +-
 .../service/impl/TenantConfigServiceImpl.java      | 73 +++++++++++-----------
 4 files changed, 50 insertions(+), 43 deletions(-)

diff --git a/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/TenantLabelSetUtils.scala b/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/TenantLabelSetUtils.scala
index 1deee62a7..49f44edba 100644
--- a/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/TenantLabelSetUtils.scala
+++ b/linkis-computation-governance/linkis-entrance/src/main/scala/org/apache/linkis/entrance/interceptor/impl/TenantLabelSetUtils.scala
@@ -95,6 +95,11 @@ object TenantLabelSetUtils extends Logging {
               "*-" + LabelUtil.getUserCreatorLabel(jobRequest.getLabels).getCreator.toLowerCase()
             )
           }
+          if (StringUtils.isBlank(tenant)) {
+            tenant = userCreatorTenantCache.get(
+              LabelUtil.getUserCreatorLabel(jobRequest.getLabels).getUser.toLowerCase() + "-*"
+            )
+          }
           logger.info("get cache tenant:" + tenant + ",jobRequest:" + jobRequest.getId)
           // Add cached data if it is not empty
           if (StringUtils.isNotBlank(tenant)) {
diff --git a/linkis-public-enhancements/linkis-configuration/src/main/java/org/apache/linkis/configuration/restful/api/TenantConfigrationRestfulApi.java b/linkis-public-enhancements/linkis-configuration/src/main/java/org/apache/linkis/configuration/restful/api/TenantConfigrationRestfulApi.java
index e399849be..9a41fe67c 100644
--- a/linkis-public-enhancements/linkis-configuration/src/main/java/org/apache/linkis/configuration/restful/api/TenantConfigrationRestfulApi.java
+++ b/linkis-public-enhancements/linkis-configuration/src/main/java/org/apache/linkis/configuration/restful/api/TenantConfigrationRestfulApi.java
@@ -75,7 +75,7 @@ public class TenantConfigrationRestfulApi {
       if (!Configuration.isAdmin(userName)) {
         return Message.error("Failed to create-tenant,msg: only administrators can configure");
       }
-      if (tenantConfigService.userExists(tenantVo.getUser(), tenantVo.getCreator(), null)) {
+      if (tenantConfigService.isExist(tenantVo.getUser(), tenantVo.getCreator())) {
         throw new ConfigurationException("User-creator is existed");
       }
       parameterVerification(tenantVo);
@@ -225,8 +225,7 @@ public class TenantConfigrationRestfulApi {
   public Message checkUserCreator(
       HttpServletRequest req,
       @RequestParam(value = "user", required = false) String user,
-      @RequestParam(value = "creator", required = false) String creator,
-      @RequestParam(value = "tenantValue", required = false) String tenantValue) {
+      @RequestParam(value = "creator", required = false) String creator) {
     Boolean result = false;
     try {
       // Parameter verification
@@ -236,14 +235,11 @@ public class TenantConfigrationRestfulApi {
       if (StringUtils.isBlank(user)) {
         throw new ConfigurationException("User Name can't be empty ");
       }
-      if (creator.equals("*")) {
-        throw new ConfigurationException("Application Name can't be '*' ");
-      }
       String userName = ModuleUserUtils.getOperationUser(req, "checkUserCreator");
       if (!Configuration.isAdmin(userName)) {
         return Message.error("Failed to check-user-creator,msg: only administrators can configure");
       }
-      result = tenantConfigService.userExists(user, creator, tenantValue);
+      result = tenantConfigService.isExist(user, creator);
     } catch (ConfigurationException e) {
       return Message.error("Failed to check-user-creator,msg:" + e.getMessage());
     }
@@ -267,5 +263,8 @@ public class TenantConfigrationRestfulApi {
     if (StringUtils.isBlank(tenantVo.getTenantValue())) {
       throw new ConfigurationException("Tenant tag can't be empty ");
     }
+    if (tenantVo.getCreator().equals("*") && tenantVo.getUser().equals("*")) {
+      throw new ConfigurationException("User && Creator cannot be both *");
+    }
   }
 }
diff --git a/linkis-public-enhancements/linkis-configuration/src/main/java/org/apache/linkis/configuration/service/TenantConfigService.java b/linkis-public-enhancements/linkis-configuration/src/main/java/org/apache/linkis/configuration/service/TenantConfigService.java
index 3d07ad676..87b14a9c5 100644
--- a/linkis-public-enhancements/linkis-configuration/src/main/java/org/apache/linkis/configuration/service/TenantConfigService.java
+++ b/linkis-public-enhancements/linkis-configuration/src/main/java/org/apache/linkis/configuration/service/TenantConfigService.java
@@ -33,7 +33,7 @@ public interface TenantConfigService {
 
   void createTenant(TenantVo tenantVo) throws ConfigurationException;
 
-  Boolean userExists(String user, String creator, String tenantValue) throws ConfigurationException;
+  Boolean isExist(String user, String creator) throws ConfigurationException;
 
   TenantVo queryTenant(String user, String creator);
 }
diff --git a/linkis-public-enhancements/linkis-configuration/src/main/java/org/apache/linkis/configuration/service/impl/TenantConfigServiceImpl.java b/linkis-public-enhancements/linkis-configuration/src/main/java/org/apache/linkis/configuration/service/impl/TenantConfigServiceImpl.java
index 8f1cd7f6e..bf9755a30 100644
--- a/linkis-public-enhancements/linkis-configuration/src/main/java/org/apache/linkis/configuration/service/impl/TenantConfigServiceImpl.java
+++ b/linkis-public-enhancements/linkis-configuration/src/main/java/org/apache/linkis/configuration/service/impl/TenantConfigServiceImpl.java
@@ -105,7 +105,6 @@ public class TenantConfigServiceImpl implements TenantConfigService {
     }
     dataProcessing(tenantVo);
     TenantVo tenantVoLowerCase = toLowerCase(tenantVo);
-    tenantVoLowerCase.setUpdateTime(new Date());
     logger.info("updateTenant : {}", tenantVoLowerCase);
     userTenantMapper.updateTenant(tenantVoLowerCase);
   }
@@ -119,49 +118,52 @@ public class TenantConfigServiceImpl implements TenantConfigService {
   public void createTenant(TenantVo tenantVo) throws ConfigurationException {
     dataProcessing(tenantVo);
     TenantVo tenantVoLowerCase = toLowerCase(tenantVo);
-    tenantVoLowerCase.setUpdateTime(new Date());
     tenantVoLowerCase.setCreateTime(new Date());
-    logger.info("updateTenant : {}", tenantVoLowerCase);
+    logger.info("createTenant : {}", tenantVoLowerCase);
     userTenantMapper.createTenant(tenantVo);
   }
 
   private void dataProcessing(TenantVo tenantVo) throws ConfigurationException {
-    AtomicReference<Boolean> tenantResult = new AtomicReference<>(false);
-    // Obtain the tenant information of the ECM list
-    Map<String, Object> resultmap = null;
-    try {
-      resultmap = HttpsUtil.sendHttp(null, null);
-      logger.info("ResourceMonitor  response  {}:", resultmap);
-    } catch (IOException e) {
-      logger.warn("failed to get ecmResource data");
+    if (!tenantVo.getCreator().equals("*")) {
+      AtomicReference<Boolean> tenantResult = new AtomicReference<>(false);
+      // Obtain the tenant information of the ECM list
+      Map<String, Object> ecmListResult = null;
+      try {
+        ecmListResult = HttpsUtil.sendHttp(null, null);
+        logger.info("Request ecm list  response  {}:", ecmListResult);
+      } catch (IOException e) {
+        logger.warn("failed to get ecmResource data");
+      }
+      Map<String, List<Map<String, Object>>> data = MapUtils.getMap(ecmListResult, "data");
+      List<Map<String, Object>> emNodeVoList = data.get("EMs");
+      // Compare ECM list tenant labels for task
+      emNodeVoList.forEach(
+          ecmInfo -> {
+            List<Map<String, Object>> labels = (List<Map<String, Object>>) ecmInfo.get("labels");
+            labels.stream()
+                .filter(labelmap -> labelmap.containsKey("tenant"))
+                .forEach(
+                    map -> {
+                      String tenant = map.get("tenant").toString().toLowerCase();
+                      if (tenant.equals(tenantVo.getTenantValue().toLowerCase())) {
+                        tenantResult.set(true);
+                      }
+                    });
+          });
+      // Compare the value of ecm tenant
+      if (!tenantResult.get())
+        throw new ConfigurationException("The ECM with the corresponding label was not found");
+      // The beginning of tenantValue needs to contain creator
+      String creator = tenantVo.getCreator().toLowerCase();
+      String[] tenantArray = tenantVo.getTenantValue().toLowerCase().split("_");
+      if (tenantArray.length > 1 && !creator.equals(tenantArray[0])) {
+        throw new ConfigurationException("tenantValue should contain creator first");
+      }
     }
-    Map<String, List<Map<String, Object>>> data = MapUtils.getMap(resultmap, "data");
-    List<Map<String, Object>> emNodeVoList = data.get("EMs");
-    emNodeVoList.forEach(
-        ecmInfo -> {
-          List<Map<String, Object>> labels = (List<Map<String, Object>>) ecmInfo.get("labels");
-          labels.stream()
-              .filter(labelmap -> labelmap.containsKey("tenant"))
-              .forEach(
-                  map -> {
-                    String tenant = map.get("tenant").toString().toLowerCase();
-                    if (tenant.equals(tenantVo.getTenantValue())) {
-                      tenantResult.set(true);
-                    }
-                  });
-        });
-    // Compare the value of ecm tenant
-    if (!tenantResult.get())
-      throw new ConfigurationException("The ECM with the corresponding label was not found");
-    // The beginning of tenantValue needs to contain creator
-    String creator = tenantVo.getCreator().toLowerCase();
-    String tenantValue = tenantVo.getTenantValue().toLowerCase().split("_")[0];
-    if (!creator.equals(tenantValue))
-      throw new ConfigurationException("tenantValue should contain creator first");
   }
 
   @Override
-  public Boolean userExists(String user, String creator, String tenantValue) {
+  public Boolean isExist(String user, String creator) {
     boolean result = true;
     Map<String, Object> resultMap =
         queryTenantList(user.toLowerCase(), creator.toLowerCase(), null, 1, 20);
@@ -180,6 +182,7 @@ public class TenantConfigServiceImpl implements TenantConfigService {
     tenantVo.setTenantValue(tenantVo.getTenantValue().toLowerCase());
     tenantVo.setCreator(tenantVo.getCreator().toLowerCase());
     tenantVo.setUser(tenantVo.getUser().toLowerCase());
+    tenantVo.setUpdateTime(new Date());
     return tenantVo;
   }
 }


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