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

bhliva pushed a commit to branch DLAB-573
in repository https://gitbox.apache.org/repos/asf/incubator-dlab.git


The following commit(s) were added to refs/heads/DLAB-573 by this push:
     new b1f47a1  DLAB-573 added refresh token param for spark reconfiguration
b1f47a1 is described below

commit b1f47a1ad09dcdbef3e1744afe7a8f635f00affb
Author: bhliva <[email protected]>
AuthorDate: Wed Mar 27 13:33:44 2019 +0200

    DLAB-573 added refresh token param for spark reconfiguration
---
 .../ComputationalClusterConfigDTO.java             |    2 +
 ...xploratoryReconfigureSparkClusterActionDTO.java |   19 +-
 .../epam/dlab/backendapi/util/RequestBuilder.java  | 1046 ++++++++++----------
 3 files changed, 544 insertions(+), 523 deletions(-)

diff --git 
a/services/dlab-model/src/main/java/com/epam/dlab/dto/computational/ComputationalClusterConfigDTO.java
 
b/services/dlab-model/src/main/java/com/epam/dlab/dto/computational/ComputationalClusterConfigDTO.java
index 016c513..7c03582 100644
--- 
a/services/dlab-model/src/main/java/com/epam/dlab/dto/computational/ComputationalClusterConfigDTO.java
+++ 
b/services/dlab-model/src/main/java/com/epam/dlab/dto/computational/ComputationalClusterConfigDTO.java
@@ -33,4 +33,6 @@ public class ComputationalClusterConfigDTO extends 
ComputationalBase<Computation
        private String copmutationalId;
        @JsonProperty("spark_configurations")
        private List<ClusterConfig> config;
+       @JsonProperty("azure_user_refresh_token")
+       private String azureUserRefreshToken;
 }
diff --git 
a/services/dlab-model/src/main/java/com/epam/dlab/dto/exploratory/ExploratoryReconfigureSparkClusterActionDTO.java
 
b/services/dlab-model/src/main/java/com/epam/dlab/dto/exploratory/ExploratoryReconfigureSparkClusterActionDTO.java
index 5226570..3a7f6ab 100644
--- 
a/services/dlab-model/src/main/java/com/epam/dlab/dto/exploratory/ExploratoryReconfigureSparkClusterActionDTO.java
+++ 
b/services/dlab-model/src/main/java/com/epam/dlab/dto/exploratory/ExploratoryReconfigureSparkClusterActionDTO.java
@@ -26,11 +26,18 @@ import java.util.List;
 
 public class ExploratoryReconfigureSparkClusterActionDTO extends 
ExploratoryActionDTO<ExploratoryReconfigureSparkClusterActionDTO> {
 
-       @JsonProperty("spark_configurations")
-       private List<ClusterConfig> config;
+    @JsonProperty("spark_configurations")
+    private List<ClusterConfig> config;
+    @JsonProperty("azure_user_refresh_token")
+    private String azureUserRefreshToken;
 
-       public ExploratoryReconfigureSparkClusterActionDTO 
withConfig(List<ClusterConfig> config) {
-               this.config = config;
-               return this;
-       }
+    public ExploratoryReconfigureSparkClusterActionDTO 
withConfig(List<ClusterConfig> config) {
+        this.config = config;
+        return this;
+    }
+
+    public ExploratoryReconfigureSparkClusterActionDTO 
withAzureUserRefreshToken(String azureUserRefreshToken) {
+        this.azureUserRefreshToken = azureUserRefreshToken;
+        return this;
+    }
 }
diff --git 
a/services/self-service/src/main/java/com/epam/dlab/backendapi/util/RequestBuilder.java
 
