You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datalab.apache.org by yk...@apache.org on 2021/02/15 11:56:25 UTC

[incubator-datalab] 04/04: [DATALAB-2228] - added possibility of change properties on external endpoint

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

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

commit 81ef75a5357a139e02f4516cf1ef0d6a1703c7f7
Author: KinashYurii <ur...@gmail.com>
AuthorDate: Mon Feb 15 13:55:24 2021 +0200

    [DATALAB-2228] - added possibility of change properties on external endpoint
---
 .../resources/admin/ChangePropertiesResource.java  | 50 ++++++++++-------
 .../service/impl/DynamicChangeProperties.java      | 65 +++++++++++-----------
 2 files changed, 61 insertions(+), 54 deletions(-)

diff --git a/services/self-service/src/main/java/com/epam/datalab/backendapi/resources/admin/ChangePropertiesResource.java b/services/self-service/src/main/java/com/epam/datalab/backendapi/resources/admin/ChangePropertiesResource.java
index 1c51385..c368102 100644
--- a/services/self-service/src/main/java/com/epam/datalab/backendapi/resources/admin/ChangePropertiesResource.java
+++ b/services/self-service/src/main/java/com/epam/datalab/backendapi/resources/admin/ChangePropertiesResource.java
@@ -7,16 +7,16 @@ import com.epam.datalab.backendapi.service.impl.DynamicChangeProperties;
 import io.dropwizard.auth.Auth;
 import lombok.NoArgsConstructor;
 
+import javax.inject.Inject;
 import javax.ws.rs.*;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import java.util.List;
 import java.util.Map;
 
-@Path("configuration")
+@Path("config")
 @Produces(MediaType.APPLICATION_JSON)
 @Consumes(MediaType.APPLICATION_JSON)
