Skip to content

Commit

Permalink
Hook up systems (#146)
Browse files Browse the repository at this point in the history
* Initial hook up of systems to the rest of the models

* Hook in DeployableSystem to all the other resources and daos
* Create utility to retrieve system id
* Create jupiter extension to set and clear deployable system
* Change system id Long to long

Relates to #140
  • Loading branch information
chrisrohr authored Mar 5, 2023
1 parent 667cbe5 commit cfb1373
Show file tree
Hide file tree
Showing 44 changed files with 1,383 additions and 755 deletions.
5 changes: 5 additions & 0 deletions service/src/main/java/org/kiwiproject/champagne/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.kiwiproject.champagne.dao.AuditRecordDao;
import org.kiwiproject.champagne.dao.BuildDao;
import org.kiwiproject.champagne.dao.ComponentDao;
import org.kiwiproject.champagne.dao.DeployableSystemDao;
import org.kiwiproject.champagne.dao.DeploymentEnvironmentDao;
import org.kiwiproject.champagne.dao.HostDao;
import org.kiwiproject.champagne.dao.ReleaseDao;
Expand All @@ -35,6 +36,7 @@
import org.kiwiproject.champagne.resource.HostConfigurationResource;
import org.kiwiproject.champagne.resource.TaskResource;
import org.kiwiproject.champagne.resource.UserResource;
import org.kiwiproject.champagne.resource.filter.DeployableSystemRequestFilter;
import org.kiwiproject.champagne.service.ManualTaskService;
import org.kiwiproject.dropwizard.error.ErrorContextBuilder;
import org.kiwiproject.dropwizard.error.dao.ApplicationErrorDao;
Expand Down Expand Up @@ -99,6 +101,7 @@ public void run(AppConfig configuration, Environment environment) {
var hostDao = jdbi.onDemand(HostDao.class);
var componentDao = jdbi.onDemand(ComponentDao.class);
var errorDao = setupApplicationErrors(jdbi, configuration, environment);
var deployableSystemDao = jdbi.onDemand(DeployableSystemDao.class);

var jsonHelper = JsonHelper.newDropwizardJsonHelper();
jdbi.registerRowMapper(Build.class, new BuildMapper(jsonHelper));
Expand All @@ -119,6 +122,8 @@ public void run(AppConfig configuration, Environment environment) {
// Setup jobs
var cleanOutAuditsJob = new CleanOutAuditsJob(auditRecordDao, configuration.getAuditRecordsMaxRetain().toMilliseconds());
registerJob(environment, "Clean Out Audits", configuration.getAuditCleanup(), cleanOutAuditsJob);

environment.jersey().register(new DeployableSystemRequestFilter(deployableSystemDao));
}

private static void setupJsonProcessing(Environment environment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import org.kiwiproject.champagne.model.AuditRecord;
import org.kiwiproject.champagne.dao.mappers.AuditRecordMapper;
import org.kiwiproject.champagne.model.AuditRecord;

import java.time.Instant;
import java.util.List;

@RegisterRowMapper(AuditRecordMapper.class)
public interface AuditRecordDao {

@SqlUpdate("insert into audit_records (user_system_identifier, action, record_type, record_id) " +
"values (:userSystemIdentifier, :action, :recordType, :recordId)")
@SqlUpdate("insert into audit_records (user_system_identifier, action, record_type, record_id, deployable_system_id) " +
"values (:userSystemIdentifier, :action, :recordType, :recordId, :deployableSystemId)")
@GetGeneratedKeys
long insertAuditRecord(@BindBean AuditRecord auditRecord);

@SqlQuery("select * from audit_records order by audit_timestamp desc offset :offset limit :limit")
List<AuditRecord> findPagedAuditRecords(@Bind("offset") int offset, @Bind("limit") int limit);
@SqlQuery("select * from audit_records where deployable_system_id = :systemId order by audit_timestamp desc offset :offset limit :limit")
List<AuditRecord> findPagedAuditRecords(@Bind("offset") int offset, @Bind("limit") int limit, @Bind("systemId") long systemId);

@SqlQuery("select count(*) from audit_records")
long countAuditRecords();
@SqlQuery("select count(*) from audit_records where deployable_system_id = :systemId")
long countAuditRecords(@Bind("systemId") long systemId);

@SqlUpdate("delete from audit_records where audit_timestamp < :maxRetainDate")
int deleteAuditRecordsOlderThan(@Bind("maxRetainDate") Instant maxRetainDate);
Expand Down
60 changes: 30 additions & 30 deletions service/src/main/java/org/kiwiproject/champagne/dao/BuildDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,77 @@
import static org.kiwiproject.base.KiwiStrings.f;
import static org.kiwiproject.champagne.dao.DaoHelper.LIKE_QUERY_FORMAT;

import java.util.List;

import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.BindBean;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import org.kiwiproject.champagne.model.Build;

import java.util.List;

public interface BuildDao {

default List<Build> findPagedBuilds(int offset, int limit, String componentIdentifierFilter, String componentVersionFilter) {
default List<Build> findPagedBuilds(int offset, int limit, long systemId, String componentIdentifierFilter, String componentVersionFilter) {
if (isBlank(componentIdentifierFilter) && isBlank(componentVersionFilter)) {
// No filtering
return findPagedBuildsNoFilter(offset, limit);
return findPagedBuildsNoFilter(offset, limit, systemId);
} else if (isBlank(componentIdentifierFilter)) {
// Version only filter
return findPagedBuildsWithVersionFilter(offset, limit, f(LIKE_QUERY_FORMAT, componentVersionFilter));
return findPagedBuildsWithVersionFilter(offset, limit, systemId, f(LIKE_QUERY_FORMAT, componentVersionFilter));
} else if (isBlank(componentVersionFilter)) {
// Name only filter
return findPagedBuildsWithIdentiferFilter(offset, limit, f(LIKE_QUERY_FORMAT, componentIdentifierFilter));
return findPagedBuildsWithIdentiferFilter(offset, limit, systemId, f(LIKE_QUERY_FORMAT, componentIdentifierFilter));
}

// Both name and version filtering
return findPagedBuildsWithVersionAndIdentifierFilters(offset, limit, componentIdentifierFilter, componentVersionFilter);
return findPagedBuildsWithVersionAndIdentifierFilters(offset, limit, systemId, componentIdentifierFilter, componentVersionFilter);
}

@SqlQuery("select * from builds order by created_at desc offset :offset limit :limit")
List<Build> findPagedBuildsNoFilter(@Bind("offset") int offset, @Bind("limit") int limit);
@SqlQuery("select * from builds where deployable_system_id = :systemId order by created_at desc offset :offset limit :limit")
List<Build> findPagedBuildsNoFilter(@Bind("offset") int offset, @Bind("limit") int limit, @Bind("systemId") long systemId);

@SqlQuery("select * from builds where component_version like :version order by created_at desc offset :offset limit :limit")
List<Build> findPagedBuildsWithVersionFilter(@Bind("offset") int offset, @Bind("limit") int limit, @Bind("version") String componentVersionFilter);
@SqlQuery("select * from builds where builds.deployable_system_id = :systemId and component_version like :version order by created_at desc offset :offset limit :limit")
List<Build> findPagedBuildsWithVersionFilter(@Bind("offset") int offset, @Bind("limit") int limit, @Bind("systemId") long systemId, @Bind("version") String componentVersionFilter);

@SqlQuery("select * from builds where component_identifier like :identifier order by created_at desc offset :offset limit :limit")
List<Build> findPagedBuildsWithIdentiferFilter(@Bind("offset") int offset, @Bind("limit") int limit, @Bind("identifier") String componentIdentifierFilter);
@SqlQuery("select * from builds where builds.deployable_system_id = :systemId and component_identifier like :identifier order by created_at desc offset :offset limit :limit")
List<Build> findPagedBuildsWithIdentiferFilter(@Bind("offset") int offset, @Bind("limit") int limit, @Bind("systemId") long systemId, @Bind("identifier") String componentIdentifierFilter);

@SqlQuery("select * from builds where component_version like :version and component_identifier like :identifier order by created_at desc offset :offset limit :limit")
List<Build> findPagedBuildsWithVersionAndIdentifierFilters(@Bind("offset") int offset, @Bind("limit") int limit, @Bind("identifier") String componentIdentifierFilter, @Bind("version") String componentVersionFilter);
@SqlQuery("select * from builds where builds.deployable_system_id = :systemId and component_version like :version and component_identifier like :identifier order by created_at desc offset :offset limit :limit")
List<Build> findPagedBuildsWithVersionAndIdentifierFilters(@Bind("offset") int offset, @Bind("limit") int limit, @Bind("systemId") long systemId, @Bind("identifier") String componentIdentifierFilter, @Bind("version") String componentVersionFilter);

default long countBuilds(String componentIdentifierFilter, String componentVersionFilter) {
default long countBuilds(long systemId, String componentIdentifierFilter, String componentVersionFilter) {
if (isBlank(componentIdentifierFilter) && isBlank(componentVersionFilter)) {
// No filtering
return countBuildsNoFilter();
return countBuildsNoFilter(systemId);
} else if (isBlank(componentIdentifierFilter)) {
// Version only filter
return countBuildsWithVersionFilter(componentVersionFilter);
return countBuildsWithVersionFilter(systemId, componentVersionFilter);
} else if (isBlank(componentVersionFilter)) {
// Name only filter
return countBuildsWithIdentifierFilter(componentIdentifierFilter);
return countBuildsWithIdentifierFilter(systemId, componentIdentifierFilter);
}

// Both name and version filtering
return countBuildsWithVersionAndIdentifierFilters(componentVersionFilter, componentIdentifierFilter);
return countBuildsWithVersionAndIdentifierFilters(systemId, componentVersionFilter, componentIdentifierFilter);
}

@SqlQuery("select count(*) from builds")
long countBuildsNoFilter();
@SqlQuery("select count(*) from builds where deployable_system_id = :systemId")
long countBuildsNoFilter(@Bind("systemId") long systemId);

@SqlQuery("select count(*) from builds where component_version like :version")
long countBuildsWithVersionFilter(@Bind("version") String componentVersionFilter);
@SqlQuery("select count(*) from builds where builds.deployable_system_id = :systemId and component_version like :version")
long countBuildsWithVersionFilter(@Bind("systemId") long systemId, @Bind("version") String componentVersionFilter);

@SqlQuery("select count(*) from builds where component_identifier like :identifier")
long countBuildsWithIdentifierFilter(@Bind("identifier") String componentIdentifierFilter);
@SqlQuery("select count(*) from builds where builds.deployable_system_id = :systemId and component_identifier like :identifier")
long countBuildsWithIdentifierFilter(@Bind("systemId") long systemId, @Bind("identifier") String componentIdentifierFilter);

@SqlQuery("select count(*) from builds where component_version like :version and component_identifier like :identifier")
long countBuildsWithVersionAndIdentifierFilters(@Bind("version") String componentVersionFilter, @Bind("identifier") String componentIdentifierFilter);
@SqlQuery("select count(*) from builds where builds.deployable_system_id = :systemId and component_version like :version and component_identifier like :identifier")
long countBuildsWithVersionAndIdentifierFilters(@Bind("systemId") long systemId, @Bind("version") String componentVersionFilter, @Bind("identifier") String componentIdentifierFilter);

@SqlUpdate("insert into builds "
+ "(repo_namespace, repo_name, commit_ref, commit_user, source_branch, component_identifier, component_version, distribution_location, extra_deployment_info, change_log, git_provider) "
+ "(repo_namespace, repo_name, commit_ref, commit_user, source_branch, component_identifier, component_version, distribution_location, extra_deployment_info, change_log, git_provider, deployable_system_id) "
+ "values "
+ "(:repoNamespace, :repoName, :commitRef, :commitUser, :sourceBranch, :componentIdentifier, :componentVersion, :distributionLocation, :extraData, :changeLog, :gitProvider)")
+ "(:repoNamespace, :repoName, :commitRef, :commitUser, :sourceBranch, :componentIdentifier, :componentVersion, :distributionLocation, :extraData, :changeLog, :gitProvider, :deployableSystemId)")
@GetGeneratedKeys
long insertBuild(@BindBean Build build, @Bind("extraData") String extraDataJson);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ public interface DeployableSystemDao {

@SqlQuery("select system_admin from users_deployable_systems where user_id = :userId and deployable_system_id = :systemId")
boolean isUserAdminOfSystem(@Bind("userId") long userId, @Bind("systemId") long systemId);

@SqlQuery("select true from users_deployable_systems uds join users u on u.id = uds.user_id join deployable_systems ds on ds.id = uds.deployable_system_id where u.system_identifier = :userName and ds.id = :systemId")
boolean isUserBySystemIdentifierInSystem(@Bind("userName") String systemIdentifier, @Bind("systemId") long systemId);
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package org.kiwiproject.champagne.dao;

import java.util.List;

import org.jdbi.v3.sqlobject.config.RegisterRowMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.BindBean;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import org.kiwiproject.champagne.model.DeploymentEnvironment;
import org.kiwiproject.champagne.dao.mappers.DeploymentEnvironmentMapper;
import org.kiwiproject.champagne.model.DeploymentEnvironment;

import java.util.List;

@RegisterRowMapper(DeploymentEnvironmentMapper.class)
public interface DeploymentEnvironmentDao {
@SqlUpdate("insert into deployment_environments (environment_name) values (:name)")
@SqlUpdate("insert into deployment_environments (environment_name, deployable_system_id) values (:name, :deployableSystemId)")
@GetGeneratedKeys
long insertEnvironment(@BindBean DeploymentEnvironment env);

@SqlUpdate("update deployment_environments set environment_name = :name, updated_at = current_timestamp where id = :id")
int updateEnvironment(@BindBean DeploymentEnvironment env);

@SqlQuery("select * from deployment_environments")
List<DeploymentEnvironment> findAllEnvironments();
@SqlQuery("select * from deployment_environments where deployable_system_id = :systemId")
List<DeploymentEnvironment> findAllEnvironments(@Bind("systemId") long systemId);

@SqlUpdate("delete from deployment_environments where id = :id")
int hardDeleteById(@Bind("id") long id);
Expand Down
12 changes: 6 additions & 6 deletions service/src/main/java/org/kiwiproject/champagne/dao/HostDao.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.kiwiproject.champagne.dao;

import java.util.List;
import java.util.Optional;

import org.jdbi.v3.sqlobject.config.RegisterRowMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.BindBean;
Expand All @@ -12,13 +9,16 @@
import org.kiwiproject.champagne.dao.mappers.HostMapper;
import org.kiwiproject.champagne.model.Host;

import java.util.List;
import java.util.Optional;

@RegisterRowMapper(HostMapper.class)
public interface HostDao {

@SqlQuery("select * from hosts where environment_id = :envId")
List<Host> findHostsByEnvId(@Bind("envId") Long envId);
@SqlQuery("select * from hosts where environment_id = :envId and deployable_system_id = :systemId")
List<Host> findHostsByEnvId(@Bind("envId") Long envId, @Bind("systemId") long systemId);

@SqlUpdate("insert into hosts (environment_id, hostname, tags, source) values (:environmentId, :hostname, :tagCsv, :source)")
@SqlUpdate("insert into hosts (environment_id, hostname, tags, source, deployable_system_id) values (:environmentId, :hostname, :tagCsv, :source, :deployableSystemId)")
@GetGeneratedKeys
long insertHost(@BindBean Host host, @Bind("tagCsv") String tagCsv);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
@RegisterRowMapper(ReleaseMapper.class)
public interface ReleaseDao {

@SqlUpdate("insert into manual_deployment_task_releases (release_number) values (:releaseNumber)")
@SqlUpdate("insert into manual_deployment_task_releases (release_number, deployable_system_id) values (:releaseNumber, :deployableSystemId)")
@GetGeneratedKeys
long insertRelease(@BindBean Release release);

@SqlQuery("select count(*) from manual_deployment_task_releases")
long countReleases();
@SqlQuery("select count(*) from manual_deployment_task_releases where deployable_system_id = :systemId")
long countReleases(@Bind("systemId") long systemId);

@SqlQuery("select * from manual_deployment_task_releases order by release_number desc offset :offset limit :limit")
List<Release> findPagedReleases(@Bind("offset") int offset, @Bind("limit") int limit);
@SqlQuery("select * from manual_deployment_task_releases where deployable_system_id = :systemId order by release_number desc offset :offset limit :limit")
List<Release> findPagedReleases(@Bind("offset") int offset, @Bind("limit") int limit, @Bind("systemId") long systemId);

@SqlUpdate("delete from manual_deployment_task_releases where id = :id")
void deleteById(@Bind("id") long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public AuditRecord map(ResultSet rs, StatementContext ctx) throws SQLException {
.action(enumValueOrNull(rs, "action", AuditRecord.Action.class))
.recordType(rs.getString("record_type"))
.recordId(rs.getLong("record_id"))
.deployableSystemId(rs.getLong("deployable_system_id"))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public enum Action {
* longs for the id field.
*/
private final Long recordId;

/**
* The Deployable System that this audit record is tied to
*/
Long deployableSystemId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import lombok.Builder;
import lombok.Getter;
import lombok.With;

/**
* Model used to represent the build of a particular deployment artifact.
Expand Down Expand Up @@ -38,4 +39,10 @@ public class Build {
private String componentVersion;
private String distributionLocation;
private Map<String, Object> extraDeploymentInfo;

/**
* The Deployable System that this build is tied to
*/
@With
Long deployableSystemId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import lombok.Builder;
import lombok.Value;
import lombok.With;

/**
* Representation of a deployable component in the system.
Expand All @@ -26,4 +27,10 @@ public class Component {
*/
String tag;

/**
* The Deployable System that this component is tied to
*/
@With
Long deployableSystemId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.kiwiproject.champagne.model;

import java.util.Optional;

import lombok.experimental.UtilityClass;

@UtilityClass
public class DeployableSystemThreadLocal {

private static final ThreadLocal<Long> THREAD_LOCAL = new ThreadLocal<>();

public static Optional<Long> getCurrentDeployableSystem() {
return Optional.ofNullable(THREAD_LOCAL.get());
}

public static void setCurrentDeployableSystem(Long id) {
THREAD_LOCAL.set(id);
}

public static void clearDeployableSystem() {
THREAD_LOCAL.remove();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import lombok.Builder;
import lombok.Value;
import lombok.With;

/**
* The core model used to track environments in the system (e.g. dev, test, production).
Expand All @@ -29,4 +30,10 @@ public class DeploymentEnvironment {

boolean deleted;

/**
* The Deployable System that this env is tied to
*/
@With
Long deployableSystemId;

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import lombok.Builder;
import lombok.Value;
import lombok.With;

/**
* Representation of a host used to deploy components. A Host could be created and stored in the database or
Expand Down Expand Up @@ -41,4 +42,10 @@ public enum Source {
* The source of truth for the definition of this host, whether it is stored in Champagne or pulled dynamically from a cloud.
*/
Source source;

/**
* The Deployable System that this host is tied to
*/
@With
Long deployableSystemId;
}
Loading

0 comments on commit cfb1373

Please sign in to comment.