Merge branch 'feature/embedded-datasource' into 'release'

Creating an embedded datasource for the action

Now the user can create an embedded datasource as well as a saved datasource for the action. 

This MR also fixes the import for a curl command by setting defaults when an action is created via the curl command. For now, the curl command only accepts short flags and not the long form flags.

See merge request theappsmith/internal-tools-server!204
This commit is contained in:
Arpit Mohan 2020-02-25 08:54:16 +00:00
commit a835c84582
5 changed files with 33 additions and 71 deletions

View File

@ -2,7 +2,6 @@ package com.appsmith.server.domains;
import com.appsmith.external.models.ActionConfiguration;
import com.appsmith.external.models.BaseDomain;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
@ -27,9 +26,6 @@ public class Action extends BaseDomain {
@Transient
Datasource datasource;
@JsonIgnore
String datasourceId;
String organizationId;
String pageId;
@ -55,20 +51,4 @@ public class Action extends BaseDomain {
String cacheResponse;
String templateId; //If action is created via a template, store the id here.
public Datasource getDatasource() {
if (this.datasource != null) {
//The action object has been created from JSON.
return this.datasource;
}
//If the action object has been fetched from the db, it would not have datasource. Create and return one.
if (this.getDatasourceId() != null) {
Datasource datasource = new Datasource();
datasource.setId(this.datasourceId);
return datasource;
}
return null;
}
}

View File

@ -112,15 +112,12 @@ public class ActionServiceImpl extends BaseService<ActionRepository, Action, Str
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.ID));
}
Mono<Action> replaceOrCreateNewDataSourceMono = replaceOrCreateNewDataSource(action);
Mono<Action> dbActionMono = repository.findById(id)
.switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "action", id)));
return Mono.zip(replaceOrCreateNewDataSourceMono, dbActionMono)
.map(tuple -> {
Action updatedActionWithDatasource = tuple.getT1();
Action dbAction = tuple.getT2();
copyNewFieldValuesIntoOldObject(updatedActionWithDatasource, dbAction);
return dbActionMono
.map(dbAction -> {
copyNewFieldValuesIntoOldObject(action, dbAction);
return dbAction;
})
.flatMap(this::validateAndSaveActionToRepository)
@ -133,33 +130,6 @@ public class ActionServiceImpl extends BaseService<ActionRepository, Action, Str
);
}
private Mono<Action> replaceOrCreateNewDataSource(Action action) {
Datasource datasource = action.getDatasource();
if (datasource != null) {
//Update action contains a change for datasource
if (datasource.getId() != null) {
//Datasource changed to another existing data source. Confirm and return.
return datasourceService.findById(datasource.getId())
.switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "datasource", datasource.getId())))
.map(datasource1 -> {
action.setDatasource(datasource1);
action.setDatasourceId(datasource1.getId());
return action;
});
}
//New datasource needs to be created here.
return datasourceService.create(datasource)
.map(datasource1 -> {
action.setDatasource(datasource1);
action.setDatasourceId(datasource1.getId());
return action;
});
}
//No changes in the datasource.
return Mono.just(action);
}
private Boolean validateActionName(String name) {
boolean isValidName = SourceVersion.isName(name);
String pattern = "^((?=[A-Za-z0-9_])(?![\\\\-]).)*$";
@ -220,17 +190,22 @@ public class ActionServiceImpl extends BaseService<ActionRepository, Action, Str
datasourceMono = datasourceService.findById(action.getDatasource().getId())
.switchIfEmpty(Mono.defer(() -> {
action.setIsValid(false);
invalids.add(AppsmithError.NO_RESOURCE_FOUND.getMessage(FieldName.DATASOURCE, action.getDatasourceId()));
invalids.add(AppsmithError.NO_RESOURCE_FOUND.getMessage(FieldName.DATASOURCE, action.getDatasource().getId()));
return Mono.just(action.getDatasource());
}));
}
Mono<Plugin> pluginMono = datasourceMono.flatMap(datasource -> pluginService.findById(datasource.getPluginId())
.switchIfEmpty(Mono.defer(() -> {
action.setIsValid(false);
invalids.add(AppsmithError.NO_RESOURCE_FOUND.getMessage(FieldName.PLUGIN, datasource.getPluginId()));
return Mono.just(new Plugin());
})));
Mono<Plugin> pluginMono = datasourceMono.flatMap(datasource -> {
if (datasource.getPluginId() == null) {
return Mono.error(new AppsmithException(AppsmithError.PLUGIN_ID_NOT_GIVEN));
}
return pluginService.findById(datasource.getPluginId())
.switchIfEmpty(Mono.defer(() -> {
action.setIsValid(false);
invalids.add(AppsmithError.NO_RESOURCE_FOUND.getMessage(FieldName.PLUGIN, datasource.getPluginId()));
return Mono.just(new Plugin());
}));
});
return pluginMono
.zipWith(datasourceMono)
@ -239,7 +214,6 @@ public class ActionServiceImpl extends BaseService<ActionRepository, Action, Str
Plugin plugin = tuple.getT1();
Datasource datasource = tuple.getT2();
action.setDatasource(datasource);
action.setDatasourceId(datasource.getId());
action.setInvalids(invalids);
action.setPluginType(plugin.getType());
return action;
@ -332,10 +306,9 @@ public class ActionServiceImpl extends BaseService<ActionRepository, Action, Str
if (action.getPluginType() == PluginType.JS) {
return Mono.error(new AppsmithException(AppsmithError.UNSUPPORTED_OPERATION));
}
if (action.getDatasourceId() != null) {
return datasourceService.findById(action.getDatasourceId());
} else if (action.getDatasource() != null && action.getDatasource().getId() != null) {
return datasourceService.findById(action.getDatasource().getId());
if (action.getDatasource() != null && action.getDatasource().getId() != null) {
return datasourceService.findById(action.getDatasource().getId())
.switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "datasource")));
}
//The data source in the action has not been persisted.
if (action.getDatasource() != null) {
@ -343,8 +316,7 @@ public class ActionServiceImpl extends BaseService<ActionRepository, Action, Str
} else {
return Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "Valid action"));
}
})
.switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.NO_RESOURCE_FOUND, "resource")));
});
Mono<Plugin> pluginMono = datasourceMono
.flatMap(datasource -> {

View File

@ -47,6 +47,10 @@ public class CurlImporterService extends BaseApiImporter {
Datasource datasource = new Datasource();
DatasourceConfiguration datasourceConfiguration = new DatasourceConfiguration();
//Set defaults
actionConfiguration.setHttpMethod(HttpMethod.GET);
// Matches : "-H 'headerKey:headerValue'
Pattern headerPattern = Pattern.compile(headerRegex);
Matcher headerMatcher = headerPattern.matcher(command);
@ -103,6 +107,10 @@ public class CurlImporterService extends BaseApiImporter {
try {
// If the string doesnt throw an exception when being converted to a URI, its a valid URL.
URI uri = new URL(cmdSplit[i]).toURI();
URL url = new URL(cmdSplit[i]);
String path = url.getFile().substring(0, url.getFile().lastIndexOf('/'));
String base = url.getProtocol() + "://" + url.getHost();
log.debug("url is : {}, \npath is : {} & \nbase is : {}", url.getProtocol() + "://" + url.getHost() + path, path, base);
// If it reaches here, we have successfully found a valid URL.
urlFound = true;
//Extract query params
@ -118,8 +126,10 @@ public class CurlImporterService extends BaseApiImporter {
queryParameters.add(queryParam);
}
actionConfiguration.setQueryParameters(queryParameters);
//Set the URL without the query params
datasourceConfiguration.setUrl(cmdSplit[i].split("\\?")[0]);
//Set the URL without the query params & the path
datasourceConfiguration.setUrl(base);
//Set the path in actionConfiguration
actionConfiguration.setPath(path);
} catch (Exception e) {
//Not a valid URL. Continue to the next word in the CURL command
}

View File

@ -37,7 +37,7 @@ public class DatasourceContextServiceImpl implements DatasourceContextService {
public Mono<DatasourceContext> getDatasourceContext(Datasource datasource) {
String datasourceId = datasource.getId();
if (datasourceId == null) {
log.debug("This is a dry run");
log.debug("This is a dry run or an embedded datasource. The datasource context would not exist in this scenario");
} else if (datasourceContextMap.get(datasourceId) != null) {
log.debug("resource context exists. Returning the same.");
return Mono.just(datasourceContextMap.get(datasourceId));

View File

@ -99,7 +99,7 @@ public class DatasourceServiceImpl extends BaseService<DatasourceRepository, Dat
datasource.setIsValid(false);
invalids.add(AppsmithError.PLUGIN_ID_NOT_GIVEN.getMessage());
datasource.setInvalids(invalids);
return super.create(datasource);
return Mono.just(datasource);
}
Mono<Organization> organizationMono = userMono