b/services/self-service/src/main/java/com/epam/dlab/backendapi/util/RequestBuilder.java
index 7d42516..aaad7d1 100644
--- 
a/services/self-service/src/main/java/com/epam/dlab/backendapi/util/RequestBuilder.java
+++ 
b/services/self-service/src/main/java/com/epam/dlab/backendapi/util/RequestBuilder.java
@@ -74,523 +74,535 @@ import static com.epam.dlab.cloud.CloudProvider.*;
 
 @Singleton
 public class RequestBuilder {
-       private static final String UNSUPPORTED_CLOUD_PROVIDER_MESSAGE = 
"Unsupported cloud provider ";
-       private static final String AZURE_REFRESH_TOKEN_KEY = "refresh_token";
-
-       @Inject
-       private SelfServiceApplicationConfiguration configuration;
-       @Inject
-       private SettingsDAO settingsDAO;
-
-       private CloudSettings cloudSettings(UserInfo userInfo) {
-               switch (cloudProvider()) {
-                       case AWS:
-                               return AwsCloudSettings.builder()
-                                               
.awsRegion(settingsDAO.getAwsRegion())
-                                               
.awsSecurityGroupIds(settingsDAO.getAwsSecurityGroups())
-                                               
.awsSubnetId(settingsDAO.getAwsSubnetId())
-                                               
.awsVpcId(settingsDAO.getAwsVpcId())
-                                               
.confTagResourceId(settingsDAO.getConfTagResourceId())
-                                               
.awsNotebookSubnetId(settingsDAO.getAwsNotebookSubnetId())
-                                               
.awsNotebookVpcId(settingsDAO.getAwsNotebookVpcId())
-                                               
.awsIamUser(userInfo.getName()).build();
-                       case AZURE:
-                               return AzureCloudSettings.builder()
-                                               
.azureRegion(settingsDAO.getAzureRegion())
-                                               
.azureResourceGroupName(settingsDAO.getAzureResourceGroupName())
-                                               
.azureSecurityGroupName(settingsDAO.getAzureSecurityGroupName())
-                                               
.azureSubnetName(settingsDAO.getAzureSubnetName())
-                                               
.azureVpcName(settingsDAO.getAzureVpcName())
-                                               
.azureIamUser(userInfo.getName()).build();
-                       case GCP:
-                               return 
GcpCloudSettings.builder().gcpIamUser(userInfo.getName()).build();
-                       default:
-                               throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
-               }
-       }
-
-       @SuppressWarnings("unchecked")
-       private <T extends ResourceBaseDTO<?>> T newResourceBaseDTO(UserInfo 
userInfo, Class<T> resourceClass) {
-               try {
-                       return (T) resourceClass.newInstance()
-                                       
.withEdgeUserName(getEdgeUserName(userInfo))
-                                       
.withCloudSettings(cloudSettings(userInfo));
-               } catch (Exception e) {
-                       throw new DlabException("Cannot create instance of 
resource class " + resourceClass.getName() + ". " +
-                                       e.getLocalizedMessage(), e);
-               }
-       }
-
-       private String getEdgeUserName(UserInfo userInfo) {
-               String edgeUser = 
UsernameUtils.replaceWhitespaces(userInfo.getSimpleName());
-               switch (cloudProvider()) {
-                       case GCP:
-                               return 
adjustUserName(configuration.getMaxUserNameLength(), edgeUser);
-                       case AWS:
-                       case AZURE:
-                               return edgeUser;
-                       default:
-                               throw new 
DlabException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
-               }
-       }
-
-       private String adjustUserName(int maxLength, String userName) {
-               return userName.length() > maxLength ?
-                               
UUID.nameUUIDFromBytes(userName.getBytes()).toString().substring(0, maxLength) 
: userName;
-       }
-
-       @SuppressWarnings("unchecked")
-       private <T extends ResourceSysBaseDTO<?>> T 
newResourceSysBaseDTO(UserInfo userInfo, Class<T> resourceClass) {
-               T resource = newResourceBaseDTO(userInfo, resourceClass);
-               return (T) resource
-                               
.withServiceBaseName(settingsDAO.getServiceBaseName())
-                               
.withConfOsFamily(settingsDAO.getConfOsFamily());
-       }
-
-       @SuppressWarnings("unchecked")
-       public UploadFile newEdgeKeyUpload(UserInfo userInfo, String content) {
-
-               switch (cloudProvider()) {
-                       case AWS:
-                               EdgeCreateAws edgeCreateAws = 
newResourceSysBaseDTO(userInfo, EdgeCreateAws.class);
-                               UploadFileAws uploadFileAws = new 
UploadFileAws();
-                               uploadFileAws.setEdge(edgeCreateAws);
-                               uploadFileAws.setContent(content);
-
-                               return uploadFileAws;
-
-                       case AZURE:
-                               EdgeCreateAzure edgeCreateAzure = 
newResourceSysBaseDTO(userInfo, EdgeCreateAzure.class)
-                                               
.withAzureDataLakeEnable(Boolean.toString(settingsDAO.isAzureDataLakeEnabled()));
-
-                               UploadFileAzure uploadFileAzure = new 
UploadFileAzure();
-                               uploadFileAzure.setEdge(edgeCreateAzure);
-                               uploadFileAzure.setContent(content);
-
-                               return uploadFileAzure;
-
-                       case GCP:
-                               return new 
UploadFileGcp(newResourceSysBaseDTO(userInfo, EdgeCreateGcp.class), content);
-                       default:
-                               throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
-               }
-       }
-
-       public ReuploadKeyDTO newKeyReupload(UserInfo userInfo, String id, 
String content, List<ResourceData> resources) {
-               return newResourceSysBaseDTO(userInfo, ReuploadKeyDTO.class)
-                               .withId(id)
-                               .withContent(content)
-                               .withResources(resources);
-       }
-
-       @SuppressWarnings("unchecked")
-       public <T extends ResourceSysBaseDTO<?>> T newEdgeAction(UserInfo 
userInfo) {
-               checkInappropriateCloudProviderOrElseThrowException();
-               return (T) newResourceSysBaseDTO(userInfo, 
ResourceSysBaseDTO.class);
-       }
-
-       public UserEnvironmentResources newUserEnvironmentStatus(UserInfo 
userInfo) {
-               checkInappropriateCloudProviderOrElseThrowException();
-               return newResourceSysBaseDTO(userInfo, 
UserEnvironmentResources.class);
-       }
-
-       @SuppressWarnings("unchecked")
-       public <T extends ExploratoryCreateDTO<T>> T 
newExploratoryCreate(Exploratory exploratory, UserInfo userInfo,
-                                                                               
                                                          
ExploratoryGitCredsDTO exploratoryGitCredsDTO) {
-
-               T exploratoryCreate;
-
-               switch (cloudProvider()) {
-                       case AWS:
-                               exploratoryCreate = (T) 
newResourceSysBaseDTO(userInfo, ExploratoryCreateAws.class)
-                                               
.withNotebookInstanceType(exploratory.getShape());
-                               break;
-                       case AZURE:
-                               exploratoryCreate = (T) 
newResourceSysBaseDTO(userInfo, ExploratoryCreateAzure.class)
-                                               
.withNotebookInstanceSize(exploratory.getShape());
-                               if (settingsDAO.isAzureDataLakeEnabled()) {
-                                       ((ExploratoryCreateAzure) 
exploratoryCreate)
-                                                       
.withAzureClientId(settingsDAO.getAzureDataLakeClientId())
-                                                       
.withAzureUserRefreshToken(userInfo.getKeys().get(AZURE_REFRESH_TOKEN_KEY));
-                               }
-
-                               ((ExploratoryCreateAzure) exploratoryCreate)
-                                               
.withAzureDataLakeEnabled(Boolean.toString(settingsDAO.isAzureDataLakeEnabled()));
-                               break;
-                       case GCP:
-                               exploratoryCreate = (T) 
newResourceSysBaseDTO(userInfo, ExploratoryCreateGcp.class)
-                                               
.withNotebookInstanceType(exploratory.getShape());
-                               break;
-
-                       default:
-                               throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
-               }
-
-               return 
exploratoryCreate.withExploratoryName(exploratory.getName())
-                               .withNotebookImage(exploratory.getDockerImage())
-                               
.withApplicationName(getApplicationNameFromImage(exploratory.getDockerImage()))
-                               
.withGitCreds(exploratoryGitCredsDTO.getGitCreds())
-                               .withImageName(exploratory.getImageName())
-                               
.withClusterConfig(exploratory.getClusterConfig());
-       }
-
-       @SuppressWarnings("unchecked")
-       public <T extends ExploratoryGitCredsUpdateDTO> T 
newExploratoryStart(UserInfo userInfo,
-                                                                               
                                                                  
UserInstanceDTO userInstance,
-                                                                               
                                                                  
ExploratoryGitCredsDTO
-                                                                               
                                                                                
  exploratoryGitCredsDTO) {
-
-               switch (cloudProvider()) {
-                       case AWS:
-                       case GCP:
-                               return (T) newResourceSysBaseDTO(userInfo, 
ExploratoryGitCredsUpdateDTO.class)
-                                               
.withNotebookInstanceName(userInstance.getExploratoryId())
-                                               
.withGitCreds(exploratoryGitCredsDTO.getGitCreds())
-                                               
.withNotebookImage(userInstance.getImageName())
-                                               
.withExploratoryName(userInstance.getExploratoryName())
-                                               
.withReuploadKeyRequired(userInstance.isReuploadKeyRequired());
-                       case AZURE:
-                               T exploratoryStart = (T) 
newResourceSysBaseDTO(userInfo, ExploratoryActionStartAzure.class)
-                                               
.withNotebookInstanceName(userInstance.getExploratoryId())
-                                               
.withGitCreds(exploratoryGitCredsDTO.getGitCreds())
-                                               
.withNotebookImage(userInstance.getImageName())
-                                               
.withExploratoryName(userInstance.getExploratoryName())
-                                               
.withReuploadKeyRequired(userInstance.isReuploadKeyRequired());
-
-                               if (settingsDAO.isAzureDataLakeEnabled()) {
-                                       ((ExploratoryActionStartAzure) 
exploratoryStart)
-                                                       
.withAzureClientId(settingsDAO.getAzureDataLakeClientId())
-                                                       
.withAzureUserRefreshToken(userInfo.getKeys().get(AZURE_REFRESH_TOKEN_KEY));
-                               }
-
-                               ((ExploratoryActionStartAzure) exploratoryStart)
-                                               
.withAzureDataLakeEnabled(Boolean.toString(settingsDAO.isAzureDataLakeEnabled()));
-
-                               return exploratoryStart;
-                       default:
-                               throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
-               }
-       }
-
-       @SuppressWarnings("unchecked")
-       public <T extends ExploratoryActionDTO<T>> T 
newExploratoryStop(UserInfo userInfo, UserInstanceDTO userInstance) {
-
-               T exploratoryStop;
-
-               switch (cloudProvider()) {
-                       case AWS:
-                       case GCP:
-                               exploratoryStop = (T) 
newResourceSysBaseDTO(userInfo, ExploratoryActionDTO.class);
-                               break;
-                       case AZURE:
-                               exploratoryStop = (T) 
newResourceSysBaseDTO(userInfo, ExploratoryActionStopAzure.class);
-                               break;
-                       default:
-                               throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
-               }
-
-               return exploratoryStop
-                               
.withNotebookInstanceName(userInstance.getExploratoryId())
-                               .withNotebookImage(userInstance.getImageName())
-                               
.withExploratoryName(userInstance.getExploratoryName())
-                               .withNotebookImage(userInstance.getImageName())
-                               
.withReuploadKeyRequired(userInstance.isReuploadKeyRequired());
-       }
-
-       public ExploratoryGitCredsUpdateDTO newGitCredentialsUpdate(UserInfo 
userInfo, UserInstanceDTO instanceDTO,
-                                                                               
                                                ExploratoryGitCredsDTO 
exploratoryGitCredsDTO) {
-               checkInappropriateCloudProviderOrElseThrowException();
-               return newResourceSysBaseDTO(userInfo, 
ExploratoryGitCredsUpdateDTO.class)
-                               .withNotebookImage(instanceDTO.getImageName())
-                               
.withApplicationName(getApplicationNameFromImage(instanceDTO.getImageName()))
-                               
.withNotebookInstanceName(instanceDTO.getExploratoryId())
-                               
.withExploratoryName(instanceDTO.getExploratoryName())
-                               
.withGitCreds(exploratoryGitCredsDTO.getGitCreds());
-       }
-
-       public LibraryInstallDTO newLibInstall(UserInfo userInfo, 
UserInstanceDTO userInstance,
-                                                                               
   List<LibInstallDTO> libs) {
-               checkInappropriateCloudProviderOrElseThrowException();
-               return newResourceSysBaseDTO(userInfo, LibraryInstallDTO.class)
-                               .withNotebookImage(userInstance.getImageName())
-                               
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()))
-                               
.withNotebookInstanceName(userInstance.getExploratoryId())
-                               
.withExploratoryName(userInstance.getExploratoryName())
-                               .withLibs(libs);
-       }
-
-       @SuppressWarnings("unchecked")
-       public <T extends ExploratoryActionDTO<T>> T 
newLibExploratoryList(UserInfo userInfo,
-                                                                               
                                                           UserInstanceDTO 
userInstance) {
-               checkInappropriateCloudProviderOrElseThrowException();
-               return (T) newResourceSysBaseDTO(userInfo, 
ExploratoryActionDTO.class)
-                               
.withNotebookInstanceName(userInstance.getExploratoryId())
-                               .withNotebookImage(userInstance.getImageName())
-                               
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()))
-                               
.withExploratoryName(userInstance.getExploratoryName());
-       }
-
-       @SuppressWarnings("unchecked")
-       public <T extends LibraryInstallDTO> T newLibInstall(UserInfo userInfo, 
UserInstanceDTO userInstance,
-                                                                               
                                 UserComputationalResource 
computationalResource,
-                                                                               
                                 List<LibInstallDTO> libs) {
-               checkInappropriateCloudProviderOrElseThrowException();
-               return (T) newResourceSysBaseDTO(userInfo, 
LibraryInstallDTO.class)
-                               
.withComputationalId(computationalResource.getComputationalId())
-                               
.withComputationalName(computationalResource.getComputationalName())
-                               
.withExploratoryName(userInstance.getExploratoryName())
-                               
.withComputationalImage(computationalResource.getImageName())
-                               
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()))
-                               .withLibs(libs);
-       }
-
-       @SuppressWarnings("unchecked")
-       public <T extends LibListComputationalDTO> T 
newLibComputationalList(UserInfo userInfo,
-                                                                               
                                                                 
UserInstanceDTO userInstance,
-                                                                               
                                                                 
UserComputationalResource
-                                                                               
                                                                                
 computationalResource) {
-
-               checkInappropriateCloudProviderOrElseThrowException();
-               return (T) newResourceSysBaseDTO(userInfo, 
LibListComputationalDTO.class)
-                               
.withComputationalId(computationalResource.getComputationalId())
-                               
.withComputationalImage(computationalResource.getImageName())
-                               
.withLibCacheKey(ExploratoryLibCache.libraryCacheKey(userInstance))
-                               
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()));
-       }
-
-       @SuppressWarnings("unchecked")
-       public <T extends ComputationalBase<T>> T 
newComputationalCreate(UserInfo userInfo,
-                                                                               
                                                         UserInstanceDTO 
userInstance,
-                                                                               
                                                         
ComputationalCreateFormDTO form) {
-               T computationalCreate;
-
-               switch (cloudProvider()) {
-                       case AZURE:
-                               throw new 
UnsupportedOperationException("Creating dataengine service is not supported 
yet");
-                       case AWS:
-                               AwsComputationalCreateForm awsForm = 
(AwsComputationalCreateForm) form;
-                               computationalCreate = (T) 
newResourceSysBaseDTO(userInfo, ComputationalCreateAws.class)
-                                               
.withInstanceCount(awsForm.getInstanceCount())
-                                               
.withMasterInstanceType(awsForm.getMasterInstanceType())
-                                               
.withSlaveInstanceType(awsForm.getSlaveInstanceType())
-                                               
.withSlaveInstanceSpot(awsForm.getSlaveInstanceSpot())
-                                               
.withSlaveInstanceSpotPctPrice(awsForm.getSlaveInstanceSpotPctPrice())
-                                               
.withVersion(awsForm.getVersion())
-                                               
.withConfig((awsForm.getConfig()));
-                               break;
-                       case GCP:
-                               GcpComputationalCreateForm gcpForm = 
(GcpComputationalCreateForm) form;
-                               computationalCreate = (T) 
newResourceSysBaseDTO(userInfo, ComputationalCreateGcp.class)
-                                               
.withMasterInstanceCount(gcpForm.getMasterInstanceCount())
-                                               
.withSlaveInstanceCount(gcpForm.getSlaveInstanceCount())
-                                               
.withPreemptibleCount(gcpForm.getPreemptibleCount())
-                                               
.withMasterInstanceType(gcpForm.getMasterInstanceType())
-                                               
.withSlaveInstanceType(gcpForm.getSlaveInstanceType())
-                                               
.withVersion(gcpForm.getVersion());
-                               break;
-
-                       default:
-                               throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
-               }
-
-               return computationalCreate
-                               .withExploratoryName(form.getNotebookName())
-                               .withComputationalName(form.getName())
-                               
.withNotebookTemplateName(userInstance.getTemplateName())
-                               
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()))
-                               
.withNotebookInstanceName(userInstance.getExploratoryId());
-       }
-
-       @SuppressWarnings("unchecked")
-       public <T extends ComputationalBase<T>> T 
newComputationalCreate(UserInfo userInfo,
-                                                                               
                                                         UserInstanceDTO 
userInstance,
-                                                                               
                                                         
SparkStandaloneClusterCreateForm form) {
-
-               T computationalCreate;
-
-               switch (cloudProvider()) {
-                       case AWS:
-                               computationalCreate = (T) 
newResourceSysBaseDTO(userInfo, SparkComputationalCreateAws.class)
-                                               
.withDataEngineInstanceCount(form.getDataEngineInstanceCount())
-                                               
.withDataEngineMasterShape(form.getDataEngineInstanceShape())
-                                               
.withDataEngineSlaveShape(form.getDataEngineInstanceShape())
-                                               .withConfig(form.getConfig());
-                               break;
-                       case AZURE:
-                               computationalCreate = (T) 
newResourceSysBaseDTO(userInfo, SparkComputationalCreateAzure.class)
-                                               
.withDataEngineInstanceCount(form.getDataEngineInstanceCount())
-                                               
.withDataEngineMasterSize(form.getDataEngineInstanceShape())
-                                               
.withDataEngineSlaveSize(form.getDataEngineInstanceShape())
-                                               .withConfig(form.getConfig());
-                               if (settingsDAO.isAzureDataLakeEnabled()) {
-                                       ((SparkComputationalCreateAzure) 
computationalCreate)
-                                                       
.withAzureClientId(settingsDAO.getAzureDataLakeClientId())
-                                                       
.withAzureUserRefreshToken(userInfo.getKeys().get(AZURE_REFRESH_TOKEN_KEY));
-                               }
-
-                               ((SparkComputationalCreateAzure) 
computationalCreate)
-                                               
.withAzureDataLakeEnabled(Boolean.toString(settingsDAO.isAzureDataLakeEnabled()));
-
-                               break;
-                       case GCP:
-                               computationalCreate = (T) 
newResourceSysBaseDTO(userInfo, SparkComputationalCreateGcp.class)
-                                               
.withDataEngineInstanceCount(form.getDataEngineInstanceCount())
-                                               
.withDataEngineMasterSize(form.getDataEngineInstanceShape())
-                                               
.withDataEngineSlaveSize(form.getDataEngineInstanceShape())
-                                               .withConfig(form.getConfig());
-                               break;
-                       default:
-                               throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
-               }
-
-               return computationalCreate
-                               .withExploratoryName(form.getNotebookName())
-                               .withComputationalName(form.getName())
-                               
.withNotebookTemplateName(userInstance.getTemplateName())
-                               
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()))
-                               
.withNotebookInstanceName(userInstance.getExploratoryId());
-       }
-
-       @SuppressWarnings("unchecked")
-       public <T extends ComputationalBase<T>> T 
newComputationalTerminate(UserInfo userInfo,
-                                                                               
                                                                String 
exploratoryName,
-                                                                               
                                                                String 
exploratoryId,
-                                                                               
                                                                String 
computationalName,
-                                                                               
                                                                String 
computationalId,
-                                                                               
                                                                DataEngineType 
dataEngineType) {
-               T computationalTerminate;
-
-               switch (cloudProvider()) {
-                       case AWS:
-                               AwsComputationalTerminateDTO terminateDTO = 
newResourceSysBaseDTO(userInfo,
-                                               
AwsComputationalTerminateDTO.class);
-                               if (dataEngineType == 
DataEngineType.CLOUD_SERVICE) {
-                                       
terminateDTO.setClusterName(computationalId);
-                               }
-                               computationalTerminate = (T) terminateDTO;
-                               break;
-                       case AZURE:
-                               computationalTerminate = (T) 
newResourceSysBaseDTO(userInfo, ComputationalTerminateDTO.class);
-                               break;
-                       case GCP:
-                               GcpComputationalTerminateDTO gcpTerminateDTO = 
newResourceSysBaseDTO(userInfo,
-                                               
GcpComputationalTerminateDTO.class);
-                               if (dataEngineType == 
DataEngineType.CLOUD_SERVICE) {
-                                       
gcpTerminateDTO.setClusterName(computationalId);
-                               }
-                               computationalTerminate = (T) gcpTerminateDTO;
-                               break;
-
-                       default:
-                               throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
-               }
-
-               return computationalTerminate
-                               .withExploratoryName(exploratoryName)
-                               .withComputationalName(computationalName)
-                               .withNotebookInstanceName(exploratoryId);
-       }
-
-       @SuppressWarnings("unchecked")
-       public <T extends ComputationalBase<T>> T newComputationalStop(UserInfo 
userInfo,
-                                                                               
                                                   UserInstanceDTO exploratory,
-                                                                               
                                                   String computationalName) {
-               return (T) newResourceSysBaseDTO(userInfo, 
ComputationalStopDTO.class)
-                               
.withExploratoryName(exploratory.getExploratoryName())
-                               .withComputationalName(computationalName)
-                               
.withNotebookInstanceName(exploratory.getExploratoryId())
-                               
.withApplicationName(getApplicationNameFromImage(exploratory.getImageName()));
-       }
-
-       @SuppressWarnings("unchecked")
-       public <T extends ComputationalBase<T>> T 
newComputationalStart(UserInfo userInfo, UserInstanceDTO exploratory,
-                                                                               
                                                        String 
computationalName) {
-               return (T) newResourceSysBaseDTO(userInfo, 
ComputationalStartDTO.class)
-                               
.withExploratoryName(exploratory.getExploratoryName())
-                               .withComputationalName(computationalName)
-                               
.withNotebookInstanceName(exploratory.getExploratoryId())
-                               
.withApplicationName(getApplicationNameFromImage(exploratory.getImageName()));
-       }
-
-       @SuppressWarnings("unchecked")
-       public <T extends ExploratoryImageDTO> T 
newExploratoryImageCreate(UserInfo userInfo, UserInstanceDTO userInstance,
-                                                                               
                                                           String imageName) {
-               checkInappropriateCloudProviderOrElseThrowException();
-               return (T) newResourceSysBaseDTO(userInfo, 
ExploratoryImageDTO.class)
-                               
.withNotebookInstanceName(userInstance.getExploratoryId())
-                               
.withExploratoryName(userInstance.getExploratoryName())
-                               
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()))
-                               .withNotebookImage(userInstance.getImageName())
-                               .withImageName(imageName);
-       }
-
-
-       @SuppressWarnings("unchecked")
-       public <T extends EnvBackupDTO> T newBackupCreate(BackupFormDTO 
backupFormDTO, String id) {
-
-               return (T) EnvBackupDTO.builder()
-                               .configFiles(backupFormDTO.getConfigFiles())
-                               .certificates(backupFormDTO.getCertificates())
-                               .keys(backupFormDTO.getKeys())
-                               .jars(backupFormDTO.getJars())
-                               
.databaseBackup(backupFormDTO.isDatabaseBackup())
-                               .logsBackup(backupFormDTO.isLogsBackup())
-                               .id(id)
-                               .build();
-       }
-
-       public ComputationalClusterConfigDTO newClusterConfigUpdate(UserInfo 
userInfo, UserInstanceDTO userInstanceDTO,
-                                                                               
                                                UserComputationalResource 
compRes,
-                                                                               
                                                List<ClusterConfig> config) {
-               final ComputationalClusterConfigDTO clusterConfigDTO = 
newResourceSysBaseDTO(userInfo,
-                               ComputationalClusterConfigDTO.class)
-                               
.withExploratoryName(userInstanceDTO.getExploratoryName())
-                               
.withNotebookInstanceName(userInstanceDTO.getExploratoryId())
-                               
.withComputationalName(compRes.getComputationalName())
-                               .withApplicationName(compRes.getImageName());
-               
clusterConfigDTO.setCopmutationalId(compRes.getComputationalId());
-               clusterConfigDTO.setConfig(config);
-               return clusterConfigDTO;
-       }
-
-       public ExploratoryReconfigureSparkClusterActionDTO 
newClusterConfigUpdate(UserInfo userInfo,
-                                                                               
                                                                          
UserInstanceDTO userInstance,
-                                                                               
                                                                          
List<ClusterConfig> config) {
-
-               return newResourceSysBaseDTO(userInfo, 
ExploratoryReconfigureSparkClusterActionDTO.class)
-                               
.withNotebookInstanceName(userInstance.getExploratoryId())
-                               
.withExploratoryName(userInstance.getExploratoryName())
-                               
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()))
-                               .withNotebookImage(userInstance.getImageName())
-                               .withConfig(config);
-       }
-
-       private CloudProvider cloudProvider() {
-               return configuration.getCloudProvider();
-       }
-
-       /**
-        * Returns application name basing on docker image
-        *
-        * @param imageName docker image name
-        * @return application name
-        */
-       private String getApplicationNameFromImage(String imageName) {
-               if (imageName != null) {
-                       int pos = imageName.indexOf('-');
-                       if (pos > 0) {
-                               return imageName.substring(pos + 1);
-                       }
-               }
-               return "";
-       }
-
-       private void checkInappropriateCloudProviderOrElseThrowException() {
-               CloudProvider provider = cloudProvider();
-               if (provider != AWS && provider != AZURE && provider != GCP) {
-                       throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + provider);
-               }
-       }
+    private static final String UNSUPPORTED_CLOUD_PROVIDER_MESSAGE = 
"Unsupported cloud provider ";
+    private static final String AZURE_REFRESH_TOKEN_KEY = "refresh_token";
+
+    @Inject
+    private SelfServiceApplicationConfiguration configuration;
+    @Inject
+    private SettingsDAO settingsDAO;
+
+    private CloudSettings cloudSettings(UserInfo userInfo) {
+        switch (cloudProvider()) {
+            case AWS:
+                return AwsCloudSettings.builder()
+                        .awsRegion(settingsDAO.getAwsRegion())
+                        
.awsSecurityGroupIds(settingsDAO.getAwsSecurityGroups())
+                        .awsSubnetId(settingsDAO.getAwsSubnetId())
+                        .awsVpcId(settingsDAO.getAwsVpcId())
+                        .confTagResourceId(settingsDAO.getConfTagResourceId())
+                        
.awsNotebookSubnetId(settingsDAO.getAwsNotebookSubnetId())
+                        .awsNotebookVpcId(settingsDAO.getAwsNotebookVpcId())
+                        .awsIamUser(userInfo.getName()).build();
+            case AZURE:
+                return AzureCloudSettings.builder()
+                        .azureRegion(settingsDAO.getAzureRegion())
+                        
.azureResourceGroupName(settingsDAO.getAzureResourceGroupName())
+                        
.azureSecurityGroupName(settingsDAO.getAzureSecurityGroupName())
+                        .azureSubnetName(settingsDAO.getAzureSubnetName())
+                        .azureVpcName(settingsDAO.getAzureVpcName())
+                        .azureIamUser(userInfo.getName()).build();
+            case GCP:
+                return 
GcpCloudSettings.builder().gcpIamUser(userInfo.getName()).build();
+            default:
+                throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    private <T extends ResourceBaseDTO<?>> T newResourceBaseDTO(UserInfo 
userInfo, Class<T> resourceClass) {
+        try {
+            return (T) resourceClass.newInstance()
+                    .withEdgeUserName(getEdgeUserName(userInfo))
+                    .withCloudSettings(cloudSettings(userInfo));
+        } catch (Exception e) {
+            throw new DlabException("Cannot create instance of resource class 
" + resourceClass.getName() + ". " +
+                    e.getLocalizedMessage(), e);
+        }
+    }
+
+    private String getEdgeUserName(UserInfo userInfo) {
+        String edgeUser = 
UsernameUtils.replaceWhitespaces(userInfo.getSimpleName());
+        switch (cloudProvider()) {
+            case GCP:
+                return adjustUserName(configuration.getMaxUserNameLength(), 
edgeUser);
+            case AWS:
+            case AZURE:
+                return edgeUser;
+            default:
+                throw new DlabException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + 
cloudProvider());
+        }
+    }
+
+    private String adjustUserName(int maxLength, String userName) {
+        return userName.length() > maxLength ?
+                
UUID.nameUUIDFromBytes(userName.getBytes()).toString().substring(0, maxLength) 
: userName;
+    }
+
+    @SuppressWarnings("unchecked")
+    private <T extends ResourceSysBaseDTO<?>> T newResourceSysBaseDTO(UserInfo 
userInfo, Class<T> resourceClass) {
+        T resource = newResourceBaseDTO(userInfo, resourceClass);
+        return (T) resource
+                .withServiceBaseName(settingsDAO.getServiceBaseName())
+                .withConfOsFamily(settingsDAO.getConfOsFamily());
+    }
+
+    @SuppressWarnings("unchecked")
+    public UploadFile newEdgeKeyUpload(UserInfo userInfo, String content) {
+
+        switch (cloudProvider()) {
+            case AWS:
+                EdgeCreateAws edgeCreateAws = newResourceSysBaseDTO(userInfo, 
EdgeCreateAws.class);
+                UploadFileAws uploadFileAws = new UploadFileAws();
+                uploadFileAws.setEdge(edgeCreateAws);
+                uploadFileAws.setContent(content);
+
+                return uploadFileAws;
+
+            case AZURE:
+                EdgeCreateAzure edgeCreateAzure = 
newResourceSysBaseDTO(userInfo, EdgeCreateAzure.class)
+                        
.withAzureDataLakeEnable(Boolean.toString(settingsDAO.isAzureDataLakeEnabled()));
+
+                UploadFileAzure uploadFileAzure = new UploadFileAzure();
+                uploadFileAzure.setEdge(edgeCreateAzure);
+                uploadFileAzure.setContent(content);
+
+                return uploadFileAzure;
+
+            case GCP:
+                return new UploadFileGcp(newResourceSysBaseDTO(userInfo, 
EdgeCreateGcp.class), content);
+            default:
+                throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
+        }
+    }
+
+    public ReuploadKeyDTO newKeyReupload(UserInfo userInfo, String id, String 
content, List<ResourceData> resources) {
+        return newResourceSysBaseDTO(userInfo, ReuploadKeyDTO.class)
+                .withId(id)
+                .withContent(content)
+                .withResources(resources);
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends ResourceSysBaseDTO<?>> T newEdgeAction(UserInfo 
userInfo) {
+        checkInappropriateCloudProviderOrElseThrowException();
+        return (T) newResourceSysBaseDTO(userInfo, ResourceSysBaseDTO.class);
+    }
+
+    public UserEnvironmentResources newUserEnvironmentStatus(UserInfo 
userInfo) {
+        checkInappropriateCloudProviderOrElseThrowException();
+        return newResourceSysBaseDTO(userInfo, UserEnvironmentResources.class);
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends ExploratoryCreateDTO<T>> T 
newExploratoryCreate(Exploratory exploratory, UserInfo userInfo,
+                                                                      
ExploratoryGitCredsDTO exploratoryGitCredsDTO) {
+
+        T exploratoryCreate;
+
+        switch (cloudProvider()) {
+            case AWS:
+                exploratoryCreate = (T) newResourceSysBaseDTO(userInfo, 
ExploratoryCreateAws.class)
+                        .withNotebookInstanceType(exploratory.getShape());
+                break;
+            case AZURE:
+                exploratoryCreate = (T) newResourceSysBaseDTO(userInfo, 
ExploratoryCreateAzure.class)
+                        .withNotebookInstanceSize(exploratory.getShape());
+                if (settingsDAO.isAzureDataLakeEnabled()) {
+                    ((ExploratoryCreateAzure) exploratoryCreate)
+                            
.withAzureClientId(settingsDAO.getAzureDataLakeClientId())
+                            
.withAzureUserRefreshToken(userInfo.getKeys().get(AZURE_REFRESH_TOKEN_KEY));
+                }
+
+                ((ExploratoryCreateAzure) exploratoryCreate)
+                        
.withAzureDataLakeEnabled(Boolean.toString(settingsDAO.isAzureDataLakeEnabled()));
+                break;
+            case GCP:
+                exploratoryCreate = (T) newResourceSysBaseDTO(userInfo, 
ExploratoryCreateGcp.class)
+                        .withNotebookInstanceType(exploratory.getShape());
+                break;
+
+            default:
+                throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
+        }
+
+        return exploratoryCreate.withExploratoryName(exploratory.getName())
+                .withNotebookImage(exploratory.getDockerImage())
+                
.withApplicationName(getApplicationNameFromImage(exploratory.getDockerImage()))
+                .withGitCreds(exploratoryGitCredsDTO.getGitCreds())
+                .withImageName(exploratory.getImageName())
+                .withClusterConfig(exploratory.getClusterConfig());
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends ExploratoryGitCredsUpdateDTO> T 
newExploratoryStart(UserInfo userInfo,
+                                                                          
UserInstanceDTO userInstance,
+                                                                          
ExploratoryGitCredsDTO
+                                                                               
   exploratoryGitCredsDTO) {
+
+        switch (cloudProvider()) {
+            case AWS:
+            case GCP:
+                return (T) newResourceSysBaseDTO(userInfo, 
ExploratoryGitCredsUpdateDTO.class)
+                        
.withNotebookInstanceName(userInstance.getExploratoryId())
+                        .withGitCreds(exploratoryGitCredsDTO.getGitCreds())
+                        .withNotebookImage(userInstance.getImageName())
+                        .withExploratoryName(userInstance.getExploratoryName())
+                        
.withReuploadKeyRequired(userInstance.isReuploadKeyRequired());
+            case AZURE:
+                T exploratoryStart = (T) newResourceSysBaseDTO(userInfo, 
ExploratoryActionStartAzure.class)
+                        
.withNotebookInstanceName(userInstance.getExploratoryId())
+                        .withGitCreds(exploratoryGitCredsDTO.getGitCreds())
+                        .withNotebookImage(userInstance.getImageName())
+                        .withExploratoryName(userInstance.getExploratoryName())
+                        
.withReuploadKeyRequired(userInstance.isReuploadKeyRequired());
+
+                if (settingsDAO.isAzureDataLakeEnabled()) {
+                    ((ExploratoryActionStartAzure) exploratoryStart)
+                            
.withAzureClientId(settingsDAO.getAzureDataLakeClientId())
+                            
.withAzureUserRefreshToken(userInfo.getKeys().get(AZURE_REFRESH_TOKEN_KEY));
+                }
+
+                ((ExploratoryActionStartAzure) exploratoryStart)
+                        
.withAzureDataLakeEnabled(Boolean.toString(settingsDAO.isAzureDataLakeEnabled()));
+
+                return exploratoryStart;
+            default:
+                throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends ExploratoryActionDTO<T>> T newExploratoryStop(UserInfo 
userInfo, UserInstanceDTO userInstance) {
+
+        T exploratoryStop;
+
+        switch (cloudProvider()) {
+            case AWS:
+            case GCP:
+                exploratoryStop = (T) newResourceSysBaseDTO(userInfo, 
ExploratoryActionDTO.class);
+                break;
+            case AZURE:
+                exploratoryStop = (T) newResourceSysBaseDTO(userInfo, 
ExploratoryActionStopAzure.class);
+                break;
+            default:
+                throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
+        }
+
+        return exploratoryStop
+                .withNotebookInstanceName(userInstance.getExploratoryId())
+                .withNotebookImage(userInstance.getImageName())
+                .withExploratoryName(userInstance.getExploratoryName())
+                .withNotebookImage(userInstance.getImageName())
+                .withReuploadKeyRequired(userInstance.isReuploadKeyRequired());
+    }
+
+    public ExploratoryGitCredsUpdateDTO newGitCredentialsUpdate(UserInfo 
userInfo, UserInstanceDTO instanceDTO,
+                                                                
ExploratoryGitCredsDTO exploratoryGitCredsDTO) {
+        checkInappropriateCloudProviderOrElseThrowException();
+        return newResourceSysBaseDTO(userInfo, 
ExploratoryGitCredsUpdateDTO.class)
+                .withNotebookImage(instanceDTO.getImageName())
+                
.withApplicationName(getApplicationNameFromImage(instanceDTO.getImageName()))
+                .withNotebookInstanceName(instanceDTO.getExploratoryId())
+                .withExploratoryName(instanceDTO.getExploratoryName())
+                .withGitCreds(exploratoryGitCredsDTO.getGitCreds());
+    }
+
+    public LibraryInstallDTO newLibInstall(UserInfo userInfo, UserInstanceDTO 
userInstance,
+                                           List<LibInstallDTO> libs) {
+        checkInappropriateCloudProviderOrElseThrowException();
+        return newResourceSysBaseDTO(userInfo, LibraryInstallDTO.class)
+                .withNotebookImage(userInstance.getImageName())
+                
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()))
+                .withNotebookInstanceName(userInstance.getExploratoryId())
+                .withExploratoryName(userInstance.getExploratoryName())
+                .withLibs(libs);
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends ExploratoryActionDTO<T>> T 
newLibExploratoryList(UserInfo userInfo,
+                                                                       
UserInstanceDTO userInstance) {
+        checkInappropriateCloudProviderOrElseThrowException();
+        return (T) newResourceSysBaseDTO(userInfo, ExploratoryActionDTO.class)
+                .withNotebookInstanceName(userInstance.getExploratoryId())
+                .withNotebookImage(userInstance.getImageName())
+                
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()))
+                .withExploratoryName(userInstance.getExploratoryName());
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends LibraryInstallDTO> T newLibInstall(UserInfo userInfo, 
UserInstanceDTO userInstance,
+                                                         
UserComputationalResource computationalResource,
+                                                         List<LibInstallDTO> 
libs) {
+        checkInappropriateCloudProviderOrElseThrowException();
+        return (T) newResourceSysBaseDTO(userInfo, LibraryInstallDTO.class)
+                
.withComputationalId(computationalResource.getComputationalId())
+                
.withComputationalName(computationalResource.getComputationalName())
+                .withExploratoryName(userInstance.getExploratoryName())
+                .withComputationalImage(computationalResource.getImageName())
+                
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()))
+                .withLibs(libs);
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends LibListComputationalDTO> T 
newLibComputationalList(UserInfo userInfo,
+                                                                         
UserInstanceDTO userInstance,
+                                                                         
UserComputationalResource
+                                                                               
  computationalResource) {
+
+        checkInappropriateCloudProviderOrElseThrowException();
+        return (T) newResourceSysBaseDTO(userInfo, 
LibListComputationalDTO.class)
+                
.withComputationalId(computationalResource.getComputationalId())
+                .withComputationalImage(computationalResource.getImageName())
+                
.withLibCacheKey(ExploratoryLibCache.libraryCacheKey(userInstance))
+                
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()));
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends ComputationalBase<T>> T newComputationalCreate(UserInfo 
userInfo,
+                                                                     
UserInstanceDTO userInstance,
+                                                                     
ComputationalCreateFormDTO form) {
+        T computationalCreate;
+
+        switch (cloudProvider()) {
+            case AZURE:
+                throw new UnsupportedOperationException("Creating dataengine 
service is not supported yet");
+            case AWS:
+                AwsComputationalCreateForm awsForm = 
(AwsComputationalCreateForm) form;
+                computationalCreate = (T) newResourceSysBaseDTO(userInfo, 
ComputationalCreateAws.class)
+                        .withInstanceCount(awsForm.getInstanceCount())
+                        
.withMasterInstanceType(awsForm.getMasterInstanceType())
+                        .withSlaveInstanceType(awsForm.getSlaveInstanceType())
+                        .withSlaveInstanceSpot(awsForm.getSlaveInstanceSpot())
+                        
.withSlaveInstanceSpotPctPrice(awsForm.getSlaveInstanceSpotPctPrice())
+                        .withVersion(awsForm.getVersion())
+                        .withConfig((awsForm.getConfig()));
+                break;
+            case GCP:
+                GcpComputationalCreateForm gcpForm = 
(GcpComputationalCreateForm) form;
+                computationalCreate = (T) newResourceSysBaseDTO(userInfo, 
ComputationalCreateGcp.class)
+                        
.withMasterInstanceCount(gcpForm.getMasterInstanceCount())
+                        
.withSlaveInstanceCount(gcpForm.getSlaveInstanceCount())
+                        .withPreemptibleCount(gcpForm.getPreemptibleCount())
+                        
.withMasterInstanceType(gcpForm.getMasterInstanceType())
+                        .withSlaveInstanceType(gcpForm.getSlaveInstanceType())
+                        .withVersion(gcpForm.getVersion());
+                break;
+
+            default:
+                throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
+        }
+
+        return computationalCreate
+                .withExploratoryName(form.getNotebookName())
+                .withComputationalName(form.getName())
+                .withNotebookTemplateName(userInstance.getTemplateName())
+                
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()))
+                .withNotebookInstanceName(userInstance.getExploratoryId());
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends ComputationalBase<T>> T newComputationalCreate(UserInfo 
userInfo,
+                                                                     
UserInstanceDTO userInstance,
+                                                                     
SparkStandaloneClusterCreateForm form) {
+
+        T computationalCreate;
+
+        switch (cloudProvider()) {
+            case AWS:
+                computationalCreate = (T) newResourceSysBaseDTO(userInfo, 
SparkComputationalCreateAws.class)
+                        
.withDataEngineInstanceCount(form.getDataEngineInstanceCount())
+                        
.withDataEngineMasterShape(form.getDataEngineInstanceShape())
+                        
.withDataEngineSlaveShape(form.getDataEngineInstanceShape())
+                        .withConfig(form.getConfig());
+                break;
+            case AZURE:
+                computationalCreate = (T) newResourceSysBaseDTO(userInfo, 
SparkComputationalCreateAzure.class)
+                        
.withDataEngineInstanceCount(form.getDataEngineInstanceCount())
+                        
.withDataEngineMasterSize(form.getDataEngineInstanceShape())
+                        
.withDataEngineSlaveSize(form.getDataEngineInstanceShape())
+                        .withConfig(form.getConfig());
+                if (settingsDAO.isAzureDataLakeEnabled()) {
+                    ((SparkComputationalCreateAzure) computationalCreate)
+                            
.withAzureClientId(settingsDAO.getAzureDataLakeClientId())
+                            
.withAzureUserRefreshToken(userInfo.getKeys().get(AZURE_REFRESH_TOKEN_KEY));
+                }
+
+                ((SparkComputationalCreateAzure) computationalCreate)
+                        
.withAzureDataLakeEnabled(Boolean.toString(settingsDAO.isAzureDataLakeEnabled()));
+
+                break;
+            case GCP:
+                computationalCreate = (T) newResourceSysBaseDTO(userInfo, 
SparkComputationalCreateGcp.class)
+                        
.withDataEngineInstanceCount(form.getDataEngineInstanceCount())
+                        
.withDataEngineMasterSize(form.getDataEngineInstanceShape())
+                        
.withDataEngineSlaveSize(form.getDataEngineInstanceShape())
+                        .withConfig(form.getConfig());
+                break;
+            default:
+                throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
+        }
+
+        return computationalCreate
+                .withExploratoryName(form.getNotebookName())
+                .withComputationalName(form.getName())
+                .withNotebookTemplateName(userInstance.getTemplateName())
+                
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()))
+                .withNotebookInstanceName(userInstance.getExploratoryId());
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends ComputationalBase<T>> T 
newComputationalTerminate(UserInfo userInfo,
+                                                                        String 
exploratoryName,
+                                                                        String 
exploratoryId,
+                                                                        String 
computationalName,
+                                                                        String 
computationalId,
+                                                                        
DataEngineType dataEngineType) {
+        T computationalTerminate;
+
+        switch (cloudProvider()) {
+            case AWS:
+                AwsComputationalTerminateDTO terminateDTO = 
newResourceSysBaseDTO(userInfo,
+                        AwsComputationalTerminateDTO.class);
+                if (dataEngineType == DataEngineType.CLOUD_SERVICE) {
+                    terminateDTO.setClusterName(computationalId);
+                }
+                computationalTerminate = (T) terminateDTO;
+                break;
+            case AZURE:
+                computationalTerminate = (T) newResourceSysBaseDTO(userInfo, 
ComputationalTerminateDTO.class);
+                break;
+            case GCP:
+                GcpComputationalTerminateDTO gcpTerminateDTO = 
newResourceSysBaseDTO(userInfo,
+                        GcpComputationalTerminateDTO.class);
+                if (dataEngineType == DataEngineType.CLOUD_SERVICE) {
+                    gcpTerminateDTO.setClusterName(computationalId);
+                }
+                computationalTerminate = (T) gcpTerminateDTO;
+                break;
+
+            default:
+                throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + cloudProvider());
+        }
+
+        return computationalTerminate
+                .withExploratoryName(exploratoryName)
+                .withComputationalName(computationalName)
+                .withNotebookInstanceName(exploratoryId);
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends ComputationalBase<T>> T newComputationalStop(UserInfo 
userInfo,
+                                                                   
UserInstanceDTO exploratory,
+                                                                   String 
computationalName) {
+        return (T) newResourceSysBaseDTO(userInfo, ComputationalStopDTO.class)
+                .withExploratoryName(exploratory.getExploratoryName())
+                .withComputationalName(computationalName)
+                .withNotebookInstanceName(exploratory.getExploratoryId())
+                
.withApplicationName(getApplicationNameFromImage(exploratory.getImageName()));
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends ComputationalBase<T>> T newComputationalStart(UserInfo 
userInfo, UserInstanceDTO exploratory,
+                                                                    String 
computationalName) {
+        return (T) newResourceSysBaseDTO(userInfo, ComputationalStartDTO.class)
+                .withExploratoryName(exploratory.getExploratoryName())
+                .withComputationalName(computationalName)
+                .withNotebookInstanceName(exploratory.getExploratoryId())
+                
.withApplicationName(getApplicationNameFromImage(exploratory.getImageName()));
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends ExploratoryImageDTO> T 
newExploratoryImageCreate(UserInfo userInfo, UserInstanceDTO userInstance,
+                                                                       String 
imageName) {
+        checkInappropriateCloudProviderOrElseThrowException();
+        return (T) newResourceSysBaseDTO(userInfo, ExploratoryImageDTO.class)
+                .withNotebookInstanceName(userInstance.getExploratoryId())
+                .withExploratoryName(userInstance.getExploratoryName())
+                
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()))
+                .withNotebookImage(userInstance.getImageName())
+                .withImageName(imageName);
+    }
+
+
+    @SuppressWarnings("unchecked")
+    public <T extends EnvBackupDTO> T newBackupCreate(BackupFormDTO 
backupFormDTO, String id) {
+
+        return (T) EnvBackupDTO.builder()
+                .configFiles(backupFormDTO.getConfigFiles())
+                .certificates(backupFormDTO.getCertificates())
+                .keys(backupFormDTO.getKeys())
+                .jars(backupFormDTO.getJars())
+                .databaseBackup(backupFormDTO.isDatabaseBackup())
+                .logsBackup(backupFormDTO.isLogsBackup())
+                .id(id)
+                .build();
+    }
+
+    public ComputationalClusterConfigDTO newClusterConfigUpdate(UserInfo 
userInfo, UserInstanceDTO userInstanceDTO,
+                                                                
UserComputationalResource compRes,
+                                                                
List<ClusterConfig> config) {
+        final ComputationalClusterConfigDTO clusterConfigDTO = 
newResourceSysBaseDTO(userInfo,
+                ComputationalClusterConfigDTO.class)
+                .withExploratoryName(userInstanceDTO.getExploratoryName())
+                .withNotebookInstanceName(userInstanceDTO.getExploratoryId())
+                .withComputationalName(compRes.getComputationalName())
+                .withApplicationName(compRes.getImageName());
+        clusterConfigDTO.setCopmutationalId(compRes.getComputationalId());
+        clusterConfigDTO.setConfig(config);
+        if (cloudProvider() == AZURE && settingsDAO.isAzureDataLakeEnabled()) {
+            
clusterConfigDTO.setAzureUserRefreshToken(userInfo.getKeys().get(AZURE_REFRESH_TOKEN_KEY));
+        }
+
+        return clusterConfigDTO;
+    }
+
+    public ExploratoryReconfigureSparkClusterActionDTO 
newClusterConfigUpdate(UserInfo userInfo,
+                                                                              
UserInstanceDTO userInstance,
+                                                                              
List<ClusterConfig> config) {
+
+        final ExploratoryReconfigureSparkClusterActionDTO dto =
+                newResourceSysBaseDTO(userInfo, 
ExploratoryReconfigureSparkClusterActionDTO.class)
+                        
.withNotebookInstanceName(userInstance.getExploratoryId())
+                        .withExploratoryName(userInstance.getExploratoryName())
+                        
.withApplicationName(getApplicationNameFromImage(userInstance.getImageName()))
+                        .withNotebookImage(userInstance.getImageName())
+                        .withConfig(config);
+        if (cloudProvider() == AZURE && settingsDAO.isAzureDataLakeEnabled()) {
+            
dto.withAzureUserRefreshToken(userInfo.getKeys().get(AZURE_REFRESH_TOKEN_KEY));
+        }
+
+        return dto;
+
+
+    }
+
+    private CloudProvider cloudProvider() {
+        return configuration.getCloudProvider();
+    }
+
+    /**
+     * Returns application name basing on docker image
+     *
+     * @param imageName docker image name
+     * @return application name
+     */
+    private String getApplicationNameFromImage(String imageName) {
+        if (imageName != null) {
+            int pos = imageName.indexOf('-');
+            if (pos > 0) {
+                return imageName.substring(pos + 1);
+            }
+        }
+        return "";
+    }
+
+    private void checkInappropriateCloudProviderOrElseThrowException() {
+        CloudProvider provider = cloudProvider();
+        if (provider != AWS && provider != AZURE && provider != GCP) {
+            throw new 
IllegalArgumentException(UNSUPPORTED_CLOUD_PROVIDER_MESSAGE + provider);
+        }
+    }
 }
 
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to