Skip to content

Commit

Permalink
Hook in DeployableSystem to all the other resources and daos
Browse files Browse the repository at this point in the history
Relates to #140
  • Loading branch information
chrisrohr committed Mar 4, 2023
1 parent 08e0713 commit 983a7b9
Show file tree
Hide file tree
Showing 39 changed files with 1,080 additions and 684 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 @@ -15,16 +15,16 @@
@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
56 changes: 28 additions & 28 deletions service/src/main/java/org/kiwiproject/champagne/dao/BuildDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,66 +15,66 @@

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
Expand Up @@ -13,15 +13,15 @@

@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
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
@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 @@ -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 @@ -42,5 +43,6 @@ public class Build {
/**
* 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 @@ -29,6 +30,7 @@ public class Component {
/**
* The Deployable System that this component is tied to
*/
@With
Long deployableSystemId;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public static void setCurrentDeployableSystem(Long id) {
THREAD_LOCAL.set(id);
}

public static void clearCurrentDeployableSystem() {
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 @@ -32,6 +33,7 @@ public class DeploymentEnvironment {
/**
* 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 @@ -45,5 +46,6 @@ public enum Source {
/**
* The Deployable System that this host is tied to
*/
@With
Long deployableSystemId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

/**
* The top-level container for all manual deployment tasks for a specific release.
Expand All @@ -28,5 +29,6 @@ public class Release {
/**
* The Deployable System that this release is tied to
*/
@With
Long deployableSystemId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import javax.ws.rs.core.Response;

import org.kiwiproject.champagne.dao.AuditRecordDao;
import org.kiwiproject.champagne.model.DeployableSystemThreadLocal;
import org.kiwiproject.jaxrs.exception.JaxrsBadRequestException;
import org.kiwiproject.spring.data.KiwiPage;

import com.codahale.metrics.annotation.ExceptionMetered;
Expand All @@ -37,8 +39,11 @@ public class AuditRecordResource {
public Response getPagedAudits(@QueryParam("pageNumber") @DefaultValue("1") int pageNumber,
@QueryParam("pageSize") @DefaultValue("50") int pageSize) {

var audits = auditRecordDao.findPagedAuditRecords(zeroBasedOffset(pageNumber, pageSize), pageSize);
var totalCount = auditRecordDao.countAuditRecords();
var systemId = DeployableSystemThreadLocal.getCurrentDeployableSystem()
.orElseThrow(() -> new JaxrsBadRequestException("Missing deployable system"));

var audits = auditRecordDao.findPagedAuditRecords(zeroBasedOffset(pageNumber, pageSize), pageSize, systemId);
var totalCount = auditRecordDao.countAuditRecords(systemId);

return Response.ok(KiwiPage.of(pageNumber, pageSize, totalCount, audits).usingOneAsFirstPage()).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import org.kiwiproject.champagne.model.AuditRecord.Action;

import lombok.extern.slf4j.Slf4j;
import org.kiwiproject.champagne.model.DeployableSystemThreadLocal;
import org.kiwiproject.dropwizard.error.ApplicationErrorThrower;
import org.kiwiproject.dropwizard.error.dao.ApplicationErrorDao;

@Slf4j
public abstract class AuditableResource {

private final AuditRecordDao auditRecordDao;
private final ApplicationErrorThrower applicationErrorThrower;

Expand All @@ -34,11 +35,12 @@ protected void auditAction(long recordId, Class<?> recordClass, Action action) {
}

var auditRecord = AuditRecord.builder()
.recordId(recordId)
.recordType(recordClass.getSimpleName())
.action(action)
.userSystemIdentifier(principal.getName())
.build();
.recordId(recordId)
.recordType(recordClass.getSimpleName())
.action(action)
.userSystemIdentifier(principal.getName())
.deployableSystemId(DeployableSystemThreadLocal.getCurrentDeployableSystem().orElse(null))
.build();

auditRecordDao.insertAuditRecord(auditRecord);
}
Expand Down
Loading

0 comments on commit 983a7b9

Please sign in to comment.