-@NoArgsConstructor
 public class ChangePropertiesResource {
 
     private static final String SELF_SERVICE = "self-service.yml";
@@ -31,12 +31,20 @@ public class ChangePropertiesResource {
     //"services/billing-gcp/billing.yml";
     private static final String BILLING_SERVICE_PROP_PATH = "/opt/datalab/conf/billing.yml";
 
+    private final DynamicChangeProperties dynamicChangeProperties;
+
+    @Inject
+    public ChangePropertiesResource(DynamicChangeProperties dynamicChangeProperties) {
+        this.dynamicChangeProperties = dynamicChangeProperties;
+    }
+
+
     @GET
     @Path("/self-service")
     public Response getSelfServiceProperties(@Auth UserInfo userInfo) {
         if (UserRoles.isAdmin(userInfo)) {
             return Response
-                    .ok(DynamicChangeProperties.getProperties(SELF_SERVICE_PROP_PATH, SELF_SERVICE))
+                    .ok(dynamicChangeProperties.getProperties(SELF_SERVICE_PROP_PATH, SELF_SERVICE))
                     .build();
         } else {
             return Response
@@ -50,7 +58,7 @@ public class ChangePropertiesResource {
     public Response getProvisioningServiceProperties(@Auth UserInfo userInfo) {
         if (UserRoles.isAdmin(userInfo)) {
             return Response
-                    .ok(DynamicChangeProperties.getProperties(PROVISIONING_SERVICE_PROP_PATH, PROVISIONING_SERVICE))
+                    .ok(dynamicChangeProperties.getProperties(PROVISIONING_SERVICE_PROP_PATH, PROVISIONING_SERVICE))
                     .build();
         } else {
             return Response
@@ -64,7 +72,7 @@ public class ChangePropertiesResource {
     public Response getBillingServiceProperties(@Auth UserInfo userInfo) {
         if (UserRoles.isAdmin(userInfo)) {
             return Response
-                    .ok(DynamicChangeProperties.getProperties(BILLING_SERVICE_PROP_PATH, BILLING_SERVICE))
+                    .ok(dynamicChangeProperties.getProperties(BILLING_SERVICE_PROP_PATH, BILLING_SERVICE))
                     .build();
         } else {
             return Response
@@ -77,7 +85,7 @@ public class ChangePropertiesResource {
     @Path("/self-service")
     public Response overwriteSelfServiceProperties(@Auth UserInfo userInfo, YmlDTO ymlDTO) {
         if (UserRoles.isAdmin(userInfo)) {
-            DynamicChangeProperties.overwriteProperties(SELF_SERVICE_PROP_PATH, SELF_SERVICE, ymlDTO.getYmlString());
+            dynamicChangeProperties.overwriteProperties(SELF_SERVICE_PROP_PATH, SELF_SERVICE, ymlDTO.getYmlString());
             return Response.ok().build();
         } else {
             return Response
@@ -90,7 +98,7 @@ public class ChangePropertiesResource {
     @Path("/provisioning-service")
     public Response overwriteProvisioningServiceProperties(@Auth UserInfo userInfo, YmlDTO ymlDTO) {
         if (UserRoles.isAdmin(userInfo)) {
-            DynamicChangeProperties.overwriteProperties(PROVISIONING_SERVICE_PROP_PATH, PROVISIONING_SERVICE,
+            dynamicChangeProperties.overwriteProperties(PROVISIONING_SERVICE_PROP_PATH, PROVISIONING_SERVICE,
                     ymlDTO.getYmlString());
             return Response.ok().build();
         } else {
@@ -104,7 +112,7 @@ public class ChangePropertiesResource {
     @Path("/billing")
     public Response overwriteBillingServiceProperties(@Auth UserInfo userInfo, YmlDTO ymlDTO) {
         if (UserRoles.isAdmin(userInfo)) {
-            DynamicChangeProperties.overwriteProperties(BILLING_SERVICE_PROP_PATH, BILLING_SERVICE, ymlDTO.getYmlString());
+            dynamicChangeProperties.overwriteProperties(BILLING_SERVICE_PROP_PATH, BILLING_SERVICE, ymlDTO.getYmlString());
             return Response.ok().build();
         } else {
             return Response
@@ -114,11 +122,11 @@ public class ChangePropertiesResource {
     }
 
     @GET
-    @Path("multiple/self-service")
+    @Path("/multiple/self-service")
     public Response getAllSelfServiceProperties(@Auth UserInfo userInfo) {
         if (UserRoles.isAdmin(userInfo)) {
             return Response
-                    .ok(DynamicChangeProperties.getPropertiesWithExternal(SELF_SERVICE_PROP_PATH, SELF_SERVICE, userInfo))
+                    .ok(dynamicChangeProperties.getPropertiesWithExternal(SELF_SERVICE_PROP_PATH, SELF_SERVICE, userInfo))
                     .build();
         } else {
             return Response
@@ -128,11 +136,11 @@ public class ChangePropertiesResource {
     }
 
     @GET
-    @Path("multiple/provisioning-service")
+    @Path("/multiple/provisioning-service")
     public Response getAllProvisioningServiceProperties(@Auth UserInfo userInfo) {
         if (UserRoles.isAdmin(userInfo)) {
             return Response
-                    .ok(DynamicChangeProperties.getPropertiesWithExternal(PROVISIONING_SERVICE_PROP_PATH, PROVISIONING_SERVICE, userInfo))
+                    .ok(dynamicChangeProperties.getPropertiesWithExternal(PROVISIONING_SERVICE_PROP_PATH, PROVISIONING_SERVICE, userInfo))
                     .build();
         } else {
             return Response
@@ -142,11 +150,11 @@ public class ChangePropertiesResource {
     }
 
     @GET
-    @Path("multiple/billing")
+    @Path("/multiple/billing")
     public Response getAllBillingServiceProperties(@Auth UserInfo userInfo) {
         if (UserRoles.isAdmin(userInfo)) {
             return Response
-                    .ok(DynamicChangeProperties.getPropertiesWithExternal(BILLING_SERVICE_PROP_PATH, BILLING_SERVICE, userInfo))
+                    .ok(dynamicChangeProperties.getPropertiesWithExternal(BILLING_SERVICE_PROP_PATH, BILLING_SERVICE, userInfo))
                     .build();
         } else {
             return Response
@@ -156,10 +164,10 @@ public class ChangePropertiesResource {
     }
 
     @POST
-    @Path("multiple/self-service")
+    @Path("/multiple/self-service")
     public Response overwriteAllSelfServiceProperties(@Auth UserInfo userInfo, Map<String, YmlDTO> ymlDTO) {
         if (UserRoles.isAdmin(userInfo)) {
-            DynamicChangeProperties.overwritePropertiesWithExternal(SELF_SERVICE_PROP_PATH, SELF_SERVICE, ymlDTO, userInfo);
+            dynamicChangeProperties.overwritePropertiesWithExternal(SELF_SERVICE_PROP_PATH, SELF_SERVICE, ymlDTO, userInfo);
             return Response.ok().build();
         } else {
             return Response
@@ -169,10 +177,10 @@ public class ChangePropertiesResource {
     }
 
     @POST
-    @Path("multiple/provisioning-service")
+    @Path("/multiple/provisioning-service")
     public Response overwriteAllProvisioningServiceProperties(@Auth UserInfo userInfo, Map<String, YmlDTO> ymlDTO) {
         if (UserRoles.isAdmin(userInfo)) {
-            DynamicChangeProperties.overwritePropertiesWithExternal(PROVISIONING_SERVICE_PROP_PATH, PROVISIONING_SERVICE,
+            dynamicChangeProperties.overwritePropertiesWithExternal(PROVISIONING_SERVICE_PROP_PATH, PROVISIONING_SERVICE,
                     ymlDTO, userInfo);
             return Response.ok().build();
         } else {
@@ -183,10 +191,10 @@ public class ChangePropertiesResource {
     }
 
     @POST
-    @Path("multiple/billing")
+    @Path("/multiple/billing")
     public Response overwriteAllBillingServiceProperties(@Auth UserInfo userInfo, Map<String, YmlDTO> ymlDTO) {
         if (UserRoles.isAdmin(userInfo)) {
-            DynamicChangeProperties.overwritePropertiesWithExternal(BILLING_SERVICE_PROP_PATH, BILLING_SERVICE, ymlDTO,
+            dynamicChangeProperties.overwritePropertiesWithExternal(BILLING_SERVICE_PROP_PATH, BILLING_SERVICE, ymlDTO,
                     userInfo);
             return Response.ok().build();
         } else {
@@ -204,7 +212,7 @@ public class ChangePropertiesResource {
                             @QueryParam("ui") boolean ui,
                             @QueryParam("endpoints") List<String> endpoints) {
         if (UserRoles.isAdmin(userInfo)) {
-            DynamicChangeProperties.restart(billing, provserv, ui);
+            dynamicChangeProperties.restart(billing, provserv, ui);
             return Response.ok().build();
         } else {
             return Response
diff --git a/services/self-service/src/main/java/com/epam/datalab/backendapi/service/impl/DynamicChangeProperties.java b/services/self-service/src/main/java/com/epam/datalab/backendapi/service/impl/DynamicChangeProperties.java
index b028802..cbc315a 100644
--- a/services/self-service/src/main/java/com/epam/datalab/backendapi/service/impl/DynamicChangeProperties.java
+++ b/services/self-service/src/main/java/com/epam/datalab/backendapi/service/impl/DynamicChangeProperties.java
@@ -30,7 +30,6 @@ import static com.epam.datalab.backendapi.domain.AuditResourceTypeEnum.EDGE_NODE
 @Slf4j
 public class DynamicChangeProperties {
 
-
     private static final String SELF_SERVICE_SUPERVISORCTL_RUN_NAME = " ui ";
     private static final String PROVISIONING_SERVICE_SUPERVISORCTL_RUN_NAME = " provserv ";
     private static final String BILLING_SERVICE_SUPERVISORCTL_RUN_NAME = " billing ";
@@ -67,55 +66,55 @@ public class DynamicChangeProperties {
     private static final int DEFAULT_VALUE_PLACE = 1;
     private static final int DEFAULT_NAME_PLACE = 0;
 
+
+    private final RESTService externalSelfService;
+    private final EndpointDAO endpointDAO;
+
     @Inject
-    private static RESTService externalSelfService;
-    @Inject
-    private static EndpointDAO endpointDAO;
+    public DynamicChangeProperties(RESTService externalSelfService, EndpointDAO endpointDAO) {
+        this.externalSelfService = externalSelfService;
+        this.endpointDAO = endpointDAO;
+    }
 
-    @Audit(action = RECONFIGURE, type = EDGE_NODE)
-    public static String getProperties(String path, String name) {
+    public String getProperties(String path, String name) {
         return readFileAsString(path, name);
     }
 
-    @Audit(action = RECONFIGURE, type = EDGE_NODE)
-    public static void overwriteProperties(String path, String name, String ymlString) {
+    public void overwriteProperties(String path, String name, String ymlString) {
         writeFileFromString(ymlString, name, path);
     }
 
-    @Audit(action = RECONFIGURE, type = EDGE_NODE)
-    public static Map<String, String> getPropertiesWithExternal(String path, String name, UserInfo userInfo) {
+    public Map<String, String> getPropertiesWithExternal(String path, String name, UserInfo userInfo) {
         List<EndpointDTO> externalEndpoints = endpointDAO.getEndpointsWithStatus("ACTIVE");
         Map<String, String> endpoints = externalEndpoints.stream()
-                .filter(endpointDTO -> !endpointDTO.getName().equals("Local"))
+                .filter(endpointDTO -> !endpointDTO.getName().equals("local"))
                 .collect(Collectors.toMap(EndpointDTO::getName,
                         dto -> readFileAsString(path, name, dto, userInfo)));
-        endpoints.put("Local", getProperties(path, name));
+        endpoints.put("local", getProperties(path, name));
         return endpoints;
 
     }
 
-    @Audit(action = RECONFIGURE, type = EDGE_NODE)
-    public static void overwritePropertiesWithExternal(String path, String name, Map<String, YmlDTO> ymlDTOS,
-                                                       UserInfo userInfo) {
+    public void overwritePropertiesWithExternal(String path, String name, Map<String, YmlDTO> ymlDTOS,
+                                                UserInfo userInfo) {
 
         List<EndpointDTO> allEndpoints = endpointDAO.getEndpointsWithStatus("ACTIVE");
         Map<EndpointDTO, String> endpointsToChange = allEndpoints.stream()
                 .filter(e -> ymlDTOS.containsKey(e.getName()))
-                .filter(e -> !e.getName().equals("Local"))
+                .filter(e -> !e.getName().equals("local"))
                 .collect(Collectors.toMap(e -> e, e -> ymlDTOS.get(e.getName()).getYmlString()));
-
         endpointsToChange.forEach((endpoint, ymlString) -> {
             log.info("Trying to write {}, for external endpoint : {} , for user: {}",
                     name, endpoint.getName(), userInfo.getSimpleName());
-            String url = endpoint.getUrl() + "/api/configuration/" + findMethodName(name);
+            String url = endpoint.getUrl() + "/api/admin/" + findMethodName(name);
             externalSelfService.post(url, ymlString, userInfo.getAccessToken(), String.class);
         });
-        if (ymlDTOS.containsKey("Local")) {
-            overwriteProperties(path, name, ymlDTOS.get("Local").getYmlString());
+        if (ymlDTOS.containsKey("local")) {
+            overwriteProperties(path, name, ymlDTOS.get("local").getYmlString());
         }
     }
 
-    private static String findMethodName(String name) {
+    private String findMethodName(String name) {
         switch (name) {
             case "self-service.yml": {
                 return "self-service";
@@ -131,7 +130,7 @@ public class DynamicChangeProperties {
         }
     }
 
-    public static void restart(boolean billing, boolean provserv, boolean ui) {
+    public void restart(boolean billing, boolean provserv, boolean ui) {
         try {
             String shCommand = buildSHRestartCommand(billing, provserv, ui);
             log.info("Tying to restart ui: {}, provserv: {}, billing: {}, with command: {}", ui,
@@ -142,7 +141,7 @@ public class DynamicChangeProperties {
         }
     }
 
-    private static String buildSHRestartCommand(boolean billing, boolean provserv, boolean ui) {
+    private String buildSHRestartCommand(boolean billing, boolean provserv, boolean ui) {
         StringBuilder stringBuilder = new StringBuilder(SUPERVISORCTL_RESTART_SH_COMMAND);
         if (billing) stringBuilder.append(BILLING_SERVICE_SUPERVISORCTL_RUN_NAME);
         if (provserv) stringBuilder.append(PROVISIONING_SERVICE_SUPERVISORCTL_RUN_NAME);
@@ -150,7 +149,7 @@ public class DynamicChangeProperties {
         return stringBuilder.toString();
     }
 
-    private static String readFileAsString(String selfServicePropPath, String serviceName) {
+    private String readFileAsString(String selfServicePropPath, String serviceName) {
         try {
             log.info("Trying to read self-service.yml, file from path {} :", selfServicePropPath);
             String currentConf = FileUtils.readFileToString(new File(selfServicePropPath), Charset.defaultCharset());
@@ -161,7 +160,7 @@ public class DynamicChangeProperties {
         }
     }
 
-    private static String readFileAsString(String selfServicePropPath, String serviceName, EndpointDTO endpoint, UserInfo userInfo) {
+    private String readFileAsString(String selfServicePropPath, String serviceName, EndpointDTO endpoint, UserInfo userInfo) {
         log.info("Trying to read self-service.yml, for external endpoint : {} , for user: {}",
                 endpoint, userInfo.getSimpleName());
         String currentConf = externalSelfService.get(endpoint.getUrl() + "/api/admin/self-service",
@@ -169,7 +168,7 @@ public class DynamicChangeProperties {
         return hideSecretsAndRemoveLicence(currentConf);
     }
 
-    private static String hideSecretsAndRemoveLicence(String currentConf) {
+    private String hideSecretsAndRemoveLicence(String currentConf) {
         Matcher m = Pattern.compile(SECRET_REGEX).matcher(currentConf);
         List<String> secrets = new ArrayList<>();
         String confWithReplacedSecretConf = removeLicence(currentConf);
@@ -184,11 +183,11 @@ public class DynamicChangeProperties {
         return confWithReplacedSecretConf;
     }
 
-    private static String removeLicence(String conf) {
+    private String removeLicence(String conf) {
         return conf.split(LICENCE_REGEX)[conf.split(LICENCE_REGEX).length - 1];
     }
 
-    private static void writeFileFromString(String newPropFile, String serviceName, String servicePath) {
+    private void writeFileFromString(String newPropFile, String serviceName, String servicePath) {
         try {
             String oldFile = FileUtils.readFileToString(new File(servicePath), Charset.defaultCharset());
             changeCHMODE(serviceName, servicePath, DEFAULT_CHMOD, WRITE_CHMOD);
@@ -205,7 +204,7 @@ public class DynamicChangeProperties {
         }
     }
 
-    private static void changeCHMODE(String serviceName, String path, String fromMode, String toMode) throws IOException {
+    private void changeCHMODE(String serviceName, String path, String fromMode, String toMode) throws IOException {
         try {
             String command = String.format(CHANGE_CHMOD_SH_COMMAND_FORMAT, toMode, path);
             log.info("Trying to change chmod for file {} {}->{}", serviceName, fromMode, toMode);
@@ -216,16 +215,16 @@ public class DynamicChangeProperties {
         }
     }
 
-    private static String addLicence() {
+    private String addLicence() {
         return LICENCE;
     }
 
-    private static String checkAndReplaceSecretIfEmpty(String newPropFile, String oldProf) {
+    private String checkAndReplaceSecretIfEmpty(String newPropFile, String oldProf) {
         Map<String, String> emptySecrets = findEmptySecret(newPropFile);
         return emptySecrets.isEmpty() ? newPropFile : replaceEmptySecret(newPropFile, oldProf, emptySecrets);
     }
 
-    private static String replaceEmptySecret(String newPropFile, String oldProf, Map<String, String> emptySecrets) {
+    private String replaceEmptySecret(String newPropFile, String oldProf, Map<String, String> emptySecrets) {
         String fileWithReplacedEmptySecrets = newPropFile;
         Matcher oldProfMatcher = Pattern.compile(SECRET_REGEX).matcher(oldProf);
         while (oldProfMatcher.find()) {
@@ -237,7 +236,7 @@ public class DynamicChangeProperties {
         return fileWithReplacedEmptySecrets;
     }
 
-    private static Map<String, String> findEmptySecret(String newPropFile) {
+    private Map<String, String> findEmptySecret(String newPropFile) {
         Matcher newPropFileMatcher = Pattern.compile(SECRET_REGEX).matcher(newPropFile);
         Map<String, String> emptySecrets = new HashMap<>();
         while (newPropFileMatcher.find()) {


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