Skip to content
This repository has been archived by the owner on Jul 29, 2021. It is now read-only.

Commit

Permalink
feat(plan): Manage plan deployment using sharding tags
Browse files Browse the repository at this point in the history
  • Loading branch information
brasseld authored and NicolasGeraud committed Jun 3, 2019
1 parent 04f62ab commit d54da3e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -32,8 +32,8 @@
<name>Gravitee.io APIM - Repository - JDBC</name>

<properties>
<gravitee-repository.version>1.26.0</gravitee-repository.version>
<gravitee-repository-test.version>1.26.0</gravitee-repository-test.version>
<gravitee-repository.version>1.27.0-SNAPSHOT</gravitee-repository.version>
<gravitee-repository-test.version>1.27.0-SNAPSHOT</gravitee-repository-test.version>
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
<HikariCP.version>3.3.1</HikariCP.version>
<liquibase.version>3.6.3</liquibase.version>
Expand Down
Expand Up @@ -77,17 +77,20 @@ public class JdbcPlanRepository implements PlanRepository {
}
};


private void addApis(Plan parent) {
List<String> apis = getApis(parent.getId());
parent.setApis(new HashSet<>(apis));
}

private void addTags(Plan parent) {
List<String> tags = getTags(parent.getId());
parent.setTags(new HashSet<>(tags));
}

private void addCharacteristics(Plan parent) {
List<String> characteristics = getCharacteristics(parent.getId());
parent.setCharacteristics(characteristics);
}


private void addExcludedGroups(Plan parent) {
List<String> excludedGroups = getExcludedGroups(parent.getId());
Expand All @@ -108,6 +111,7 @@ public Optional<Plan> findById(String id) throws TechnicalException {
if (result.isPresent()) {
addCharacteristics(result.get());
addExcludedGroups(result.get());
addTags(result.get());
}
return result;
} catch (final Exception ex) {
Expand All @@ -125,6 +129,7 @@ public Plan create(Plan item) throws TechnicalException {
storeApis(item, false);
storeCharacteristics(item, false);
storeExcludedGroups(item, false);
storeTags(item, false);
return findById(item.getId()).orElse(null);
} catch (final Exception ex) {
LOGGER.error("Failed to create plan", ex);
Expand All @@ -143,6 +148,7 @@ public Plan update(final Plan plan) throws TechnicalException {
storeApis(plan, true);
storeCharacteristics(plan, true);
storeExcludedGroups(plan, true);
storeTags(plan, true);
return findById(plan.getId()).orElseThrow(() ->
new IllegalStateException(format("No plan found with id [%s]", plan.getId())));
} catch (final IllegalStateException ex) {
Expand All @@ -158,6 +164,7 @@ public void delete(String id) throws TechnicalException {
LOGGER.debug("JdbcPlanRepository.delete({})", id);
try {
jdbcTemplate.update("delete from plan_apis where plan_id = ?", id);
jdbcTemplate.update("delete from plan_tags where plan_id = ?", id);
jdbcTemplate.update("delete from plan_characteristics where plan_id = ?", id);
jdbcTemplate.update("delete from plan_excluded_groups where plan_id = ?", id);
jdbcTemplate.update(ORM.getDeleteSql(), id);
Expand All @@ -171,6 +178,11 @@ private List<String> getApis(String planId) {
LOGGER.debug("JdbcPlanRepository.getApis({})", planId);
return jdbcTemplate.queryForList("select api from plan_apis where plan_id = ?", String.class, planId);
}

private List<String> getTags(String planId) {
LOGGER.debug("JdbcPlanRepository.getTags({})", planId);
return jdbcTemplate.queryForList("select tag from plan_tags where plan_id = ?", String.class, planId);
}

private List<String> getCharacteristics(String planId) {
LOGGER.debug("JdbcPlanRepository.getCharacteristics({})", planId);
Expand Down Expand Up @@ -237,7 +249,24 @@ public int getBatchSize() {
}
});
}
}
}

private void storeTags(Plan plan, boolean deleteFirst) throws TechnicalException {
LOGGER.debug("JdbcPlanRepository.storeTags({}, {})", plan, deleteFirst);
try {
if (deleteFirst) {
jdbcTemplate.update("delete from plan_tags where plan_id = ?", plan.getId());
}
List<String> filteredTags = ORM.filterStrings(plan.getTags());
if (! filteredTags.isEmpty()) {
jdbcTemplate.batchUpdate("insert into plan_tags ( plan_id, tag ) values ( ?, ? )"
, ORM.getBatchStringSetter(plan.getId(), filteredTags));
}
} catch (final Exception ex) {
LOGGER.error("Failed to store tags:", ex);
throw new TechnicalException("Failed to store tags", ex);
}
}

@Override
public Set<Plan> findByApi(String apiId) throws TechnicalException {
Expand All @@ -257,6 +286,7 @@ public Set<Plan> findByApi(String apiId) throws TechnicalException {
addCharacteristics(plan);
addExcludedGroups(plan);
addApis(plan);
addTags(plan);
}
return new HashSet<>(plans);
} catch (final Exception ex) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/resources/liquibase/changelogs/v1_27_0/schema.yml
@@ -0,0 +1,15 @@
databaseChangeLog:
- changeSet:
id: 1.27.0
author: GraviteeSource Team
changes:
- createTable:
tableName: plan_tags
columns:
- column: {name: plan_id, type: nvarchar(64), constraints: { nullable: false } }
- column: {name: tag, type: nvarchar(64), constraints: { nullable: false } }

- addPrimaryKey:
constraintName: pk_plan_tags
columnNames: plan_id, tag
tableName: plan_tags
2 changes: 2 additions & 0 deletions src/main/resources/liquibase/master.yml
Expand Up @@ -49,3 +49,5 @@ databaseChangeLog:
- file: liquibase/changelogs/v1_25_3/schema.yml
- include:
- file: liquibase/changelogs/v1_26_0/schema.yml
- include:
- file: liquibase/changelogs/v1_27_0/schema.yml
Expand Up @@ -85,6 +85,7 @@ public class JdbcTestRepositoryInitializer implements TestRepositoryInitializer
"plan_apis",
"plan_characteristics",
"plan_excluded_groups",
"plan_tags",
"portal_notifications",
"portal_notification_configs",
"portal_notification_config_hooks",
Expand Down

0 comments on commit d54da3e

Please sign in to comment.