Skip to content

Commit

Permalink
Decouple secrets manager library usage (#66)
Browse files Browse the repository at this point in the history
* start refactor using templates

* dirs creation using json template

* generate model using templates

* remove redundant dirs from json array

* refactor default task name

* extract base task methods to module builder, build azure pipeline with templates

* update usecase generation with template

* update usecase generation with template

* generate rest entrypoints with template

* generate rest entrypoints with template

* generate adapters and entrypoints using templates

* append dependencies to subproject, update yaml properties

* add some unit tests

* add some unit tests

* update version and readme

* update gitignore

* add new projec dependencies automatically to build.gradle, update h2 setup for jpa, generate generic driven adapter and entry point

* release new version with mustache templates

* add some test

* fix source compatibility

* fix source compatibility

* move jpa commons directly in jpa driven adapter

* move mongo commons directly in mongodb driven adapter

* update functional test

* fix merge

* setup config model for mongodb

* add some unit test assertions

* allow secrets manager option creating a datasource driven adapter

* update readme task options

* create some unit test

* update version, fix some code smell

* remove unused autogenerated methods

* remove module depencency
  • Loading branch information
juancgalvis authored Jun 17, 2020
1 parent bee277d commit 9d747ae
Show file tree
Hide file tree
Showing 37 changed files with 367 additions and 114 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,24 @@ gradle generateDrivenAdapter --type=[drivenAdapterType]
gradle gda --type [drivenAdapterType]
```

| Reference driven adapter value | Name |
| ------------------ | ------------ |
| GENERIC|Empty Driven Adapter |
| JPA|JPA Repository |
| MONGODB|Mongo Repository |
| ASYNCEVENTBUS|Async Event Bus |
|Reference for drivenAdapterType|Name |Additional Options |
|-------------------------------|--------------------|---------------------|
|GENERIC |Empty Driven Adapter|--name [name] |
|JPA |JPA Repository |--secret [true-false]|
|MONGODB |Mongo Repository |--secret [true-false]|
|ASYNCEVENTBUS |Async Event Bus | |

5 The ```generateEntryPoint | gep``` task will generate a class in Infrastructure layer, this task have one required parameter ```type```.
```sh
gradle generateEntryPoint --type=[entryPointType]
gradle gep --type [entryPointType]
```
| Reference entry point value | Name |
| ------------------ | ------------ |
| GENERIC|Empty Entry Point |
| RESTMVC|API REST (Spring Boot Starter Web) |
| WEBFLUX|API REST (Spring Boot Starter WebFlux) |


|Reference for entryPointType|Name |Additional Options|
|----------------------------|--------------------------------------|------------------|
|GENERIC |Empty Entry Point |--name [name] |
|RESTMVC |API REST (Spring Boot Starter Web) | |
|WEBFLUX |API REST (Spring Boot Starter WebFlux)| |

6 The ```validateStructure | vs``` Validate that project references aren't violated.
```sh
Expand All @@ -85,9 +84,10 @@ gradle vs
gradle generatePipeline --type=[pipelineType]
gradle gpl --type=[pipelineType]
````
| Reference pipeline value | Name |
| ------------------ | ------------ |
| AZURE|Azure Pipeline |

|Reference for pipelineType|Name |
|--------------------------|--------------|
|AZURE |Azure Pipeline|


How I can help?
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
package=co.com.bancolombia
systemProp.version=1.6.0
systemProp.version=1.6.1
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,8 @@ public void canRunTaskGenerateDrivenAdapterWithParameters() {
assertTrue(new File("build/functionalTest/infrastructure/driven-adapters/jpa-repository/build.gradle").exists());
assertTrue(new File("build/functionalTest/infrastructure/driven-adapters/jpa-repository/src/main/java/co/com/bancolombia/jpa/JPARepository.java").exists());
assertTrue(new File("build/functionalTest/infrastructure/driven-adapters/jpa-repository/src/main/java/co/com/bancolombia/jpa/JPARepositoryAdapter.java").exists());

assertTrue(new File("build/functionalTest/infrastructure//helpers/jpa-repository-commons/build.gradle").exists());
assertTrue(new File("build/functionalTest/infrastructure/helpers/jpa-repository-commons/src/main/java/co/com/bancolombia/jpa/AdapterOperations.java").exists());

// TODO: Enable test
// assertTrue(new File("build/functionalTest/applications/app-service/src/main/java/co/com/bancolombia/config/jpa/JpaConfig.java").exists());
// assertTrue(new File("build/functionalTest/applications/app-service/src/main/resources/application-jpaAdapter.yaml").exists());
// assertTrue(new File("build/functionalTest/domain/model/src/main/java/co/com/bancolombia/model/secret/Secret.java").exists());
assertTrue(new File("build/functionalTest/infrastructure/driven-adapters/jpa-repository/src/main/java/co/com/bancolombia/jpa/helper/AdapterOperations.java").exists());
assertTrue(new File("build/functionalTest/applications/app-service/src/main/java/co/com/bancolombia/config/JpaConfig.java").exists());

assertEquals(result.task(":" + task).getOutcome(), TaskOutcome.SUCCESS);
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/co/com/bancolombia/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ public class Constants {
public static final String SPRING_CLOUD_VERSION = "Greenwich.M1";
public static final String SONAR_VERSION = "2.7";
public static final String JACOCO_VERSION = "0.8.5";
public static final String PLUGIN_VERSION = "1.6.0";
public static final String PLUGIN_VERSION = "1.6.1";
public static final String SECRETS_VERSION = "2.1.0";

public enum BooleanOption {
TRUE, FALSE
}
}
14 changes: 12 additions & 2 deletions src/main/java/co/com/bancolombia/factory/ModuleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ public void setupFromTemplate(String resourceGroup) throws IOException, ParamNot
}

public void appendToSettings(String module, String baseDir) throws IOException {
appendToFile("settings.gradle", settings -> settings + ("\ninclude ':" + module + "'\n" +
"project(':" + module + "').projectDir = file('./" + baseDir + "/" + module + "')"));
appendToFile("settings.gradle", settings -> {
String toAppend = "\ninclude ':" + module + "'\nproject(':" + module + "').projectDir = file('./" + baseDir
+ "/" + module + "')";
if (settings.contains(toAppend)) {
return settings;
}
return settings + toAppend;
});
}

public void appendDependencyToModule(String module, String dependency) throws IOException {
Expand Down Expand Up @@ -127,6 +133,10 @@ public String getStringParam(String key) {
return (String) params.get(key);
}

public Boolean getBooleanParam(String key) {
return (Boolean) params.get(key);
}

private void appendToFile(String path, FileAppender appender) throws IOException {
FileModel current = files.get(path);
String content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class DrivenAdapterJPA implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.addParamPackage(FileUtils.readProperties("package"));
builder.setupFromTemplate("helper/jpa-repository-commons"); // TODO: include in driven adapter
builder.setupFromTemplate("driven-adapter/jpa-repository");
builder.appendToSettings("jpa-repository", "infrastructure/driven-adapters");
builder.appendToSettings("jpa-repository-commons", "infrastructure/helpers");
Expand All @@ -23,6 +22,6 @@ public void buildModule(ModuleBuilder builder) throws IOException, CleanExceptio
builder.appendToProperties("spring.jpa")
.put("databasePlatform", "org.hibernate.dialect.H2Dialect");
builder.appendDependencyToModule("app-service", "implementation project(':jpa-repository')");
// TODO: Generate JPA Config validate secrets manager usage
new DrivenAdapterSecrets().buildModule(builder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ public class DrivenAdapterMongoDB implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
builder.addParamPackage(FileUtils.readProperties("package"));
builder.setupFromTemplate("helper/mongo-repository-commons"); // TODO: include in driven adapter
builder.setupFromTemplate("driven-adapter/mongo-repository");
builder.appendToSettings("mongo-repository", "infrastructure/driven-adapters");
builder.appendToSettings("mongo-repository-commons", "infrastructure/helpers");
builder.appendToProperties("spring.data.mongodb")
.put("uri", "mongodb://user:pass@hostname/db");
.put("uri", "mongodb://localhost:27017/test");
builder.appendDependencyToModule("app-service",
"implementation project(':mongo-repository')");
new DrivenAdapterSecrets().buildModule(builder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package co.com.bancolombia.factory.adapters;

import co.com.bancolombia.Constants;
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;

import java.io.IOException;

public class DrivenAdapterSecrets implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
if (builder.getBooleanParam("include-secret")) {
builder.setupFromTemplate("driven-adapter/secrets");
builder.appendDependencyToModule("app-service",
"compile 'co.com.bancolombia:secretsmanager:" + Constants.SECRETS_VERSION + "'");
builder.appendToProperties("aws")
.put("region", "us-east-1")
.put("secretName", "my-secret");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@ public class ModuleFactoryDrivenAdapter {

public static ModuleFactory getDrivenAdapterFactory(DrivenAdapterType type) throws InvalidTaskOptionException {
switch (type) {
case JPA: {
case JPA:
return new DrivenAdapterJPA();
}
case MONGODB: {
case MONGODB:
return new DrivenAdapterMongoDB();
}
case ASYNCEVENTBUS: {
case ASYNCEVENTBUS:
return new DrivenAdapterAsyncEventBus();
}
case GENERIC: {
case GENERIC:
return new DrivenAdapterGeneric();
}
default: {
default:
throw new InvalidTaskOptionException("Driven Adapter type invalid");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ public class ModuleFactoryEntryPoint {

public static ModuleFactory getEntryPointFactory(EntryPointType type) throws InvalidTaskOptionException {
switch (type) {
case RESTMVC: {
case RESTMVC:
return new EntryPointRestMvc();
}
case WEBFLUX: {
case WEBFLUX:
return new EntryPointRestWebflux();
}
case GENERIC: {
case GENERIC:
return new EntryPointGeneric();
}
default: {
default:
throw new InvalidTaskOptionException("Entry Point type invalid");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ public class ModuleFactoryPipeline {
@SuppressWarnings("SwitchStatementWithTooFewBranches")
public static ModuleFactory getPipelineFactory(PipelineType type) throws InvalidTaskOptionException {
switch (type) {
case AZURE: {
case AZURE:
return new PipelineAzure();
}
default: {
default:
throw new InvalidTaskOptionException("Pipeline value invalid");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package co.com.bancolombia.models;

import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;
import java.util.Map;

@Data
@NoArgsConstructor
@Getter
@Setter
public class TemplateDefinition {
private Map<String, String> files;
private List<String> folders;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.com.bancolombia.task;

import co.com.bancolombia.Constants.BooleanOption;
import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
Expand All @@ -23,6 +24,7 @@ public class GenerateDrivenAdapterTask extends DefaultTask {

private DrivenAdapterType type;
private String name;
private BooleanOption secret = BooleanOption.FALSE;

@Option(option = "type", description = "Set type of driven adapter to be generated")
public void setType(DrivenAdapterType type) {
Expand All @@ -34,13 +36,21 @@ public void setName(String name) {
this.name = name;
}

// TODO: Enable autogeneration of secrets manager boolean
@Option(option = "secret", description = "Enable secrets for this driven adapter")
public void setSecret(BooleanOption secret) {
this.secret = secret;
}

@OptionValues("type")
public List<DrivenAdapterType> getTypes() {
return new ArrayList<>(Arrays.asList(DrivenAdapterType.values()));
}

@OptionValues("secret")
public List<BooleanOption> getSecretOptions() {
return new ArrayList<>(Arrays.asList(BooleanOption.values()));
}

@TaskAction
public void generateDrivenAdapterTask() throws IOException, CleanException {
if (type == null) {
Expand All @@ -51,6 +61,7 @@ public void generateDrivenAdapterTask() throws IOException, CleanException {
logger.lifecycle("Clean Architecture plugin version: {}", Utils.getVersionPlugin());
logger.lifecycle("Driven Adapter type: {}", type);
builder.addParam("task-param-name", name);
builder.addParam("include-secret", secret == BooleanOption.TRUE);
moduleFactory.buildModule(builder);
builder.persist();
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/co/com/bancolombia/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public static String formatTaskOptions(List<?> options) {
}

public static String addDependency(String build, String dependency) {
if (build.contains(dependency)) {
return build;
}
int start = build.indexOf("dependencies");
int realStart = build.indexOf('{', start);
return build.substring(0, realStart + 1) + "\n\t" + dependency + build.substring(realStart + 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
dependencies {
implementation project(':model')
implementation project(':jpa-repository-commons')
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.reactivecommons.utils:object-mapper-api:0.1.0'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.reactivecommons.utils:object-mapper-api:0.1.0'
runtime 'com.h2database:h2'
testCompile 'org.reactivecommons.utils:object-mapper:0.1.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package {{package}}.jpa.config;

{{#include-secret}}
import lombok.Data;
{{/include-secret}}
{{^include-secret}}
import lombok.Builder;
import lombok.Getter;
{{/include-secret}}

{{#include-secret}}
@Data
{{/include-secret}}
{{^include-secret}}
@Builder
@Getter
{{/include-secret}}
public class DBSecret {
private {{^include-secret}}final {{/include-secret}}String url;
private {{^include-secret}}final {{/include-secret}}String username;
private {{^include-secret}}final {{/include-secret}}String password;
}
Loading

0 comments on commit 9d747ae

Please sign in to comment.