You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datalab.apache.org by of...@apache.org on 2020/12/04 18:05:16 UTC

[incubator-datalab] branch develop updated: [DATALAB-2175] -- added roles for local endpoints (#1002)

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

ofuks pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-datalab.git


The following commit(s) were added to refs/heads/develop by this push:
     new a5bb423  [DATALAB-2175] -- added roles for local endpoints (#1002)
a5bb423 is described below

commit a5bb423008d75e84181c1a043ea7dcc1a77bfb21
Author: Kinash Yurii <ur...@gmail.com>
AuthorDate: Fri Dec 4 20:05:09 2020 +0200

    [DATALAB-2175] -- added roles for local endpoints (#1002)
    
    [DATALAB-2175] -- added roles for local endpoints
---
 .../dropwizard/listeners/MongoStartupListener.java | 32 +++++++++++++++++-----
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/services/self-service/src/main/java/com/epam/datalab/backendapi/dropwizard/listeners/MongoStartupListener.java b/services/self-service/src/main/java/com/epam/datalab/backendapi/dropwizard/listeners/MongoStartupListener.java
index 8ccc08d..38e63bf 100644
--- a/services/self-service/src/main/java/com/epam/datalab/backendapi/dropwizard/listeners/MongoStartupListener.java
+++ b/services/self-service/src/main/java/com/epam/datalab/backendapi/dropwizard/listeners/MongoStartupListener.java
@@ -23,7 +23,9 @@ import com.epam.datalab.backendapi.conf.SelfServiceApplicationConfiguration;
 import com.epam.datalab.backendapi.dao.EndpointDAO;
 import com.epam.datalab.backendapi.dao.SettingsDAO;
 import com.epam.datalab.backendapi.dao.UserRoleDAO;
+import com.epam.datalab.backendapi.domain.EndpointDTO;
 import com.epam.datalab.backendapi.resources.dto.UserRoleDTO;
+import com.epam.datalab.cloud.CloudProvider;
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.inject.Inject;
@@ -38,7 +40,9 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import java.util.TreeSet;
+import java.util.stream.Collectors;
 
+import static java.lang.String.format;
 import static java.util.Comparator.comparing;
 import static java.util.stream.Collectors.collectingAndThen;
 import static java.util.stream.Collectors.toCollection;
@@ -46,11 +50,12 @@ import static java.util.stream.Collectors.toCollection;
 @Slf4j
 public class MongoStartupListener implements ServerLifecycleListener {
 
-    private static final String PATH_TO_GENERAL_ROLES = "/mongo/general/mongo_roles.json";
+    private static final String PATH_TO_ROLES = "/mongo/%s/mongo_roles.json";
     private static final ObjectMapper MAPPER = new ObjectMapper();
     private final UserRoleDAO userRoleDao;
     private final SelfServiceApplicationConfiguration configuration;
     private final SettingsDAO settingsDAO;
+    private final EndpointDAO endpointDAO;
 
     @Inject
     public MongoStartupListener(UserRoleDAO userRoleDao, SelfServiceApplicationConfiguration configuration,
@@ -58,6 +63,7 @@ public class MongoStartupListener implements ServerLifecycleListener {
         this.userRoleDao = userRoleDao;
         this.configuration = configuration;
         this.settingsDAO = settingsDAO;
+        this.endpointDAO = endpointDAO;
     }
 
     @Override
@@ -65,23 +71,35 @@ public class MongoStartupListener implements ServerLifecycleListener {
         settingsDAO.setServiceBaseName(configuration.getServiceBaseName());
         settingsDAO.setConfOsFamily(configuration.getOs());
         settingsDAO.setSsnInstanceSize(configuration.getSsnInstanceSize());
+        List<EndpointDTO> endpointDTOs = endpointDAO.getEndpoints();
         if (userRoleDao.findAll().isEmpty()) {
-            log.debug("Populating DataLab roles into database");
-            userRoleDao.insert(getRoles());
+            log.info("Populating DataLab default roles into database");
+            List<UserRoleDTO> cloudRoles = getRoles(CloudProvider.GENERAL);
+            List<CloudProvider> connectedCloudProviders = getConnectedProviders(endpointDTOs);
+            log.info("Check for connected endpoints:\n connected endpoints: {} \n connected clouds: {}",
+                    endpointDTOs.size(), connectedCloudProviders);
+            connectedCloudProviders.forEach(provider -> cloudRoles.addAll(getRoles(provider)));
+            userRoleDao.insert(cloudRoles);
         } else {
             log.info("Roles already populated. Do nothing ...");
         }
     }
 
-    private List<UserRoleDTO> getRoles() {
-        Set<UserRoleDTO> userRoles = new HashSet<>(getUserRoleFromFile());
+    private List<CloudProvider> getConnectedProviders(List<EndpointDTO> endpointDTOs) {
+        return endpointDTOs.stream()
+                .map(EndpointDTO::getCloudProvider)
+                .collect(Collectors.toList());
+    }
+
+    private List<UserRoleDTO> getRoles(CloudProvider cloudProvider) {
+        Set<UserRoleDTO> userRoles = new HashSet<>(getUserRoleFromFile(cloudProvider));
         return userRoles.stream()
                 .collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparing(UserRoleDTO::getId))),
                         ArrayList::new));
     }
 
-    private List<UserRoleDTO> getUserRoleFromFile() {
-        try (InputStream is = getClass().getResourceAsStream(PATH_TO_GENERAL_ROLES)) {
+    private List<UserRoleDTO> getUserRoleFromFile(CloudProvider cloudProvider) {
+        try (InputStream is = getClass().getResourceAsStream(format(PATH_TO_ROLES, cloudProvider.getName()))) {
             return MAPPER.readValue(is, new TypeReference<List<UserRoleDTO>>() {
             });
         } catch (IOException e) {


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